--- linuxsampler/trunk/src/Sampler.cpp 2004/07/06 04:41:23 175 +++ linuxsampler/trunk/src/Sampler.cpp 2004/07/18 00:29:39 209 @@ -24,8 +24,8 @@ #include "Sampler.h" -#include "audiodriver/AudioOutputDeviceFactory.h" -#include "mididriver/MidiInputDeviceFactory.h" +#include "drivers/audio/AudioOutputDeviceFactory.h" +#include "drivers/midi/MidiInputDeviceFactory.h" #include "engines/gig/Engine.h" namespace LinuxSampler { @@ -92,15 +92,15 @@ void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { SetMidiInput(pDevice, this->midiPort, this->midiChannel); } - + void SamplerChannel::SetMidiInputPort(int MidiPort) { SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); } - + void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); } - + void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { // dereference old midi input port. MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); @@ -142,15 +142,15 @@ 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) { @@ -169,8 +169,8 @@ Sampler::~Sampler() { // delete sampler channels { - std::vector::iterator iter = vSamplerChannels.begin(); - for (; iter != vSamplerChannels.end(); iter++) delete *iter; + SamplerChannelMap::iterator iter = mSamplerChannels.begin(); + for (; iter != mSamplerChannels.end(); iter++) delete iter->second; } // delete midi input devices @@ -195,25 +195,52 @@ } 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; + 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; + 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; 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; return; }