--- linuxsampler/trunk/src/Sampler.cpp 2004/08/20 17:25:19 221 +++ linuxsampler/trunk/src/Sampler.cpp 2006/02/26 13:00:08 840 @@ -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 * @@ -24,9 +25,11 @@ #include "Sampler.h" +#include "engines/EngineFactory.h" +#include "engines/EngineChannelFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" -#include "engines/gig/Engine.h" +#include "network/lscpserver.h" namespace LinuxSampler { @@ -35,100 +38,144 @@ SamplerChannel::SamplerChannel(Sampler* pS) { pSampler = pS; - pEngine = NULL; - pMidiInputDevice = NULL; + pEngineChannel = NULL; pAudioOutputDevice = NULL; - midiPort = 0; - midiChannel = MidiInputPort::midi_chan_all; + pMidiInputDevice = NULL; + iMidiPort = 0; + midiChannel = midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { - if (pEngine) { - MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - if (pMidiInputPort) pMidiInputPort->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...")); - - // create new engine - Engine* pNewEngine = NULL; - switch (EngineType) { - case Engine::type_gig: - pNewEngine = new gig::Engine; - break; - default: - throw LinuxSamplerException("Unknown engine type"); - } + void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { + dmsg(2,("SamplerChannel: Assigning 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(this->midiPort); - // disconnect old engine - if (pEngine) { - if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); - delete pEngine; - } + 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); + } + + // connect new engine channel + 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); - // connect new engine - pEngine = pNewEngine; - if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); - if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); dmsg(2,("OK\n")); } 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* 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 && pEngine) pMidiInputPort->Disconnect(pEngine); - // 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 && pEngine) pMidiInputPort->Connect(pEngine, 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) + "."); } - Engine* SamplerChannel::GetEngine() { - return pEngine; + 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() { @@ -136,6 +183,8 @@ } MidiInputDevice* SamplerChannel::GetMidiInputDevice() { + if (pEngineChannel) + pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; return pMidiInputDevice; } @@ -153,8 +202,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; @@ -181,6 +231,7 @@ if (!mSamplerChannels.size()) { SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[0] = pChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); return pChannel; } @@ -195,6 +246,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_channel_count, i)); return pChannel; } throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); @@ -203,6 +255,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_channel_count, lastIndex + 1)); return pChannel; } @@ -220,6 +273,7 @@ if (iterChan->second == pSamplerChannel) { mSamplerChannels.erase(iterChan); delete pSamplerChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); return; } } @@ -282,6 +336,8 @@ // destroy and free device from memory delete pDevice; + + break; } } } @@ -302,13 +358,15 @@ // destroy and free device from memory delete pDevice; + + break; } } } MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (LinuxSamplerException) { // 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 @@ -321,12 +379,24 @@ 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 { - SamplerChannelMap::iterator iter = mSamplerChannels.begin(); - for (; iter != mSamplerChannels.end(); iter++) { - RemoveSamplerChannel(iter->second); + while (true) { + SamplerChannelMap::iterator iter = mSamplerChannels.begin(); + if (iter == mSamplerChannels.end()) break; + RemoveSamplerChannel(iter->second); } } catch(...) { @@ -336,9 +406,10 @@ // delete midi input devices try { - MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); - for (; iter != mMidiInputDevices.end(); iter++) { - DestroyMidiInputDevice(iter->second); + while (true) { + MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); + if (iter == mMidiInputDevices.end()) break; + DestroyMidiInputDevice(iter->second); } } catch(...) { @@ -348,9 +419,10 @@ // delete audio output devices try { - AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); - for (; iter != mAudioOutputDevices.end(); iter++) { - DestroyAudioOutputDevice(iter->second); + while (true) { + AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); + if (iter == mAudioOutputDevices.end()) break; + DestroyAudioOutputDevice(iter->second); } } catch(...) {