/[svn]/linuxsampler/trunk/src/engines/gig/Engine.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 668 by schoenebeck, Mon Jun 20 15:30:47 2005 UTC revision 1037 by schoenebeck, Tue Jan 23 20:03:22 2007 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 Christian Schoenebeck                              *   *   Copyright (C) 2005-2007 Christian Schoenebeck                        *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 29  Line 29 
29    
30  #include "Engine.h"  #include "Engine.h"
31    
 #if defined(__APPLE__)  
 # include <stdlib.h>  
 #else  
 # include <malloc.h>  
 #endif  
   
32  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
33    
34      InstrumentResourceManager Engine::instruments;      InstrumentResourceManager Engine::instruments;
# Line 103  namespace LinuxSampler { namespace gig { Line 97  namespace LinuxSampler { namespace gig {
97          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
98          pDiskThread        = NULL;          pDiskThread        = NULL;
99          pEventGenerator    = NULL;          pEventGenerator    = NULL;
100          pSysexBuffer       = new RingBuffer<uint8_t>(CONFIG_SYSEX_BUFFER_SIZE, 0);          pSysexBuffer       = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0);
101          pEventQueue        = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue        = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
102          pEventPool         = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT);
103          pVoicePool         = new Pool<Voice>(CONFIG_MAX_VOICES);          pVoicePool         = new Pool<Voice>(CONFIG_MAX_VOICES);
104          pVoiceStealingQueue = new RTList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
# Line 114  namespace LinuxSampler { namespace gig { Line 108  namespace LinuxSampler { namespace gig {
108          }          }
109          pVoicePool->clear();          pVoicePool->clear();
110    
         pSynthesisParameters[0] = NULL; // we allocate when an audio device is connected  
         pBasicFilterParameters  = NULL;  
         pMainFilterParameters   = NULL;  
   
111          ResetInternal();          ResetInternal();
112          ResetScaleTuning();          ResetScaleTuning();
113      }      }
# Line 126  namespace LinuxSampler { namespace gig { Line 116  namespace LinuxSampler { namespace gig {
116       * Destructor       * Destructor
117       */       */
118      Engine::~Engine() {      Engine::~Engine() {
119            MidiInputPort::RemoveSysexListener(this);
120          if (pDiskThread) {          if (pDiskThread) {
121              dmsg(1,("Stopping disk thread..."));              dmsg(1,("Stopping disk thread..."));
122              pDiskThread->StopThread();              pDiskThread->StopThread();
# Line 139  namespace LinuxSampler { namespace gig { Line 130  namespace LinuxSampler { namespace gig {
130              delete pVoicePool;              delete pVoicePool;
131          }          }
132          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
         if (pMainFilterParameters) delete[] pMainFilterParameters;  
         if (pBasicFilterParameters) delete[] pBasicFilterParameters;  
         if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);  
133          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
134          if (pSysexBuffer) delete pSysexBuffer;          if (pSysexBuffer) delete pSysexBuffer;
135          EngineFactory::Destroy(this);          Unregister();
136      }      }
137    
138      void Engine::Enable() {      void Engine::Enable() {
# Line 178  namespace LinuxSampler { namespace gig { Line 166  namespace LinuxSampler { namespace gig {
166    
167      /**      /**
168       *  Reset all voices and disk thread and clear input event queue and all       *  Reset all voices and disk thread and clear input event queue and all
169       *  control and status variables. This method is not thread safe!       *  control and status variables. This method is protected by a mutex.
170       */       */
171      void Engine::ResetInternal() {      void Engine::ResetInternal() {
172            ResetInternalMutex.Lock();
173    
174            // make sure that the engine does not get any sysex messages
175            // while it's reseting
176            bool sysexDisabled = MidiInputPort::RemoveSysexListener(this);
177          ActiveVoiceCount    = 0;          ActiveVoiceCount    = 0;
178          ActiveVoiceCountMax = 0;          ActiveVoiceCountMax = 0;
179    
# Line 203  namespace LinuxSampler { namespace gig { Line 196  namespace LinuxSampler { namespace gig {
196    
197          // delete all input events          // delete all input events
198          pEventQueue->init();          pEventQueue->init();
199            pSysexBuffer->init();
200            if (sysexDisabled) MidiInputPort::AddSysexListener(this);
201            ResetInternalMutex.Unlock();
202      }      }
203    
204      /**      /**
# Line 232  namespace LinuxSampler { namespace gig { Line 228  namespace LinuxSampler { namespace gig {
228          }          }
229          catch (AudioOutputException e) {          catch (AudioOutputException e) {
230              String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();              String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();
231              throw LinuxSamplerException(msg);              throw Exception(msg);
232          }          }
233    
234          this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle();          this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle();
# Line 240  namespace LinuxSampler { namespace gig { Line 236  namespace LinuxSampler { namespace gig {
236    
237          // FIXME: audio drivers with varying fragment sizes might be a problem here          // FIXME: audio drivers with varying fragment sizes might be a problem here
238          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;
239          if (MaxFadeOutPos < 0)          if (MaxFadeOutPos < 0) {
240              throw LinuxSamplerException("CONFIG_EG_MIN_RELEASE_TIME too big for current audio fragment size / sampling rate!");              std::cerr << "gig::Engine: WARNING, CONFIG_EG_MIN_RELEASE_TIME "
241                          << "too big for current audio fragment size & sampling rate! "
242                          << "May lead to click sounds if voice stealing chimes in!\n" << std::flush;
243                // force volume ramp downs at the beginning of each fragment
244                MaxFadeOutPos = 0;
245                // lower minimum release time
246                const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate;
247                for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
248                    iterVoice->EG1.CalculateFadeOutCoeff(minReleaseTime, SampleRate);
249                }
250                pVoicePool->clear();
251            }
252    
253          // (re)create disk thread          // (re)create disk thread
254          if (this->pDiskThread) {          if (this->pDiskThread) {
# Line 266  namespace LinuxSampler { namespace gig { Line 273  namespace LinuxSampler { namespace gig {
273          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
274          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());
275    
         // (re)allocate synthesis parameter matrix  
         if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);  
   
         #if defined(__APPLE__)  
         pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle());  
         #else  
         pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()));  
         #endif  
         for (int dst = 1; dst < Event::destination_count; dst++)  
             pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();  
   
         // (re)allocate biquad filter parameter sequence  
         if (pBasicFilterParameters) delete[] pBasicFilterParameters;  
         if (pMainFilterParameters)  delete[] pMainFilterParameters;  
         pBasicFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()];  
         pMainFilterParameters  = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()];  
   
276          dmsg(1,("Starting disk thread..."));          dmsg(1,("Starting disk thread..."));
277          pDiskThread->StartThread();          pDiskThread->StartThread();
278          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
# Line 316  namespace LinuxSampler { namespace gig { Line 306  namespace LinuxSampler { namespace gig {
306       *                  current audio cycle       *                  current audio cycle
307       */       */
308      void Engine::ImportEvents(uint Samples) {      void Engine::ImportEvents(uint Samples) {
309          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
310          Event* pEvent;          Event* pEvent;
311          while (true) {          while (true) {
312              // get next event from input event queue              // get next event from input event queue
# Line 339  namespace LinuxSampler { namespace gig { Line 329  namespace LinuxSampler { namespace gig {
329      }      }
330    
331      /**      /**
332       *  Let this engine proceed to render the given amount of sample points. The       * Let this engine proceed to render the given amount of sample points.
333       *  calculated audio data of all voices of this engine will be placed into       * The engine will iterate through all engine channels and render audio
334       *  the engine's audio sum buffer which has to be copied and eventually be       * for each engine channel independently. The calculated audio data of
335       *  converted to the appropriate value range by the audio output class (e.g.       * all voices of each engine channel will be placed into the audio sum
336       *  AlsaIO or JackIO) right after.       * buffers of the respective audio output device, connected to the
337         * respective engine channel.
338       *       *
339       *  @param Samples - number of sample points to be rendered       *  @param Samples - number of sample points to be rendered
340       *  @returns       0 on success       *  @returns       0 on success
341       */       */
342      int Engine::RenderAudio(uint Samples) {      int Engine::RenderAudio(uint Samples) {
343          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));          dmsg(7,("RenderAudio(Samples=%d)\n", Samples));
344    
345          // return if engine disabled          // return if engine disabled
346          if (EngineDisabled.Pop()) {          if (EngineDisabled.Pop()) {
# Line 401  namespace LinuxSampler { namespace gig { Line 392  namespace LinuxSampler { namespace gig {
392          // now that all ordinary voices on ALL engine channels are rendered, render new stolen voices          // now that all ordinary voices on ALL engine channels are rendered, render new stolen voices
393          RenderStolenVoices(Samples);          RenderStolenVoices(Samples);
394    
395            // handle audio routing for engine channels with FX sends
396            for (int i = 0; i < engineChannels.size(); i++) {
397                if (engineChannels[i]->fxSends.empty()) continue; // ignore if no FX sends
398                RouteAudio(engineChannels[i], Samples);
399            }
400    
401          // handle cleanup on all engine channels for the next audio fragment          // handle cleanup on all engine channels for the next audio fragment
402          for (int i = 0; i < engineChannels.size(); i++) {          for (int i = 0; i < engineChannels.size(); i++) {
403              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded
# Line 481  namespace LinuxSampler { namespace gig { Line 478  namespace LinuxSampler { namespace gig {
478       *                         this audio fragment cycle       *                         this audio fragment cycle
479       */       */
480      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {
481            #if !CONFIG_PROCESS_MUTED_CHANNELS
482            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
483            #endif
484    
485          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
486          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
487          while (iuiKey != end) { // iterate through all active keys          while (iuiKey != end) { // iterate through all active keys
# Line 537  namespace LinuxSampler { namespace gig { Line 538  namespace LinuxSampler { namespace gig {
538      }      }
539    
540      /**      /**
541         * Will be called in case the respective engine channel sports FX send
542         * channels. In this particular case, engine channel local buffers are
543         * used to render and mix all voices to. This method is responsible for
544         * copying the audio data from those local buffers to the master audio
545         * output channels as well as to the FX send audio output channels with
546         * their respective FX send levels.
547         *
548         * @param pEngineChannel - engine channel from which audio should be
549         *                         routed
550         * @param Samples        - amount of sample points to be routed in
551         *                         this audio fragment cycle
552         */
553        void Engine::RouteAudio(EngineChannel* pEngineChannel, uint Samples) {
554            // route master signal
555            {
556                AudioChannel* pDstL = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelLeft);
557                AudioChannel* pDstR = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelRight);
558                pEngineChannel->pChannelLeft->MixTo(pDstL, Samples);
559                pEngineChannel->pChannelRight->MixTo(pDstR, Samples);
560            }
561            // route FX send signal
562            {
563                for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) {
564                    FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend);
565                    // left channel
566                    const int iDstL = pFxSend->DestinationChannel(0);
567                    if (iDstL < 0) {
568                        dmsg(1,("Engine::RouteAudio() Error: invalid FX send (L) destination channel"));
569                    } else {
570                        AudioChannel* pDstL = pAudioOutputDevice->Channel(iDstL);
571                        if (!pDstL) {
572                            dmsg(1,("Engine::RouteAudio() Error: invalid FX send (L) destination channel"));
573                        } else pEngineChannel->pChannelLeft->MixTo(pDstL, Samples, pFxSend->Level());
574                    }
575                    // right channel
576                    const int iDstR = pFxSend->DestinationChannel(1);
577                    if (iDstR < 0) {
578                        dmsg(1,("Engine::RouteAudio() Error: invalid FX send (R) destination channel"));
579                    } else {
580                        AudioChannel* pDstR = pAudioOutputDevice->Channel(iDstR);
581                        if (!pDstR) {
582                            dmsg(1,("Engine::RouteAudio() Error: invalid FX send (R) destination channel"));
583                        } else pEngineChannel->pChannelRight->MixTo(pDstR, Samples, pFxSend->Level());
584                    }
585                }
586            }
587            // reset buffers with silence (zero out) for the next audio cycle
588            pEngineChannel->pChannelLeft->Clear();
589            pEngineChannel->pChannelRight->Clear();
590        }
591    
592        /**
593       * Free all keys which have turned inactive in this audio fragment, from       * Free all keys which have turned inactive in this audio fragment, from
594       * the list of active keys and clear all event lists on that engine       * the list of active keys and clear all event lists on that engine
595       * channel.       * channel.
# Line 609  namespace LinuxSampler { namespace gig { Line 662  namespace LinuxSampler { namespace gig {
662       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
663       */       */
664      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
665            #if !CONFIG_PROCESS_MUTED_CHANNELS
666            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
667            #endif
668    
669          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
670            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];
671    
672            // move note on event to the key's own event list
673            RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
674    
675            // if Solo Mode then kill all already active voices
676            if (pEngineChannel->SoloMode) {
677                Pool<uint>::Iterator itYoungestKey = pEngineChannel->pActiveKeys->last();
678                if (itYoungestKey) {
679                    const int iYoungestKey = *itYoungestKey;
680                    const midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[iYoungestKey];
681                    if (pOtherKey->Active) {
682                        // get final portamento position of currently active voice
683                        if (pEngineChannel->PortamentoMode) {
684                            RTList<Voice>::Iterator itVoice = pOtherKey->pActiveVoices->last();
685                            if (itVoice) itVoice->UpdatePortamentoPos(itNoteOnEventOnKeyList);
686                        }
687                        // kill all voices on the (other) key
688                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
689                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
690                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
691                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger)
692                                itVoiceToBeKilled->Kill(itNoteOnEventOnKeyList);
693                        }
694                    }
695                }
696                // set this key as 'currently active solo key'
697                pEngineChannel->SoloKey = key;
698            }
699    
700          // Change key dimension value if key is in keyswitching area          // Change key dimension value if key is in keyswitching area
701          {          {
702              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;
703              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)
704                  pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) /                  pEngineChannel->CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) /
705                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
706          }          }
707    
         midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];  
   
708          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
709          pKey->Velocity   = itNoteOnEvent->Param.Note.Velocity;          pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;
710          pKey->NoteOnTime = FrameTime + itNoteOnEvent->FragmentPos(); // will be used to calculate note length          pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length
711    
712          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
713          if (pKey->Active && !pEngineChannel->SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
714              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
715              if (itCancelReleaseEvent) {              if (itCancelReleaseEvent) {
716                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event                  *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event
717                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type
718              }              }
719              else dmsg(1,("Event pool emtpy!\n"));              else dmsg(1,("Event pool emtpy!\n"));
720          }          }
721    
         // move note on event to the key's own event list  
         RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);  
   
722          // allocate and trigger new voice(s) for the key          // allocate and trigger new voice(s) for the key
723          {          {
724              // first, get total amount of required voices (dependant on amount of layers)              // first, get total amount of required voices (dependant on amount of layers)
# Line 655  namespace LinuxSampler { namespace gig { Line 735  namespace LinuxSampler { namespace gig {
735          if (!pKey->Active && !pKey->VoiceTheftsQueued)          if (!pKey->Active && !pKey->VoiceTheftsQueued)
736              pKey->pEvents->free(itNoteOnEventOnKeyList);              pKey->pEvents->free(itNoteOnEventOnKeyList);
737    
738            if (!pEngineChannel->SoloMode || pEngineChannel->PortamentoPos < 0.0f) pEngineChannel->PortamentoPos = (float) key;
739          pKey->RoundRobinIndex++;          pKey->RoundRobinIndex++;
740      }      }
741    
# Line 668  namespace LinuxSampler { namespace gig { Line 749  namespace LinuxSampler { namespace gig {
749       *  @param itNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
750       */       */
751      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
752          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          #if !CONFIG_PROCESS_MUTED_CHANNELS
753            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
754            #endif
755    
756            const int iKey = itNoteOffEvent->Param.Note.Key;
757            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[iKey];
758          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
759    
760          // release voices on this key if needed          // move event to the key's own event list
761          if (pKey->Active && !pEngineChannel->SustainPedal) {          RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
             itNoteOffEvent->Type = Event::type_release; // transform event type  
762    
763              // move event to the key's own event list          bool bShouldRelease = pKey->Active && ShouldReleaseVoice(pEngineChannel, itNoteOffEventOnKeyList->Param.Note.Key);
764              RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);  
765            // in case Solo Mode is enabled, kill all voices on this key and respawn a voice on the highest pressed key (if any)
766            if (pEngineChannel->SoloMode) { //TODO: this feels like too much code just for handling solo mode :P
767                bool bOtherKeysPressed = false;
768                if (iKey == pEngineChannel->SoloKey) {
769                    pEngineChannel->SoloKey = -1;
770                    // if there's still a key pressed down, respawn a voice (group) on the highest key
771                    for (int i = 127; i > 0; i--) {
772                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[i];
773                        if (pOtherKey->KeyPressed) {
774                            bOtherKeysPressed = true;
775                            // make the other key the new 'currently active solo key'
776                            pEngineChannel->SoloKey = i;
777                            // get final portamento position of currently active voice
778                            if (pEngineChannel->PortamentoMode) {
779                                RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first();
780                                if (itVoice) itVoice->UpdatePortamentoPos(itNoteOffEventOnKeyList);
781                            }
782                            // create a pseudo note on event
783                            RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend();
784                            if (itPseudoNoteOnEvent) {
785                                // copy event
786                                *itPseudoNoteOnEvent = *itNoteOffEventOnKeyList;
787                                // transform event to a note on event
788                                itPseudoNoteOnEvent->Type                = Event::type_note_on;
789                                itPseudoNoteOnEvent->Param.Note.Key      = i;
790                                itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity;
791                                // allocate and trigger new voice(s) for the other key
792                                {
793                                    // first, get total amount of required voices (dependant on amount of layers)
794                                    ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(i);
795                                    if (pRegion) {
796                                        int voicesRequired = pRegion->Layers;
797                                        // now launch the required amount of voices
798                                        for (int iLayer = 0; iLayer < voicesRequired; iLayer++)
799                                            LaunchVoice(pEngineChannel, itPseudoNoteOnEvent, iLayer, false, true, false);
800                                    }
801                                }
802                                // if neither a voice was spawned or postponed then remove note on event from key again
803                                if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)
804                                    pOtherKey->pEvents->free(itPseudoNoteOnEvent);
805    
806                            } else dmsg(1,("Could not respawn voice, no free event left\n"));
807                            break; // done
808                        }
809                    }
810                }
811                if (bOtherKeysPressed) {
812                    if (pKey->Active) { // kill all voices on this key
813                        bShouldRelease = false; // no need to release, as we kill it here
814                        RTList<Voice>::Iterator itVoiceToBeKilled = pKey->pActiveVoices->first();
815                        RTList<Voice>::Iterator end               = pKey->pActiveVoices->end();
816                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
817                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger)
818                                itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);
819                        }
820                    }
821                } else pEngineChannel->PortamentoPos = -1.0f;
822            }
823    
824            // if no solo mode (the usual case) or if solo mode and no other key pressed, then release voices on this key if needed
825            if (bShouldRelease) {
826                itNoteOffEventOnKeyList->Type = Event::type_release; // transform event type
827    
828              // spawn release triggered voice(s) if needed              // spawn release triggered voice(s) if needed
829              if (pKey->ReleaseTrigger) {              if (pKey->ReleaseTrigger) {
# Line 695  namespace LinuxSampler { namespace gig { Line 841  namespace LinuxSampler { namespace gig {
841                  }                  }
842                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
843              }              }
   
             // if neither a voice was spawned or postponed then remove note off event from key again  
             if (!pKey->Active && !pKey->VoiceTheftsQueued)  
                 pKey->pEvents->free(itNoteOffEventOnKeyList);  
844          }          }
845    
846            // if neither a voice was spawned or postponed on this key then remove note off event from key again
847            if (!pKey->Active && !pKey->VoiceTheftsQueued)
848                pKey->pEvents->free(itNoteOffEventOnKeyList);
849      }      }
850    
851      /**      /**
852       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the engine
853       *  event list.       *  channel's event list. It will actually processed later by the
854         *  respective voice.
855       *       *
856       *  @param pEngineChannel - engine channel on which this event occured on       *  @param pEngineChannel - engine channel on which this event occured on
857       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
858       */       */
859      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {
860          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
         itPitchbendEvent.moveToEndOf(pEngineChannel->pSynthesisEvents[Event::destination_vco]);  
861      }      }
862    
863      /**      /**
# Line 735  namespace LinuxSampler { namespace gig { Line 881  namespace LinuxSampler { namespace gig {
881       *           defined for the given key).       *           defined for the given key).
882       */       */
883      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing, bool HandleKeyGroupConflicts) {      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing, bool HandleKeyGroupConflicts) {
884          midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          int MIDIKey            = itNoteOnEvent->Param.Note.Key;
885          ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);          midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[MIDIKey];
886            ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(MIDIKey);
887    
888          // if nothing defined for this key          // if nothing defined for this key
889          if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do          if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
890    
891            // only mark the first voice of a layered voice (group) to be in a
892            // key group, so the layered voices won't kill each other
893            int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
894    
895          // handle key group (a.k.a. exclusive group) conflicts          // handle key group (a.k.a. exclusive group) conflicts
896          if (HandleKeyGroupConflicts) {          if (HandleKeyGroupConflicts) {
             // only mark the first voice of a layered voice (group) to be in a  
             // key group, so the layered voices won't kill each other  
             int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;  
897              if (iKeyGroup) { // if this voice / key belongs to a key group              if (iKeyGroup) { // if this voice / key belongs to a key group
898                  uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];                  uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];
899                  if (*ppKeyGroup) { // if there's already an active key in that key group                  if (*ppKeyGroup) { // if there's already an active key in that key group
# Line 763  namespace LinuxSampler { namespace gig { Line 911  namespace LinuxSampler { namespace gig {
911              }              }
912          }          }
913    
914            Voice::type_t VoiceType = Voice::type_normal;
915    
916            // get current dimension values to select the right dimension region
917            //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
918            //FIXME: controller values for selecting the dimension region here are currently not sample accurate
919            uint DimValues[8] = { 0 };
920            for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
921                switch (pRegion->pDimensionDefinitions[i].dimension) {
922                    case ::gig::dimension_samplechannel:
923                        DimValues[i] = 0; //TODO: we currently ignore this dimension
924                        break;
925                    case ::gig::dimension_layer:
926                        DimValues[i] = iLayer;
927                        break;
928                    case ::gig::dimension_velocity:
929                        DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
930                        break;
931                    case ::gig::dimension_channelaftertouch:
932                        DimValues[i] = pEngineChannel->ControllerTable[128];
933                        break;
934                    case ::gig::dimension_releasetrigger:
935                        VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
936                        DimValues[i] = (uint) ReleaseTriggerVoice;
937                        break;
938                    case ::gig::dimension_keyboard:
939                        DimValues[i] = (uint) (pEngineChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones);
940                        break;
941                    case ::gig::dimension_roundrobin:
942                        DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on
943                        break;
944                    case ::gig::dimension_random:
945                        RandomSeed   = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
946                        DimValues[i] = (uint) RandomSeed >> (32 - pRegion->pDimensionDefinitions[i].bits); // highest bits are most random
947                        break;
948                    case ::gig::dimension_modwheel:
949                        DimValues[i] = pEngineChannel->ControllerTable[1];
950                        break;
951                    case ::gig::dimension_breath:
952                        DimValues[i] = pEngineChannel->ControllerTable[2];
953                        break;
954                    case ::gig::dimension_foot:
955                        DimValues[i] = pEngineChannel->ControllerTable[4];
956                        break;
957                    case ::gig::dimension_portamentotime:
958                        DimValues[i] = pEngineChannel->ControllerTable[5];
959                        break;
960                    case ::gig::dimension_effect1:
961                        DimValues[i] = pEngineChannel->ControllerTable[12];
962                        break;
963                    case ::gig::dimension_effect2:
964                        DimValues[i] = pEngineChannel->ControllerTable[13];
965                        break;
966                    case ::gig::dimension_genpurpose1:
967                        DimValues[i] = pEngineChannel->ControllerTable[16];
968                        break;
969                    case ::gig::dimension_genpurpose2:
970                        DimValues[i] = pEngineChannel->ControllerTable[17];
971                        break;
972                    case ::gig::dimension_genpurpose3:
973                        DimValues[i] = pEngineChannel->ControllerTable[18];
974                        break;
975                    case ::gig::dimension_genpurpose4:
976                        DimValues[i] = pEngineChannel->ControllerTable[19];
977                        break;
978                    case ::gig::dimension_sustainpedal:
979                        DimValues[i] = pEngineChannel->ControllerTable[64];
980                        break;
981                    case ::gig::dimension_portamento:
982                        DimValues[i] = pEngineChannel->ControllerTable[65];
983                        break;
984                    case ::gig::dimension_sostenutopedal:
985                        DimValues[i] = pEngineChannel->ControllerTable[66];
986                        break;
987                    case ::gig::dimension_softpedal:
988                        DimValues[i] = pEngineChannel->ControllerTable[67];
989                        break;
990                    case ::gig::dimension_genpurpose5:
991                        DimValues[i] = pEngineChannel->ControllerTable[80];
992                        break;
993                    case ::gig::dimension_genpurpose6:
994                        DimValues[i] = pEngineChannel->ControllerTable[81];
995                        break;
996                    case ::gig::dimension_genpurpose7:
997                        DimValues[i] = pEngineChannel->ControllerTable[82];
998                        break;
999                    case ::gig::dimension_genpurpose8:
1000                        DimValues[i] = pEngineChannel->ControllerTable[83];
1001                        break;
1002                    case ::gig::dimension_effect1depth:
1003                        DimValues[i] = pEngineChannel->ControllerTable[91];
1004                        break;
1005                    case ::gig::dimension_effect2depth:
1006                        DimValues[i] = pEngineChannel->ControllerTable[92];
1007                        break;
1008                    case ::gig::dimension_effect3depth:
1009                        DimValues[i] = pEngineChannel->ControllerTable[93];
1010                        break;
1011                    case ::gig::dimension_effect4depth:
1012                        DimValues[i] = pEngineChannel->ControllerTable[94];
1013                        break;
1014                    case ::gig::dimension_effect5depth:
1015                        DimValues[i] = pEngineChannel->ControllerTable[95];
1016                        break;
1017                    case ::gig::dimension_none:
1018                        std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
1019                        break;
1020                    default:
1021                        std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
1022                }
1023            }
1024            ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
1025    
1026            // no need to continue if sample is silent
1027            if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
1028    
1029          // allocate a new voice for the key          // allocate a new voice for the key
1030          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
1031          if (itNewVoice) {          if (itNewVoice) {
1032              // launch the new voice              // launch the new voice
1033              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pDimRgn, VoiceType, iKeyGroup) < 0) {
1034                  dmsg(4,("Voice not triggered\n"));                  dmsg(4,("Voice not triggered\n"));
1035                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
1036              }              }
# Line 1031  namespace LinuxSampler { namespace gig { Line 1294  namespace LinuxSampler { namespace gig {
1294          // update controller value in the engine channel's controller table          // update controller value in the engine channel's controller table
1295          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
1296    
1297          // move event from the unsorted event list to the control change event list          // handle hard coded MIDI controllers
1298          Pool<Event>::Iterator itControlChangeEventOnCCList = itControlChangeEvent.moveToEndOf(pEngineChannel->pCCEvents);          switch (itControlChangeEvent->Param.CC.Controller) {
1299                case 5: { // portamento time
1300          switch (itControlChangeEventOnCCList->Param.CC.Controller) {                  pEngineChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;
1301                    break;
1302                }
1303              case 7: { // volume              case 7: { // volume
1304                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1305                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;                  pEngineChannel->MidiVolume = VolumeCurve[itControlChangeEvent->Param.CC.Value];
1306                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1307                  break;                  break;
1308              }              }
1309              case 10: { // panpot              case 10: { // panpot
1310                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1311                  const int pan = (int) itControlChangeEventOnCCList->Param.CC.Value - 64;                  pEngineChannel->GlobalPanLeft  = PanCurve[128 - itControlChangeEvent->Param.CC.Value];
1312                  pEngineChannel->GlobalPanLeft  = 1.0f - float(RTMath::Max(pan, 0)) /  63.0f;                  pEngineChannel->GlobalPanRight = PanCurve[itControlChangeEvent->Param.CC.Value];
                 pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f;  
1313                  break;                  break;
1314              }              }
1315              case 64: { // sustain              case 64: { // sustain
1316                  if (itControlChangeEventOnCCList->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {
1317                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("DAMPER (RIGHT) PEDAL DOWN\n"));
1318                      pEngineChannel->SustainPedal = true;                      pEngineChannel->SustainPedal = true;
1319    
1320                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1321                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1322                        #endif
1323    
1324                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
1325                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1326                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1060  namespace LinuxSampler { namespace gig { Line 1328  namespace LinuxSampler { namespace gig {
1328                          if (!pKey->KeyPressed) {                          if (!pKey->KeyPressed) {
1329                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1330                              if (itNewEvent) {                              if (itNewEvent) {
1331                                  *itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1332                                  itNewEvent->Type = Event::type_cancel_release; // transform event type                                  itNewEvent->Type = Event::type_cancel_release; // transform event type
1333                              }                              }
1334                              else dmsg(1,("Event pool emtpy!\n"));                              else dmsg(1,("Event pool emtpy!\n"));
1335                          }                          }
1336                      }                      }
1337                  }                  }
1338                  if (itControlChangeEventOnCCList->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {
1339                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("DAMPER (RIGHT) PEDAL UP\n"));
1340                      pEngineChannel->SustainPedal = false;                      pEngineChannel->SustainPedal = false;
1341    
1342                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1343                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1344                        #endif
1345    
1346                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
1347                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1348                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
1349                          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];                          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
1350                          if (!pKey->KeyPressed) {                          if (!pKey->KeyPressed && ShouldReleaseVoice(pEngineChannel, *iuiKey)) {
1351                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1352                              if (itNewEvent) {                              if (itNewEvent) {
1353                                  *itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1354                                    itNewEvent->Type = Event::type_release; // transform event type
1355                                }
1356                                else dmsg(1,("Event pool emtpy!\n"));
1357                            }
1358                        }
1359                    }
1360                    break;
1361                }
1362                case 65: { // portamento on / off
1363                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1364                    pEngineChannel->PortamentoMode = itControlChangeEvent->Param.CC.Value >= 64;
1365                    break;
1366                }
1367                case 66: { // sostenuto
1368                    if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SostenutoPedal) {
1369                        dmsg(4,("SOSTENUTO (CENTER) PEDAL DOWN\n"));
1370                        pEngineChannel->SostenutoPedal = true;
1371    
1372                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1373                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1374                        #endif
1375    
1376                        SostenutoKeyCount = 0;
1377                        // Remeber the pressed keys
1378                        RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1379                        for (; iuiKey; ++iuiKey) {
1380                            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
1381                            if (pKey->KeyPressed && SostenutoKeyCount < 128) SostenutoKeys[SostenutoKeyCount++] = *iuiKey;
1382                        }
1383                    }
1384                    if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SostenutoPedal) {
1385                        dmsg(4,("SOSTENUTO (CENTER) PEDAL UP\n"));
1386                        pEngineChannel->SostenutoPedal = false;
1387    
1388                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1389                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1390                        #endif
1391    
1392                        // release voices if the damper pedal is up and their respective key is not pressed
1393                        for (int i = 0; i < SostenutoKeyCount; i++) {
1394                            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[SostenutoKeys[i]];
1395                            if (!pKey->KeyPressed && !pEngineChannel->SustainPedal) {
1396                                RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1397                                if (itNewEvent) {
1398                                    *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1399                                  itNewEvent->Type = Event::type_release; // transform event type                                  itNewEvent->Type = Event::type_release; // transform event type
1400                              }                              }
1401                              else dmsg(1,("Event pool emtpy!\n"));                              else dmsg(1,("Event pool emtpy!\n"));
# Line 1092  namespace LinuxSampler { namespace gig { Line 1409  namespace LinuxSampler { namespace gig {
1409              // Channel Mode Messages              // Channel Mode Messages
1410    
1411              case 120: { // all sound off              case 120: { // all sound off
1412                  KillAllVoices(pEngineChannel, itControlChangeEventOnCCList);                  KillAllVoices(pEngineChannel, itControlChangeEvent);
1413                  break;                  break;
1414              }              }
1415              case 121: { // reset all controllers              case 121: { // reset all controllers
# Line 1100  namespace LinuxSampler { namespace gig { Line 1417  namespace LinuxSampler { namespace gig {
1417                  break;                  break;
1418              }              }
1419              case 123: { // all notes off              case 123: { // all notes off
1420                  ReleaseAllVoices(pEngineChannel, itControlChangeEventOnCCList);                  #if CONFIG_PROCESS_ALL_NOTES_OFF
1421                    ReleaseAllVoices(pEngineChannel, itControlChangeEvent);
1422                    #endif // CONFIG_PROCESS_ALL_NOTES_OFF
1423                    break;
1424                }
1425                case 126: { // mono mode on
1426                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1427                    pEngineChannel->SoloMode = true;
1428                    break;
1429                }
1430                case 127: { // poly mode on
1431                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1432                    pEngineChannel->SoloMode = false;
1433                  break;                  break;
1434              }              }
1435          }          }
1436    
1437            // handle FX send controllers
1438            if (!pEngineChannel->fxSends.empty()) {
1439                for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) {
1440                    FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend);
1441                    if (pFxSend->MidiController() == itControlChangeEvent->Param.CC.Controller)
1442                        pFxSend->SetLevel(itControlChangeEvent->Param.CC.Value);
1443                }
1444            }
1445      }      }
1446    
1447      /**      /**
# Line 1112  namespace LinuxSampler { namespace gig { Line 1450  namespace LinuxSampler { namespace gig {
1450       *  @param itSysexEvent - sysex data size and time stamp of the sysex event       *  @param itSysexEvent - sysex data size and time stamp of the sysex event
1451       */       */
1452      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {
1453          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();          RingBuffer<uint8_t,false>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
1454    
1455          uint8_t exclusive_status, id;          uint8_t exclusive_status, id;
1456          if (!reader.pop(&exclusive_status)) goto free_sysex_data;          if (!reader.pop(&exclusive_status)) goto free_sysex_data;
# Line 1131  namespace LinuxSampler { namespace gig { Line 1469  namespace LinuxSampler { namespace gig {
1469    
1470                  // command address                  // command address
1471                  uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)                  uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)
1472                  const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later                  const RingBuffer<uint8_t,false>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later
1473                  if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;                  if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;
1474                  if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters                  if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters
1475                      dmsg(3,("\tSystem Parameter\n"));                      dmsg(3,("\tSystem Parameter\n"));
# Line 1178  namespace LinuxSampler { namespace gig { Line 1516  namespace LinuxSampler { namespace gig {
1516       *                     question       *                     question
1517       * @param DataSize   - size of the GS message data (in bytes)       * @param DataSize   - size of the GS message data (in bytes)
1518       */       */
1519      uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) {      uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) {
1520          RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader;          RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader;
1521          uint bytes = 3 /*addr*/ + DataSize;          uint bytes = 3 /*addr*/ + DataSize;
1522          uint8_t addr_and_data[bytes];          uint8_t addr_and_data[bytes];
1523          reader.read(&addr_and_data[0], bytes);          reader.read(&addr_and_data[0], bytes);
# Line 1244  namespace LinuxSampler { namespace gig { Line 1582  namespace LinuxSampler { namespace gig {
1582      }      }
1583    
1584      /**      /**
1585       * Initialize the parameter sequence for the modulation destination given by       * Determines whether the specified voice should be released.
1586       * by 'dst' with the constant value given by val.       *
1587       */       * @param pEngineChannel - The engine channel on which the voice should be checked
1588      void Engine::ResetSynthesisParameters(Event::destination_t dst, float val) {       * @param Key - The key number
1589          int maxsamples = pAudioOutputDevice->MaxSamplesPerCycle();       * @returns true if the specified should be released, false otherwise.
1590          float* m = &pSynthesisParameters[dst][0];       */
1591          for (int i = 0; i < maxsamples; i += 4) {      bool Engine::ShouldReleaseVoice(EngineChannel* pEngineChannel, int Key) {
1592             m[i]   = val;          if (pEngineChannel->SustainPedal) return false;
1593             m[i+1] = val;  
1594             m[i+2] = val;          if (pEngineChannel->SostenutoPedal) {
1595             m[i+3] = val;              for (int i = 0; i < SostenutoKeyCount; i++)
1596                    if (Key == SostenutoKeys[i]) return false;
1597          }          }
1598    
1599            return true;
1600      }      }
1601    
1602      uint Engine::VoiceCount() {      uint Engine::VoiceCount() {
# Line 1295  namespace LinuxSampler { namespace gig { Line 1636  namespace LinuxSampler { namespace gig {
1636      }      }
1637    
1638      String Engine::Version() {      String Engine::Version() {
1639          String s = "$Revision: 1.46 $";          String s = "$Revision: 1.70 $";
1640          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
1641      }      }
1642    
1643        InstrumentManager* Engine::GetInstrumentManager() {
1644            return &instruments;
1645        }
1646    
1647        // static constant initializers
1648        const float* Engine::VolumeCurve(InitVolumeCurve());
1649        const float* Engine::PanCurve(InitPanCurve());
1650        const float* Engine::CrossfadeCurve(InitCrossfadeCurve());
1651    
1652        float* Engine::InitVolumeCurve() {
1653            // line-segment approximation
1654            const float segments[] = {
1655                0, 0, 2, 0.0046, 16, 0.016, 31, 0.051, 45, 0.115, 54.5, 0.2,
1656                64.5, 0.39, 74, 0.74, 92, 1.03, 114, 1.94, 119.2, 2.2, 127, 2.2
1657            };
1658            return InitCurve(segments);
1659        }
1660    
1661        float* Engine::InitPanCurve() {
1662            // line-segment approximation
1663            const float segments[] = {
1664                0, 0, 1, 0,
1665                2, 0.05, 31.5, 0.7, 51, 0.851, 74.5, 1.12,
1666                127, 1.41, 128, 1.41
1667            };
1668            return InitCurve(segments, 129);
1669        }
1670    
1671        float* Engine::InitCrossfadeCurve() {
1672            // line-segment approximation
1673            const float segments[] = {
1674                0, 0, 1, 0.03, 10, 0.1, 51, 0.58, 127, 1
1675            };
1676            return InitCurve(segments);
1677        }
1678    
1679        float* Engine::InitCurve(const float* segments, int size) {
1680            float* y = new float[size];
1681            for (int x = 0 ; x < size ; x++) {
1682                if (x > segments[2]) segments += 2;
1683                y[x] = segments[1] + (x - segments[0]) *
1684                    (segments[3] - segments[1]) / (segments[2] - segments[0]);
1685            }
1686            return y;
1687        }
1688    
1689  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.668  
changed lines
  Added in v.1037

  ViewVC Help
Powered by ViewVC