--- linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2007/02/03 20:46:44 1039 +++ linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2009/01/24 14:32:35 1826 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005, 2006 Christian Schoenebeck * + * Copyright (C) 2005 - 2008 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 * @@ -23,9 +23,16 @@ #include "EngineChannel.h" +#include "../../common/global_private.h" +#include "../../Sampler.h" + namespace LinuxSampler { namespace gig { - EngineChannel::EngineChannel() { + EngineChannel::EngineChannel() : + InstrumentChangeCommandReader(InstrumentChangeCommand), + virtualMidiDevicesReader_AudioThread(virtualMidiDevices), + virtualMidiDevicesReader_MidiThread(virtualMidiDevices) + { pMIDIKeyInfo = new midi_key_info_t[128]; pEngine = NULL; pInstrument = NULL; @@ -57,7 +64,15 @@ EngineChannel::~EngineChannel() { DisconnectAudioOutputDevice(); - if (pInstrument) Engine::instruments.HandBack(pInstrument, this); + + // In case the channel was removed before the instrument was + // fully loaded, try to give back instrument again (see bug #113) + instrument_change_command_t& cmd = ChangeInstrument(NULL); + if (cmd.pInstrument) { + Engine::instruments.HandBack(cmd.pInstrument, this); + } + /////// + if (pEventQueue) delete pEventQueue; if (pActiveKeys) delete pActiveKeys; if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; @@ -159,27 +174,18 @@ * 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() { - ::gig::Instrument* oldInstrument = pInstrument; - - // free old instrument - if (oldInstrument) { - if (pEngine) { - // make sure we don't trigger any new notes with the - // old instrument - ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); - - // give old instrument back to instrument manager, but - // keep the dimension regions and samples that are in - // use - Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); - } else { - Engine::instruments.HandBack(oldInstrument, this); - } + // make sure we don't trigger any new notes with an old + // instrument + instrument_change_command_t& cmd = ChangeInstrument(0); + if (cmd.pInstrument) { + // give old instrument back to instrument manager, but + // keep the dimension regions and samples that are in use + Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse); } + cmd.pDimRegionsInUse->clear(); // delete all key groups ActiveKeyGroups.clear(); @@ -192,23 +198,24 @@ instrid.Index = InstrumentIdx; newInstrument = Engine::instruments.Borrow(instrid, this); if (!newInstrument) { - InstrumentStat = -1; - dmsg(1,("no instrument loaded!!!\n")); - exit(EXIT_FAILURE); + 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 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 Exception(msg); } catch (...) { InstrumentStat = -4; + StatusChanged(true); throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); } @@ -219,8 +226,26 @@ InstrumentIdxName = newInstrument->pInfo->Name; InstrumentStat = 100; - if (pEngine) pEngine->ChangeInstrument(this, newInstrument); - else pInstrument = newInstrument; + ChangeInstrument(newInstrument); + + StatusChanged(true); + } + + + /** + * Changes the instrument for an engine channel. + * + * @param pInstrument - new instrument + * @returns the resulting instrument change command after the + * command switch, containing the old instrument and + * the dimregions it is using + */ + EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) { + instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); + cmd.pInstrument = pInstrument; + cmd.bChangeInstrument = true; + + return InstrumentChangeCommand.SwitchConfig(); } /** @@ -265,6 +290,22 @@ pEngine = Engine::AcquireEngine(this, pAudioOut); ResetInternal(); pEvents = new RTList(pEngine->pEventPool); + + // reset the instrument change command struct (need to be done + // twice, as it is double buffered) + { + instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); + cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]); + cmd.pInstrument = 0; + cmd.bChangeInstrument = false; + } + { + instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); + cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]); + cmd.pInstrument = 0; + cmd.bChangeInstrument = false; + } + for (uint i = 0; i < 128; i++) { pMIDIKeyInfo[i].pActiveVoices = new RTList(pEngine->pVoicePool); pMIDIKeyInfo[i].pEvents = new RTList(pEngine->pEventPool); @@ -287,6 +328,35 @@ void EngineChannel::DisconnectAudioOutputDevice() { if (pEngine) { // if clause to prevent disconnect loops + + // delete the structures used for instrument change + RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse; + if (d) delete d; + EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); + d = cmd.pDimRegionsInUse; + + if (cmd.pInstrument) { + // release the currently loaded instrument + Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d); + } + + if (d) delete d; + + // release all active dimension regions to resource + // manager + RTList::Iterator iuiKey = pActiveKeys->first(); + RTList::Iterator end = pActiveKeys->end(); + while (iuiKey != end) { // iterate through all active keys + midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; + ++iuiKey; + + RTList::Iterator itVoice = pKey->pActiveVoices->first(); + RTList::Iterator itVoicesEnd = pKey->pActiveVoices->end(); + for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key + Engine::instruments.HandBackDimReg(itVoice->pDimRgn); + } + } + ResetInternal(); if (pEvents) { delete pEvents; @@ -338,6 +408,8 @@ default: throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); } + + bStatusChanged = true; } int EngineChannel::OutputChannel(uint EngineAudioChannel) { @@ -390,6 +462,8 @@ } fxSends.push_back(pFxSend); if (pEngine) pEngine->Enable(); + fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); + return pFxSend; } @@ -427,6 +501,7 @@ } } if (pEngine) pEngine->Enable(); + fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); } /** @@ -447,6 +522,16 @@ event.pEngineChannel = this; if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); else dmsg(1,("EngineChannel: Input event queue full!")); + // inform connected virtual MIDI devices if any ... + // (e.g. virtual MIDI keyboard in instrument editor(s)) + ArrayList& devices = + const_cast&>( + virtualMidiDevicesReader_MidiThread.Lock() + ); + for (int i = 0; i < devices.size(); i++) { + devices[i]->SendNoteOnToDevice(Key, Velocity); + } + virtualMidiDevicesReader_MidiThread.Unlock(); } } @@ -473,6 +558,16 @@ event.pEngineChannel = this; if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); else dmsg(1,("EngineChannel: Input event queue full!")); + // inform connected virtual MIDI devices if any ... + // (e.g. virtual MIDI keyboard in instrument editor(s)) + ArrayList& devices = + const_cast&>( + virtualMidiDevicesReader_MidiThread.Lock() + ); + for (int i = 0; i < devices.size(); i++) { + devices[i]->SendNoteOnToDevice(Key, Velocity); + } + virtualMidiDevicesReader_MidiThread.Unlock(); } } @@ -494,6 +589,16 @@ event.pEngineChannel = this; if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); else dmsg(1,("EngineChannel: Input event queue full!")); + // inform connected virtual MIDI devices if any ... + // (e.g. virtual MIDI keyboard in instrument editor(s)) + ArrayList& devices = + const_cast&>( + virtualMidiDevicesReader_MidiThread.Lock() + ); + for (int i = 0; i < devices.size(); i++) { + devices[i]->SendNoteOffToDevice(Key, Velocity); + } + virtualMidiDevicesReader_MidiThread.Unlock(); } } @@ -520,6 +625,16 @@ event.pEngineChannel = this; if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); else dmsg(1,("EngineChannel: Input event queue full!")); + // inform connected virtual MIDI devices if any ... + // (e.g. virtual MIDI keyboard in instrument editor(s)) + ArrayList& devices = + const_cast&>( + virtualMidiDevicesReader_MidiThread.Lock() + ); + for (int i = 0; i < devices.size(); i++) { + devices[i]->SendNoteOffToDevice(Key, Velocity); + } + virtualMidiDevicesReader_MidiThread.Unlock(); } } @@ -633,8 +748,17 @@ MidiVolume = 1.0; GlobalPanLeft = 1.0f; GlobalPanRight = 1.0f; + iLastPanRequest = 64; + GlobalTranspose = 0; // set all MIDI controller values to zero memset(ControllerTable, 0x00, 129); + // reset all FX Send levels + for ( + std::vector::iterator iter = fxSends.begin(); + iter != fxSends.end(); iter++ + ) { + (*iter)->Reset(); + } } /** @@ -651,6 +775,40 @@ * current audio cycle */ void EngineChannel::ImportEvents(uint Samples) { + // import events from pure software MIDI "devices" + // (e.g. virtual keyboard in instrument editor) + { + const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices + Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); + VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device + // as we're going to (carefully) write some status to the + // synchronized struct, we cast away the const + ArrayList& devices = + const_cast&>(virtualMidiDevicesReader_AudioThread.Lock()); + // iterate through all virtual MIDI devices + for (int i = 0; i < devices.size(); i++) { + VirtualMidiDevice* pDev = devices[i]; + // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) + while (pDev->GetMidiEventFromDevice(devEvent)) { + event.Type = + (devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ? + Event::type_note_on : Event::type_note_off; + event.Param.Note.Key = devEvent.Key; + event.Param.Note.Velocity = devEvent.Velocity; + event.pEngineChannel = this; + // copy event to internal event list + if (pEvents->poolIsEmpty()) { + dmsg(1,("Event pool emtpy!\n")); + goto exitVirtualDevicesLoop; + } + *pEvents->allocAppend() = event; + } + } + } + exitVirtualDevicesLoop: + virtualMidiDevicesReader_AudioThread.Unlock(); + + // import events from the regular MIDI devices RingBuffer::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); Event* pEvent; while (true) { @@ -688,7 +846,7 @@ if (pEngine && pEngine->pAudioOutputDevice) { // fallback to render directly to the AudioOutputDevice's buffer pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); - } else pChannelRight = NULL; + } else pChannelRight = NULL; } } for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; @@ -696,6 +854,30 @@ if (pEngine) pEngine->Enable(); } + void EngineChannel::Connect(VirtualMidiDevice* pDevice) { + // double buffer ... double work ... + { + ArrayList& devices = virtualMidiDevices.GetConfigForUpdate(); + devices.add(pDevice); + } + { + ArrayList& devices = virtualMidiDevices.SwitchConfig(); + devices.add(pDevice); + } + } + + void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) { + // double buffer ... double work ... + { + ArrayList& devices = virtualMidiDevices.GetConfigForUpdate(); + devices.remove(pDevice); + } + { + ArrayList& devices = virtualMidiDevices.SwitchConfig(); + devices.remove(pDevice); + } + } + float EngineChannel::Volume() { return GlobalVolume; } @@ -705,6 +887,19 @@ bStatusChanged = true; // status of engine channel has changed, so set notify flag } + float EngineChannel::Pan() { + return float(iLastPanRequest - 64) / 64.0f; + } + + void EngineChannel::Pan(float f) { + int iMidiPan = int(f * 64.0f) + 64; + if (iMidiPan > 127) iMidiPan = 127; + else if (iMidiPan < 0) iMidiPan = 0; + GlobalPanLeft = Engine::PanCurve[128 - iMidiPan]; + GlobalPanRight = Engine::PanCurve[iMidiPan]; + iLastPanRequest = iMidiPan; + } + uint EngineChannel::Channels() { return 2; }