--- linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp 2005/02/26 02:01:14 411 +++ linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp 2005/05/17 18:16:54 551 @@ -23,6 +23,8 @@ #include "MidiInputPort.h" +#include "../../Sampler.h" + namespace LinuxSampler { // *************** ParameterName *************** @@ -70,6 +72,7 @@ this->pDevice = pDevice; this->portNumber = portNumber; Parameters["NAME"] = new ParameterName(this); + pPreviousProgramChangeEngineChannel = NULL; } MidiInputDevice* MidiInputPort::GetDevice() { @@ -169,16 +172,48 @@ } } + void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) { + if (!pDevice || !pDevice->pSampler) { + std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change." + << "This is a bug, please report it!\n" << std::flush; + return; + } + + Sampler* pSampler = (Sampler*) pDevice->pSampler; + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(Program); + if (!pSamplerChannel) return; + + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) return; + + // disconnect from the engine channel which was connected by the last PC event + if (pPreviousProgramChangeEngineChannel) + Disconnect(pPreviousProgramChangeEngineChannel); + + // now connect to the new engine channel and remember it + try { + Connect(pEngineChannel, (midi_chan_t) MidiChannel); + pPreviousProgramChangeEngineChannel = pEngineChannel; + } + catch (...) { /* NOOP */ } + } + void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) { if (MidiChannel < 0 || MidiChannel > 16) throw MidiInputException("MIDI channel index out of bounds"); + Disconnect(pEngineChannel); + + MidiChannelMapMutex.Lock(); MidiChannelMap[MidiChannel].insert(pEngineChannel); + MidiChannelMapMutex.Unlock(); } void MidiInputPort::Disconnect(EngineChannel* pEngineChannel) { + MidiChannelMapMutex.Lock(); try { for (int i = 0; i <= 16; i++) MidiChannelMap[i].erase(pEngineChannel); } catch(...) { /* NOOP */ } + MidiChannelMapMutex.Unlock(); } } // namespace LinuxSampler