3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2016 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
23 |
|
|
24 |
#include "AudioOutputDeviceFactory.h" |
#include "AudioOutputDeviceFactory.h" |
25 |
#include "AudioOutputDevice.h" |
#include "AudioOutputDevice.h" |
26 |
|
#include "../../common/global_private.h" |
27 |
|
#include "../../common/IDGenerator.h" |
28 |
|
|
29 |
namespace LinuxSampler { |
namespace LinuxSampler { |
30 |
|
|
111 |
return std::vector<int>(); |
return std::vector<int>(); |
112 |
} |
} |
113 |
|
|
114 |
|
int AudioOutputDevice::ParameterSampleRate::ValueAsInt() { |
115 |
|
return (pDevice) ? (int) ((AudioOutputDevice*)pDevice)->SampleRate() |
116 |
|
: DeviceCreationParameterInt::ValueAsInt(); |
117 |
|
} |
118 |
|
|
119 |
void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (Exception) { |
void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (Exception) { |
120 |
/* cannot happen, as parameter is fix */ |
/* cannot happen, as parameter is fix */ |
121 |
} |
} |
161 |
} |
} |
162 |
|
|
163 |
optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) { |
optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) { |
164 |
return 100; |
return optional<int>::nothing; |
165 |
} |
} |
166 |
|
|
167 |
std::vector<int> AudioOutputDevice::ParameterChannels::PossibilitiesAsInt(std::map<String,String> Parameters) { |
std::vector<int> AudioOutputDevice::ParameterChannels::PossibilitiesAsInt(std::map<String,String> Parameters) { |
184 |
AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) |
AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) |
185 |
: EnginesReader(Engines) { |
: EnginesReader(Engines) { |
186 |
this->Parameters = DriverParameters; |
this->Parameters = DriverParameters; |
187 |
|
EffectChainIDs = new IDGenerator(); |
188 |
} |
} |
189 |
|
|
190 |
AudioOutputDevice::~AudioOutputDevice() { |
AudioOutputDevice::~AudioOutputDevice() { |
207 |
} |
} |
208 |
Parameters.clear(); |
Parameters.clear(); |
209 |
} |
} |
210 |
|
|
211 |
|
// delete all master effect chains |
212 |
|
{ |
213 |
|
std::vector<EffectChain*>::iterator iter = vEffectChains.begin(); |
214 |
|
while (iter != vEffectChains.end()) { |
215 |
|
delete *iter; |
216 |
|
iter++; |
217 |
|
} |
218 |
|
vEffectChains.clear(); |
219 |
|
} |
220 |
|
|
221 |
|
delete EffectChainIDs; |
222 |
} |
} |
223 |
|
|
224 |
void AudioOutputDevice::Connect(Engine* pEngine) { |
void AudioOutputDevice::Connect(Engine* pEngine) { |
240 |
//pEngine->DisconnectAudioOutputDevice(); |
//pEngine->DisconnectAudioOutputDevice(); |
241 |
} |
} |
242 |
} |
} |
243 |
|
|
244 |
|
void AudioOutputDevice::ReconnectAll() { |
245 |
|
// copy by value, not by reference here ! |
246 |
|
std::set<Engine*> engines = Engines.GetConfigForUpdate(); |
247 |
|
{ |
248 |
|
std::set<Engine*>::iterator iterEngine = engines.begin(); |
249 |
|
std::set<Engine*>::iterator end = engines.end(); |
250 |
|
for (; iterEngine != end; iterEngine++) { |
251 |
|
(*iterEngine)->ReconnectAudioOutputDevice(); |
252 |
|
} |
253 |
|
} |
254 |
|
|
255 |
|
// update all effects as well |
256 |
|
for (std::vector<EffectChain*>::iterator it = vEffectChains.begin(); |
257 |
|
it != vEffectChains.end(); ++it) |
258 |
|
{ |
259 |
|
EffectChain* pChain = *it; |
260 |
|
pChain->Reconnect(this); |
261 |
|
} |
262 |
|
} |
263 |
|
|
264 |
AudioChannel* AudioOutputDevice::Channel(uint ChannelIndex) { |
AudioChannel* AudioOutputDevice::Channel(uint ChannelIndex) { |
265 |
return (ChannelIndex < Channels.size()) ? Channels[ChannelIndex] : NULL; |
return (ChannelIndex < Channels.size()) ? Channels[ChannelIndex] : NULL; |
267 |
|
|
268 |
void AudioOutputDevice::AcquireChannels(uint Channels) { |
void AudioOutputDevice::AcquireChannels(uint Channels) { |
269 |
if (Channels > this->Channels.size()) { |
if (Channels > this->Channels.size()) { |
270 |
for (int c = this->Channels.size(); c < Channels; c++) { |
for (size_t c = this->Channels.size(); c < Channels; c++) { |
271 |
this->Channels.push_back(CreateChannel(c)); |
this->Channels.push_back(CreateChannel(uint(c))); |
272 |
} |
} |
273 |
} |
} |
274 |
} |
} |
275 |
|
|
276 |
uint AudioOutputDevice::ChannelCount() { |
uint AudioOutputDevice::ChannelCount() { |
277 |
return Channels.size(); |
return (uint) Channels.size(); |
278 |
} |
} |
279 |
|
|
280 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() { |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() { |
281 |
return Parameters; |
return Parameters; |
282 |
} |
} |
283 |
|
|
284 |
|
EffectChain* AudioOutputDevice::AddSendEffectChain() { |
285 |
|
EffectChain* pChain = new EffectChain(this, EffectChainIDs->create()); |
286 |
|
vEffectChains.push_back(pChain); |
287 |
|
return pChain; |
288 |
|
} |
289 |
|
|
290 |
|
void AudioOutputDevice::RemoveSendEffectChain(uint iChain) throw (Exception) { |
291 |
|
if (iChain >= vEffectChains.size()) |
292 |
|
throw Exception( |
293 |
|
"Could not remove send effect chain " + ToString(iChain) + |
294 |
|
", index out of bounds" |
295 |
|
); |
296 |
|
std::vector<EffectChain*>::iterator iter = vEffectChains.begin(); |
297 |
|
for (int i = 0; i < iChain; ++i) ++iter; |
298 |
|
EffectChainIDs->destroy((*iter)->ID()); |
299 |
|
vEffectChains.erase(iter); |
300 |
|
} |
301 |
|
|
302 |
|
EffectChain* AudioOutputDevice::SendEffectChain(uint iChain) const { |
303 |
|
if (iChain >= vEffectChains.size()) return NULL; |
304 |
|
return vEffectChains[iChain]; |
305 |
|
} |
306 |
|
|
307 |
|
EffectChain* AudioOutputDevice::SendEffectChainByID(uint iChainID) const { |
308 |
|
for (int i = 0; i < SendEffectChainCount(); i++) { |
309 |
|
if (SendEffectChain(i)->ID() == iChainID) return SendEffectChain(i); |
310 |
|
} |
311 |
|
|
312 |
|
return NULL; |
313 |
|
} |
314 |
|
|
315 |
|
uint AudioOutputDevice::SendEffectChainCount() const { |
316 |
|
return (uint) vEffectChains.size(); |
317 |
|
} |
318 |
|
|
319 |
|
// TODO: to be removed |
320 |
|
EffectChain* AudioOutputDevice::AddMasterEffectChain() { |
321 |
|
return AddSendEffectChain(); |
322 |
|
} |
323 |
|
|
324 |
|
// TODO: to be removed |
325 |
|
void AudioOutputDevice::RemoveMasterEffectChain(uint iChain) throw (Exception) { |
326 |
|
RemoveSendEffectChain(iChain); |
327 |
|
} |
328 |
|
|
329 |
|
// TODO: to be removed |
330 |
|
EffectChain* AudioOutputDevice::MasterEffectChain(uint iChain) const { |
331 |
|
return SendEffectChain(iChain); |
332 |
|
} |
333 |
|
|
334 |
|
// TODO: to be removed |
335 |
|
uint AudioOutputDevice::MasterEffectChainCount() const { |
336 |
|
return SendEffectChainCount(); |
337 |
|
} |
338 |
|
|
339 |
|
float AudioOutputDevice::latency() { |
340 |
|
return float(MaxSamplesPerCycle()) / float(SampleRate()); |
341 |
|
} |
342 |
|
|
343 |
int AudioOutputDevice::RenderAudio(uint Samples) { |
int AudioOutputDevice::RenderAudio(uint Samples) { |
344 |
if (Channels.empty()) return 0; |
if (Channels.empty()) return 0; |
345 |
|
|
348 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
349 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
350 |
for (; iterChannels != end; iterChannels++) |
for (; iterChannels != end; iterChannels++) |
351 |
(*iterChannels)->Clear(); // zero out audio buffer |
(*iterChannels)->Clear(Samples); // zero out audio buffer |
352 |
|
} |
353 |
|
// do the same for master effects |
354 |
|
{ |
355 |
|
std::vector<EffectChain*>::iterator iterChains = vEffectChains.begin(); |
356 |
|
std::vector<EffectChain*>::iterator end = vEffectChains.end(); |
357 |
|
for (; iterChains != end; ++iterChains) |
358 |
|
(*iterChains)->ClearAllChannels(); // zero out audio buffers |
359 |
} |
} |
360 |
|
|
361 |
int result = 0; |
int result = 0; |
379 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
380 |
} |
} |
381 |
#endif // CONFIG_RT_EXCEPTIONS |
#endif // CONFIG_RT_EXCEPTIONS |
|
|
|
382 |
EnginesReader.Unlock(); |
EnginesReader.Unlock(); |
383 |
|
|
384 |
|
// now that the engines (might) have left fx send signals for master |
385 |
|
// effects, render all master effects |
386 |
|
{ |
387 |
|
std::vector<EffectChain*>::iterator iterChains = vEffectChains.begin(); |
388 |
|
std::vector<EffectChain*>::iterator end = vEffectChains.end(); |
389 |
|
for (; iterChains != end; ++iterChains) { |
390 |
|
if (!(*iterChains)->EffectCount()) continue; |
391 |
|
(*iterChains)->RenderAudio(Samples); |
392 |
|
// mix the result of the last effect in the chain to the audio |
393 |
|
// output device channel(s) |
394 |
|
Effect* pLastEffect = |
395 |
|
(*iterChains)->GetEffect((*iterChains)->EffectCount() - 1); |
396 |
|
for (int iChan = 0; iChan < pLastEffect->OutputChannelCount() && iChan < ChannelCount(); ++iChan) |
397 |
|
pLastEffect->OutputChannel(iChan)->MixTo(Channel(iChan), Samples); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
return result; |
return result; |
402 |
} |
} |
403 |
|
|
409 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
410 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
411 |
for (; iterChannels != end; iterChannels++) |
for (; iterChannels != end; iterChannels++) |
412 |
(*iterChannels)->Clear(); // zero out audio buffer |
(*iterChannels)->Clear(Samples); // zero out audio buffer |
413 |
} |
} |
414 |
|
|
415 |
return 0; |
return 0; |