--- linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2005/02/26 02:01:14 411 +++ linuxsampler/trunk/src/engines/gig/EngineChannel.cpp 2005/03/17 23:56:56 475 @@ -23,13 +23,15 @@ #include "EngineChannel.h" -namespace LinuxSampler { namespace gig { +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); + pEvents = NULL; // we allocate when we retrieve the right Engine object + pCCEvents = NULL; // we allocate when we retrieve the right Engine object + 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 @@ -37,6 +39,11 @@ pMIDIKeyInfo[i].Active = false; pMIDIKeyInfo[i].ReleaseTrigger = false; pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object + pMIDIKeyInfo[i].VoiceTheftsQueued = 0; + pMIDIKeyInfo[i].RoundRobinIndex = 0; + } + for (uint i = 0; i < Event::destination_count; i++) { + pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object } InstrumentIdx = -1; InstrumentStat = -1; @@ -45,17 +52,8 @@ } EngineChannel::~EngineChannel() { + DisconnectAudioOutputDevice(); 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; - } - } if (pEventQueue) delete pEventQueue; if (pActiveKeys) delete pActiveKeys; if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; @@ -68,10 +66,11 @@ 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); + ResetControllers(); // reset key info for (uint i = 0; i < 128; i++) { @@ -83,6 +82,7 @@ pMIDIKeyInfo[i].Active = false; pMIDIKeyInfo[i].ReleaseTrigger = false; pMIDIKeyInfo[i].itSelf = Pool::Iterator(); + pMIDIKeyInfo[i].VoiceTheftsQueued = 0; } // reset all key groups @@ -98,10 +98,6 @@ if (pEngine) pEngine->ResetInternal(); } - int EngineChannel::RenderAudio(uint Samples) { - return (pEngine) ? pEngine->RenderAudio(this, Samples) : 0; - } - LinuxSampler::Engine* EngineChannel::GetEngine() { return pEngine; } @@ -134,9 +130,9 @@ void EngineChannel::LoadInstrument() { if (pEngine) pEngine->DisableAndLock(); - + ResetInternal(); - + // free old instrument if (pInstrument) { // give old instrument back to instrument manager @@ -214,9 +210,18 @@ if (pEngine) pEngine->Enable(); } - void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { + void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { + if (pEngine) { + if (pEngine->pAudioOutputDevice == pAudioOut) return; + DisconnectAudioOutputDevice(); + } pEngine = Engine::AcquireEngine(this, pAudioOut); - ResetInternal(); + ResetInternal(); + pEvents = new RTList(pEngine->pEventPool); + pCCEvents = new RTList(pEngine->pEventPool); + for (uint i = 0; i < Event::destination_count; i++) { + pSynthesisEvents[i] = new RTList(pEngine->pEventPool); + } for (uint i = 0; i < 128; i++) { pMIDIKeyInfo[i].pActiveVoices = new RTList(pEngine->pVoicePool); pMIDIKeyInfo[i].pEvents = new RTList(pEngine->pEventPool); @@ -230,9 +235,29 @@ void EngineChannel::DisconnectAudioOutputDevice() { if (pEngine) { // if clause to prevent disconnect loops ResetInternal(); + if (pEvents) { + delete pEvents; + pEvents = NULL; + } + if (pCCEvents) { + delete pCCEvents; + pCCEvents = NULL; + } for (uint i = 0; i < 128; i++) { - if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices; - if (pMIDIKeyInfo[i].pEvents) delete pMIDIKeyInfo[i].pEvents; + if (pMIDIKeyInfo[i].pActiveVoices) { + delete pMIDIKeyInfo[i].pActiveVoices; + pMIDIKeyInfo[i].pActiveVoices = NULL; + } + if (pMIDIKeyInfo[i].pEvents) { + delete pMIDIKeyInfo[i].pEvents; + pMIDIKeyInfo[i].pEvents = NULL; + } + } + for (uint i = 0; i < Event::destination_count; i++) { + if (pSynthesisEvents[i]) { + delete pSynthesisEvents[i]; + pSynthesisEvents[i] = NULL; + } } Engine* oldEngine = pEngine; AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; @@ -240,13 +265,12 @@ Engine::FreeEngine(this, oldAudioDevice); AudioDeviceChannelLeft = -1; AudioDeviceChannelRight = -1; - oldAudioDevice->Disconnect(this); } } 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) { @@ -287,8 +311,9 @@ 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,("Engine: Input event queue full!")); + else dmsg(1,("EngineChannel: Input event queue full!")); } } @@ -305,8 +330,9 @@ 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,("Engine: Input event queue full!")); + else dmsg(1,("EngineChannel: Input event queue full!")); } } @@ -317,12 +343,13 @@ * @param Pitch - MIDI pitch value (-8192 ... +8191) */ void EngineChannel::SendPitchbend(int Pitch) { - if (pEngine) { + 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,("Engine: Input event queue full!")); + else dmsg(1,("EngineChannel: Input event queue full!")); } } @@ -339,11 +366,69 @@ 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,("Engine: Input event queue full!")); + else dmsg(1,("EngineChannel: Input event queue full!")); + } + } + + void EngineChannel::ClearEventLists() { + pEvents->clear(); + pCCEvents->clear(); + for (uint i = 0; i < Event::destination_count; i++) { + pSynthesisEvents[i]->clear(); + } + // empty MIDI key specific event lists + { + RTList::Iterator iuiKey = pActiveKeys->first(); + RTList::Iterator end = pActiveKeys->end(); + for(; iuiKey != end; ++iuiKey) { + pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key + } } } + void EngineChannel::ResetControllers() { + // set all MIDI controller values to zero + memset(ControllerTable, 0x00, 128); + } + + /** + * Copy all events from the engine channel's input event queue buffer to + * the internal event list. This will be done at the beginning of each + * audio cycle (that is each RenderAudio() call) to distinguish all + * events which have to be processed in the current audio cycle. Each + * EngineChannel has it's own input event queue for the common channel + * specific events (like NoteOn, NoteOff and ControlChange events). + * Beside that, the engine also has a input event queue for global + * events (usually SysEx messages). + * + * @param Samples - number of sample points to be processed in the + * current audio cycle + */ + void EngineChannel::ImportEvents(uint Samples) { + RingBuffer::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); + Event* pEvent; + while (true) { + // get next event from input event queue + if (!(pEvent = eventQueueReader.pop())) break; + // if younger event reached, ignore that and all subsequent ones for now + if (pEvent->FragmentPos() >= Samples) { + eventQueueReader--; + dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); + pEvent->ResetFragmentPos(); + break; + } + // copy event to internal event list + if (pEvents->poolIsEmpty()) { + dmsg(1,("Event pool emtpy!\n")); + break; + } + *pEvents->allocAppend() = *pEvent; + } + eventQueueReader.free(); // free all copied events from input queue + } + float EngineChannel::Volume() { return GlobalVolume; } @@ -370,6 +455,10 @@ int EngineChannel::InstrumentStatus() { return InstrumentStat; - } + } + String EngineChannel::EngineName() { + return LS_GIG_ENGINE_NAME; + } + }} // namespace LinuxSampler::gig