1 |
schoenebeck |
200 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
1424 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
schoenebeck |
200 |
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "AudioOutputDeviceFactory.h" |
25 |
|
|
#include "AudioOutputDevice.h" |
26 |
schoenebeck |
1424 |
#include "../../common/global_private.h" |
27 |
schoenebeck |
200 |
|
28 |
|
|
namespace LinuxSampler { |
29 |
|
|
|
30 |
schoenebeck |
226 |
// *************** ParameterActive *************** |
31 |
|
|
// * |
32 |
|
|
|
33 |
|
|
AudioOutputDevice::ParameterActive::ParameterActive() : DeviceCreationParameterBool() { |
34 |
|
|
InitWithDefault(); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
AudioOutputDevice::ParameterActive::ParameterActive(String s) : DeviceCreationParameterBool(s) { |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
String AudioOutputDevice::ParameterActive::Description() { |
41 |
|
|
return "Enable / disable device"; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
bool AudioOutputDevice::ParameterActive::Fix() { |
45 |
|
|
return false; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
bool AudioOutputDevice::ParameterActive::Mandatory() { |
49 |
|
|
return false; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterActive::DependsAsParameters() { |
53 |
|
|
return std::map<String,DeviceCreationParameter*>(); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
optional<bool> AudioOutputDevice::ParameterActive::DefaultAsBool(std::map<String,String> Parameters) { |
57 |
|
|
return true; |
58 |
|
|
} |
59 |
|
|
|
60 |
schoenebeck |
880 |
void AudioOutputDevice::ParameterActive::OnSetValue(bool b) throw (Exception) { |
61 |
schoenebeck |
226 |
if (b) ((AudioOutputDevice*)pDevice)->Play(); |
62 |
|
|
else ((AudioOutputDevice*)pDevice)->Stop(); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
String AudioOutputDevice::ParameterActive::Name() { |
66 |
|
|
return "ACTIVE"; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
// *************** ParameterSampleRate *************** |
72 |
|
|
// * |
73 |
|
|
|
74 |
|
|
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate() : DeviceCreationParameterInt() { |
75 |
|
|
InitWithDefault(); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate(String s) : DeviceCreationParameterInt(s) { |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
String AudioOutputDevice::ParameterSampleRate::Description() { |
82 |
|
|
return "Output sample rate"; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
bool AudioOutputDevice::ParameterSampleRate::Fix() { |
86 |
|
|
return true; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
bool AudioOutputDevice::ParameterSampleRate::Mandatory() { |
90 |
|
|
return false; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterSampleRate::DependsAsParameters() { |
94 |
|
|
return std::map<String,DeviceCreationParameter*>(); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
optional<int> AudioOutputDevice::ParameterSampleRate::DefaultAsInt(std::map<String,String> Parameters) { |
98 |
|
|
return 44100; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMinAsInt(std::map<String,String> Parameters) { |
102 |
|
|
return optional<int>::nothing; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMaxAsInt(std::map<String,String> Parameters) { |
106 |
|
|
return optional<int>::nothing; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
std::vector<int> AudioOutputDevice::ParameterSampleRate::PossibilitiesAsInt(std::map<String,String> Parameters) { |
110 |
|
|
return std::vector<int>(); |
111 |
|
|
} |
112 |
|
|
|
113 |
schoenebeck |
1607 |
int AudioOutputDevice::ParameterSampleRate::ValueAsInt() { |
114 |
|
|
return (pDevice) ? (int) ((AudioOutputDevice*)pDevice)->SampleRate() |
115 |
|
|
: DeviceCreationParameterInt::ValueAsInt(); |
116 |
|
|
} |
117 |
|
|
|
118 |
schoenebeck |
880 |
void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (Exception) { |
119 |
schoenebeck |
226 |
/* cannot happen, as parameter is fix */ |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
String AudioOutputDevice::ParameterSampleRate::Name() { |
123 |
|
|
return "SAMPLERATE"; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
// *************** ParameterChannels *************** |
129 |
|
|
// * |
130 |
|
|
|
131 |
|
|
AudioOutputDevice::ParameterChannels::ParameterChannels() : DeviceCreationParameterInt() { |
132 |
|
|
InitWithDefault(); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
AudioOutputDevice::ParameterChannels::ParameterChannels(String s) : DeviceCreationParameterInt(s) { |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
String AudioOutputDevice::ParameterChannels::Description() { |
139 |
|
|
return "Number of output channels"; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
bool AudioOutputDevice::ParameterChannels::Fix() { |
143 |
|
|
return false; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
bool AudioOutputDevice::ParameterChannels::Mandatory() { |
147 |
|
|
return false; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterChannels::DependsAsParameters() { |
151 |
|
|
return std::map<String,DeviceCreationParameter*>(); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
optional<int> AudioOutputDevice::ParameterChannels::DefaultAsInt(std::map<String,String> Parameters) { |
155 |
|
|
return 2; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
optional<int> AudioOutputDevice::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) { |
159 |
iliev |
1305 |
return 1; |
160 |
schoenebeck |
226 |
} |
161 |
|
|
|
162 |
|
|
optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) { |
163 |
schoenebeck |
1599 |
return optional<int>::nothing; |
164 |
schoenebeck |
226 |
} |
165 |
|
|
|
166 |
|
|
std::vector<int> AudioOutputDevice::ParameterChannels::PossibilitiesAsInt(std::map<String,String> Parameters) { |
167 |
|
|
return std::vector<int>(); |
168 |
|
|
} |
169 |
|
|
|
170 |
schoenebeck |
880 |
void AudioOutputDevice::ParameterChannels::OnSetValue(int i) throw (Exception) { |
171 |
schoenebeck |
226 |
((AudioOutputDevice*)pDevice)->AcquireChannels(i); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
String AudioOutputDevice::ParameterChannels::Name() { |
175 |
|
|
return "CHANNELS"; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
// *************** AudioOutputDevice *************** |
181 |
|
|
// * |
182 |
|
|
|
183 |
persson |
846 |
AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) |
184 |
|
|
: EnginesReader(Engines) { |
185 |
schoenebeck |
200 |
this->Parameters = DriverParameters; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
AudioOutputDevice::~AudioOutputDevice() { |
189 |
schoenebeck |
226 |
// delete all audio channels |
190 |
|
|
{ |
191 |
|
|
std::vector<AudioChannel*>::iterator iter = Channels.begin(); |
192 |
|
|
while (iter != Channels.end()) { |
193 |
|
|
delete *iter; |
194 |
|
|
iter++; |
195 |
|
|
} |
196 |
persson |
836 |
Channels.clear(); |
197 |
schoenebeck |
200 |
} |
198 |
schoenebeck |
226 |
|
199 |
|
|
// delete all device parameters |
200 |
|
|
{ |
201 |
|
|
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
202 |
|
|
while (iter != Parameters.end()) { |
203 |
|
|
delete iter->second; |
204 |
|
|
iter++; |
205 |
|
|
} |
206 |
persson |
835 |
Parameters.clear(); |
207 |
schoenebeck |
226 |
} |
208 |
schoenebeck |
200 |
} |
209 |
|
|
|
210 |
schoenebeck |
412 |
void AudioOutputDevice::Connect(Engine* pEngine) { |
211 |
persson |
840 |
std::set<Engine*>& engines = Engines.GetConfigForUpdate(); |
212 |
|
|
if (engines.find(pEngine) == engines.end()) { |
213 |
|
|
engines.insert(pEngine); |
214 |
|
|
Engines.SwitchConfig().insert(pEngine); |
215 |
schoenebeck |
412 |
// make sure the engine knows about the connection |
216 |
|
|
//pEngine->Connect(this); |
217 |
schoenebeck |
200 |
} |
218 |
|
|
} |
219 |
|
|
|
220 |
schoenebeck |
412 |
void AudioOutputDevice::Disconnect(Engine* pEngine) { |
221 |
persson |
840 |
std::set<Engine*>& engines = Engines.GetConfigForUpdate(); |
222 |
|
|
if (engines.find(pEngine) != engines.end()) { // if clause to prevent disconnect loop |
223 |
|
|
engines.erase(pEngine); |
224 |
|
|
Engines.SwitchConfig().erase(pEngine); |
225 |
schoenebeck |
412 |
// make sure the engine knows about the disconnection |
226 |
|
|
//pEngine->DisconnectAudioOutputDevice(); |
227 |
schoenebeck |
200 |
} |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
AudioChannel* AudioOutputDevice::Channel(uint ChannelIndex) { |
231 |
|
|
return (ChannelIndex < Channels.size()) ? Channels[ChannelIndex] : NULL; |
232 |
|
|
} |
233 |
|
|
|
234 |
schoenebeck |
226 |
void AudioOutputDevice::AcquireChannels(uint Channels) { |
235 |
|
|
if (Channels > this->Channels.size()) { |
236 |
|
|
for (int c = this->Channels.size(); c < Channels; c++) { |
237 |
|
|
this->Channels.push_back(CreateChannel(c)); |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
} |
241 |
|
|
|
242 |
schoenebeck |
1001 |
uint AudioOutputDevice::ChannelCount() { |
243 |
|
|
return Channels.size(); |
244 |
|
|
} |
245 |
|
|
|
246 |
schoenebeck |
200 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() { |
247 |
|
|
return Parameters; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
int AudioOutputDevice::RenderAudio(uint Samples) { |
251 |
|
|
if (Channels.empty()) return 0; |
252 |
|
|
|
253 |
|
|
// reset all channels with silence |
254 |
|
|
{ |
255 |
|
|
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
256 |
|
|
std::vector<AudioChannel*>::iterator end = Channels.end(); |
257 |
|
|
for (; iterChannels != end; iterChannels++) |
258 |
|
|
(*iterChannels)->Clear(); // zero out audio buffer |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
int result = 0; |
262 |
|
|
|
263 |
|
|
// let all connected engines render audio for the current audio fragment cycle |
264 |
persson |
846 |
const std::set<Engine*>& engines = EnginesReader.Lock(); |
265 |
schoenebeck |
554 |
#if CONFIG_RT_EXCEPTIONS |
266 |
schoenebeck |
272 |
try |
267 |
schoenebeck |
554 |
#endif // CONFIG_RT_EXCEPTIONS |
268 |
schoenebeck |
200 |
{ |
269 |
persson |
840 |
std::set<Engine*>::iterator iterEngine = engines.begin(); |
270 |
|
|
std::set<Engine*>::iterator end = engines.end(); |
271 |
schoenebeck |
412 |
for (; iterEngine != end; iterEngine++) { |
272 |
|
|
int res = (*iterEngine)->RenderAudio(Samples); |
273 |
schoenebeck |
200 |
if (res != 0) result = res; |
274 |
|
|
} |
275 |
|
|
} |
276 |
schoenebeck |
554 |
#if CONFIG_RT_EXCEPTIONS |
277 |
schoenebeck |
272 |
catch (std::runtime_error se) { |
278 |
|
|
std::cerr << "std::runtime_error: " << se.what() << std::endl << std::flush; |
279 |
|
|
exit(EXIT_FAILURE); |
280 |
|
|
} |
281 |
schoenebeck |
554 |
#endif // CONFIG_RT_EXCEPTIONS |
282 |
schoenebeck |
200 |
|
283 |
persson |
846 |
EnginesReader.Unlock(); |
284 |
schoenebeck |
200 |
return result; |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
int AudioOutputDevice::RenderSilence(uint Samples) { |
288 |
|
|
if (Channels.empty()) return 0; |
289 |
|
|
|
290 |
|
|
// reset all channels with silence |
291 |
|
|
{ |
292 |
|
|
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
293 |
|
|
std::vector<AudioChannel*>::iterator end = Channels.end(); |
294 |
|
|
for (; iterChannels != end; iterChannels++) |
295 |
|
|
(*iterChannels)->Clear(); // zero out audio buffer |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
return 0; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
} // namespace LinuxSampler |