--- linuxsampler/trunk/src/Sampler.cpp 2004/07/13 22:44:13 203 +++ linuxsampler/trunk/src/Sampler.cpp 2005/02/06 21:01:38 359 @@ -27,6 +27,7 @@ #include "drivers/audio/AudioOutputDeviceFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" #include "engines/gig/Engine.h" +#include "network/lscpserver.h" namespace LinuxSampler { @@ -39,13 +40,13 @@ pMidiInputDevice = NULL; pAudioOutputDevice = NULL; midiPort = 0; - midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; + midiChannel = MidiInputPort::midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { if (pEngine) { - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); delete pEngine; @@ -66,7 +67,7 @@ } // dereference midi input port. - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); // disconnect old engine if (pEngine) { if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); @@ -97,37 +98,37 @@ SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); } - void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); } - void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { // dereference old midi input port. - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); // disconnect old device port if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); // new device, port and channel pMidiInputDevice = pDevice; - this->midiPort = MidiPort; + this->midiPort = iMidiPort; this->midiChannel = MidiChannel; // connect new device port pMidiInputPort = GetMidiInputDevicePort(this->midiPort); if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); // Ooops. if (pMidiInputPort == NULL) - throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + "."); + throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); } Engine* SamplerChannel::GetEngine() { return pEngine; } - MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { + MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { return this->midiChannel; } int SamplerChannel::GetMidiInputPort() { - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); } @@ -142,24 +143,26 @@ uint SamplerChannel::Index() { if (iIndex >= 0) return iIndex; - std::vector::iterator iter = pSampler->vSamplerChannels.begin(); - for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) { - if (*iter == this) { - iIndex = i; - return i; + Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin(); + for (; iter != pSampler->mSamplerChannels.end(); iter++) { + if (iter->second == this) { + iIndex = iter->first; + return iIndex; } } - throw LinuxSamplerException("SamplerChannel index not found"); + throw LinuxSamplerException("Internal error: SamplerChannel index not found"); } - MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) { - MidiInputDevice::MidiInputPort *pMidiInputPort = NULL; + MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { + MidiInputPort* pMidiInputPort = NULL; if (pMidiInputDevice) - pMidiInputPort = pMidiInputDevice->GetPort(MidiPort); + pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); return pMidiInputPort; } + + // ****************************************************************** // * Sampler @@ -167,54 +170,61 @@ } Sampler::~Sampler() { - // delete sampler channels - { - std::vector::iterator iter = vSamplerChannels.begin(); - for (; iter != vSamplerChannels.end(); iter++) delete *iter; - } - - // delete midi input devices - { - MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); - for (; iter != mMidiInputDevices.end(); iter++) { - MidiInputDevice* pDevice = iter->second; - pDevice->StopListen(); - delete pDevice; - } - } - - // delete audio output devices - { - AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); - for (; iter != mAudioOutputDevices.end(); iter++) { - AudioOutputDevice* pDevice = iter->second; - pDevice->Stop(); - delete pDevice; - } - } + Reset(); } uint Sampler::SamplerChannels() { - return vSamplerChannels.size(); + return mSamplerChannels.size(); } 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_channels, 1)); + return pChannel; + } + + // get the highest used sampler channel index + uint lastIndex = (--(mSamplerChannels.end()))->first; + + // check if we reached the index limit + if (lastIndex + 1 < lastIndex) { + // search for an unoccupied sampler channel index starting from 0 + for (uint i = 0; i < lastIndex; i++) { + if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue; + // we found an unused index, so insert the new channel there + SamplerChannel* pChannel = new SamplerChannel(this); + mSamplerChannels[i] = pChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i)); + return pChannel; + } + throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); + } + + // we have not reached the index limit so we just add the channel past the highest index SamplerChannel* pChannel = new SamplerChannel(this); - vSamplerChannels.push_back(pChannel); + mSamplerChannels[lastIndex + 1] = pChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1)); return pChannel; } SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { - if (uiSamplerChannel >= SamplerChannels()) return NULL; - return vSamplerChannels[uiSamplerChannel]; + return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL; + } + + std::map Sampler::GetSamplerChannels() { + return mSamplerChannels; } void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { - std::vector::iterator iterChan = vSamplerChannels.begin(); - for (; iterChan != vSamplerChannels.end(); iterChan++) { - if (*iterChan == pSamplerChannel) { - vSamplerChannels.erase(iterChan); + SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); + for (; iterChan != mSamplerChannels.end(); iterChan++) { + if (iterChan->second == pSamplerChannel) { + mSamplerChannels.erase(iterChan); delete pSamplerChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size())); return; } } @@ -316,4 +326,45 @@ return pDevice; } + void Sampler::Reset() { + // delete sampler channels + try { + while (true) { + SamplerChannelMap::iterator iter = mSamplerChannels.begin(); + if (iter == mSamplerChannels.end()) break; + RemoveSamplerChannel(iter->second); + } + } + catch(...) { + std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; + exit(EXIT_FAILURE); + } + + // delete midi input devices + try { + while (true) { + MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); + if (iter == mMidiInputDevices.end()) break; + DestroyMidiInputDevice(iter->second); + } + } + catch(...) { + std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; + exit(EXIT_FAILURE); + } + + // delete audio output devices + try { + while (true) { + AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); + if (iter == mAudioOutputDevices.end()) break; + DestroyAudioOutputDevice(iter->second); + } + } + catch(...) { + std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; + exit(EXIT_FAILURE); + } + } + } // namespace LinuxSampler