--- linuxsampler/trunk/src/Sampler.cpp 2004/04/26 17:15:51 53 +++ linuxsampler/trunk/src/Sampler.cpp 2004/07/18 00:29:39 209 @@ -2,7 +2,7 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and 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,11 +20,12 @@ * MA 02111-1307 USA * ***************************************************************************/ +#include + #include "Sampler.h" -#include "audiodriver/AudioOutputDeviceAlsa.h" -#include "audiodriver/AudioOutputDeviceJack.h" -#include "mididriver/MidiInputDeviceAlsa.h" +#include "drivers/audio/AudioOutputDeviceFactory.h" +#include "drivers/midi/MidiInputDeviceFactory.h" #include "engines/gig/Engine.h" namespace LinuxSampler { @@ -37,96 +38,127 @@ pEngine = NULL; pMidiInputDevice = NULL; pAudioOutputDevice = NULL; + midiPort = 0; + midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; iIndex = -1; } SamplerChannel::~SamplerChannel() { if (pEngine) { - if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); + MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); delete pEngine; } } - void SamplerChannel::LoadEngine(engine_type_t EngineType) { - dmsg(1,("SamplerChannel: Loading engine\n")); + void SamplerChannel::LoadEngine(Engine::type_t EngineType) { + dmsg(2,("SamplerChannel: Loading engine...")); // create new engine Engine* pNewEngine = NULL; switch (EngineType) { - case engine_type_gig: + case Engine::type_gig: pNewEngine = new gig::Engine; break; default: throw LinuxSamplerException("Unknown engine type"); } + // dereference midi input port. + MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); // disconnect old engine if (pEngine) { - if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); + if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); delete pEngine; } // connect new engine pEngine = pNewEngine; - if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); + if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); - dmsg(1,("SamplerChannel: Engine loaded.\n")); + dmsg(2,("OK\n")); } - void SamplerChannel::SetAudioOutputDevice(audio_output_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); - // connect new device pAudioOutputDevice = pDevice; if (pEngine) pAudioOutputDevice->Connect(pEngine); } - void SamplerChannel::SetMidiInputDevice(midi_input_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, this->midiPort, this->midiChannel); + } - // disconnect old device - if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); + void SamplerChannel::SetMidiInputPort(int MidiPort) { + SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); + } - // connect new device + void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); + } + + void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { + // dereference old midi input port. + MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + // disconnect old device port + if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); + // new device, port and channel pMidiInputDevice = pDevice; - if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); + this->midiPort = MidiPort; + this->midiChannel = MidiChannel; + // connect new device port + pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); + // Ooops. + if (pMidiInputPort == NULL) + throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + "."); } Engine* SamplerChannel::GetEngine() { return pEngine; } - MidiInputDevice* SamplerChannel::GetMidiInputDevice() { - return pMidiInputDevice; + MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { + return this->midiChannel; + } + + int SamplerChannel::GetMidiInputPort() { + MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); + return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); } AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { return pAudioOutputDevice; } + MidiInputDevice* SamplerChannel::GetMidiInputDevice() { + 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"); } + MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) { + MidiInputDevice::MidiInputPort *pMidiInputPort = NULL; + if (pMidiInputDevice) + pMidiInputPort = pMidiInputDevice->GetPort(MidiPort); + return pMidiInputPort; + } // ****************************************************************** // * Sampler @@ -137,14 +169,14 @@ Sampler::~Sampler() { // delete sampler channels { - std::vector::iterator iter = vSamplerChannels.begin(); - for (; iter != vSamplerChannels.end(); iter++) delete *iter; + SamplerChannelMap::iterator iter = mSamplerChannels.begin(); + for (; iter != mSamplerChannels.end(); iter++) delete iter->second; } // delete midi input devices { - MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); - for (; iter != MidiInputDevices.end(); iter++) { + MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); + for (; iter != mMidiInputDevices.end(); iter++) { MidiInputDevice* pDevice = iter->second; pDevice->StopListen(); delete pDevice; @@ -153,8 +185,8 @@ // delete audio output devices { - AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); - for (; iter != AudioOutputDevices.end(); iter++) { + AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); + for (; iter != mAudioOutputDevices.end(); iter++) { AudioOutputDevice* pDevice = iter->second; pDevice->Stop(); delete pDevice; @@ -163,25 +195,52 @@ } uint Sampler::SamplerChannels() { - 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; + 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; + 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; 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; return; } @@ -194,57 +253,94 @@ RemoveSamplerChannel(pChannel); } - AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_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 audio_output_type_alsa: - pDevice = new AudioOutputDeviceAlsa; - break; - case audio_output_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; - default: - throw LinuxSamplerException("Unknown audio output device type"); + } } - // activate device - pDevice->Play(); - return pDevice; } - AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { - AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); - return (iter != AudioOutputDevices.end()) ? iter->second : NULL; + uint Sampler::AudioOutputDevices() { + return mAudioOutputDevices.size(); } - MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { - // check if device already created - MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); - if (pDevice) return pDevice; + uint Sampler::MidiInputDevices() { + return mMidiInputDevices.size(); + } - // create new device - switch (MidiType) { - case midi_input_type_alsa: - pDevice = new MidiInputDeviceAlsa; - break; - default: - throw LinuxSamplerException("Unknown audio output device type"); + std::map Sampler::GetAudioOutputDevices() { + return mAudioOutputDevices; + } + + 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; + } } + } - // activate device - pDevice->Listen(); + 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."); - return pDevice; + // disable device + pDevice->StopListen(); + + // remove device from the device list + mMidiInputDevices.erase(iter); + + // destroy and free device from memory + delete pDevice; + } + } } - MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { - MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); - return (iter != MidiInputDevices.end()) ? iter->second : NULL; + MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map Parameters) throw (LinuxSamplerException) { + // create new device + MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); + + // 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; } } // namespace LinuxSampler