1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* Copyright 2011,2013-2014 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "audio_registry.h"
#include <gnuradio/logger.h>
#include <gnuradio/prefs.h>
#include <stdexcept>
#include <vector>
namespace gr {
namespace audio {
/***********************************************************************
* Create registries
**********************************************************************/
static std::vector<source_entry_t>& get_source_registry(void)
{
static bool src_reg = false;
static std::vector<source_entry_t> s_registry;
if (!src_reg) {
#ifdef ALSA_FOUND
s_registry.push_back(register_source(REG_PRIO_HIGH, "alsa", alsa_source_fcn));
#endif /* ALSA_FOUND */
#ifdef OSS_FOUND
s_registry.push_back(register_source(REG_PRIO_LOW, "oss", oss_source_fcn));
#endif /* OSS_FOUND */
#ifdef PORTAUDIO_FOUND
s_registry.push_back(
register_source(REG_PRIO_MED, "portaudio", portaudio_source_fcn));
#endif /* PORTAUDIO_FOUND */
#ifdef JACK_FOUND
s_registry.push_back(register_source(REG_PRIO_MED, "jack", jack_source_fcn));
#endif /* JACK_FOUND */
#ifdef OSX_FOUND
s_registry.push_back(register_source(REG_PRIO_HIGH, "osx", osx_source_fcn));
#endif /* OSX_FOUND */
#ifdef WIN32_FOUND
s_registry.push_back(
register_source(REG_PRIO_LOW, "windows", windows_source_fcn));
#endif /* WIN32_FOUND */
src_reg = true;
}
return s_registry;
}
static std::vector<sink_entry_t>& get_sink_registry(void)
{
static bool snk_reg = false;
static std::vector<sink_entry_t> s_registry;
if (!snk_reg) {
#if ALSA_FOUND
s_registry.push_back(register_sink(REG_PRIO_HIGH, "alsa", alsa_sink_fcn));
#endif /* ALSA_FOUND */
#if OSS_FOUND
s_registry.push_back(register_sink(REG_PRIO_LOW, "oss", oss_sink_fcn));
#endif /* OSS_FOUND */
#if PORTAUDIO_FOUND
s_registry.push_back(
register_sink(REG_PRIO_MED, "portaudio", portaudio_sink_fcn));
#endif /* PORTAUDIO_FOUND */
#if JACK_FOUND
s_registry.push_back(register_sink(REG_PRIO_MED, "jack", jack_sink_fcn));
#endif /* JACK_FOUND */
#ifdef OSX_FOUND
s_registry.push_back(register_sink(REG_PRIO_HIGH, "osx", osx_sink_fcn));
#endif /* OSX_FOUND */
#ifdef WIN32_FOUND
s_registry.push_back(register_sink(REG_PRIO_HIGH, "windows", windows_sink_fcn));
#endif /* WIN32_FOUND */
snk_reg = true;
}
return s_registry;
}
/***********************************************************************
* Register functions
**********************************************************************/
source_entry_t
register_source(reg_prio_type prio, const std::string& arch, source_factory_t source)
{
source_entry_t entry;
entry.prio = prio;
entry.arch = arch;
entry.source = source;
return entry;
}
sink_entry_t
register_sink(reg_prio_type prio, const std::string& arch, sink_factory_t sink)
{
sink_entry_t entry;
entry.prio = prio;
entry.arch = arch;
entry.sink = sink;
return entry;
}
/***********************************************************************
* Factory functions
**********************************************************************/
static std::string default_arch_name(void)
{
return prefs::singleton()->get_string("audio", "audio_module", "auto");
}
static void do_arch_warning(const std::string& arch)
{
if (arch == "auto")
return; // no warning when arch not specified
gr::logger_ptr logger, debug_logger;
gr::configure_default_loggers(logger, debug_logger, "audio_registry");
std::ostringstream msg;
msg << "Could not find audio architecture \"" << arch << "\" in registry.";
msg << " Defaulting to the first available architecture.";
GR_LOG_ERROR(logger, msg.str());
}
source::sptr
source::make(int sampling_rate, const std::string device_name, bool ok_to_block)
{
gr::logger_ptr logger, debug_logger;
configure_default_loggers(logger, debug_logger, "audio_source");
if (get_source_registry().empty()) {
throw std::runtime_error("no available audio source factories");
}
std::string arch = default_arch_name();
source_entry_t entry = get_source_registry().front();
for (const auto& e : get_source_registry()) {
if (e.prio > entry.prio)
entry = e; // entry is highest prio
if (arch != e.arch)
continue; // continue when no match
return e.source(sampling_rate, device_name, ok_to_block);
}
GR_LOG_INFO(debug_logger, boost::format("Audio source arch: %1%") % (entry.arch));
return entry.source(sampling_rate, device_name, ok_to_block);
}
sink::sptr sink::make(int sampling_rate, const std::string device_name, bool ok_to_block)
{
gr::logger_ptr logger, debug_logger;
configure_default_loggers(logger, debug_logger, "audio source");
if (get_sink_registry().empty()) {
throw std::runtime_error("no available audio sink factories");
}
std::string arch = default_arch_name();
sink_entry_t entry = get_sink_registry().front();
for (const sink_entry_t& e : get_sink_registry()) {
if (e.prio > entry.prio)
entry = e; // entry is highest prio
if (arch != e.arch)
continue; // continue when no match
return e.sink(sampling_rate, device_name, ok_to_block);
}
do_arch_warning(arch);
GR_LOG_INFO(debug_logger, boost::format("Audio sink arch: %1%") % (entry.arch));
return entry.sink(sampling_rate, device_name, ok_to_block);
}
} /* namespace audio */
} /* namespace gr */
|