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 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This library 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 * |
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" |
#include "drivers/midi/MidiInstrumentMapper.h" |
35 |
|
#include "common/Features.h" |
36 |
|
|
37 |
namespace LinuxSampler { |
namespace LinuxSampler { |
38 |
|
|
69 |
|
|
70 |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
71 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
72 |
|
|
73 |
|
if (pEngineChannel) { |
74 |
|
if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { |
75 |
|
dmsg(2,("OK\n")); |
76 |
|
return; |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
fireEngineToBeChanged(); |
81 |
|
|
82 |
// create new engine channel |
// create new engine channel |
83 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
84 |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
85 |
|
|
86 |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
pNewEngineChannel->SetSamplerChannel(this); |
|
pNewEngineChannel->iSamplerChannelIndex = Index(); |
|
87 |
|
|
88 |
// dereference midi input port. |
// dereference midi input port. |
89 |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
215 |
throw Exception("Internal error: SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
216 |
} |
} |
217 |
|
|
218 |
|
Sampler* SamplerChannel::GetSampler() { |
219 |
|
return pSampler; |
220 |
|
} |
221 |
|
|
222 |
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
223 |
llEngineChangeListeners.AddListener(l); |
llEngineChangeListeners.AddListener(l); |
224 |
} |
} |
231 |
llEngineChangeListeners.RemoveAllListeners(); |
llEngineChangeListeners.RemoveAllListeners(); |
232 |
} |
} |
233 |
|
|
234 |
|
void SamplerChannel::fireEngineToBeChanged() { |
235 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
236 |
|
llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index()); |
237 |
|
} |
238 |
|
} |
239 |
|
|
240 |
void SamplerChannel::fireEngineChanged() { |
void SamplerChannel::fireEngineChanged() { |
241 |
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
242 |
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
282 |
} |
} |
283 |
} |
} |
284 |
|
|
285 |
|
void Sampler::fireChannelAdded(SamplerChannel* pChannel) { |
286 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
287 |
|
llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel); |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
|
void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) { |
292 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
293 |
|
llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel); |
294 |
|
} |
295 |
|
} |
296 |
|
|
297 |
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
298 |
llAudioDeviceCountListeners.AddListener(l); |
llAudioDeviceCountListeners.AddListener(l); |
299 |
} |
} |
322 |
} |
} |
323 |
} |
} |
324 |
|
|
325 |
|
void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
326 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
327 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice); |
328 |
|
} |
329 |
|
} |
330 |
|
|
331 |
|
void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) { |
332 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
333 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice); |
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
338 |
llVoiceCountListeners.AddListener(l); |
llVoiceCountListeners.AddListener(l); |
339 |
} |
} |
376 |
} |
} |
377 |
} |
} |
378 |
|
|
379 |
|
void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) { |
380 |
|
llTotalStreamCountListeners.AddListener(l); |
381 |
|
} |
382 |
|
|
383 |
|
void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) { |
384 |
|
llTotalStreamCountListeners.RemoveListener(l); |
385 |
|
} |
386 |
|
|
387 |
|
void Sampler::fireTotalStreamCountChanged(int NewCount) { |
388 |
|
for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { |
389 |
|
llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); |
390 |
|
} |
391 |
|
} |
392 |
|
|
393 |
void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { |
void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { |
394 |
llTotalVoiceCountListeners.AddListener(l); |
llTotalVoiceCountListeners.AddListener(l); |
395 |
} |
} |
418 |
} |
} |
419 |
} |
} |
420 |
|
|
421 |
|
void Sampler::EventHandler::EngineToBeChanged(int ChannelId) { |
422 |
|
// nothing to do here |
423 |
|
} |
424 |
|
|
425 |
void Sampler::EventHandler::EngineChanged(int ChannelId) { |
void Sampler::EventHandler::EngineChanged(int ChannelId) { |
426 |
EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); |
EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); |
427 |
if(engineChannel == NULL) return; |
if(engineChannel == NULL) return; |
438 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
439 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
440 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
441 |
|
fireChannelAdded(pChannel); |
442 |
fireChannelCountChanged(1); |
fireChannelCountChanged(1); |
443 |
pChannel->AddEngineChangeListener(&eventHandler); |
pChannel->AddEngineChangeListener(&eventHandler); |
444 |
return pChannel; |
return pChannel; |
455 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
456 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
457 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
458 |
|
fireChannelAdded(pChannel); |
459 |
fireChannelCountChanged(SamplerChannels()); |
fireChannelCountChanged(SamplerChannels()); |
460 |
pChannel->AddEngineChangeListener(&eventHandler); |
pChannel->AddEngineChangeListener(&eventHandler); |
461 |
return pChannel; |
return pChannel; |
466 |
// 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 |
467 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
468 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
469 |
|
fireChannelAdded(pChannel); |
470 |
fireChannelCountChanged(SamplerChannels()); |
fireChannelCountChanged(SamplerChannels()); |
471 |
pChannel->AddEngineChangeListener(&eventHandler); |
pChannel->AddEngineChangeListener(&eventHandler); |
472 |
return pChannel; |
return pChannel; |
484 |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
485 |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
486 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
487 |
|
fireChannelToBeRemoved(pSamplerChannel); |
488 |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
489 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
490 |
delete pSamplerChannel; |
delete pSamplerChannel; |
575 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (uint i = 0; i < SamplerChannels(); i++) |
576 |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
577 |
|
|
578 |
|
fireMidiDeviceToBeDestroyed(pDevice); |
579 |
|
|
580 |
// disable device |
// disable device |
581 |
pDevice->StopListen(); |
pDevice->StopListen(); |
582 |
|
|
604 |
} |
} |
605 |
} |
} |
606 |
|
|
607 |
|
fireMidiDeviceCreated(pDevice); |
608 |
fireMidiDeviceCountChanged(MidiInputDevices()); |
fireMidiDeviceCountChanged(MidiInputDevices()); |
609 |
return pDevice; |
return pDevice; |
610 |
} |
} |
611 |
|
|
612 |
|
int Sampler::GetDiskStreamCount() { |
613 |
|
int count = 0; |
614 |
|
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
615 |
|
|
616 |
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
617 |
|
count += (*it)->DiskStreamCount(); |
618 |
|
} |
619 |
|
|
620 |
|
return count; |
621 |
|
} |
622 |
|
|
623 |
int Sampler::GetVoiceCount() { |
int Sampler::GetVoiceCount() { |
624 |
int count = 0; |
int count = 0; |
625 |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
679 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
680 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
681 |
} |
} |
682 |
|
|
683 |
|
// unload all instrument editor DLLs |
684 |
|
InstrumentEditorFactory::ClosePlugins(); |
685 |
|
} |
686 |
|
|
687 |
|
bool Sampler::EnableDenormalsAreZeroMode() { |
688 |
|
Features::detect(); |
689 |
|
return Features::enableDenormalsAreZeroMode(); |
690 |
} |
} |
691 |
|
|
692 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |