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