--- linuxsampler/trunk/src/Sampler.cpp 2005/02/26 02:01:14 411 +++ linuxsampler/trunk/src/Sampler.cpp 2006/12/17 22:35:01 981 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2005, 2006 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -25,6 +25,7 @@ #include "Sampler.h" +#include "engines/EngineFactory.h" #include "engines/EngineChannelFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" @@ -38,93 +39,145 @@ 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); + Engine* engine = pEngineChannel->GetEngine(); + if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); + + MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngineChannel); - delete pEngineChannel; + if (pEngineChannel) { + if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); + EngineChannelFactory::Destroy(pEngineChannel); + + // reconnect engine if it still exists + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); + } } } - void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { + void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { dmsg(2,("SamplerChannel: Assigning engine type...")); // create new engine channel EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); - if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); + if (!pNewEngineChannel) throw Exception("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) { + Engine* engine = pEngineChannel->GetEngine(); + if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); + if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngineChannel); - delete pEngineChannel; + if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); + EngineChannelFactory::Destroy(pEngineChannel); + + // reconnect engine if it still exists + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); } // connect new engine channel + if (pAudioOutputDevice) { + pNewEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); + } + if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); pEngineChannel = pNewEngineChannel; - if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel); - if (pAudioOutputDevice) pAudioOutputDevice->Connect(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")); } void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { + if(pAudioOutputDevice == pDevice) return; + // disconnect old device - if (pAudioOutputDevice && pEngineChannel) pAudioOutputDevice->Disconnect(pEngineChannel); + if (pAudioOutputDevice && pEngineChannel) { + Engine* engine = pEngineChannel->GetEngine(); + pAudioOutputDevice->Disconnect(engine); + + pEngineChannel->DisconnectAudioOutputDevice(); + + // reconnect engine if it still exists + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); + } + // connect new device pAudioOutputDevice = pDevice; - if (pEngineChannel) pAudioOutputDevice->Connect(pEngineChannel); + if (pEngineChannel) { + pEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); + } } 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 Exception("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) - throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); + if (pNewMidiInputPort == NULL) + throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + "."); } EngineChannel* SamplerChannel::GetEngineChannel() { 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() { @@ -132,6 +185,8 @@ } MidiInputDevice* SamplerChannel::GetMidiInputDevice() { + if (pEngineChannel) + pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; return pMidiInputDevice; } @@ -146,11 +201,12 @@ } } - throw LinuxSamplerException("Internal error: SamplerChannel index not found"); + throw Exception("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; @@ -177,7 +233,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; } @@ -192,16 +248,16 @@ // 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."); + throw Exception("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); mSamplerChannels[lastIndex + 1] = pChannel; - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); return pChannel; } @@ -219,7 +275,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; } } @@ -235,7 +291,15 @@ return AudioOutputDeviceFactory::AvailableDrivers(); } - AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map Parameters) throw (LinuxSamplerException) { + std::vector Sampler::AvailableMidiInputDrivers() { + return MidiInputDeviceFactory::AvailableDrivers(); + } + + std::vector Sampler::AvailableEngineTypes() { + return EngineFactory::AvailableEngineTypes(); + } + + AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map Parameters) throw (Exception) { // create new device AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); @@ -247,6 +311,7 @@ } } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); return pDevice; } @@ -266,13 +331,13 @@ return mMidiInputDevices; } - void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { + 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 LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device."); + if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); // disable device pDevice->Stop(); @@ -282,17 +347,20 @@ // destroy and free device from memory delete pDevice; + + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); + break; } } } - void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { + 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 LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device."); + if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); // disable device pDevice->StopListen(); @@ -302,13 +370,16 @@ // destroy and free device from memory delete pDevice; + + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); + break; } } } - MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (LinuxSamplerException) { + MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (Exception) { // create new device - MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); + 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 @@ -318,9 +389,21 @@ } } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); return pDevice; } + int Sampler::GetVoiceCount() { + int count = 0; + std::set::iterator it = EngineFactory::EngineInstances().begin(); + + for(; it != EngineFactory::EngineInstances().end(); it++) { + count += (*it)->VoiceCount(); + } + + return count; + } + void Sampler::Reset() { // delete sampler channels try {