/[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 410 by schoenebeck, Sat Feb 19 02:40:24 2005 UTC revision 411 by schoenebeck, Sat Feb 26 02:01:14 2005 UTC
# Line 3  Line 3 
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                              *
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 35  Line 36 
36    
37  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
38    
39      InstrumentResourceManager Engine::Instruments;      InstrumentResourceManager Engine::instruments;
40    
41        std::map<AudioOutputDevice*,Engine*> Engine::engines;
42    
43        Engine* Engine::AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) {
44            Engine* pEngine;
45            if (engines.count(pDevice)) {
46                pEngine = engines[pDevice];
47            } else {
48                pEngine = new Engine;
49                pEngine->Connect(pDevice);
50            }
51            pEngine->samplerChannels.push_back(pChannel);
52            return pEngine;
53        }
54    
55        void Engine::FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) {
56            Engine* pEngine = engines[pDevice];
57            pEngine->samplerChannels.remove(pChannel);
58            if (pEngine->samplerChannels.empty()) delete pEngine;
59        }
60    
61      Engine::Engine() {      Engine::Engine() {
         pRIFF              = NULL;  
         pGig               = NULL;  
         pInstrument        = NULL;  
62          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
63          pDiskThread        = NULL;          pDiskThread        = NULL;
64          pEventGenerator    = NULL;          pEventGenerator    = NULL;
65          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);
66          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
67          pEventPool         = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT);
68          pVoicePool         = new Pool<Voice>(MAX_AUDIO_VOICES);          pVoicePool         = new Pool<Voice>(MAX_AUDIO_VOICES);        
         pActiveKeys        = new Pool<uint>(128);  
69          pVoiceStealingQueue = new RTList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
70          pEvents            = new RTList<Event>(pEventPool);          pEvents            = new RTList<Event>(pEventPool);
71          pCCEvents          = new RTList<Event>(pEventPool);          pCCEvents          = new RTList<Event>(pEventPool);
72            
73          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
74              pSynthesisEvents[i] = new RTList<Event>(pEventPool);              pSynthesisEvents[i] = new RTList<Event>(pEventPool);
75          }          }
         for (uint i = 0; i < 128; i++) {  
             pMIDIKeyInfo[i].pActiveVoices  = new RTList<Voice>(pVoicePool);  
             pMIDIKeyInfo[i].KeyPressed     = false;  
             pMIDIKeyInfo[i].Active         = false;  
             pMIDIKeyInfo[i].ReleaseTrigger = false;  
             pMIDIKeyInfo[i].pEvents        = new RTList<Event>(pEventPool);  
         }  
76          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
77              iterVoice->SetEngine(this);              iterVoice->SetEngine(this);
78          }          }
# Line 71  namespace LinuxSampler { namespace gig { Line 82  namespace LinuxSampler { namespace gig {
82          pBasicFilterParameters  = NULL;          pBasicFilterParameters  = NULL;
83          pMainFilterParameters   = NULL;          pMainFilterParameters   = NULL;
84    
         InstrumentIdx = -1;  
         InstrumentStat = -1;  
   
         AudioDeviceChannelLeft  = -1;  
         AudioDeviceChannelRight = -1;  
   
85          ResetInternal();          ResetInternal();
86      }      }
87    
# Line 87  namespace LinuxSampler { namespace gig { Line 92  namespace LinuxSampler { namespace gig {
92              delete pDiskThread;              delete pDiskThread;
93              dmsg(1,("OK\n"));              dmsg(1,("OK\n"));
94          }          }
   
         if (pInstrument) Instruments.HandBack(pInstrument, this);  
   
         if (pGig)  delete pGig;  
         if (pRIFF) delete pRIFF;  
         for (uint i = 0; i < 128; i++) {  
             if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices;  
             if (pMIDIKeyInfo[i].pEvents)       delete pMIDIKeyInfo[i].pEvents;  
         }  
95          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
96              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];
97          }          }
# Line 103  namespace LinuxSampler { namespace gig { Line 99  namespace LinuxSampler { namespace gig {
99          if (pCCEvents)   delete pCCEvents;          if (pCCEvents)   delete pCCEvents;
100          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
101          if (pEventPool)  delete pEventPool;          if (pEventPool)  delete pEventPool;
102          if (pVoicePool) {          if (pVoicePool) {
103                  pVoicePool->clear();              pVoicePool->clear();
104                  delete pVoicePool;              delete pVoicePool;
105          }          }
         if (pActiveKeys) delete pActiveKeys;  
         if (pSysexBuffer) delete pSysexBuffer;  
106          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
107          if (pMainFilterParameters) delete[] pMainFilterParameters;          if (pMainFilterParameters) delete[] pMainFilterParameters;
108          if (pBasicFilterParameters) delete[] pBasicFilterParameters;          if (pBasicFilterParameters) delete[] pBasicFilterParameters;
109          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
110          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
111            if (pSysexBuffer) delete pSysexBuffer;
112      }      }
113    
114      void Engine::Enable() {      void Engine::Enable() {
# Line 140  namespace LinuxSampler { namespace gig { Line 135  namespace LinuxSampler { namespace gig {
135       */       */
136      void Engine::Reset() {      void Engine::Reset() {
137          DisableAndLock();          DisableAndLock();
   
         //if (pAudioOutputDevice->IsPlaying()) { // if already running  
             /*  
             // signal audio thread not to enter render part anymore  
             SuspensionRequested = true;  
             // sleep until wakened by audio thread  
             pthread_mutex_lock(&__render_state_mutex);  
             pthread_cond_wait(&__render_exit_condition, &__render_state_mutex);  
             pthread_mutex_unlock(&__render_state_mutex);  
             */  
         //}  
   
         //if (wasplaying) pAudioOutputDevice->Stop();  
   
138          ResetInternal();          ResetInternal();
   
         // signal audio thread to continue with rendering  
         //SuspensionRequested = false;  
139          Enable();          Enable();
140      }      }
141    
# Line 166  namespace LinuxSampler { namespace gig { Line 144  namespace LinuxSampler { namespace gig {
144       *  control and status variables. This method is not thread safe!       *  control and status variables. This method is not thread safe!
145       */       */
146      void Engine::ResetInternal() {      void Engine::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
147          ActiveVoiceCount    = 0;          ActiveVoiceCount    = 0;
148          ActiveVoiceCountMax = 0;          ActiveVoiceCountMax = 0;
         GlobalVolume        = 1.0;  
         CurrentKeyDimension = 0;  
149    
150          // reset voice stealing parameters          // reset voice stealing parameters
151          itLastStolenVoice = RTList<Voice>::Iterator();          itLastStolenVoice = RTList<Voice>::Iterator();
# Line 181  namespace LinuxSampler { namespace gig { Line 155  namespace LinuxSampler { namespace gig {
155          // reset to normal chromatic scale (means equal temper)          // reset to normal chromatic scale (means equal temper)
156          memset(&ScaleTuning[0], 0x00, 12);          memset(&ScaleTuning[0], 0x00, 12);
157    
         // set all MIDI controller values to zero  
         memset(ControllerTable, 0x00, 128);  
   
         // reset key info  
         for (uint i = 0; i < 128; i++) {  
             pMIDIKeyInfo[i].pActiveVoices->clear();  
             pMIDIKeyInfo[i].pEvents->clear();  
             pMIDIKeyInfo[i].KeyPressed     = false;  
             pMIDIKeyInfo[i].Active         = false;  
             pMIDIKeyInfo[i].ReleaseTrigger = false;  
             pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();  
         }  
   
         // reset all key groups  
         map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();  
         for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;  
   
158          // reset all voices          // reset all voices
159          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
160              iterVoice->Reset();              iterVoice->Reset();
161          }          }
162          pVoicePool->clear();          pVoicePool->clear();
163    
         // free all active keys  
         pActiveKeys->clear();  
   
164          // reset disk thread          // reset disk thread
165          if (pDiskThread) pDiskThread->Reset();          if (pDiskThread) pDiskThread->Reset();
166    
# Line 214  namespace LinuxSampler { namespace gig { Line 168  namespace LinuxSampler { namespace gig {
168          pEventQueue->init();          pEventQueue->init();
169      }      }
170    
     /**  
      * 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.  
      *  
      * @param FileName   - file name of the Gigasampler instrument file  
      * @param Instrument - index of the instrument in the .gig file  
      * @see LoadInstrument()  
      */  
     void Engine::PrepareLoadInstrument(const char* FileName, uint Instrument) {  
         InstrumentFile = FileName;  
         InstrumentIdx  = Instrument;  
         InstrumentStat = 0;  
     }  
   
     /**  
      * Load an instrument from a .gig file. PrepareLoadInstrument() has to  
      * be called first to provide the information which instrument to load.  
      * 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 Engine::LoadInstrument() {  
   
         DisableAndLock();  
   
         ResetInternal(); // reset engine  
   
         // free old instrument  
         if (pInstrument) {  
             // give old instrument back to instrument manager  
             Instruments.HandBack(pInstrument, this);  
         }  
   
         // delete all key groups  
         ActiveKeyGroups.clear();  
   
         // request gig instrument from instrument manager  
         try {  
             instrument_id_t instrid;  
             instrid.FileName    = InstrumentFile;  
             instrid.iInstrument = InstrumentIdx;  
             pInstrument = Instruments.Borrow(instrid, this);  
             if (!pInstrument) {  
                 InstrumentStat = -1;  
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
             }  
         }  
         catch (RIFF::Exception e) {  
             InstrumentStat = -2;  
             String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;  
             throw LinuxSamplerException(msg);  
         }  
         catch (InstrumentResourceManagerException e) {  
             InstrumentStat = -3;  
             String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();  
             throw LinuxSamplerException(msg);  
         }  
         catch (...) {  
             InstrumentStat = -4;  
             throw LinuxSamplerException("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;  
   
         InstrumentIdxName = pInstrument->pInfo->Name;  
         InstrumentStat = 100;  
   
         // inform audio driver for the need of two channels  
         try {  
             if (pAudioOutputDevice) 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);  
         }  
   
         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 Engine::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {  
         dmsg(3,("gig::Engine: Received instrument update message.\n"));  
         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 Engine::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())  
         Enable();  
     }  
   
171      void Engine::Connect(AudioOutputDevice* pAudioOut) {      void Engine::Connect(AudioOutputDevice* pAudioOut) {
172          pAudioOutputDevice = pAudioOut;          pAudioOutputDevice = pAudioOut;
173    
# Line 334  namespace LinuxSampler { namespace gig { Line 181  namespace LinuxSampler { namespace gig {
181              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();
182              throw LinuxSamplerException(msg);              throw LinuxSamplerException(msg);
183          }          }
184            
         this->AudioDeviceChannelLeft  = 0;  
         this->AudioDeviceChannelRight = 1;  
         this->pOutputLeft             = pAudioOutputDevice->Channel(0)->Buffer();  
         this->pOutputRight            = pAudioOutputDevice->Channel(1)->Buffer();  
185          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();
186          this->SampleRate              = pAudioOutputDevice->SampleRate();          this->SampleRate              = pAudioOutputDevice->SampleRate();
187    
188          // FIXME: audio drivers with varying fragment sizes might be a problem here          // FIXME: audio drivers with varying fragment sizes might be a problem here
189          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1;          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1;
190          if (MaxFadeOutPos < 0)          if (MaxFadeOutPos < 0)
191              throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!");              throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h too big for current audio fragment size / sampling rate!");
192    
193          // (re)create disk thread          // (re)create disk thread
194          if (this->pDiskThread) {          if (this->pDiskThread) {
# Line 399  namespace LinuxSampler { namespace gig { Line 242  namespace LinuxSampler { namespace gig {
242          }          }
243      }      }
244    
     void Engine::DisconnectAudioOutputDevice() {  
         if (pAudioOutputDevice) { // if clause to prevent disconnect loops  
             AudioOutputDevice* olddevice = pAudioOutputDevice;  
             pAudioOutputDevice = NULL;  
             olddevice->Disconnect(this);  
             AudioDeviceChannelLeft  = -1;  
             AudioDeviceChannelRight = -1;  
         }  
     }  
   
245      /**      /**
246       *  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. The
247       *  calculated audio data of all voices of this engine will be placed into       *  calculated audio data of all voices of this engine will be placed into
# Line 416  namespace LinuxSampler { namespace gig { Line 249  namespace LinuxSampler { namespace gig {
249       *  converted to the appropriate value range by the audio output class (e.g.       *  converted to the appropriate value range by the audio output class (e.g.
250       *  AlsaIO or JackIO) right after.       *  AlsaIO or JackIO) right after.
251       *       *
252         *  @param pEngineChannel - the engine's channel to be rendered
253       *  @param Samples - number of sample points to be rendered       *  @param Samples - number of sample points to be rendered
254       *  @returns       0 on success       *  @returns       0 on success
255       */       */
256      int Engine::RenderAudio(uint Samples) {      int Engine::RenderAudio(LinuxSampler::gig::EngineChannel* pEngineChannel, uint Samples) {
257          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));
258    
259          // return if no instrument loaded or engine disabled          // return if no instrument loaded or engine disabled
# Line 427  namespace LinuxSampler { namespace gig { Line 261  namespace LinuxSampler { namespace gig {
261              dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe()));              dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe()));
262              return 0;              return 0;
263          }          }
264          if (!pInstrument) {          if (!pEngineChannel->pInstrument) {
265              dmsg(5,("gig::Engine: no instrument loaded\n"));              dmsg(5,("gig::Engine: no instrument loaded\n"));
266              return 0;              return 0;
267          }          }
# Line 444  namespace LinuxSampler { namespace gig { Line 278  namespace LinuxSampler { namespace gig {
278              pSynthesisEvents[i]->clear();              pSynthesisEvents[i]->clear();
279          }          }
280          {          {
281              RTList<uint>::Iterator iuiKey = pActiveKeys->first();              RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
282              RTList<uint>::Iterator end    = pActiveKeys->end();              RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
283              for(; iuiKey != end; ++iuiKey) {              for(; iuiKey != end; ++iuiKey) {
284                  pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key                  pEngineChannel->pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
285              }              }
286          }          }
287    
# Line 485  namespace LinuxSampler { namespace gig { Line 319  namespace LinuxSampler { namespace gig {
319                  switch (itEvent->Type) {                  switch (itEvent->Type) {
320                      case Event::type_note_on:                      case Event::type_note_on:
321                          dmsg(5,("Engine: Note on received\n"));                          dmsg(5,("Engine: Note on received\n"));
322                          ProcessNoteOn(itEvent);                          ProcessNoteOn(pEngineChannel, itEvent);
323                          break;                          break;
324                      case Event::type_note_off:                      case Event::type_note_off:
325                          dmsg(5,("Engine: Note off received\n"));                          dmsg(5,("Engine: Note off received\n"));
326                          ProcessNoteOff(itEvent);                          ProcessNoteOff(pEngineChannel, itEvent);
327                          break;                          break;
328                      case Event::type_control_change:                      case Event::type_control_change:
329                          dmsg(5,("Engine: MIDI CC received\n"));                          dmsg(5,("Engine: MIDI CC received\n"));
330                          ProcessControlChange(itEvent);                          ProcessControlChange(pEngineChannel, itEvent);
331                          break;                          break;
332                      case Event::type_pitchbend:                      case Event::type_pitchbend:
333                          dmsg(5,("Engine: Pitchbend received\n"));                          dmsg(5,("Engine: Pitchbend received\n"));
334                          ProcessPitchbend(itEvent);                          ProcessPitchbend(pEngineChannel, itEvent);
335                          break;                          break;
336                      case Event::type_sysex:                      case Event::type_sysex:
337                          dmsg(5,("Engine: Sysex received\n"));                          dmsg(5,("Engine: Sysex received\n"));
# Line 512  namespace LinuxSampler { namespace gig { Line 346  namespace LinuxSampler { namespace gig {
346    
347          // render audio from all active voices          // render audio from all active voices
348          {          {
349              RTList<uint>::Iterator iuiKey = pActiveKeys->first();              RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
350              RTList<uint>::Iterator end    = pActiveKeys->end();              RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
351              while (iuiKey != end) { // iterate through all active keys              while (iuiKey != end) { // iterate through all active keys
352                  midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];                  midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
353                  ++iuiKey;                  ++iuiKey;
354    
355                  RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();                  RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
# Line 525  namespace LinuxSampler { namespace gig { Line 359  namespace LinuxSampler { namespace gig {
359                      itVoice->Render(Samples);                      itVoice->Render(Samples);
360                      if (itVoice->IsActive()) active_voices++; // still active                      if (itVoice->IsActive()) active_voices++; // still active
361                      else { // voice reached end, is now inactive                      else { // voice reached end, is now inactive
362                          FreeVoice(itVoice); // remove voice from the list of active voices                          FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices
363                      }                      }
364                  }                  }
365              }              }
# Line 537  namespace LinuxSampler { namespace gig { Line 371  namespace LinuxSampler { namespace gig {
371              RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first();              RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first();
372              RTList<Event>::Iterator end               = pVoiceStealingQueue->end();              RTList<Event>::Iterator end               = pVoiceStealingQueue->end();
373              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
374                  Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);                  Pool<Voice>::Iterator itNewVoice =
375                        LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);
376                  if (itNewVoice) {                  if (itNewVoice) {
377                      for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) {                      for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) {
378                          itNewVoice->Render(Samples);                          itNewVoice->Render(Samples);
379                          if (itNewVoice->IsActive()) active_voices++; // still active                          if (itNewVoice->IsActive()) active_voices++; // still active
380                          else { // voice reached end, is now inactive                          else { // voice reached end, is now inactive
381                              FreeVoice(itNewVoice); // remove voice from the list of active voices                              FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices
382                          }                          }
383                      }                      }
384                  }                  }
# Line 558  namespace LinuxSampler { namespace gig { Line 393  namespace LinuxSampler { namespace gig {
393    
394          // free all keys which have no active voices left          // free all keys which have no active voices left
395          {          {
396              RTList<uint>::Iterator iuiKey = pActiveKeys->first();              RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
397              RTList<uint>::Iterator end    = pActiveKeys->end();              RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
398              while (iuiKey != end) { // iterate through all active keys              while (iuiKey != end) { // iterate through all active keys
399                  midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];                  midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
400                  ++iuiKey;                  ++iuiKey;
401                  if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey);                  if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey);
402                  #if DEVMODE                  #if DEVMODE
403                  else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)                  else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
404                      RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();                      RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
# Line 586  namespace LinuxSampler { namespace gig { Line 421  namespace LinuxSampler { namespace gig {
421    
422    
423          return 0;          return 0;
424      }      }    
   
     /**  
      *  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 Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) {  
         Event event               = pEventGenerator->CreateEvent();  
         event.Type                = Event::type_note_on;  
         event.Param.Note.Key      = Key;  
         event.Param.Note.Velocity = Velocity;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
   
     /**  
      *  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 Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) {  
         Event event               = pEventGenerator->CreateEvent();  
         event.Type                = Event::type_note_off;  
         event.Param.Note.Key      = Key;  
         event.Param.Note.Velocity = Velocity;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
   
     /**  
      *  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 Engine::SendPitchbend(int Pitch) {  
         Event event             = pEventGenerator->CreateEvent();  
         event.Type              = Event::type_pitchbend;  
         event.Param.Pitch.Pitch = Pitch;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
   
     /**  
      *  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 Engine::SendControlChange(uint8_t Controller, uint8_t Value) {  
         Event event               = pEventGenerator->CreateEvent();  
         event.Type                = Event::type_control_change;  
         event.Param.CC.Controller = Controller;  
         event.Param.CC.Value      = Value;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
425    
426      /**      /**
427       *  Will be called by the MIDI input device whenever a MIDI system       *  Will be called by the MIDI input device whenever a MIDI system
# Line 684  namespace LinuxSampler { namespace gig { Line 457  namespace LinuxSampler { namespace gig {
457      /**      /**
458       *  Assigns and triggers a new voice for the respective MIDI key.       *  Assigns and triggers a new voice for the respective MIDI key.
459       *       *
460         *  @param pEngineChannel - engine channel on which this event occured on
461       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
462       */       */
463      void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
464            
465          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
466    
467          // Change key dimension value if key is in keyswitching area          // Change key dimension value if key is in keyswitching area
468          if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)          {
469              CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) /              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;
470                  (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)
471                    pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) /
472                        (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
473            }
474    
475          midi_key_info_t* pKey = &pMIDIKeyInfo[key];          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];
476    
477          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
478    
479          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
480          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
481              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
482              if (itCancelReleaseEvent) {              if (itCancelReleaseEvent) {
483                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event
# Line 713  namespace LinuxSampler { namespace gig { Line 490  namespace LinuxSampler { namespace gig {
490          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
491    
492          // allocate and trigger a new voice for the key          // allocate and trigger a new voice for the key
493          LaunchVoice(itNoteOnEventOnKeyList, 0, false, true);          LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, 0, false, true);
494      }      }
495    
496      /**      /**
# Line 722  namespace LinuxSampler { namespace gig { Line 499  namespace LinuxSampler { namespace gig {
499       *  sustain pedal will be released or voice turned inactive by itself (e.g.       *  sustain pedal will be released or voice turned inactive by itself (e.g.
500       *  due to completion of sample playback).       *  due to completion of sample playback).
501       *       *
502         *  @param pEngineChannel - engine channel on which this event occured on
503       *  @param itNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
504       */       */
505      void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
506          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
507    
508          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
509    
510          // release voices on this key if needed          // release voices on this key if needed
511          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
512              itNoteOffEvent->Type = Event::type_release; // transform event type              itNoteOffEvent->Type = Event::type_release; // transform event type
513          }          }
514    
# Line 739  namespace LinuxSampler { namespace gig { Line 517  namespace LinuxSampler { namespace gig {
517    
518          // spawn release triggered voice(s) if needed          // spawn release triggered voice(s) if needed
519          if (pKey->ReleaseTrigger) {          if (pKey->ReleaseTrigger) {
520              LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples              LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
521              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
522          }          }
523      }      }
# Line 748  namespace LinuxSampler { namespace gig { Line 526  namespace LinuxSampler { namespace gig {
526       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the pitch
527       *  event list.       *  event list.
528       *       *
529         *  @param pEngineChannel - engine channel on which this event occured on
530       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
531       */       */
532      void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) {      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {
533          this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
534          itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]);          itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]);
535      }      }
536    
# Line 760  namespace LinuxSampler { namespace gig { Line 539  namespace LinuxSampler { namespace gig {
539       *  called by the ProcessNoteOn() method and by the voices itself       *  called by the ProcessNoteOn() method and by the voices itself
540       *  (e.g. to spawn further voices on the same key for layered sounds).       *  (e.g. to spawn further voices on the same key for layered sounds).
541       *       *
542         *  @param pEngineChannel      - engine channel on which this event occured on
543       *  @param itNoteOnEvent       - key, velocity and time stamp of the event       *  @param itNoteOnEvent       - key, velocity and time stamp of the event
544       *  @param iLayer              - layer index for the new voice (optional - only       *  @param iLayer              - layer index for the new voice (optional - only
545       *                               in case of layered sounds of course)       *                               in case of layered sounds of course)
# Line 772  namespace LinuxSampler { namespace gig { Line 552  namespace LinuxSampler { namespace gig {
552       *           if the voice wasn't triggered (for example when no region is       *           if the voice wasn't triggered (for example when no region is
553       *           defined for the given key).       *           defined for the given key).
554       */       */
555      Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {
556          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
557    
558          // allocate a new voice for the key          // allocate a new voice for the key
559          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
560          if (itNewVoice) {          if (itNewVoice) {
561              // launch the new voice              // launch the new voice
562              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {
563                  dmsg(4,("Voice not triggered\n"));                  dmsg(4,("Voice not triggered\n"));
564                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
565              }              }
566              else { // on success              else { // on success
567                  uint** ppKeyGroup = NULL;                  uint** ppKeyGroup = NULL;
568                  if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group                  if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group
569                      ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup];                      ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
570                      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
571                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];                          midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
572                          // kill all voices on the (other) key                          // kill all voices on the (other) key
573                          RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();                          RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
574                          RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();                          RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
# Line 799  namespace LinuxSampler { namespace gig { Line 579  namespace LinuxSampler { namespace gig {
579                  }                  }
580                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
581                      pKey->Active = true;                      pKey->Active = true;
582                      pKey->itSelf = pActiveKeys->allocAppend();                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
583                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
584                  }                  }
585                  if (itNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
# Line 811  namespace LinuxSampler { namespace gig { Line 591  namespace LinuxSampler { namespace gig {
591          }          }
592          else if (VoiceStealing) {          else if (VoiceStealing) {
593              // first, get total amount of required voices (dependant on amount of layers)              // first, get total amount of required voices (dependant on amount of layers)
594              ::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);              ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
595              if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed              if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed
596              int voicesRequired = pRegion->Layers;              int voicesRequired = pRegion->Layers;
597    
598              // now steal the (remaining) amount of voices              // now steal the (remaining) amount of voices
599              for (int i = iLayer; i < voicesRequired; i++)              for (int i = iLayer; i < voicesRequired; i++)
600                  StealVoice(itNoteOnEvent);                  StealVoice(pEngineChannel, itNoteOnEvent);
601    
602              // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died              // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
603              RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();              RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
# Line 838  namespace LinuxSampler { namespace gig { Line 618  namespace LinuxSampler { namespace gig {
618       *  voice stealing and postpone the note-on event until the selected       *  voice stealing and postpone the note-on event until the selected
619       *  voice actually died.       *  voice actually died.
620       *       *
621         *  @param pEngineChannel - engine channel on which this event occured on
622       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
623       */       */
624      void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
625          if (!pEventPool->poolIsEmpty()) {          if (!pEventPool->poolIsEmpty()) {
626    
627              RTList<uint>::Iterator  iuiOldestKey;              RTList<uint>::Iterator  iuiOldestKey;
# Line 854  namespace LinuxSampler { namespace gig { Line 635  namespace LinuxSampler { namespace gig {
635                  // key, or no voice left to kill there, then procceed with                  // key, or no voice left to kill there, then procceed with
636                  // 'oldestkey' algorithm                  // 'oldestkey' algorithm
637                  case voice_steal_algo_keymask: {                  case voice_steal_algo_keymask: {
638                      midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
639                      if (itLastStolenVoice) {                      if (itLastStolenVoice) {
640                          itOldestVoice = itLastStolenVoice;                          itOldestVoice = itLastStolenVoice;
641                          ++itOldestVoice;                          ++itOldestVoice;
# Line 872  namespace LinuxSampler { namespace gig { Line 653  namespace LinuxSampler { namespace gig {
653                  // (caution: must stay after 'keymask' algorithm !)                  // (caution: must stay after 'keymask' algorithm !)
654                  case voice_steal_algo_oldestkey: {                  case voice_steal_algo_oldestkey: {
655                      if (itLastStolenVoice) {                      if (itLastStolenVoice) {
656                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey];                          midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiLastStolenKey];
657                          itOldestVoice = itLastStolenVoice;                          itOldestVoice = itLastStolenVoice;
658                          ++itOldestVoice;                          ++itOldestVoice;
659                          if (!itOldestVoice) {                          if (!itOldestVoice) {
660                              iuiOldestKey = iuiLastStolenKey;                              iuiOldestKey = iuiLastStolenKey;
661                              ++iuiOldestKey;                              ++iuiOldestKey;
662                              if (iuiOldestKey) {                              if (iuiOldestKey) {
663                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];                                  midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey];
664                                  itOldestVoice = pOldestKey->pActiveVoices->first();                                  itOldestVoice = pOldestKey->pActiveVoices->first();
665                              }                              }
666                              else {                              else {
# Line 890  namespace LinuxSampler { namespace gig { Line 671  namespace LinuxSampler { namespace gig {
671                          else iuiOldestKey = iuiLastStolenKey;                          else iuiOldestKey = iuiLastStolenKey;
672                      }                      }
673                      else { // no voice stolen in this audio fragment cycle yet                      else { // no voice stolen in this audio fragment cycle yet
674                          iuiOldestKey = pActiveKeys->first();                          iuiOldestKey = pEngineChannel->pActiveKeys->first();
675                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];                          midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey];
676                          itOldestVoice = pOldestKey->pActiveVoices->first();                          itOldestVoice = pOldestKey->pActiveVoices->first();
677                      }                      }
678                      break;                      break;
# Line 923  namespace LinuxSampler { namespace gig { Line 704  namespace LinuxSampler { namespace gig {
704       *  it finished to playback its sample, finished its release stage or       *  it finished to playback its sample, finished its release stage or
705       *  just was killed.       *  just was killed.
706       *       *
707         *  @param pEngineChannel - engine channel on which this event occured on
708       *  @param itVoice - points to the voice to be freed       *  @param itVoice - points to the voice to be freed
709       */       */
710      void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) {      void Engine::FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice) {
711          if (itVoice) {          if (itVoice) {
712              midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];              midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itVoice->MIDIKey];
713    
714              uint keygroup = itVoice->KeyGroup;              uint keygroup = itVoice->KeyGroup;
715    
# Line 936  namespace LinuxSampler { namespace gig { Line 718  namespace LinuxSampler { namespace gig {
718    
719              // if no other voices left and member of a key group, remove from key group              // if no other voices left and member of a key group, remove from key group
720              if (pKey->pActiveVoices->isEmpty() && keygroup) {              if (pKey->pActiveVoices->isEmpty() && keygroup) {
721                  uint** ppKeyGroup = &ActiveKeyGroups[keygroup];                  uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[keygroup];
722                  if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group                  if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
723              }              }
724          }          }
# Line 947  namespace LinuxSampler { namespace gig { Line 729  namespace LinuxSampler { namespace gig {
729       *  Called when there's no more voice left on a key, this call will       *  Called when there's no more voice left on a key, this call will
730       *  update the key info respectively.       *  update the key info respectively.
731       *       *
732         *  @param pEngineChannel - engine channel on which this event occured on
733       *  @param pKey - key which is now inactive       *  @param pKey - key which is now inactive
734       */       */
735      void Engine::FreeKey(midi_key_info_t* pKey) {      void Engine::FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey) {
736          if (pKey->pActiveVoices->isEmpty()) {          if (pKey->pActiveVoices->isEmpty()) {
737              pKey->Active = false;              pKey->Active = false;
738              pActiveKeys->free(pKey->itSelf); // remove key from list of active keys              pEngineChannel->pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
739              pKey->itSelf = RTList<uint>::Iterator();              pKey->itSelf = RTList<uint>::Iterator();
740              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
741              pKey->pEvents->clear();              pKey->pEvents->clear();
# Line 965  namespace LinuxSampler { namespace gig { Line 748  namespace LinuxSampler { namespace gig {
748       *  Reacts on supported control change commands (e.g. pitch bend wheel,       *  Reacts on supported control change commands (e.g. pitch bend wheel,
749       *  modulation wheel, aftertouch).       *  modulation wheel, aftertouch).
750       *       *
751         *  @param pEngineChannel - engine channel on which this event occured on
752       *  @param itControlChangeEvent - controller, value and time stamp of the event       *  @param itControlChangeEvent - controller, value and time stamp of the event
753       */       */
754      void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) {      void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) {
755          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));
756    
757          switch (itControlChangeEvent->Param.CC.Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
758              case 64: {              case 64: {
759                  if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {
760                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
761                      SustainPedal = true;                      pEngineChannel->SustainPedal = true;
762    
763                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
764                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
765                      if (iuiKey) {                      if (iuiKey) {
766                          itControlChangeEvent->Type = Event::type_cancel_release; // transform event type                          itControlChangeEvent->Type = Event::type_cancel_release; // transform event type
767                          while (iuiKey) {                          while (iuiKey) {
768                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];                              midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
769                              ++iuiKey;                              ++iuiKey;
770                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
771                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
# Line 991  namespace LinuxSampler { namespace gig { Line 775  namespace LinuxSampler { namespace gig {
775                          }                          }
776                      }                      }
777                  }                  }
778                  if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {
779                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
780                      SustainPedal = false;                      pEngineChannel->SustainPedal = false;
781    
782                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
783                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
784                      if (iuiKey) {                      if (iuiKey) {
785                          itControlChangeEvent->Type = Event::type_release; // transform event type                          itControlChangeEvent->Type = Event::type_release; // transform event type
786                          while (iuiKey) {                          while (iuiKey) {
787                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];                              midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
788                              ++iuiKey;                              ++iuiKey;
789                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
790                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
# Line 1015  namespace LinuxSampler { namespace gig { Line 799  namespace LinuxSampler { namespace gig {
799          }          }
800    
801          // update controller value in the engine's controller table          // update controller value in the engine's controller table
802          ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
803    
804          // move event from the unsorted event list to the control change event list          // move event from the unsorted event list to the control change event list
805          itControlChangeEvent.moveToEndOf(pCCEvents);          itControlChangeEvent.moveToEndOf(pCCEvents);
# Line 1117  namespace LinuxSampler { namespace gig { Line 901  namespace LinuxSampler { namespace gig {
901             m[i+2] = val;             m[i+2] = val;
902             m[i+3] = val;             m[i+3] = val;
903          }          }
904      }      }    
   
     float Engine::Volume() {  
         return GlobalVolume;  
     }  
   
     void Engine::Volume(float f) {  
         GlobalVolume = f;  
     }  
   
     uint Engine::Channels() {  
         return 2;  
     }  
   
     void Engine::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {  
         AudioChannel* pChannel = 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 Engine::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));  
         }  
     }  
905    
906      uint Engine::VoiceCount() {      uint Engine::VoiceCount() {
907          return ActiveVoiceCount;          return ActiveVoiceCount;
# Line 1191  namespace LinuxSampler { namespace gig { Line 935  namespace LinuxSampler { namespace gig {
935          return "GigEngine";          return "GigEngine";
936      }      }
937    
     String Engine::InstrumentFileName() {  
         return InstrumentFile;  
     }  
   
     String Engine::InstrumentName() {  
         return InstrumentIdxName;  
     }  
   
     int Engine::InstrumentIndex() {  
         return InstrumentIdx;  
     }  
   
     int Engine::InstrumentStatus() {  
         return InstrumentStat;  
     }  
   
938      String Engine::Description() {      String Engine::Description() {
939          return "Gigasampler Engine";          return "Gigasampler Engine";
940      }      }
941    
942      String Engine::Version() {      String Engine::Version() {
943          String s = "$Revision: 1.25 $";          String s = "$Revision: 1.26 $";
944          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
945      }      }
946    

Legend:
Removed from v.410  
changed lines
  Added in v.411

  ViewVC Help
Powered by ViewVC