--- linuxsampler/trunk/src/Sampler.cpp 2006/12/17 22:35:01 981 +++ linuxsampler/trunk/src/Sampler.cpp 2009/02/16 17:56:50 1835 @@ -3,20 +3,20 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005, 2006 Christian Schoenebeck * + * Copyright (C) 2005 - 2008 Christian Schoenebeck * * * - * This program is free software; you can redistribute it and/or modify * + * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * - * This program is distributed in the hope that it will be useful, * + * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software * + * along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * * MA 02111-1307 USA * ***************************************************************************/ @@ -25,10 +25,14 @@ #include "Sampler.h" +#include "common/global_private.h" #include "engines/EngineFactory.h" #include "engines/EngineChannelFactory.h" +#include "plugins/InstrumentEditorFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" +#include "drivers/midi/MidiInstrumentMapper.h" +#include "common/Features.h" #include "network/lscpserver.h" namespace LinuxSampler { @@ -66,13 +70,21 @@ void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { dmsg(2,("SamplerChannel: Assigning engine type...")); + + if (pEngineChannel) { + if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { + dmsg(2,("OK\n")); + return; + } + } + + fireEngineToBeChanged(); // create new engine channel EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); if (!pNewEngineChannel) throw Exception("Unknown engine type"); - //FIXME: hack to allow fast retrieval of engine channel's sampler channel index - pNewEngineChannel->iSamplerChannelIndex = Index(); + pNewEngineChannel->SetSamplerChannel(this); // dereference midi input port. MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); @@ -103,7 +115,7 @@ this->iMidiPort = 0; pEngineChannel->StatusChanged(true); - + fireEngineChanged(); dmsg(2,("OK\n")); } @@ -204,6 +216,34 @@ throw Exception("Internal error: SamplerChannel index not found"); } + Sampler* SamplerChannel::GetSampler() { + return pSampler; + } + + void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { + llEngineChangeListeners.AddListener(l); + } + + void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) { + llEngineChangeListeners.RemoveListener(l); + } + + void SamplerChannel::RemoveAllEngineChangeListeners() { + llEngineChangeListeners.RemoveAllListeners(); + } + + void SamplerChannel::fireEngineToBeChanged() { + for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { + llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index()); + } + } + + void SamplerChannel::fireEngineChanged() { + for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { + llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); + } + } + MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { MidiInputPort* pMidiInputPort = NULL; MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); @@ -218,6 +258,8 @@ // * Sampler Sampler::Sampler() { + eventHandler.SetSampler(this); + uiOldTotalVoiceCount = uiOldTotalStreamCount = 0; } Sampler::~Sampler() { @@ -228,12 +270,201 @@ return mSamplerChannels.size(); } + void Sampler::AddChannelCountListener(ChannelCountListener* l) { + llChannelCountListeners.AddListener(l); + } + + void Sampler::RemoveChannelCountListener(ChannelCountListener* l) { + llChannelCountListeners.RemoveListener(l); + } + + void Sampler::fireChannelCountChanged(int NewCount) { + for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { + llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount); + } + } + + void Sampler::fireChannelAdded(SamplerChannel* pChannel) { + for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { + llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel); + } + } + + void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) { + for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { + llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel); + } + } + + void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { + llAudioDeviceCountListeners.AddListener(l); + } + + void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) { + llAudioDeviceCountListeners.RemoveListener(l); + } + + void Sampler::fireAudioDeviceCountChanged(int NewCount) { + for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) { + llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount); + } + } + + void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) { + llMidiDeviceCountListeners.AddListener(l); + } + + void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) { + llMidiDeviceCountListeners.RemoveListener(l); + } + + void Sampler::fireMidiDeviceCountChanged(int NewCount) { + for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { + llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount); + } + } + + void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { + for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { + llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice); + } + } + + void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) { + for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { + llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice); + } + } + + void Sampler::AddVoiceCountListener(VoiceCountListener* l) { + llVoiceCountListeners.AddListener(l); + } + + void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) { + llVoiceCountListeners.RemoveListener(l); + } + + void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { + std::map::iterator it = mOldVoiceCounts.find(ChannelId); + if (it != mOldVoiceCounts.end()) { + uint oldCount = it->second; + if (NewCount == oldCount) return; + } + + mOldVoiceCounts[ChannelId] = NewCount; + + for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { + llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); + } + } + + void Sampler::AddStreamCountListener(StreamCountListener* l) { + llStreamCountListeners.AddListener(l); + } + + void Sampler::RemoveStreamCountListener(StreamCountListener* l) { + llStreamCountListeners.RemoveListener(l); + } + + void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { + std::map::iterator it = mOldStreamCounts.find(ChannelId); + if (it != mOldStreamCounts.end()) { + uint oldCount = it->second; + if (NewCount == oldCount) return; + } + + mOldStreamCounts[ChannelId] = NewCount; + + for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { + llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); + } + } + + void Sampler::AddBufferFillListener(BufferFillListener* l) { + llBufferFillListeners.AddListener(l); + } + + void Sampler::RemoveBufferFillListener(BufferFillListener* l) { + llBufferFillListeners.RemoveListener(l); + } + + void Sampler::fireBufferFillChanged(int ChannelId, String FillData) { + for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) { + llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData); + } + } + + void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) { + llTotalStreamCountListeners.AddListener(l); + } + + void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) { + llTotalStreamCountListeners.RemoveListener(l); + } + + void Sampler::fireTotalStreamCountChanged(int NewCount) { + if (NewCount == uiOldTotalStreamCount) return; + uiOldTotalStreamCount = NewCount; + + for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { + llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); + } + } + + void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { + llTotalVoiceCountListeners.AddListener(l); + } + + void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) { + llTotalVoiceCountListeners.RemoveListener(l); + } + + void Sampler::fireTotalVoiceCountChanged(int NewCount) { + if (NewCount == uiOldTotalVoiceCount) return; + uiOldTotalVoiceCount = NewCount; + + for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { + llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); + } + } + + void Sampler::AddFxSendCountListener(FxSendCountListener* l) { + llFxSendCountListeners.AddListener(l); + } + + void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) { + llFxSendCountListeners.RemoveListener(l); + } + + void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) { + for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) { + llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount); + } + } + + void Sampler::EventHandler::EngineToBeChanged(int ChannelId) { + // nothing to do here + } + + void Sampler::EventHandler::EngineChanged(int ChannelId) { + EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); + if(engineChannel == NULL) return; + engineChannel->AddFxSendCountListener(this); + } + + void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) { + pSampler->fireFxSendCountChanged(ChannelId, NewCount); + } + + SamplerChannel* Sampler::AddSamplerChannel() { // if there's no sampler channel yet if (!mSamplerChannels.size()) { SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[0] = pChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); + fireChannelAdded(pChannel); + fireChannelCountChanged(1); + pChannel->AddEngineChangeListener(&eventHandler); return pChannel; } @@ -248,7 +479,9 @@ // we found an unused index, so insert the new channel there SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[i] = pChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); + fireChannelAdded(pChannel); + fireChannelCountChanged(SamplerChannels()); + pChannel->AddEngineChangeListener(&eventHandler); return pChannel; } throw Exception("Internal error: could not find unoccupied sampler channel index."); @@ -257,7 +490,9 @@ // we have not reached the index limit so we just add the channel past the highest index SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[lastIndex + 1] = pChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); + fireChannelAdded(pChannel); + fireChannelCountChanged(SamplerChannels()); + pChannel->AddEngineChangeListener(&eventHandler); return pChannel; } @@ -273,9 +508,13 @@ SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); for (; iterChan != mSamplerChannels.end(); iterChan++) { if (iterChan->second == pSamplerChannel) { + fireChannelToBeRemoved(pSamplerChannel); + mOldVoiceCounts.erase(pSamplerChannel->Index()); + mOldStreamCounts.erase(pSamplerChannel->Index()); + pSamplerChannel->RemoveAllEngineChangeListeners(); mSamplerChannels.erase(iterChan); delete pSamplerChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); + fireChannelCountChanged(SamplerChannels()); return; } } @@ -287,6 +526,19 @@ RemoveSamplerChannel(pChannel); } + void Sampler::RemoveAllSamplerChannels() { + /* + * In maps iterator invalidation occurs when the iterator point + * to the element that is being erased. So we need to copy the map + * by calling GetSamplerChannels() to prevent that. + */ + SamplerChannelMap chns = GetSamplerChannels(); + SamplerChannelMap::iterator iter = chns.begin(); + for(; iter != chns.end(); iter++) { + RemoveSamplerChannel(iter->second); + } + } + std::vector Sampler::AvailableAudioOutputDrivers() { return AudioOutputDeviceFactory::AvailableDrivers(); } @@ -311,7 +563,7 @@ } } - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); + fireAudioDeviceCountChanged(AudioOutputDevices()); return pDevice; } @@ -336,8 +588,9 @@ for (; iter != mAudioOutputDevices.end(); iter++) { if (iter->second == pDevice) { // check if there are still sampler engines connected to this device - for (uint i = 0; i < SamplerChannels(); i++) - if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); + for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); + iterChan != mSamplerChannels.end(); iterChan++) + if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device."); // disable device pDevice->Stop(); @@ -348,19 +601,35 @@ // destroy and free device from memory delete pDevice; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); + fireAudioDeviceCountChanged(AudioOutputDevices()); break; } } } + void Sampler::DestroyAllAudioOutputDevices() throw (Exception) { + /* + * In maps iterator invalidation occurs when the iterator point + * to the element that is being erased. So we need to copy the map + * by calling GetAudioOutputDevices() to prevent that. + */ + AudioOutputDeviceMap devs = GetAudioOutputDevices(); + AudioOutputDeviceMap::iterator iter = devs.begin(); + for(; iter != devs.end(); iter++) { + DestroyAudioOutputDevice(iter->second); + } + } + void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); for (; iter != mMidiInputDevices.end(); iter++) { if (iter->second == pDevice) { // check if there are still sampler engines connected to this device - for (uint i = 0; i < SamplerChannels(); i++) - if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); + for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); + iterChan != mSamplerChannels.end(); iterChan++) + if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device."); + + fireMidiDeviceToBeDestroyed(pDevice); // disable device pDevice->StopListen(); @@ -371,12 +640,25 @@ // destroy and free device from memory delete pDevice; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); + fireMidiDeviceCountChanged(MidiInputDevices()); break; } } } + void Sampler::DestroyAllMidiInputDevices() throw (Exception) { + /* + * In maps iterator invalidation occurs when the iterator point + * to the element that is being erased. So we need to copy the map + * by calling GetMidiInputDevices() to prevent that. + */ + MidiInputDeviceMap devs = GetMidiInputDevices(); + MidiInputDeviceMap::iterator iter = devs.begin(); + for(; iter != devs.end(); iter++) { + DestroyMidiInputDevice(iter->second); + } + } + MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (Exception) { // create new device MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); @@ -389,10 +671,22 @@ } } - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); + fireMidiDeviceCreated(pDevice); + fireMidiDeviceCountChanged(MidiInputDevices()); return pDevice; } + int Sampler::GetDiskStreamCount() { + int count = 0; + std::set::iterator it = EngineFactory::EngineInstances().begin(); + + for(; it != EngineFactory::EngineInstances().end(); it++) { + count += (*it)->DiskStreamCount(); + } + + return count; + } + int Sampler::GetVoiceCount() { int count = 0; std::set::iterator it = EngineFactory::EngineInstances().begin(); @@ -407,11 +701,7 @@ void Sampler::Reset() { // delete sampler channels try { - while (true) { - SamplerChannelMap::iterator iter = mSamplerChannels.begin(); - if (iter == mSamplerChannels.end()) break; - RemoveSamplerChannel(iter->second); - } + RemoveAllSamplerChannels(); } catch(...) { std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; @@ -420,11 +710,7 @@ // delete midi input devices try { - while (true) { - MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); - if (iter == mMidiInputDevices.end()) break; - DestroyMidiInputDevice(iter->second); - } + DestroyAllMidiInputDevices(); } catch(...) { std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; @@ -433,16 +719,59 @@ // delete audio output devices try { - while (true) { - AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); - if (iter == mAudioOutputDevices.end()) break; - DestroyAudioOutputDevice(iter->second); - } + DestroyAllAudioOutputDevices(); } catch(...) { std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; exit(EXIT_FAILURE); } + + // delete MIDI instrument maps + try { + MidiInstrumentMapper::RemoveAllMaps(); + } + catch(...) { + std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; + exit(EXIT_FAILURE); + } + + // unload all instrument editor DLLs + InstrumentEditorFactory::ClosePlugins(); + } + + bool Sampler::EnableDenormalsAreZeroMode() { + Features::detect(); + return Features::enableDenormalsAreZeroMode(); + } + + void Sampler::fireStatistics() { + static const LSCPEvent::event_t eventsArr[] = { + LSCPEvent::event_voice_count, LSCPEvent::event_stream_count, + LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count + }; + static const std::list events(eventsArr, eventsArr + 4); + + if (LSCPServer::EventSubscribers(events)) + { + LSCPServer::LockRTNotify(); + std::map channels = GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + SamplerChannel* pSamplerChannel = iter->second; + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) continue; + Engine* pEngine = pEngineChannel->GetEngine(); + if (!pEngine) continue; + fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount()); + fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); + fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); + } + + fireTotalStreamCountChanged(GetDiskStreamCount()); + fireTotalVoiceCountChanged(GetVoiceCount()); + + LSCPServer::UnlockRTNotify(); + } } } // namespace LinuxSampler