--- linuxsampler/trunk/src/Sampler.cpp 2008/09/06 16:44:42 1765 +++ linuxsampler/trunk/src/Sampler.cpp 2009/07/12 10:35:55 1934 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2008 Christian Schoenebeck * + * Copyright (C) 2005 - 2009 Christian Schoenebeck * * * * 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 * @@ -70,7 +70,7 @@ 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")); @@ -259,6 +259,7 @@ Sampler::Sampler() { eventHandler.SetSampler(this); + uiOldTotalVoiceCount = uiOldTotalStreamCount = 0; } Sampler::~Sampler() { @@ -344,6 +345,14 @@ } 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); } @@ -358,6 +367,14 @@ } 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); } @@ -386,6 +403,9 @@ } void Sampler::fireTotalStreamCountChanged(int NewCount) { + if (NewCount == uiOldTotalStreamCount) return; + uiOldTotalStreamCount = NewCount; + for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); } @@ -400,6 +420,9 @@ } void Sampler::fireTotalVoiceCountChanged(int NewCount) { + if (NewCount == uiOldTotalVoiceCount) return; + uiOldTotalVoiceCount = NewCount; + for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); } @@ -486,6 +509,8 @@ 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; @@ -501,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(); } @@ -517,79 +555,85 @@ // create new device AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); - // add new audio device to the audio device list - for (uint i = 0; ; i++) { // seek for a free place starting from the beginning - if (!mAudioOutputDevices[i]) { - mAudioOutputDevices[i] = pDevice; - break; - } - } - fireAudioDeviceCountChanged(AudioOutputDevices()); return pDevice; } uint Sampler::AudioOutputDevices() { - return mAudioOutputDevices.size(); + return AudioOutputDeviceFactory::Devices().size(); } uint Sampler::MidiInputDevices() { - return mMidiInputDevices.size(); + return MidiInputDeviceFactory::Devices().size(); } std::map Sampler::GetAudioOutputDevices() { - return mAudioOutputDevices; + return AudioOutputDeviceFactory::Devices(); } std::map Sampler::GetMidiInputDevices() { - return mMidiInputDevices; + return MidiInputDeviceFactory::Devices(); } void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { - AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); - 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."); - - // disable device - pDevice->Stop(); + if (pDevice) { + // check if there are still sampler engines connected to this 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."); + + //TODO: should we add fireAudioDeviceToBeDestroyed() here ? + AudioOutputDeviceFactory::Destroy(pDevice); + fireAudioDeviceCountChanged(AudioOutputDevices()); + } + } - // remove device from the device list - mAudioOutputDevices.erase(iter); + 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. + */ + std::map devs = GetAudioOutputDevices(); + std::map::iterator iter = devs.begin(); + for (; iter != devs.end(); iter++) { + AudioOutputDevice* pDevice = iter->second; - // destroy and free device from memory - delete pDevice; + // skip non-autonomous devices + if (!pDevice->isAutonomousDevice()) continue; - fireAudioDeviceCountChanged(AudioOutputDevices()); - break; - } + DestroyAudioOutputDevice(pDevice); } } 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."); - - fireMidiDeviceToBeDestroyed(pDevice); - - // disable device - pDevice->StopListen(); + if (pDevice) { + // check if there are still sampler engines connected to this 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); + MidiInputDeviceFactory::Destroy(pDevice); + fireMidiDeviceCountChanged(MidiInputDevices()); + } + } - // remove device from the device list - mMidiInputDevices.erase(iter); + 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. + */ + std::map devs = GetMidiInputDevices(); + std::map::iterator iter = devs.begin(); + for (; iter != devs.end(); iter++) { + MidiInputDevice* pDevice = iter->second; - // destroy and free device from memory - delete pDevice; + // skip non-autonomous devices + if (!pDevice->isAutonomousDevice()) continue; - fireMidiDeviceCountChanged(MidiInputDevices()); - break; - } + DestroyMidiInputDevice(pDevice); } } @@ -597,14 +641,6 @@ // create new device MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); - // add new device to the midi device list - for (uint i = 0; ; i++) { // seek for a free place starting from the beginning - if (!mMidiInputDevices[i]) { - mMidiInputDevices[i] = pDevice; - break; - } - } - fireMidiDeviceCreated(pDevice); fireMidiDeviceCountChanged(MidiInputDevices()); return pDevice; @@ -635,11 +671,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; @@ -648,11 +680,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; @@ -661,11 +689,7 @@ // 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; @@ -711,11 +735,42 @@ fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount()); fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); - fireTotalStreamCountChanged(GetDiskStreamCount()); - fireTotalVoiceCountChanged(GetVoiceCount()); } + + fireTotalStreamCountChanged(GetDiskStreamCount()); + fireTotalVoiceCountChanged(GetVoiceCount()); + LSCPServer::UnlockRTNotify(); } } +#if defined(WIN32) + static HINSTANCE dllInstance = NULL; + + String Sampler::GetInstallDir() { + char buf[MAX_PATH + 1]; + if (GetModuleFileName(dllInstance, buf, MAX_PATH)) { + String s(buf); + size_t n = s.rfind('\\'); + if (n != String::npos) { + return s.substr(0, n); + } + } + return ""; + } +#endif } // namespace LinuxSampler + +#if defined(WIN32) +extern "C" { + BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) + { + switch (reason) { + case DLL_PROCESS_ATTACH: + LinuxSampler::dllInstance = instance; + break; + } + return TRUE; + } +} +#endif