/[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 250 by schoenebeck, Mon Sep 20 00:31:13 2004 UTC revision 285 by schoenebeck, Thu Oct 14 21:31:26 2004 UTC
# Line 23  Line 23 
23  #include <sstream>  #include <sstream>
24  #include "DiskThread.h"  #include "DiskThread.h"
25  #include "Voice.h"  #include "Voice.h"
26    #include "EGADSR.h"
27    
28  #include "Engine.h"  #include "Engine.h"
29    
# Line 39  namespace LinuxSampler { namespace gig { Line 40  namespace LinuxSampler { namespace gig {
40          pEventGenerator    = NULL;          pEventGenerator    = NULL;
41          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);
42          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
43          pEventPool         = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT);
44          pVoicePool         = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES);          pVoicePool         = new Pool<Voice>(MAX_AUDIO_VOICES);
45          pActiveKeys        = new RTELMemoryPool<uint>(128);          pActiveKeys        = new Pool<uint>(128);
46          pVoiceStealingQueue = new RTEList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
47          pEvents            = new RTEList<Event>(pEventPool);          pEvents            = new RTList<Event>(pEventPool);
48          pCCEvents          = new RTEList<Event>(pEventPool);          pCCEvents          = new RTList<Event>(pEventPool);
49          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
50              pSynthesisEvents[i] = new RTEList<Event>(pEventPool);              pSynthesisEvents[i] = new RTList<Event>(pEventPool);
51          }          }
52          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
53              pMIDIKeyInfo[i].pActiveVoices  = new RTEList<Voice>(pVoicePool);              pMIDIKeyInfo[i].pActiveVoices  = new RTList<Voice>(pVoicePool);
54              pMIDIKeyInfo[i].KeyPressed     = false;              pMIDIKeyInfo[i].KeyPressed     = false;
55              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
56              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
57              pMIDIKeyInfo[i].pSelf          = NULL;              pMIDIKeyInfo[i].pEvents        = new RTList<Event>(pEventPool);
             pMIDIKeyInfo[i].pEvents        = new RTEList<Event>(pEventPool);  
58          }          }
59          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
60              pVoice->SetEngine(this);              iterVoice->SetEngine(this);
61          }          }
62          pVoicePool->clear();          pVoicePool->clear();
63    
# Line 160  namespace LinuxSampler { namespace gig { Line 160  namespace LinuxSampler { namespace gig {
160          GlobalVolume        = 1.0;          GlobalVolume        = 1.0;
161    
162          // reset voice stealing parameters          // reset voice stealing parameters
163          pLastStolenVoice = NULL;          itLastStolenVoice = RTList<Voice>::Iterator();
164          puiLastStolenKey = NULL;          iuiLastStolenKey  = RTList<uint>::Iterator();
165          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
166    
167          // reset to normal chromatic scale (means equal temper)          // reset to normal chromatic scale (means equal temper)
# Line 177  namespace LinuxSampler { namespace gig { Line 177  namespace LinuxSampler { namespace gig {
177              pMIDIKeyInfo[i].KeyPressed     = false;              pMIDIKeyInfo[i].KeyPressed     = false;
178              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
179              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
180              pMIDIKeyInfo[i].pSelf          = NULL;              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
181          }          }
182    
183          // reset all key groups          // reset all key groups
# Line 185  namespace LinuxSampler { namespace gig { Line 185  namespace LinuxSampler { namespace gig {
185          for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;          for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;
186    
187          // reset all voices          // reset all voices
188          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
189              pVoice->Reset();              iterVoice->Reset();
190          }          }
191          pVoicePool->clear();          pVoicePool->clear();
192    
# Line 314  namespace LinuxSampler { namespace gig { Line 314  namespace LinuxSampler { namespace gig {
314          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();
315          this->SampleRate              = pAudioOutputDevice->SampleRate();          this->SampleRate              = pAudioOutputDevice->SampleRate();
316    
317            // FIXME: audio drivers with varying fragment sizes might be a problem here
318            MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1;
319            if (MaxFadeOutPos < 0)
320                throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!");
321    
322          // (re)create disk thread          // (re)create disk thread
323          if (this->pDiskThread) {          if (this->pDiskThread) {
324              this->pDiskThread->StopThread();              this->pDiskThread->StopThread();
# Line 325  namespace LinuxSampler { namespace gig { Line 330  namespace LinuxSampler { namespace gig {
330              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
331          }          }
332    
333          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
334              pVoice->pDiskThread = this->pDiskThread;              iterVoice->pDiskThread = this->pDiskThread;
335              dmsg(3,("d"));              dmsg(3,("d"));
336          }          }
337          pVoicePool->clear();          pVoicePool->clear();
# Line 351  namespace LinuxSampler { namespace gig { Line 356  namespace LinuxSampler { namespace gig {
356          pDiskThread->StartThread();          pDiskThread->StartThread();
357          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
358    
359          for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
360              if (!pVoice->pDiskThread) {              if (!iterVoice->pDiskThread) {
361                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));
362                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
363              }              }
# Line 399  namespace LinuxSampler { namespace gig { Line 404  namespace LinuxSampler { namespace gig {
404          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
405              pSynthesisEvents[i]->clear();              pSynthesisEvents[i]->clear();
406          }          }
407          for (uint* puiKey = pActiveKeys->first(); puiKey; puiKey = pActiveKeys->next()) {          {
408              midi_key_info_t* pKey = &pMIDIKeyInfo[*puiKey];              RTList<uint>::Iterator iuiKey = pActiveKeys->first();
409              pKey->pEvents->clear(); // free all events on the key              RTList<uint>::Iterator end    = pActiveKeys->end();
410                for(; iuiKey != end; ++iuiKey) {
411                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
412                }
413          }          }
414    
415          // read and copy events from input queue          // read and copy events from input queue
416          Event event = pEventGenerator->CreateEvent();          Event event = pEventGenerator->CreateEvent();
417          while (true) {          while (true) {
418              if (!pEventQueue->pop(&event)) break;              if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break;
419              pEvents->alloc_assign(event);              *pEvents->allocAppend() = event;
420          }          }
421    
422    
# Line 417  namespace LinuxSampler { namespace gig { Line 425  namespace LinuxSampler { namespace gig {
425    
426    
427          // process events          // process events
428          Event* pNextEvent = pEvents->first();          {
429          while (pNextEvent) {              RTList<Event>::Iterator itEvent = pEvents->first();
430              Event* pEvent = pNextEvent;              RTList<Event>::Iterator end     = pEvents->end();
431              pEvents->set_current(pEvent);              for (; itEvent != end; ++itEvent) {
432              pNextEvent = pEvents->next();                  switch (itEvent->Type) {
433              switch (pEvent->Type) {                      case Event::type_note_on:
434                  case Event::type_note_on:                          dmsg(5,("Engine: Note on received\n"));
435                      dmsg(5,("Engine: Note on received\n"));                          ProcessNoteOn(itEvent);
436                      ProcessNoteOn(pEvent);                          break;
437                      break;                      case Event::type_note_off:
438                  case Event::type_note_off:                          dmsg(5,("Engine: Note off received\n"));
439                      dmsg(5,("Engine: Note off received\n"));                          ProcessNoteOff(itEvent);
440                      ProcessNoteOff(pEvent);                          break;
441                      break;                      case Event::type_control_change:
442                  case Event::type_control_change:                          dmsg(5,("Engine: MIDI CC received\n"));
443                      dmsg(5,("Engine: MIDI CC received\n"));                          ProcessControlChange(itEvent);
444                      ProcessControlChange(pEvent);                          break;
445                      break;                      case Event::type_pitchbend:
446                  case Event::type_pitchbend:                          dmsg(5,("Engine: Pitchbend received\n"));
447                      dmsg(5,("Engine: Pitchbend received\n"));                          ProcessPitchbend(itEvent);
448                      ProcessPitchbend(pEvent);                          break;
449                      break;                      case Event::type_sysex:
450                  case Event::type_sysex:                          dmsg(5,("Engine: Sysex received\n"));
451                      dmsg(5,("Engine: Sysex received\n"));                          ProcessSysex(itEvent);
452                      ProcessSysex(pEvent);                          break;
453                      break;                  }
454              }              }
455          }          }
456    
457    
         // render audio from all active voices  
458          int active_voices = 0;          int active_voices = 0;
459          uint* piKey = pActiveKeys->first();  
460          while (piKey) { // iterate through all active keys          // render audio from all active voices
461              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];          {
462              pActiveKeys->set_current(piKey);              RTList<uint>::Iterator iuiKey = pActiveKeys->first();
463              piKey = pActiveKeys->next();              RTList<uint>::Iterator end    = pActiveKeys->end();
464                while (iuiKey != end) { // iterate through all active keys
465              Voice* pVoiceNext = pKey->pActiveVoices->first();                  midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
466              while (pVoiceNext) { // iterate through all voices on this key                  ++iuiKey;
467                  // already get next voice on key  
468                  Voice* pVoice = pVoiceNext;                  RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
469                  pKey->pActiveVoices->set_current(pVoice);                  RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
470                  pVoiceNext = pKey->pActiveVoices->next();                  for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
471                        // now render current voice
472                  // now render current voice                      itVoice->Render(Samples);
473                  pVoice->Render(Samples);                      if (itVoice->IsActive()) active_voices++; // still active
474                  if (pVoice->IsActive()) active_voices++; // still active                      else { // voice reached end, is now inactive
475                  else { // voice reached end, is now inactive                          FreeVoice(itVoice); // remove voice from the list of active voices
476                      KillVoiceImmediately(pVoice); // remove voice from the list of active voices                      }
477                  }                  }
478              }              }
479          }          }
480    
481    
482          // now render all postponed voices from voice stealing          // now render all postponed voices from voice stealing
483          Event* pVoiceStealEvent = pVoiceStealingQueue->first();          {
484          while (pVoiceStealEvent) {              RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first();
485              Voice* pNewVoice = LaunchVoice(pVoiceStealEvent, pVoiceStealEvent->Param.Note.Layer, pVoiceStealEvent->Param.Note.ReleaseTrigger, false);              RTList<Event>::Iterator end               = pVoiceStealingQueue->end();
486              if (pNewVoice) {              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
487                  pNewVoice->Render(Samples);                  Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);
488                  if (pNewVoice->IsActive()) active_voices++; // still active                  if (itNewVoice) {
489                  else { // voice reached end, is now inactive                      for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) {
490                      KillVoiceImmediately(pNewVoice); // remove voice from the list of active voices                          itNewVoice->Render(Samples);
491                            if (itNewVoice->IsActive()) active_voices++; // still active
492                            else { // voice reached end, is now inactive
493                                FreeVoice(itNewVoice); // remove voice from the list of active voices
494                            }
495                        }
496                  }                  }
497                    else dmsg(1,("Ouch, voice stealing didn't work out!\n"));
498              }              }
             else dmsg(1,("Ouch, voice stealing didn't work out!\n"));  
             pVoiceStealEvent = pVoiceStealingQueue->next();  
499          }          }
500          // reset voice stealing for the new fragment          // reset voice stealing for the new fragment
501          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
502          pLastStolenVoice = NULL;          itLastStolenVoice = RTList<Voice>::Iterator();
503          puiLastStolenKey = NULL;          iuiLastStolenKey  = RTList<uint>::Iterator();
504    
505    
506          // write that to the disk thread class so that it can print it          // write that to the disk thread class so that it can print it
# Line 597  namespace LinuxSampler { namespace gig { Line 608  namespace LinuxSampler { namespace gig {
608      /**      /**
609       *  Assigns and triggers a new voice for the respective MIDI key.       *  Assigns and triggers a new voice for the respective MIDI key.
610       *       *
611       *  @param pNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
612       */       */
613      void Engine::ProcessNoteOn(Event* pNoteOnEvent) {      void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) {
614          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
615    
616          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
617    
618          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
619          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !SustainPedal) {
620              Event* pCancelReleaseEvent = pKey->pEvents->alloc();              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
621              if (pCancelReleaseEvent) {              if (itCancelReleaseEvent) {
622                  *pCancelReleaseEvent = *pNoteOnEvent;                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event
623                  pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type
624              }              }
625              else dmsg(1,("Event pool emtpy!\n"));              else dmsg(1,("Event pool emtpy!\n"));
626          }          }
627    
628          // allocate and trigger a new voice for the key          // move note on event to the key's own event list
629          LaunchVoice(pNoteOnEvent);          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
630    
631          // finally move note on event to the key's own event list          // allocate and trigger a new voice for the key
632          pEvents->move(pNoteOnEvent, pKey->pEvents);          LaunchVoice(itNoteOnEventOnKeyList);
633      }      }
634    
635      /**      /**
# Line 627  namespace LinuxSampler { namespace gig { Line 638  namespace LinuxSampler { namespace gig {
638       *  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.
639       *  due to completion of sample playback).       *  due to completion of sample playback).
640       *       *
641       *  @param pNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
642       */       */
643      void Engine::ProcessNoteOff(Event* pNoteOffEvent) {      void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) {
644          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
645    
646          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
647    
648          // release voices on this key if needed          // release voices on this key if needed
649          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !SustainPedal) {
650              pNoteOffEvent->Type = Event::type_release; // transform event type              itNoteOffEvent->Type = Event::type_release; // transform event type
651          }          }
652    
653            // move event to the key's own event list
654            RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
655    
656          // spawn release triggered voice(s) if needed          // spawn release triggered voice(s) if needed
657          if (pKey->ReleaseTrigger) {          if (pKey->ReleaseTrigger) {
658              LaunchVoice(pNoteOffEvent, 0, true);              LaunchVoice(itNoteOffEventOnKeyList, 0, true);
659              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
660          }          }
   
         // move event to the key's own event list  
         pEvents->move(pNoteOffEvent, pKey->pEvents);  
661      }      }
662    
663      /**      /**
664       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the pitch
665       *  event list.       *  event list.
666       *       *
667       *  @param pPitchbendEvent - absolute pitch value and time stamp of the event       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
668       */       */
669      void Engine::ProcessPitchbend(Event* pPitchbendEvent) {      void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) {
670          this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value          this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
671          pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]);          itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]);
672      }      }
673    
674      /**      /**
# Line 665  namespace LinuxSampler { namespace gig { Line 676  namespace LinuxSampler { namespace gig {
676       *  called by the ProcessNoteOn() method and by the voices itself       *  called by the ProcessNoteOn() method and by the voices itself
677       *  (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).
678       *       *
679       *  @param pNoteOnEvent        - key, velocity and time stamp of the event       *  @param itNoteOnEvent       - key, velocity and time stamp of the event
680       *  @param iLayer              - layer index for the new voice (optional - only       *  @param iLayer              - layer index for the new voice (optional - only
681       *                               in case of layered sounds of course)       *                               in case of layered sounds of course)
682       *  @param ReleaseTriggerVoice - if new voice is a release triggered voice       *  @param ReleaseTriggerVoice - if new voice is a release triggered voice
# Line 676  namespace LinuxSampler { namespace gig { Line 687  namespace LinuxSampler { namespace gig {
687       *  @returns pointer to new voice or NULL if there was no free voice or       *  @returns pointer to new voice or NULL if there was no free voice or
688       *           if an error occured while trying to trigger the new voice       *           if an error occured while trying to trigger the new voice
689       */       */
690      Voice* Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {      Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {
691          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
692    
693          // allocate a new voice for the key          // allocate a new voice for the key
694          Voice* pNewVoice = pKey->pActiveVoices->alloc();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
695          if (pNewVoice) {          if (itNewVoice) {
696              // launch the new voice              // launch the new voice
697              if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {
698                  dmsg(1,("Triggering new voice failed!\n"));                  dmsg(1,("Triggering new voice failed!\n"));
699                  pKey->pActiveVoices->free(pNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
700              }              }
701              else { // on success              else { // on success
702                  uint** ppKeyGroup = NULL;                  uint** ppKeyGroup = NULL;
703                  if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group                  if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group
704                      ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup];                      ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup];
705                      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
706                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];
707                          // kill all voices on the (other) key                          // kill all voices on the (other) key
708                          Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first();                          RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
709                          while (pVoiceToBeKilled) {                          RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
710                              Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next();                          for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
711                              if (pVoiceToBeKilled->Type != Voice::type_release_trigger) pVoiceToBeKilled->Kill(pNoteOnEvent);                              if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent);
                             pOtherKey->pActiveVoices->set_current(pVoiceToBeKilled);  
                             pVoiceToBeKilled = pVoiceToBeKilledNext;  
712                          }                          }
713                      }                      }
714                  }                  }
715                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
716                      pKey->Active = true;                      pKey->Active = true;
717                      pKey->pSelf  = pActiveKeys->alloc();                      pKey->itSelf = pActiveKeys->allocAppend();
718                      *pKey->pSelf = pNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
719                  }                  }
720                  if (pNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
721                      *ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group                      *ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group
722                  }                  }
723                  if (pNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)                  if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)
724                  return pNewVoice; // success                  return itNewVoice; // success
725              }              }
726          }          }
727          else if (VoiceStealing) StealVoice(pNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one          else if (VoiceStealing) {
728                // first, get total amount of required voices (dependant on amount of layers)
729                ::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
730                if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed
731                int voicesRequired = pRegion->Layers;
732    
733                // now steal the (remaining) amount of voices
734                for (int i = iLayer; i < voicesRequired; i++)
735                    StealVoice(itNoteOnEvent);
736    
737          return NULL; // no free voice or error              // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
738                RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
739                if (itStealEvent) {
740                    *itStealEvent = *itNoteOnEvent; // copy event
741                    itStealEvent->Param.Note.Layer = iLayer;
742                    itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
743                }
744                else dmsg(1,("Voice stealing queue full!\n"));
745            }
746    
747            return Pool<Voice>::Iterator(); // no free voice or error
748      }      }
749    
750      /**      /**
# Line 726  namespace LinuxSampler { namespace gig { Line 753  namespace LinuxSampler { namespace gig {
753       *  voice stealing and postpone the note-on event until the selected       *  voice stealing and postpone the note-on event until the selected
754       *  voice actually died.       *  voice actually died.
755       *       *
756       *  @param pNoteOnEvent        - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
      *  @param iLayer              - layer index for the new voice  
      *  @param ReleaseTriggerVoice - if new voice is a release triggered voice  
757       */       */
758      void Engine::StealVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) {      void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) {
759          if (!pEventPool->pool_is_empty()) {          if (!pEventPool->poolIsEmpty()) {
760    
761              uint*  puiOldestKey;              RTList<uint>::Iterator  iuiOldestKey;
762              Voice* pOldestVoice;              RTList<Voice>::Iterator itOldestVoice;
763    
764              // Select one voice for voice stealing              // Select one voice for voice stealing
765              switch (VOICE_STEAL_ALGORITHM) {              switch (VOICE_STEAL_ALGORITHM) {
# Line 744  namespace LinuxSampler { namespace gig { Line 769  namespace LinuxSampler { namespace gig {
769                  // key, or no voice left to kill there, then procceed with                  // key, or no voice left to kill there, then procceed with
770                  // 'oldestkey' algorithm                  // 'oldestkey' algorithm
771                  case voice_steal_algo_keymask: {                  case voice_steal_algo_keymask: {
772                      midi_key_info_t* pOldestKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
773                      if (pLastStolenVoice) {                      if (itLastStolenVoice) {
774                          pOldestKey->pActiveVoices->set_current(pLastStolenVoice);                          itOldestVoice = itLastStolenVoice;
775                          pOldestVoice = pOldestKey->pActiveVoices->next();                          ++itOldestVoice;
776                      }                      }
777                      else { // no voice stolen in this audio fragment cycle yet                      else { // no voice stolen in this audio fragment cycle yet
778                          pOldestVoice = pOldestKey->pActiveVoices->first();                          itOldestVoice = pOldestKey->pActiveVoices->first();
779                      }                      }
780                      if (pOldestVoice) {                      if (itOldestVoice) {
781                          puiOldestKey = pOldestKey->pSelf;                          iuiOldestKey = pOldestKey->itSelf;
782                          break; // selection succeeded                          break; // selection succeeded
783                      }                      }
784                  } // no break - intentional !                  } // no break - intentional !
# Line 761  namespace LinuxSampler { namespace gig { Line 786  namespace LinuxSampler { namespace gig {
786                  // try to pick the oldest voice on the oldest active key                  // try to pick the oldest voice on the oldest active key
787                  // (caution: must stay after 'keymask' algorithm !)                  // (caution: must stay after 'keymask' algorithm !)
788                  case voice_steal_algo_oldestkey: {                  case voice_steal_algo_oldestkey: {
789                      if (pLastStolenVoice) {                      if (itLastStolenVoice) {
790                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiLastStolenKey];                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey];
791                          pOldestKey->pActiveVoices->set_current(pLastStolenVoice);                          itOldestVoice = itLastStolenVoice;
792                          pOldestVoice = pOldestKey->pActiveVoices->next();                          ++itOldestVoice;
793                          if (!pOldestVoice) {                          if (!itOldestVoice) {
794                              pActiveKeys->set_current(puiLastStolenKey);                              iuiOldestKey = iuiLastStolenKey;
795                              puiOldestKey = pActiveKeys->next();                              ++iuiOldestKey;
796                              if (puiOldestKey) {                              if (iuiOldestKey) {
797                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];
798                                  pOldestVoice = pOldestKey->pActiveVoices->first();                                  itOldestVoice = pOldestKey->pActiveVoices->first();
799                              }                              }
800                              else { // too less voices, even for voice stealing                              else {
801                                  dmsg(1,("Voice overflow! - You might recompile with higher MAX_AUDIO_VOICES!\n"));                                  dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n"));
802                                  return;                                  return;
803                              }                              }
804                          }                          }
805                          else puiOldestKey = puiLastStolenKey;                          else iuiOldestKey = iuiLastStolenKey;
806                      }                      }
807                      else { // no voice stolen in this audio fragment cycle yet                      else { // no voice stolen in this audio fragment cycle yet
808                          puiOldestKey = pActiveKeys->first();                          iuiOldestKey = pActiveKeys->first();
809                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];
810                          pOldestVoice = pOldestKey->pActiveVoices->first();                          itOldestVoice = pOldestKey->pActiveVoices->first();
811                      }                      }
812                      break;                      break;
813                  }                  }
# Line 796  namespace LinuxSampler { namespace gig { Line 821  namespace LinuxSampler { namespace gig {
821              }              }
822    
823              // now kill the selected voice              // now kill the selected voice
824              pOldestVoice->Kill(pNoteOnEvent);              itOldestVoice->Kill(itNoteOnEvent);
825              // remember which voice on which key we stole, so we can simply proceed for the next voice stealing              // remember which voice on which key we stole, so we can simply proceed for the next voice stealing
826              this->pLastStolenVoice = pOldestVoice;              this->itLastStolenVoice = itOldestVoice;
827              this->puiLastStolenKey = puiOldestKey;              this->iuiLastStolenKey = iuiOldestKey;
             // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died  
             Event* pStealEvent = pVoiceStealingQueue->alloc();  
             *pStealEvent = *pNoteOnEvent;  
             pStealEvent->Param.Note.Layer = iLayer;  
             pStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;  
828          }          }
829          else dmsg(1,("Event pool emtpy!\n"));          else dmsg(1,("Event pool emtpy!\n"));
830      }      }
831    
832      /**      /**
833       *  Immediately kills the voice given with pVoice (no matter if sustain is       *  Removes the given voice from the MIDI key's list of active voices.
834       *  pressed or not) and removes it from the MIDI key's list of active voice.       *  This method will be called when a voice went inactive, e.g. because
835       *  This method will e.g. be called if a voice went inactive by itself.       *  it finished to playback its sample, finished its release stage or
836         *  just was killed.
837       *       *
838       *  @param pVoice - points to the voice to be killed       *  @param itVoice - points to the voice to be freed
839       */       */
840      void Engine::KillVoiceImmediately(Voice* pVoice) {      void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) {
841          if (pVoice) {          if (itVoice) {
842              if (pVoice->IsActive()) pVoice->KillImmediately();              midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];
843    
844              midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey];              uint keygroup = itVoice->KeyGroup;
845    
846              // free the voice object              // free the voice object
847              pVoicePool->free(pVoice);              pVoicePool->free(itVoice);
848    
849              // check if there are no voices left on the MIDI key and update the key info if so              // check if there are no voices left on the MIDI key and update the key info if so
850              if (pKey->pActiveVoices->is_empty()) {              if (pKey->pActiveVoices->isEmpty()) {
851                  if (pVoice->KeyGroup) { // if voice / key belongs to a key group                  if (keygroup) { // if voice / key belongs to a key group
852                      uint** ppKeyGroup = &ActiveKeyGroups[pVoice->KeyGroup];                      uint** ppKeyGroup = &ActiveKeyGroups[keygroup];
853                      if (*ppKeyGroup == pKey->pSelf) *ppKeyGroup = NULL; // remove key from key group                      if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
854                  }                  }
855                  pKey->Active = false;                  pKey->Active = false;
856                  pActiveKeys->free(pKey->pSelf); // remove key from list of active keys                  pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
857                  pKey->pSelf = NULL;                  pKey->itSelf = RTList<uint>::Iterator();
858                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
859                  pKey->pEvents->clear();                  pKey->pEvents->clear();
860                  dmsg(3,("Key has no more voices now\n"));                  dmsg(3,("Key has no more voices now\n"));
861              }              }
862          }          }
863          else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush;          else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;
864      }      }
865    
866      /**      /**
867       *  Reacts on supported control change commands (e.g. pitch bend wheel,       *  Reacts on supported control change commands (e.g. pitch bend wheel,
868       *  modulation wheel, aftertouch).       *  modulation wheel, aftertouch).
869       *       *
870       *  @param pControlChangeEvent - controller, value and time stamp of the event       *  @param itControlChangeEvent - controller, value and time stamp of the event
871       */       */
872      void Engine::ProcessControlChange(Event* pControlChangeEvent) {      void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) {
873          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Param.CC.Controller, pControlChangeEvent->Param.CC.Value));          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));
874    
875          switch (pControlChangeEvent->Param.CC.Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
876              case 64: {              case 64: {
877                  if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {
878                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
879                      SustainPedal = true;                      SustainPedal = true;
880    
881                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
882                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();
883                      if (piKey) {                      if (iuiKey) {
884                          pControlChangeEvent->Type = Event::type_cancel_release; // transform event type                          itControlChangeEvent->Type = Event::type_cancel_release; // transform event type
885                          while (piKey) {                          while (iuiKey) {
886                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
887                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
888                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
889                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
890                                  if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list                                  if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
891                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
892                              }                              }
893                          }                          }
894                      }                      }
895                  }                  }
896                  if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {
897                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
898                      SustainPedal = false;                      SustainPedal = false;
899    
900                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
901                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();
902                      if (piKey) {                      if (iuiKey) {
903                          pControlChangeEvent->Type = Event::type_release; // transform event type                          itControlChangeEvent->Type = Event::type_release; // transform event type
904                          while (piKey) {                          while (iuiKey) {
905                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
906                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
907                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
908                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
909                                  if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list                                  if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
910                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
911                              }                              }
912                          }                          }
# Line 898  namespace LinuxSampler { namespace gig { Line 917  namespace LinuxSampler { namespace gig {
917          }          }
918    
919          // update controller value in the engine's controller table          // update controller value in the engine's controller table
920          ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value;          ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
921    
922          // 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
923          pEvents->move(pControlChangeEvent, pCCEvents);          itControlChangeEvent.moveToEndOf(pCCEvents);
924      }      }
925    
926      /**      /**
927       *  Reacts on MIDI system exclusive messages.       *  Reacts on MIDI system exclusive messages.
928       *       *
929       *  @param pSysexEvent - sysex data size and time stamp of the sysex event       *  @param itSysexEvent - sysex data size and time stamp of the sysex event
930       */       */
931      void Engine::ProcessSysex(Event* pSysexEvent) {      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {
932          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
933    
934          uint8_t exclusive_status, id;          uint8_t exclusive_status, id;
# Line 957  namespace LinuxSampler { namespace gig { Line 976  namespace LinuxSampler { namespace gig {
976          }          }
977    
978          free_sysex_data: // finally free sysex data          free_sysex_data: // finally free sysex data
979          pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size);          pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size);
980      }      }
981    
982      /**      /**
# Line 1091  namespace LinuxSampler { namespace gig { Line 1110  namespace LinuxSampler { namespace gig {
1110      }      }
1111    
1112      String Engine::Version() {      String Engine::Version() {
1113          String s = "$Revision: 1.14 $";          String s = "$Revision: 1.16 $";
1114          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
1115      }      }
1116    

Legend:
Removed from v.250  
changed lines
  Added in v.285

  ViewVC Help
Powered by ViewVC