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 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2009 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 * |
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 |
|
#include "network/lscpserver.h" |
37 |
|
|
38 |
namespace LinuxSampler { |
namespace LinuxSampler { |
39 |
|
|
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()); |
216 |
throw Exception("Internal error: SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
217 |
} |
} |
218 |
|
|
219 |
|
Sampler* SamplerChannel::GetSampler() { |
220 |
|
return pSampler; |
221 |
|
} |
222 |
|
|
223 |
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
224 |
llEngineChangeListeners.AddListener(l); |
llEngineChangeListeners.AddListener(l); |
225 |
} |
} |
259 |
|
|
260 |
Sampler::Sampler() { |
Sampler::Sampler() { |
261 |
eventHandler.SetSampler(this); |
eventHandler.SetSampler(this); |
262 |
|
uiOldTotalVoiceCount = uiOldTotalStreamCount = 0; |
263 |
} |
} |
264 |
|
|
265 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
324 |
} |
} |
325 |
} |
} |
326 |
|
|
327 |
|
void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
328 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
329 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice); |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
|
void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) { |
334 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
335 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice); |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
340 |
llVoiceCountListeners.AddListener(l); |
llVoiceCountListeners.AddListener(l); |
341 |
} |
} |
345 |
} |
} |
346 |
|
|
347 |
void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { |
void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { |
348 |
|
std::map<uint, uint>::iterator it = mOldVoiceCounts.find(ChannelId); |
349 |
|
if (it != mOldVoiceCounts.end()) { |
350 |
|
uint oldCount = it->second; |
351 |
|
if (NewCount == oldCount) return; |
352 |
|
} |
353 |
|
|
354 |
|
mOldVoiceCounts[ChannelId] = NewCount; |
355 |
|
|
356 |
for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { |
357 |
llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); |
llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); |
358 |
} |
} |
367 |
} |
} |
368 |
|
|
369 |
void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { |
void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { |
370 |
|
std::map<uint, uint>::iterator it = mOldStreamCounts.find(ChannelId); |
371 |
|
if (it != mOldStreamCounts.end()) { |
372 |
|
uint oldCount = it->second; |
373 |
|
if (NewCount == oldCount) return; |
374 |
|
} |
375 |
|
|
376 |
|
mOldStreamCounts[ChannelId] = NewCount; |
377 |
|
|
378 |
for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { |
379 |
llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); |
llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); |
380 |
} |
} |
403 |
} |
} |
404 |
|
|
405 |
void Sampler::fireTotalStreamCountChanged(int NewCount) { |
void Sampler::fireTotalStreamCountChanged(int NewCount) { |
406 |
|
if (NewCount == uiOldTotalStreamCount) return; |
407 |
|
uiOldTotalStreamCount = NewCount; |
408 |
|
|
409 |
for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { |
410 |
llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); |
llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); |
411 |
} |
} |
420 |
} |
} |
421 |
|
|
422 |
void Sampler::fireTotalVoiceCountChanged(int NewCount) { |
void Sampler::fireTotalVoiceCountChanged(int NewCount) { |
423 |
|
if (NewCount == uiOldTotalVoiceCount) return; |
424 |
|
uiOldTotalVoiceCount = NewCount; |
425 |
|
|
426 |
for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { |
427 |
llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); |
llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); |
428 |
} |
} |
509 |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
510 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
511 |
fireChannelToBeRemoved(pSamplerChannel); |
fireChannelToBeRemoved(pSamplerChannel); |
512 |
|
mOldVoiceCounts.erase(pSamplerChannel->Index()); |
513 |
|
mOldStreamCounts.erase(pSamplerChannel->Index()); |
514 |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
515 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
516 |
delete pSamplerChannel; |
delete pSamplerChannel; |
526 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
527 |
} |
} |
528 |
|
|
529 |
|
void Sampler::RemoveAllSamplerChannels() { |
530 |
|
/* |
531 |
|
* In maps iterator invalidation occurs when the iterator point |
532 |
|
* to the element that is being erased. So we need to copy the map |
533 |
|
* by calling GetSamplerChannels() to prevent that. |
534 |
|
*/ |
535 |
|
SamplerChannelMap chns = GetSamplerChannels(); |
536 |
|
SamplerChannelMap::iterator iter = chns.begin(); |
537 |
|
for(; iter != chns.end(); iter++) { |
538 |
|
RemoveSamplerChannel(iter->second); |
539 |
|
} |
540 |
|
} |
541 |
|
|
542 |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
543 |
return AudioOutputDeviceFactory::AvailableDrivers(); |
return AudioOutputDeviceFactory::AvailableDrivers(); |
544 |
} |
} |
588 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
589 |
if (iter->second == pDevice) { |
if (iter->second == pDevice) { |
590 |
// check if there are still sampler engines connected to this device |
// check if there are still sampler engines connected to this device |
591 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
592 |
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
iterChan != mSamplerChannels.end(); iterChan++) |
593 |
|
if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device."); |
594 |
|
|
595 |
// disable device |
// disable device |
596 |
pDevice->Stop(); |
pDevice->Stop(); |
607 |
} |
} |
608 |
} |
} |
609 |
|
|
610 |
|
void Sampler::DestroyAllAudioOutputDevices() throw (Exception) { |
611 |
|
/* |
612 |
|
* In maps iterator invalidation occurs when the iterator point |
613 |
|
* to the element that is being erased. So we need to copy the map |
614 |
|
* by calling GetAudioOutputDevices() to prevent that. |
615 |
|
*/ |
616 |
|
AudioOutputDeviceMap devs = GetAudioOutputDevices(); |
617 |
|
AudioOutputDeviceMap::iterator iter = devs.begin(); |
618 |
|
for(; iter != devs.end(); iter++) { |
619 |
|
DestroyAudioOutputDevice(iter->second); |
620 |
|
} |
621 |
|
} |
622 |
|
|
623 |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
624 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
625 |
for (; iter != mMidiInputDevices.end(); iter++) { |
for (; iter != mMidiInputDevices.end(); iter++) { |
626 |
if (iter->second == pDevice) { |
if (iter->second == pDevice) { |
627 |
// check if there are still sampler engines connected to this device |
// check if there are still sampler engines connected to this device |
628 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
629 |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
iterChan != mSamplerChannels.end(); iterChan++) |
630 |
|
if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device."); |
631 |
|
|
632 |
|
fireMidiDeviceToBeDestroyed(pDevice); |
633 |
|
|
634 |
// disable device |
// disable device |
635 |
pDevice->StopListen(); |
pDevice->StopListen(); |
646 |
} |
} |
647 |
} |
} |
648 |
|
|
649 |
|
void Sampler::DestroyAllMidiInputDevices() throw (Exception) { |
650 |
|
/* |
651 |
|
* In maps iterator invalidation occurs when the iterator point |
652 |
|
* to the element that is being erased. So we need to copy the map |
653 |
|
* by calling GetMidiInputDevices() to prevent that. |
654 |
|
*/ |
655 |
|
MidiInputDeviceMap devs = GetMidiInputDevices(); |
656 |
|
MidiInputDeviceMap::iterator iter = devs.begin(); |
657 |
|
for(; iter != devs.end(); iter++) { |
658 |
|
DestroyMidiInputDevice(iter->second); |
659 |
|
} |
660 |
|
} |
661 |
|
|
662 |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
663 |
// create new device |
// create new device |
664 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
671 |
} |
} |
672 |
} |
} |
673 |
|
|
674 |
|
fireMidiDeviceCreated(pDevice); |
675 |
fireMidiDeviceCountChanged(MidiInputDevices()); |
fireMidiDeviceCountChanged(MidiInputDevices()); |
676 |
return pDevice; |
return pDevice; |
677 |
} |
} |
701 |
void Sampler::Reset() { |
void Sampler::Reset() { |
702 |
// delete sampler channels |
// delete sampler channels |
703 |
try { |
try { |
704 |
while (true) { |
RemoveAllSamplerChannels(); |
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
|
|
if (iter == mSamplerChannels.end()) break; |
|
|
RemoveSamplerChannel(iter->second); |
|
|
} |
|
705 |
} |
} |
706 |
catch(...) { |
catch(...) { |
707 |
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; |
710 |
|
|
711 |
// delete midi input devices |
// delete midi input devices |
712 |
try { |
try { |
713 |
while (true) { |
DestroyAllMidiInputDevices(); |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
|
|
if (iter == mMidiInputDevices.end()) break; |
|
|
DestroyMidiInputDevice(iter->second); |
|
|
} |
|
714 |
} |
} |
715 |
catch(...) { |
catch(...) { |
716 |
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; |
719 |
|
|
720 |
// delete audio output devices |
// delete audio output devices |
721 |
try { |
try { |
722 |
while (true) { |
DestroyAllAudioOutputDevices(); |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
|
|
if (iter == mAudioOutputDevices.end()) break; |
|
|
DestroyAudioOutputDevice(iter->second); |
|
|
} |
|
723 |
} |
} |
724 |
catch(...) { |
catch(...) { |
725 |
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; |
739 |
InstrumentEditorFactory::ClosePlugins(); |
InstrumentEditorFactory::ClosePlugins(); |
740 |
} |
} |
741 |
|
|
742 |
|
bool Sampler::EnableDenormalsAreZeroMode() { |
743 |
|
Features::detect(); |
744 |
|
return Features::enableDenormalsAreZeroMode(); |
745 |
|
} |
746 |
|
|
747 |
|
void Sampler::fireStatistics() { |
748 |
|
static const LSCPEvent::event_t eventsArr[] = { |
749 |
|
LSCPEvent::event_voice_count, LSCPEvent::event_stream_count, |
750 |
|
LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count |
751 |
|
}; |
752 |
|
static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4); |
753 |
|
|
754 |
|
if (LSCPServer::EventSubscribers(events)) |
755 |
|
{ |
756 |
|
LSCPServer::LockRTNotify(); |
757 |
|
std::map<uint,SamplerChannel*> channels = GetSamplerChannels(); |
758 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
759 |
|
for (; iter != channels.end(); iter++) { |
760 |
|
SamplerChannel* pSamplerChannel = iter->second; |
761 |
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
762 |
|
if (!pEngineChannel) continue; |
763 |
|
Engine* pEngine = pEngineChannel->GetEngine(); |
764 |
|
if (!pEngine) continue; |
765 |
|
fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount()); |
766 |
|
fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); |
767 |
|
fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); |
768 |
|
} |
769 |
|
|
770 |
|
fireTotalStreamCountChanged(GetDiskStreamCount()); |
771 |
|
fireTotalVoiceCountChanged(GetVoiceCount()); |
772 |
|
|
773 |
|
LSCPServer::UnlockRTNotify(); |
774 |
|
} |
775 |
|
} |
776 |
|
|
777 |
|
#if defined(WIN32) |
778 |
|
static HINSTANCE dllInstance = NULL; |
779 |
|
|
780 |
|
String Sampler::GetInstallDir() { |
781 |
|
char buf[MAX_PATH + 1]; |
782 |
|
if (GetModuleFileName(dllInstance, buf, MAX_PATH)) { |
783 |
|
String s(buf); |
784 |
|
size_t n = s.rfind('\\'); |
785 |
|
if (n != String::npos) { |
786 |
|
return s.substr(0, n); |
787 |
|
} |
788 |
|
} |
789 |
|
return ""; |
790 |
|
} |
791 |
|
#endif |
792 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
793 |
|
|
794 |
|
#if defined(WIN32) |
795 |
|
extern "C" { |
796 |
|
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
797 |
|
{ |
798 |
|
switch (reason) { |
799 |
|
case DLL_PROCESS_ATTACH: |
800 |
|
LinuxSampler::dllInstance = instance; |
801 |
|
break; |
802 |
|
} |
803 |
|
return TRUE; |
804 |
|
} |
805 |
|
} |
806 |
|
#endif |