--- linuxsampler/trunk/src/Sampler.cpp 2005/05/17 18:16:54 551 +++ linuxsampler/trunk/src/Sampler.cpp 2005/07/30 08:18:08 730 @@ -38,20 +38,20 @@ SamplerChannel::SamplerChannel(Sampler* pS) { pSampler = pS; pEngineChannel = NULL; - pMidiInputDevice = NULL; pAudioOutputDevice = NULL; - midiPort = 0; - midiChannel = MidiInputPort::midi_chan_all; + pMidiInputDevice = NULL; + iMidiPort = 0; + midiChannel = midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { if (pEngineChannel) { - MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); if (pEngineChannel) { if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); - delete pEngineChannel; + EngineChannelFactory::Destroy(pEngineChannel); } } } @@ -63,22 +63,32 @@ EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); + //FIXME: hack to allow fast retrieval of engine channel's sampler channel index + pNewEngineChannel->iSamplerChannelIndex = Index(); + // dereference midi input port. - MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - // disconnect old engine + MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); + // disconnect old engine channel if (pEngineChannel) { if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); - delete pEngineChannel; + EngineChannelFactory::Destroy(pEngineChannel); } // connect new engine channel - pEngineChannel = pNewEngineChannel; - if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel); + if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); if (pAudioOutputDevice) { pNewEngineChannel->Connect(pAudioOutputDevice); pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); } + pEngineChannel = pNewEngineChannel; + + // from now on get MIDI device and port from EngineChannel object + this->pMidiInputDevice = NULL; + this->iMidiPort = 0; + + pEngineChannel->StatusChanged(true); + dmsg(2,("OK\n")); } @@ -94,31 +104,37 @@ } void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { - SetMidiInput(pDevice, this->midiPort, this->midiChannel); + SetMidiInput(pDevice, 0, GetMidiInputChannel()); } void SamplerChannel::SetMidiInputPort(int MidiPort) { - SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); + SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); } - void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { - SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); + void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { + SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); } - void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { - // dereference old midi input port. - MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { + if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned."); + + // get old and new midi input port + MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); + MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); + // disconnect old device port - if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel); - // new device, port and channel - pMidiInputDevice = pDevice; - this->midiPort = iMidiPort; - this->midiChannel = MidiChannel; + if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); + // remember new device, port and channel if not engine channel yet created + if (!pEngineChannel) { + this->pMidiInputDevice = pDevice; + this->iMidiPort = iMidiPort; + this->midiChannel = MidiChannel; + } + // connect new device port - pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel); + if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); // Ooops. - if (pMidiInputPort == NULL) + if (pNewMidiInputPort == NULL) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); } @@ -126,13 +142,15 @@ return pEngineChannel; } - MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { + midi_chan_t SamplerChannel::GetMidiInputChannel() { + if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); return this->midiChannel; } int SamplerChannel::GetMidiInputPort() { - MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); + MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; + if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); + return iMidiPort; } AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { @@ -140,6 +158,8 @@ } MidiInputDevice* SamplerChannel::GetMidiInputDevice() { + if (pEngineChannel) + pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; return pMidiInputDevice; } @@ -157,8 +177,9 @@ throw LinuxSamplerException("Internal error: SamplerChannel index not found"); } - MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { + MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { MidiInputPort* pMidiInputPort = NULL; + MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); if (pMidiInputDevice) pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); return pMidiInputPort; @@ -185,7 +206,7 @@ if (!mSamplerChannels.size()) { SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[0] = pChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); return pChannel; } @@ -200,7 +221,7 @@ // 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)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); return pChannel; } throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); @@ -209,7 +230,7 @@ // 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_channels, lastIndex + 1)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); return pChannel; } @@ -227,7 +248,7 @@ if (iterChan->second == pSamplerChannel) { mSamplerChannels.erase(iterChan); delete pSamplerChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size())); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); return; } }