--- linuxsampler/trunk/src/Sampler.cpp 2004/07/18 00:29:39 209 +++ linuxsampler/trunk/src/Sampler.cpp 2005/06/17 19:49:30 660 @@ -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,10 @@ #include "Sampler.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,58 +37,63 @@ SamplerChannel::SamplerChannel(Sampler* pS) { pSampler = pS; - pEngine = NULL; + pEngineChannel = NULL; pMidiInputDevice = NULL; pAudioOutputDevice = NULL; midiPort = 0; - midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; + midiChannel = MidiInputPort::midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { - if (pEngine) { - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); - delete pEngine; + if (pEngineChannel) { + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); + if (pEngineChannel) { + if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); + EngineChannelFactory::Destroy(pEngineChannel); + } } } - 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. - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); // disconnect old engine - if (pEngine) { - if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); - if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); - delete pEngine; + if (pEngineChannel) { + if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); + if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); + EngineChannelFactory::Destroy(pEngineChannel); } - // connect new engine - pEngine = pNewEngine; - if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); - if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); + // connect new engine channel + pEngineChannel = pNewEngineChannel; + if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel); + if (pAudioOutputDevice) { + pNewEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); + } dmsg(2,("OK\n")); } void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { // disconnect old device - if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); + if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice(); // connect new device pAudioOutputDevice = pDevice; - if (pEngine) pAudioOutputDevice->Connect(pEngine); + if (pEngineChannel) { + pEngineChannel->Connect(pAudioOutputDevice); + pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); + } } void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { @@ -97,37 +104,37 @@ SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); } - void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); } - void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { // dereference old midi input port. - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); // disconnect old device port - if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); + if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel); // new device, port and channel pMidiInputDevice = pDevice; - this->midiPort = MidiPort; + this->midiPort = iMidiPort; this->midiChannel = MidiChannel; // connect new device port pMidiInputPort = GetMidiInputDevicePort(this->midiPort); - if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); + if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel); // Ooops. if (pMidiInputPort == NULL) - throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + "."); + throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); } - Engine* SamplerChannel::GetEngine() { - return pEngine; + EngineChannel* SamplerChannel::GetEngineChannel() { + return pEngineChannel; } - MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { + MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { return this->midiChannel; } int SamplerChannel::GetMidiInputPort() { - MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); } @@ -153,13 +160,15 @@ throw LinuxSamplerException("Internal error: SamplerChannel index not found"); } - MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) { - MidiInputDevice::MidiInputPort *pMidiInputPort = NULL; + MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { + MidiInputPort* pMidiInputPort = NULL; if (pMidiInputDevice) - pMidiInputPort = pMidiInputDevice->GetPort(MidiPort); + pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); return pMidiInputPort; } + + // ****************************************************************** // * Sampler @@ -167,31 +176,7 @@ } Sampler::~Sampler() { - // delete sampler channels - { - SamplerChannelMap::iterator iter = mSamplerChannels.begin(); - for (; iter != mSamplerChannels.end(); iter++) delete iter->second; - } - - // delete midi input devices - { - MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); - for (; iter != mMidiInputDevices.end(); iter++) { - MidiInputDevice* pDevice = iter->second; - pDevice->StopListen(); - delete pDevice; - } - } - - // delete audio output devices - { - AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); - for (; iter != mAudioOutputDevices.end(); iter++) { - AudioOutputDevice* pDevice = iter->second; - pDevice->Stop(); - delete pDevice; - } - } + Reset(); } uint Sampler::SamplerChannels() { @@ -203,6 +188,7 @@ if (!mSamplerChannels.size()) { SamplerChannel* pChannel = new SamplerChannel(this); mSamplerChannels[0] = pChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); return pChannel; } @@ -217,6 +203,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."); @@ -225,6 +212,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; } @@ -242,6 +230,7 @@ if (iterChan->second == pSamplerChannel) { mSamplerChannels.erase(iterChan); delete pSamplerChannel; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); return; } } @@ -330,7 +319,7 @@ 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 @@ -343,4 +332,45 @@ return pDevice; } + 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