--- linuxsampler/trunk/src/Sampler.cpp 2004/05/14 19:10:21 76 +++ linuxsampler/trunk/src/Sampler.cpp 2006/03/19 16:38:22 846 @@ -3,6 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 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 * @@ -20,12 +21,15 @@ * MA 02111-1307 USA * ***************************************************************************/ +#include + #include "Sampler.h" -#include "audiodriver/AudioOutputDeviceAlsa.h" -#include "audiodriver/AudioOutputDeviceJack.h" -#include "mididriver/MidiInputDeviceAlsa.h" -#include "engines/gig/Engine.h" +#include "engines/EngineFactory.h" +#include "engines/EngineChannelFactory.h" +#include "drivers/audio/AudioOutputDeviceFactory.h" +#include "drivers/midi/MidiInputDeviceFactory.h" +#include "network/lscpserver.h" namespace LinuxSampler { @@ -34,100 +38,180 @@ SamplerChannel::SamplerChannel(Sampler* pS) { pSampler = pS; - pEngine = NULL; - pMidiInputDevice = NULL; + pEngineChannel = NULL; pAudioOutputDevice = NULL; + pMidiInputDevice = NULL; + iMidiPort = 0; + midiChannel = midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { - if (pEngine) { - if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); - delete pEngine; + if (pEngineChannel) { + Engine* engine = pEngineChannel->GetEngine(); + if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); + + MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); + if (pMidiInputPort) pMidiInputPort->Disconnect(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::LoadEngine(Engine::type_t EngineType) { - dmsg(2,("SamplerChannel: Loading engine...")); + void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { + dmsg(2,("SamplerChannel: Assigning engine type...")); - // create new engine - Engine* pNewEngine = NULL; - switch (EngineType) { - case Engine::type_gig: - pNewEngine = new gig::Engine; - break; - default: - throw LinuxSamplerException("Unknown engine type"); + // create new engine channel + 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(GetMidiInputPort()); + // disconnect old engine channel + if (pEngineChannel) { + Engine* engine = pEngineChannel->GetEngine(); + if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); + + if (pMidiInputPort) pMidiInputPort->Disconnect(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); } - // disconnect old engine - if (pEngine) { - if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); - delete pEngine; + // connect new engine channel + if (pAudioOutputDevice) { + pNewEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); } + if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); + pEngineChannel = pNewEngineChannel; + + // from now on get MIDI device and port from EngineChannel object + this->pMidiInputDevice = NULL; + this->iMidiPort = 0; + + pEngineChannel->StatusChanged(true); - // connect new engine - pEngine = pNewEngine; - if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); - if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); dmsg(2,("OK\n")); } - void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice::type_t AudioType) { - // get / create desired audio device - AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType); - if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType); - + void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { // disconnect old device - if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); + 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 (pEngine) pAudioOutputDevice->Connect(pEngine); + if (pEngineChannel) { + pEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); + } } - void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { - // get / create desired midi device - MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); - if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); + void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { + SetMidiInput(pDevice, 0, GetMidiInputChannel()); + } - // disconnect old device - if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); + void SamplerChannel::SetMidiInputPort(int MidiPort) { + SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); + } - // connect new device - pMidiInputDevice = pDevice; - if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); + void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { + SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); } - Engine* SamplerChannel::GetEngine() { - return pEngine; + 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 (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 + if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); + // Ooops. + if (pNewMidiInputPort == NULL) + throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); } - MidiInputDevice* SamplerChannel::GetMidiInputDevice() { - return pMidiInputDevice; + EngineChannel* SamplerChannel::GetEngineChannel() { + return pEngineChannel; + } + + midi_chan_t SamplerChannel::GetMidiInputChannel() { + if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); + return this->midiChannel; + } + + int SamplerChannel::GetMidiInputPort() { + MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; + if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); + return iMidiPort; } AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { return pAudioOutputDevice; } + MidiInputDevice* SamplerChannel::GetMidiInputDevice() { + if (pEngineChannel) + pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; + return pMidiInputDevice; + } + 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"); + } + + MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { + MidiInputPort* pMidiInputPort = NULL; + MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); + if (pMidiInputDevice) + pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); + return pMidiInputPort; } + // ****************************************************************** // * Sampler @@ -135,54 +219,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 = MidiInputDevices.begin(); - for (; iter != MidiInputDevices.end(); iter++) { - MidiInputDevice* pDevice = iter->second; - pDevice->StopListen(); - delete pDevice; - } - } - - // delete audio output devices - { - AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); - for (; iter != AudioOutputDevices.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_channel_count, 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_channel_count, 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_channel_count, 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_channel_count, mSamplerChannels.size())); return; } } @@ -194,65 +285,150 @@ RemoveSamplerChannel(pChannel); } - AudioOutputDevice* Sampler::CreateAudioOutputDevice(AudioOutputDevice::type_t AudioType) { - // check if device already created - AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); - if (pDevice) return pDevice; + std::vector Sampler::AvailableAudioOutputDrivers() { + return AudioOutputDeviceFactory::AvailableDrivers(); + } + AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map Parameters) throw (LinuxSamplerException) { // create new device - switch (AudioType) { - case AudioOutputDevice::type_alsa: - pDevice = new AudioOutputDeviceAlsa; - break; -#if HAVE_JACK - case AudioOutputDevice::type_jack: - pDevice = new AudioOutputDeviceJack; + AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); + + // add new audio device to the audio device list + for (uint i = 0; ; i++) { // seek for a free place starting from the beginning + if (!mAudioOutputDevices[i]) { + mAudioOutputDevices[i] = pDevice; break; -#endif - default: - throw LinuxSamplerException("Unknown audio output device type"); + } } - // activate device - pDevice->Play(); + return pDevice; + } - // add new audio device to the audio device list - AudioOutputDevices[AudioType] = pDevice; + uint Sampler::AudioOutputDevices() { + return mAudioOutputDevices.size(); + } - return pDevice; + uint Sampler::MidiInputDevices() { + return mMidiInputDevices.size(); } - AudioOutputDevice* Sampler::GetAudioOutputDevice(AudioOutputDevice::type_t AudioType) { - AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); - return (iter != AudioOutputDevices.end()) ? iter->second : NULL; + std::map Sampler::GetAudioOutputDevices() { + return mAudioOutputDevices; } - MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) { - // check if device already created - MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); - if (pDevice) return pDevice; + std::map Sampler::GetMidiInputDevices() { + return mMidiInputDevices; + } + + void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { + 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."); + + // disable device + pDevice->Stop(); + + // remove device from the device list + mAudioOutputDevices.erase(iter); + + // destroy and free device from memory + delete pDevice; + + break; + } + } + } + + void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { + 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."); + + // disable device + pDevice->StopListen(); + + // remove device from the device list + mMidiInputDevices.erase(iter); + + // destroy and free device from memory + delete pDevice; - // create new device - switch (MidiType) { - case MidiInputDevice::type_alsa: - pDevice = new MidiInputDeviceAlsa; break; - default: - throw LinuxSamplerException("Unknown audio output device type"); + } } + } - // activate device - pDevice->Listen(); + MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (LinuxSamplerException) { + // create new device + MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); - // add new MIDI device to the MIDI device list - MidiInputDevices[MidiType] = pDevice; + // add new device to the midi device list + for (uint i = 0; ; i++) { // seek for a free place starting from the beginning + if (!mMidiInputDevices[i]) { + mMidiInputDevices[i] = pDevice; + break; + } + } return pDevice; } - MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) { - MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); - return (iter != MidiInputDevices.end()) ? iter->second : NULL; + 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 { + 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