--- linuxsampler/trunk/src/Sampler.cpp 2004/07/06 04:41:23 175 +++ linuxsampler/trunk/src/Sampler.cpp 2005/05/21 01:10:12 556 @@ -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 "audiodriver/AudioOutputDeviceFactory.h" -#include "mididriver/MidiInputDeviceFactory.h" -#include "engines/gig/Engine.h" +#include "engines/EngineChannelFactory.h" +#include "drivers/audio/AudioOutputDeviceFactory.h" +#include "drivers/midi/MidiInputDeviceFactory.h" +#include "network/lscpserver.h" namespace LinuxSampler { @@ -35,99 +37,101 @@ 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(); + delete 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"); // 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(); + delete 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) { SetMidiInput(pDevice, this->midiPort, this->midiChannel); } - + void SamplerChannel::SetMidiInputPort(int MidiPort) { 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); } @@ -142,24 +146,26 @@ 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"); } - 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,54 +173,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 = 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() { - 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; } } @@ -303,7 +316,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 @@ -316,4 +329,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