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 - 2010 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This library 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 * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
* (at your option) any later version. * |
12 |
* * |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
* This library is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
* GNU General Public License for more details. * |
17 |
* * |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
* along with this library; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
25 |
|
|
26 |
#include "Sampler.h" |
#include "Sampler.h" |
27 |
|
|
28 |
|
#include "common/global_private.h" |
29 |
#include "engines/EngineFactory.h" |
#include "engines/EngineFactory.h" |
30 |
#include "engines/EngineChannelFactory.h" |
#include "engines/EngineChannelFactory.h" |
31 |
|
#include "plugins/InstrumentEditorFactory.h" |
32 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
33 |
#include "drivers/midi/MidiInputDeviceFactory.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
34 |
|
#include "drivers/midi/MidiInstrumentMapper.h" |
35 |
|
#include "common/Features.h" |
36 |
#include "network/lscpserver.h" |
#include "network/lscpserver.h" |
37 |
|
|
38 |
namespace LinuxSampler { |
namespace LinuxSampler { |
71 |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
72 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
73 |
|
|
74 |
|
if (pEngineChannel) { |
75 |
|
if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { |
76 |
|
dmsg(2,("OK\n")); |
77 |
|
return; |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
|
fireEngineToBeChanged(); |
82 |
|
|
83 |
// create new engine channel |
// create new engine channel |
84 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
85 |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
86 |
|
|
87 |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
pNewEngineChannel->SetSamplerChannel(this); |
|
pNewEngineChannel->iSamplerChannelIndex = Index(); |
|
88 |
|
|
89 |
// dereference midi input port. |
// dereference midi input port. |
90 |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
91 |
|
midi_chan_t midiChannel = GetMidiInputChannel(); |
92 |
// disconnect old engine channel |
// disconnect old engine channel |
93 |
if (pEngineChannel) { |
if (pEngineChannel) { |
94 |
Engine* engine = pEngineChannel->GetEngine(); |
Engine* engine = pEngineChannel->GetEngine(); |
108 |
pNewEngineChannel->Connect(pAudioOutputDevice); |
pNewEngineChannel->Connect(pAudioOutputDevice); |
109 |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
110 |
} |
} |
111 |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, midiChannel); |
112 |
pEngineChannel = pNewEngineChannel; |
pEngineChannel = pNewEngineChannel; |
113 |
|
|
114 |
// from now on get MIDI device and port from EngineChannel object |
// from now on get MIDI device and port from EngineChannel object |
116 |
this->iMidiPort = 0; |
this->iMidiPort = 0; |
117 |
|
|
118 |
pEngineChannel->StatusChanged(true); |
pEngineChannel->StatusChanged(true); |
119 |
|
fireEngineChanged(); |
120 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
121 |
} |
} |
122 |
|
|
123 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
124 |
if(pAudioOutputDevice == pDevice) return; |
if(pAudioOutputDevice == pDevice) return; |
125 |
|
|
126 |
// disconnect old device |
// disconnect old device |
127 |
if (pAudioOutputDevice && pEngineChannel) { |
if (pAudioOutputDevice && pEngineChannel) { |
128 |
|
if (!pAudioOutputDevice->isAutonomousDevice()) |
129 |
|
throw Exception("The audio output device '" + pAudioOutputDevice->Driver() + "' cannot be dropped from this sampler channel!"); |
130 |
|
|
131 |
Engine* engine = pEngineChannel->GetEngine(); |
Engine* engine = pEngineChannel->GetEngine(); |
132 |
pAudioOutputDevice->Disconnect(engine); |
pAudioOutputDevice->Disconnect(engine); |
133 |
|
|
146 |
} |
} |
147 |
} |
} |
148 |
|
|
149 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
150 |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
151 |
} |
} |
152 |
|
|
153 |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
void SamplerChannel::SetMidiInputPort(int MidiPort) throw (Exception) { |
154 |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
155 |
} |
} |
156 |
|
|
158 |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
159 |
} |
} |
160 |
|
|
161 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) throw (Exception) { |
162 |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
163 |
|
|
164 |
// get old and new midi input port |
// get old and new midi input port |
166 |
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
167 |
|
|
168 |
// disconnect old device port |
// disconnect old device port |
169 |
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
if (pOldMidiInputPort && pEngineChannel) { |
170 |
|
MidiInputDevice* pOldDevice = pOldMidiInputPort->GetDevice(); |
171 |
|
if (pOldMidiInputPort != pNewMidiInputPort && |
172 |
|
pOldDevice && !pOldDevice->isAutonomousDevice() |
173 |
|
) throw Exception("The MIDI input port '" + pOldDevice->Driver() + "' cannot be altered on this sampler channel!"); |
174 |
|
|
175 |
|
pOldMidiInputPort->Disconnect(pEngineChannel); |
176 |
|
} |
177 |
|
|
178 |
// remember new device, port and channel if not engine channel yet created |
// remember new device, port and channel if not engine channel yet created |
179 |
if (!pEngineChannel) { |
if (!pEngineChannel) { |
180 |
this->pMidiInputDevice = pDevice; |
this->pMidiInputDevice = pDevice; |
228 |
throw Exception("Internal error: SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
229 |
} |
} |
230 |
|
|
231 |
|
Sampler* SamplerChannel::GetSampler() { |
232 |
|
return pSampler; |
233 |
|
} |
234 |
|
|
235 |
|
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
236 |
|
llEngineChangeListeners.AddListener(l); |
237 |
|
} |
238 |
|
|
239 |
|
void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) { |
240 |
|
llEngineChangeListeners.RemoveListener(l); |
241 |
|
} |
242 |
|
|
243 |
|
void SamplerChannel::RemoveAllEngineChangeListeners() { |
244 |
|
llEngineChangeListeners.RemoveAllListeners(); |
245 |
|
} |
246 |
|
|
247 |
|
void SamplerChannel::fireEngineToBeChanged() { |
248 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
249 |
|
llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index()); |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
|
void SamplerChannel::fireEngineChanged() { |
254 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
255 |
|
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
260 |
MidiInputPort* pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
261 |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
270 |
// * Sampler |
// * Sampler |
271 |
|
|
272 |
Sampler::Sampler() { |
Sampler::Sampler() { |
273 |
|
eventHandler.SetSampler(this); |
274 |
|
uiOldTotalVoiceCount = uiOldTotalStreamCount = 0; |
275 |
} |
} |
276 |
|
|
277 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
282 |
return mSamplerChannels.size(); |
return mSamplerChannels.size(); |
283 |
} |
} |
284 |
|
|
285 |
|
void Sampler::AddChannelCountListener(ChannelCountListener* l) { |
286 |
|
llChannelCountListeners.AddListener(l); |
287 |
|
} |
288 |
|
|
289 |
|
void Sampler::RemoveChannelCountListener(ChannelCountListener* l) { |
290 |
|
llChannelCountListeners.RemoveListener(l); |
291 |
|
} |
292 |
|
|
293 |
|
void Sampler::fireChannelCountChanged(int NewCount) { |
294 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
295 |
|
llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount); |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
void Sampler::fireChannelAdded(SamplerChannel* pChannel) { |
300 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
301 |
|
llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel); |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
|
void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) { |
306 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
307 |
|
llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel); |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
|
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
312 |
|
llAudioDeviceCountListeners.AddListener(l); |
313 |
|
} |
314 |
|
|
315 |
|
void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) { |
316 |
|
llAudioDeviceCountListeners.RemoveListener(l); |
317 |
|
} |
318 |
|
|
319 |
|
void Sampler::fireAudioDeviceCountChanged(int NewCount) { |
320 |
|
for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) { |
321 |
|
llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount); |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) { |
326 |
|
llMidiDeviceCountListeners.AddListener(l); |
327 |
|
} |
328 |
|
|
329 |
|
void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) { |
330 |
|
llMidiDeviceCountListeners.RemoveListener(l); |
331 |
|
} |
332 |
|
|
333 |
|
void Sampler::fireMidiDeviceCountChanged(int NewCount) { |
334 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
335 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount); |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
|
void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
340 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
341 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice); |
342 |
|
} |
343 |
|
} |
344 |
|
|
345 |
|
void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) { |
346 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
347 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice); |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
|
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
352 |
|
llVoiceCountListeners.AddListener(l); |
353 |
|
} |
354 |
|
|
355 |
|
void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) { |
356 |
|
llVoiceCountListeners.RemoveListener(l); |
357 |
|
} |
358 |
|
|
359 |
|
void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { |
360 |
|
std::map<uint, uint>::iterator it = mOldVoiceCounts.find(ChannelId); |
361 |
|
if (it != mOldVoiceCounts.end()) { |
362 |
|
uint oldCount = it->second; |
363 |
|
if (NewCount == oldCount) return; |
364 |
|
} |
365 |
|
|
366 |
|
mOldVoiceCounts[ChannelId] = NewCount; |
367 |
|
|
368 |
|
for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { |
369 |
|
llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); |
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
|
void Sampler::AddStreamCountListener(StreamCountListener* l) { |
374 |
|
llStreamCountListeners.AddListener(l); |
375 |
|
} |
376 |
|
|
377 |
|
void Sampler::RemoveStreamCountListener(StreamCountListener* l) { |
378 |
|
llStreamCountListeners.RemoveListener(l); |
379 |
|
} |
380 |
|
|
381 |
|
void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { |
382 |
|
std::map<uint, uint>::iterator it = mOldStreamCounts.find(ChannelId); |
383 |
|
if (it != mOldStreamCounts.end()) { |
384 |
|
uint oldCount = it->second; |
385 |
|
if (NewCount == oldCount) return; |
386 |
|
} |
387 |
|
|
388 |
|
mOldStreamCounts[ChannelId] = NewCount; |
389 |
|
|
390 |
|
for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { |
391 |
|
llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
void Sampler::AddBufferFillListener(BufferFillListener* l) { |
396 |
|
llBufferFillListeners.AddListener(l); |
397 |
|
} |
398 |
|
|
399 |
|
void Sampler::RemoveBufferFillListener(BufferFillListener* l) { |
400 |
|
llBufferFillListeners.RemoveListener(l); |
401 |
|
} |
402 |
|
|
403 |
|
void Sampler::fireBufferFillChanged(int ChannelId, String FillData) { |
404 |
|
for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) { |
405 |
|
llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData); |
406 |
|
} |
407 |
|
} |
408 |
|
|
409 |
|
void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) { |
410 |
|
llTotalStreamCountListeners.AddListener(l); |
411 |
|
} |
412 |
|
|
413 |
|
void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) { |
414 |
|
llTotalStreamCountListeners.RemoveListener(l); |
415 |
|
} |
416 |
|
|
417 |
|
void Sampler::fireTotalStreamCountChanged(int NewCount) { |
418 |
|
if (NewCount == uiOldTotalStreamCount) return; |
419 |
|
uiOldTotalStreamCount = NewCount; |
420 |
|
|
421 |
|
for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { |
422 |
|
llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); |
423 |
|
} |
424 |
|
} |
425 |
|
|
426 |
|
void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { |
427 |
|
llTotalVoiceCountListeners.AddListener(l); |
428 |
|
} |
429 |
|
|
430 |
|
void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) { |
431 |
|
llTotalVoiceCountListeners.RemoveListener(l); |
432 |
|
} |
433 |
|
|
434 |
|
void Sampler::fireTotalVoiceCountChanged(int NewCount) { |
435 |
|
if (NewCount == uiOldTotalVoiceCount) return; |
436 |
|
uiOldTotalVoiceCount = NewCount; |
437 |
|
|
438 |
|
for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { |
439 |
|
llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); |
440 |
|
} |
441 |
|
} |
442 |
|
|
443 |
|
void Sampler::AddFxSendCountListener(FxSendCountListener* l) { |
444 |
|
llFxSendCountListeners.AddListener(l); |
445 |
|
} |
446 |
|
|
447 |
|
void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) { |
448 |
|
llFxSendCountListeners.RemoveListener(l); |
449 |
|
} |
450 |
|
|
451 |
|
void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) { |
452 |
|
for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) { |
453 |
|
llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount); |
454 |
|
} |
455 |
|
} |
456 |
|
|
457 |
|
void Sampler::EventHandler::EngineToBeChanged(int ChannelId) { |
458 |
|
// nothing to do here |
459 |
|
} |
460 |
|
|
461 |
|
void Sampler::EventHandler::EngineChanged(int ChannelId) { |
462 |
|
EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); |
463 |
|
if(engineChannel == NULL) return; |
464 |
|
engineChannel->AddFxSendCountListener(this); |
465 |
|
} |
466 |
|
|
467 |
|
void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) { |
468 |
|
pSampler->fireFxSendCountChanged(ChannelId, NewCount); |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
473 |
// if there's no sampler channel yet |
// if there's no sampler channel yet |
474 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
475 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
476 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
477 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
fireChannelAdded(pChannel); |
478 |
|
fireChannelCountChanged(1); |
479 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
480 |
return pChannel; |
return pChannel; |
481 |
} |
} |
482 |
|
|
491 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
492 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
493 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
494 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
fireChannelAdded(pChannel); |
495 |
|
fireChannelCountChanged(SamplerChannels()); |
496 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
497 |
return pChannel; |
return pChannel; |
498 |
} |
} |
499 |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
502 |
// we have not reached the index limit so we just add the channel past the highest index |
// we have not reached the index limit so we just add the channel past the highest index |
503 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
504 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
505 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
fireChannelAdded(pChannel); |
506 |
|
fireChannelCountChanged(SamplerChannels()); |
507 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
508 |
return pChannel; |
return pChannel; |
509 |
} |
} |
510 |
|
|
520 |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
521 |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
522 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
523 |
|
fireChannelToBeRemoved(pSamplerChannel); |
524 |
|
mOldVoiceCounts.erase(pSamplerChannel->Index()); |
525 |
|
mOldStreamCounts.erase(pSamplerChannel->Index()); |
526 |
|
pSamplerChannel->RemoveAllEngineChangeListeners(); |
527 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
528 |
delete pSamplerChannel; |
delete pSamplerChannel; |
529 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
fireChannelCountChanged(SamplerChannels()); |
530 |
return; |
return; |
531 |
} |
} |
532 |
} |
} |
538 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
539 |
} |
} |
540 |
|
|
541 |
|
void Sampler::RemoveAllSamplerChannels() { |
542 |
|
/* |
543 |
|
* In maps iterator invalidation occurs when the iterator point |
544 |
|
* to the element that is being erased. So we need to copy the map |
545 |
|
* by calling GetSamplerChannels() to prevent that. |
546 |
|
*/ |
547 |
|
SamplerChannelMap chns = GetSamplerChannels(); |
548 |
|
SamplerChannelMap::iterator iter = chns.begin(); |
549 |
|
for(; iter != chns.end(); iter++) { |
550 |
|
RemoveSamplerChannel(iter->second); |
551 |
|
} |
552 |
|
} |
553 |
|
|
554 |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
555 |
return AudioOutputDeviceFactory::AvailableDrivers(); |
return AudioOutputDeviceFactory::AvailableDrivers(); |
556 |
} |
} |
567 |
// create new device |
// create new device |
568 |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
569 |
|
|
570 |
// add new audio device to the audio device list |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
|
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
|
|
if (!mAudioOutputDevices[i]) { |
|
|
mAudioOutputDevices[i] = pDevice; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
|
571 |
return pDevice; |
return pDevice; |
572 |
} |
} |
573 |
|
|
574 |
uint Sampler::AudioOutputDevices() { |
uint Sampler::AudioOutputDevices() { |
575 |
return mAudioOutputDevices.size(); |
return AudioOutputDeviceFactory::Devices().size(); |
576 |
} |
} |
577 |
|
|
578 |
uint Sampler::MidiInputDevices() { |
uint Sampler::MidiInputDevices() { |
579 |
return mMidiInputDevices.size(); |
return MidiInputDeviceFactory::Devices().size(); |
580 |
} |
} |
581 |
|
|
582 |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
583 |
return mAudioOutputDevices; |
return AudioOutputDeviceFactory::Devices(); |
584 |
} |
} |
585 |
|
|
586 |
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
587 |
return mMidiInputDevices; |
return MidiInputDeviceFactory::Devices(); |
588 |
} |
} |
589 |
|
|
590 |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
591 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
if (pDevice) { |
592 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
// check if there are still sampler engines connected to this device |
593 |
if (iter->second == pDevice) { |
for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
594 |
// check if there are still sampler engines connected to this device |
iterChan != mSamplerChannels.end(); iterChan++ |
595 |
for (uint i = 0; i < SamplerChannels(); i++) |
) if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device."); |
596 |
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
|
597 |
|
//TODO: should we add fireAudioDeviceToBeDestroyed() here ? |
598 |
// disable device |
AudioOutputDeviceFactory::Destroy(pDevice); |
599 |
pDevice->Stop(); |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
600 |
|
} |
601 |
|
} |
602 |
|
|
603 |
// remove device from the device list |
void Sampler::DestroyAllAudioOutputDevices() throw (Exception) { |
604 |
mAudioOutputDevices.erase(iter); |
/* |
605 |
|
* In maps iterator invalidation occurs when the iterator point |
606 |
|
* to the element that is being erased. So we need to copy the map |
607 |
|
* by calling GetAudioOutputDevices() to prevent that. |
608 |
|
*/ |
609 |
|
std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices(); |
610 |
|
std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin(); |
611 |
|
for (; iter != devs.end(); iter++) { |
612 |
|
AudioOutputDevice* pDevice = iter->second; |
613 |
|
|
614 |
// destroy and free device from memory |
// skip non-autonomous devices |
615 |
delete pDevice; |
if (!pDevice->isAutonomousDevice()) continue; |
616 |
|
|
617 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
DestroyAudioOutputDevice(pDevice); |
|
break; |
|
|
} |
|
618 |
} |
} |
619 |
} |
} |
620 |
|
|
621 |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
622 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
if (pDevice) { |
623 |
for (; iter != mMidiInputDevices.end(); iter++) { |
// check if there are still sampler engines connected to this device |
624 |
if (iter->second == pDevice) { |
for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
625 |
// check if there are still sampler engines connected to this device |
iterChan != mSamplerChannels.end(); iterChan++ |
626 |
for (uint i = 0; i < SamplerChannels(); i++) |
) if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device."); |
627 |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
|
628 |
|
fireMidiDeviceToBeDestroyed(pDevice); |
629 |
// disable device |
MidiInputDeviceFactory::Destroy(pDevice); |
630 |
pDevice->StopListen(); |
fireMidiDeviceCountChanged(MidiInputDevices()); |
631 |
|
} |
632 |
|
} |
633 |
|
|
634 |
// remove device from the device list |
void Sampler::DestroyAllMidiInputDevices() throw (Exception) { |
635 |
mMidiInputDevices.erase(iter); |
/* |
636 |
|
* In maps iterator invalidation occurs when the iterator point |
637 |
|
* to the element that is being erased. So we need to copy the map |
638 |
|
* by calling GetMidiInputDevices() to prevent that. |
639 |
|
*/ |
640 |
|
std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices(); |
641 |
|
std::map<uint, MidiInputDevice*>::iterator iter = devs.begin(); |
642 |
|
for (; iter != devs.end(); iter++) { |
643 |
|
MidiInputDevice* pDevice = iter->second; |
644 |
|
|
645 |
// destroy and free device from memory |
// skip non-autonomous devices |
646 |
delete pDevice; |
if (!pDevice->isAutonomousDevice()) continue; |
647 |
|
|
648 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
DestroyMidiInputDevice(pDevice); |
|
break; |
|
|
} |
|
649 |
} |
} |
650 |
} |
} |
651 |
|
|
653 |
// create new device |
// create new device |
654 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
655 |
|
|
656 |
// add new device to the midi device list |
fireMidiDeviceCreated(pDevice); |
657 |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
fireMidiDeviceCountChanged(MidiInputDevices()); |
|
if (!mMidiInputDevices[i]) { |
|
|
mMidiInputDevices[i] = pDevice; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
|
658 |
return pDevice; |
return pDevice; |
659 |
} |
} |
660 |
|
|
661 |
|
int Sampler::GetDiskStreamCount() { |
662 |
|
int count = 0; |
663 |
|
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
664 |
|
|
665 |
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
666 |
|
count += (*it)->DiskStreamCount(); |
667 |
|
} |
668 |
|
|
669 |
|
return count; |
670 |
|
} |
671 |
|
|
672 |
int Sampler::GetVoiceCount() { |
int Sampler::GetVoiceCount() { |
673 |
int count = 0; |
int count = 0; |
674 |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
683 |
void Sampler::Reset() { |
void Sampler::Reset() { |
684 |
// delete sampler channels |
// delete sampler channels |
685 |
try { |
try { |
686 |
while (true) { |
RemoveAllSamplerChannels(); |
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
|
|
if (iter == mSamplerChannels.end()) break; |
|
|
RemoveSamplerChannel(iter->second); |
|
|
} |
|
687 |
} |
} |
688 |
catch(...) { |
catch(...) { |
689 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
692 |
|
|
693 |
// delete midi input devices |
// delete midi input devices |
694 |
try { |
try { |
695 |
while (true) { |
DestroyAllMidiInputDevices(); |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
|
|
if (iter == mMidiInputDevices.end()) break; |
|
|
DestroyMidiInputDevice(iter->second); |
|
|
} |
|
696 |
} |
} |
697 |
catch(...) { |
catch(...) { |
698 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
701 |
|
|
702 |
// delete audio output devices |
// delete audio output devices |
703 |
try { |
try { |
704 |
while (true) { |
DestroyAllAudioOutputDevices(); |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
|
|
if (iter == mAudioOutputDevices.end()) break; |
|
|
DestroyAudioOutputDevice(iter->second); |
|
|
} |
|
705 |
} |
} |
706 |
catch(...) { |
catch(...) { |
707 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
708 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
709 |
} |
} |
710 |
|
|
711 |
|
// delete MIDI instrument maps |
712 |
|
try { |
713 |
|
MidiInstrumentMapper::RemoveAllMaps(); |
714 |
|
} |
715 |
|
catch(...) { |
716 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
717 |
|
exit(EXIT_FAILURE); |
718 |
|
} |
719 |
|
|
720 |
|
// unload all instrument editor DLLs |
721 |
|
InstrumentEditorFactory::ClosePlugins(); |
722 |
|
} |
723 |
|
|
724 |
|
bool Sampler::EnableDenormalsAreZeroMode() { |
725 |
|
Features::detect(); |
726 |
|
return Features::enableDenormalsAreZeroMode(); |
727 |
|
} |
728 |
|
|
729 |
|
void Sampler::fireStatistics() { |
730 |
|
static const LSCPEvent::event_t eventsArr[] = { |
731 |
|
LSCPEvent::event_voice_count, LSCPEvent::event_stream_count, |
732 |
|
LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count |
733 |
|
}; |
734 |
|
static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4); |
735 |
|
|
736 |
|
if (LSCPServer::EventSubscribers(events)) |
737 |
|
{ |
738 |
|
LSCPServer::LockRTNotify(); |
739 |
|
std::map<uint,SamplerChannel*> channels = GetSamplerChannels(); |
740 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
741 |
|
for (; iter != channels.end(); iter++) { |
742 |
|
SamplerChannel* pSamplerChannel = iter->second; |
743 |
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
744 |
|
if (!pEngineChannel) continue; |
745 |
|
Engine* pEngine = pEngineChannel->GetEngine(); |
746 |
|
if (!pEngine) continue; |
747 |
|
fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount()); |
748 |
|
fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); |
749 |
|
fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); |
750 |
|
} |
751 |
|
|
752 |
|
fireTotalStreamCountChanged(GetDiskStreamCount()); |
753 |
|
fireTotalVoiceCountChanged(GetVoiceCount()); |
754 |
|
|
755 |
|
LSCPServer::UnlockRTNotify(); |
756 |
|
} |
757 |
} |
} |
758 |
|
|
759 |
|
#if defined(WIN32) |
760 |
|
static HINSTANCE dllInstance = NULL; |
761 |
|
|
762 |
|
String Sampler::GetInstallDir() { |
763 |
|
char buf[MAX_PATH + 1]; |
764 |
|
if (GetModuleFileName(dllInstance, buf, MAX_PATH)) { |
765 |
|
String s(buf); |
766 |
|
size_t n = s.rfind('\\'); |
767 |
|
if (n != String::npos) { |
768 |
|
return s.substr(0, n); |
769 |
|
} |
770 |
|
} |
771 |
|
return ""; |
772 |
|
} |
773 |
|
#endif |
774 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
775 |
|
|
776 |
|
#if defined(WIN32) |
777 |
|
extern "C" { |
778 |
|
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
779 |
|
{ |
780 |
|
switch (reason) { |
781 |
|
case DLL_PROCESS_ATTACH: |
782 |
|
LinuxSampler::dllInstance = instance; |
783 |
|
break; |
784 |
|
} |
785 |
|
return TRUE; |
786 |
|
} |
787 |
|
} |
788 |
|
#endif |