--- linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2005/03/09 22:12:15 438 +++ linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2010/01/09 09:37:01 2043 @@ -2,8 +2,9 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005-2009 Christian Schoenebeck * + * Copyright (C) 2009 Grigor Iliev * * * * 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 * @@ -22,107 +23,50 @@ ***************************************************************************/ #include "EngineChannel.h" +#include "Engine.h" namespace LinuxSampler { namespace gig { - EngineChannel::EngineChannel() { - pMIDIKeyInfo = new midi_key_info_t[128]; - pEngine = NULL; - pInstrument = NULL; - pEventQueue = new RingBuffer(MAX_EVENTS_PER_FRAGMENT, 0); - pActiveKeys = new Pool(128); - for (uint i = 0; i < 128; i++) { - pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object - pMIDIKeyInfo[i].KeyPressed = false; - pMIDIKeyInfo[i].Active = false; - pMIDIKeyInfo[i].ReleaseTrigger = false; - pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object - pMIDIKeyInfo[i].RoundRobinIndex = 0; - } - InstrumentIdx = -1; - InstrumentStat = -1; - AudioDeviceChannelLeft = -1; - AudioDeviceChannelRight = -1; + } EngineChannel::~EngineChannel() { - if (pInstrument) Engine::instruments.HandBack(pInstrument, this); - for (uint i = 0; i < 128; i++) { - if (pMIDIKeyInfo[i].pActiveVoices) { - pMIDIKeyInfo[i].pActiveVoices->clear(); - delete pMIDIKeyInfo[i].pActiveVoices; - } - if (pMIDIKeyInfo[i].pEvents) { - pMIDIKeyInfo[i].pEvents->clear(); - delete pMIDIKeyInfo[i].pEvents; - } + DisconnectAudioOutputDevice(); + // In case the channel was removed before the instrument was + // fully loaded, try to give back instrument again (see bug #113) + InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(NULL); + if (cmd.pInstrument) { + InstrumentResourceManager* instrs = dynamic_cast(pEngine->GetInstrumentManager()); + instrs->HandBack(cmd.pInstrument, this); } - if (pEventQueue) delete pEventQueue; - if (pActiveKeys) delete pActiveKeys; - if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; + /////// } - /** - * This method is not thread safe! - */ + AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::GIG; } + + /** This method is not thread safe! */ void EngineChannel::ResetInternal() { - Pitch = 0; - SustainPedal = false; - GlobalVolume = 1.0; - GlobalPanLeft = 1.0f; - GlobalPanRight = 1.0f; CurrentKeyDimension = 0; - - // set all MIDI controller values to zero - memset(ControllerTable, 0x00, 128); - - // reset voice stealing parameters - itLastStolenVoice = RTList::Iterator(); - iuiLastStolenKey = RTList::Iterator(); - - // reset key info - for (uint i = 0; i < 128; i++) { - if (pMIDIKeyInfo[i].pActiveVoices) - pMIDIKeyInfo[i].pActiveVoices->clear(); - if (pMIDIKeyInfo[i].pEvents) - pMIDIKeyInfo[i].pEvents->clear(); - pMIDIKeyInfo[i].KeyPressed = false; - pMIDIKeyInfo[i].Active = false; - pMIDIKeyInfo[i].ReleaseTrigger = false; - pMIDIKeyInfo[i].itSelf = Pool::Iterator(); - } - - // reset all key groups - std::map::iterator iter = ActiveKeyGroups.begin(); - for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; - - // free all active keys - pActiveKeys->clear(); - - // delete all input events - pEventQueue->init(); - - if (pEngine) pEngine->ResetInternal(); - } - - LinuxSampler::Engine* EngineChannel::GetEngine() { - return pEngine; + EngineChannelBase::ResetInternal(); } /** - * More or less a workaround to set the instrument name, index and load - * status variable to zero percent immediately, that is without blocking - * the calling thread. It might be used in future for other preparations - * as well though. + * Will be called by the MIDIIn Thread to signal that a program + * change should be performed. As a program change isn't + * real-time safe, the actual change is performed by the disk + * thread. * - * @param FileName - file name of the Gigasampler instrument file - * @param Instrument - index of the instrument in the .gig file - * @see LoadInstrument() + * @param Program - MIDI program change number */ - void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { - InstrumentFile = FileName; - InstrumentIdx = Instrument; - InstrumentStat = 0; + void EngineChannel::SendProgramChange(uint8_t Program) { + Engine* engine = dynamic_cast(pEngine); + if(engine == NULL) return; + + if(engine->GetDiskThread()) { + engine->GetDiskThread()->OrderProgramChange(Program, this); + } else { + // TODO: + } } /** @@ -131,260 +75,86 @@ * This method will then actually start to load the instrument and block * the calling thread until loading was completed. * - * @returns detailed description of the method call result * @see PrepareLoadInstrument() */ void EngineChannel::LoadInstrument() { + InstrumentResourceManager* pInstrumentManager = dynamic_cast(pEngine->GetInstrumentManager()); - if (pEngine) pEngine->DisableAndLock(); - - ResetInternal(); - - // free old instrument - if (pInstrument) { - // give old instrument back to instrument manager - Engine::instruments.HandBack(pInstrument, this); + // make sure we don't trigger any new notes with an old + // instrument + InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(0); + if (cmd.pInstrument) { + // give old instrument back to instrument manager, but + // keep the dimension regions and samples that are in use + pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse); } + cmd.pRegionsInUse->clear(); // delete all key groups ActiveKeyGroups.clear(); // request gig instrument from instrument manager + ::gig::Instrument* newInstrument; try { - instrument_id_t instrid; - instrid.FileName = InstrumentFile; - instrid.iInstrument = InstrumentIdx; - pInstrument = Engine::instruments.Borrow(instrid, this); - if (!pInstrument) { - InstrumentStat = -1; - dmsg(1,("no instrument loaded!!!\n")); - exit(EXIT_FAILURE); + InstrumentManager::instrument_id_t instrid; + instrid.FileName = InstrumentFile; + instrid.Index = InstrumentIdx; + + newInstrument = pInstrumentManager->Borrow(instrid, this); + if (!newInstrument) { + throw InstrumentManagerException("resource was not created"); } } catch (RIFF::Exception e) { InstrumentStat = -2; + StatusChanged(true); String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; - throw LinuxSamplerException(msg); + throw Exception(msg); } - catch (InstrumentResourceManagerException e) { + catch (InstrumentManagerException e) { InstrumentStat = -3; + StatusChanged(true); String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); - throw LinuxSamplerException(msg); + throw Exception(msg); } catch (...) { InstrumentStat = -4; - throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); + StatusChanged(true); + throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); } - // rebuild ActiveKeyGroups map with key groups of current instrument - for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) - if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; + RoundRobinIndex = 0; + for (int i = 0 ; i < 128 ; i++) pMIDIKeyInfo[i].pRoundRobinIndex = NULL; - InstrumentIdxName = pInstrument->pInfo->Name; - InstrumentStat = 100; - - // inform audio driver for the need of two channels - try { - if (pEngine && pEngine->pAudioOutputDevice) - pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo - } - catch (AudioOutputException e) { - String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); - throw LinuxSamplerException(msg); - } - - if (pEngine) pEngine->Enable(); - } - - /** - * Will be called by the InstrumentResourceManager when the instrument - * we are currently using in this engine is going to be updated, so we - * can stop playback before that happens. - */ - void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { - dmsg(3,("gig::Engine: Received instrument update message.\n")); - if (pEngine) pEngine->DisableAndLock(); - ResetInternal(); - this->pInstrument = NULL; - } - - /** - * Will be called by the InstrumentResourceManager when the instrument - * update process was completed, so we can continue with playback. - */ - void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { - this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) - if (pEngine) pEngine->Enable(); - } - - void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { - if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) { - DisconnectAudioOutputDevice(); - } - pEngine = Engine::AcquireEngine(this, pAudioOut); - ResetInternal(); - for (uint i = 0; i < 128; i++) { - pMIDIKeyInfo[i].pActiveVoices = new RTList(pEngine->pVoicePool); - pMIDIKeyInfo[i].pEvents = new RTList(pEngine->pEventPool); - } - AudioDeviceChannelLeft = 0; - AudioDeviceChannelRight = 1; - pOutputLeft = pAudioOut->Channel(0)->Buffer(); - pOutputRight = pAudioOut->Channel(1)->Buffer(); - } + // rebuild ActiveKeyGroups map with key groups of current + // instrument and set the round robin pointers to use one + // counter for each region + int region = 0; + for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) { + if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; - void EngineChannel::DisconnectAudioOutputDevice() { - if (pEngine) { // if clause to prevent disconnect loops - ResetInternal(); - for (uint i = 0; i < 128; i++) { - if (pMIDIKeyInfo[i].pActiveVoices) { - delete pMIDIKeyInfo[i].pActiveVoices; - pMIDIKeyInfo[i].pActiveVoices = NULL; - } - if (pMIDIKeyInfo[i].pEvents) { - delete pMIDIKeyInfo[i].pEvents; - pMIDIKeyInfo[i].pEvents = NULL; - } + RoundRobinIndexes[region] = 0; + for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { + pMIDIKeyInfo[iKey].pRoundRobinIndex = &RoundRobinIndexes[region]; } - Engine* oldEngine = pEngine; - AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; - pEngine = NULL; - Engine::FreeEngine(this, oldAudioDevice); - AudioDeviceChannelLeft = -1; - AudioDeviceChannelRight = -1; + region++; } - } - - void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { - if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); - - AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); - if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); - switch (EngineAudioChannel) { - case 0: // left output channel - pOutputLeft = pChannel->Buffer(); - AudioDeviceChannelLeft = AudioDeviceChannel; - break; - case 1: // right output channel - pOutputRight = pChannel->Buffer(); - AudioDeviceChannelRight = AudioDeviceChannel; - break; - default: - throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); - } - } - int EngineChannel::OutputChannel(uint EngineAudioChannel) { - switch (EngineAudioChannel) { - case 0: // left channel - return AudioDeviceChannelLeft; - case 1: // right channel - return AudioDeviceChannelRight; - default: - throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); - } - } - - /** - * Will be called by the MIDIIn Thread to let the audio thread trigger a new - * voice for the given key. - * - * @param Key - MIDI key number of the triggered key - * @param Velocity - MIDI velocity value of the triggered key - */ - void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { - if (pEngine) { - Event event = pEngine->pEventGenerator->CreateEvent(); - event.Type = Event::type_note_on; - event.Param.Note.Key = Key; - event.Param.Note.Velocity = Velocity; - event.pEngineChannel = this; - if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); - else dmsg(1,("EngineChannel: Input event queue full!")); - } - } + InstrumentIdxName = newInstrument->pInfo->Name; + InstrumentStat = 100; - /** - * Will be called by the MIDIIn Thread to signal the audio thread to release - * voice(s) on the given key. - * - * @param Key - MIDI key number of the released key - * @param Velocity - MIDI release velocity value of the released key - */ - void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { - if (pEngine) { - Event event = pEngine->pEventGenerator->CreateEvent(); - event.Type = Event::type_note_off; - event.Param.Note.Key = Key; - event.Param.Note.Velocity = Velocity; - event.pEngineChannel = this; - if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); - else dmsg(1,("EngineChannel: Input event queue full!")); - } - } + ChangeInstrument(newInstrument); - /** - * Will be called by the MIDIIn Thread to signal the audio thread to change - * the pitch value for all voices. - * - * @param Pitch - MIDI pitch value (-8192 ... +8191) - */ - void EngineChannel::SendPitchbend(int Pitch) { - if (pEngine) { - Event event = pEngine->pEventGenerator->CreateEvent(); - event.Type = Event::type_pitchbend; - event.Param.Pitch.Pitch = Pitch; - event.pEngineChannel = this; - if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); - else dmsg(1,("EngineChannel: Input event queue full!")); - } + StatusChanged(true); } - /** - * Will be called by the MIDIIn Thread to signal the audio thread that a - * continuous controller value has changed. - * - * @param Controller - MIDI controller number of the occured control change - * @param Value - value of the control change - */ - void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { - if (pEngine) { - Event event = pEngine->pEventGenerator->CreateEvent(); - event.Type = Event::type_control_change; - event.Param.CC.Controller = Controller; - event.Param.CC.Value = Value; - event.pEngineChannel = this; - if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); - else dmsg(1,("EngineChannel: Input event queue full!")); + void EngineChannel::ProcessKeySwitchChange(int key) { + // Change key dimension value if key is in keyswitching area + { + if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) + CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) / + (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); } } - float EngineChannel::Volume() { - return GlobalVolume; - } - - void EngineChannel::Volume(float f) { - GlobalVolume = f; - } - - uint EngineChannel::Channels() { - return 2; - } - - String EngineChannel::InstrumentFileName() { - return InstrumentFile; - } - - String EngineChannel::InstrumentName() { - return InstrumentIdxName; - } - - int EngineChannel::InstrumentIndex() { - return InstrumentIdx; - } - - int EngineChannel::InstrumentStatus() { - return InstrumentStat; - } - }} // namespace LinuxSampler::gig