/[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 270 by schoenebeck, Mon Sep 20 00:31:13 2004 UTC revision 271 by schoenebeck, Fri Oct 8 20:51:39 2004 UTC
# Line 39  namespace LinuxSampler { namespace gig { Line 39  namespace LinuxSampler { namespace gig {
39          pEventGenerator    = NULL;          pEventGenerator    = NULL;
40          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);
41          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
42          pEventPool         = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT);
43          pVoicePool         = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES);          pVoicePool         = new Pool<Voice>(MAX_AUDIO_VOICES);
44          pActiveKeys        = new RTELMemoryPool<uint>(128);          pActiveKeys        = new Pool<uint>(128);
45          pVoiceStealingQueue = new RTEList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
46          pEvents            = new RTEList<Event>(pEventPool);          pEvents            = new RTList<Event>(pEventPool);
47          pCCEvents          = new RTEList<Event>(pEventPool);          pCCEvents          = new RTList<Event>(pEventPool);
48          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
49              pSynthesisEvents[i] = new RTEList<Event>(pEventPool);              pSynthesisEvents[i] = new RTList<Event>(pEventPool);
50          }          }
51          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
52              pMIDIKeyInfo[i].pActiveVoices  = new RTEList<Voice>(pVoicePool);              pMIDIKeyInfo[i].pActiveVoices  = new RTList<Voice>(pVoicePool);
53              pMIDIKeyInfo[i].KeyPressed     = false;              pMIDIKeyInfo[i].KeyPressed     = false;
54              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
55              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
56              pMIDIKeyInfo[i].pSelf          = NULL;              pMIDIKeyInfo[i].pEvents        = new RTList<Event>(pEventPool);
             pMIDIKeyInfo[i].pEvents        = new RTEList<Event>(pEventPool);  
57          }          }
58          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
59              pVoice->SetEngine(this);              iterVoice->SetEngine(this);
60          }          }
61          pVoicePool->clear();          pVoicePool->clear();
62    
# Line 160  namespace LinuxSampler { namespace gig { Line 159  namespace LinuxSampler { namespace gig {
159          GlobalVolume        = 1.0;          GlobalVolume        = 1.0;
160    
161          // reset voice stealing parameters          // reset voice stealing parameters
162          pLastStolenVoice = NULL;          itLastStolenVoice = RTList<Voice>::Iterator();
163          puiLastStolenKey = NULL;          iuiLastStolenKey  = RTList<uint>::Iterator();
164          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
165    
166          // reset to normal chromatic scale (means equal temper)          // reset to normal chromatic scale (means equal temper)
# Line 177  namespace LinuxSampler { namespace gig { Line 176  namespace LinuxSampler { namespace gig {
176              pMIDIKeyInfo[i].KeyPressed     = false;              pMIDIKeyInfo[i].KeyPressed     = false;
177              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
178              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
179              pMIDIKeyInfo[i].pSelf          = NULL;              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
180          }          }
181    
182          // reset all key groups          // reset all key groups
# Line 185  namespace LinuxSampler { namespace gig { Line 184  namespace LinuxSampler { namespace gig {
184          for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;          for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;
185    
186          // reset all voices          // reset all voices
187          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
188              pVoice->Reset();              iterVoice->Reset();
189          }          }
190          pVoicePool->clear();          pVoicePool->clear();
191    
# Line 325  namespace LinuxSampler { namespace gig { Line 324  namespace LinuxSampler { namespace gig {
324              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
325          }          }
326    
327          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
328              pVoice->pDiskThread = this->pDiskThread;              iterVoice->pDiskThread = this->pDiskThread;
329              dmsg(3,("d"));              dmsg(3,("d"));
330          }          }
331          pVoicePool->clear();          pVoicePool->clear();
# Line 351  namespace LinuxSampler { namespace gig { Line 350  namespace LinuxSampler { namespace gig {
350          pDiskThread->StartThread();          pDiskThread->StartThread();
351          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
352    
353          for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
354              if (!pVoice->pDiskThread) {              if (!iterVoice->pDiskThread) {
355                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));
356                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
357              }              }
# Line 399  namespace LinuxSampler { namespace gig { Line 398  namespace LinuxSampler { namespace gig {
398          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
399              pSynthesisEvents[i]->clear();              pSynthesisEvents[i]->clear();
400          }          }
401          for (uint* puiKey = pActiveKeys->first(); puiKey; puiKey = pActiveKeys->next()) {          {
402              midi_key_info_t* pKey = &pMIDIKeyInfo[*puiKey];              RTList<uint>::Iterator iuiKey = pActiveKeys->first();
403              pKey->pEvents->clear(); // free all events on the key              RTList<uint>::Iterator end    = pActiveKeys->end();
404                for(; iuiKey != end; ++iuiKey) {
405                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
406                }
407          }          }
408    
409          // read and copy events from input queue          // read and copy events from input queue
410          Event event = pEventGenerator->CreateEvent();          Event event = pEventGenerator->CreateEvent();
411          while (true) {          while (true) {
412              if (!pEventQueue->pop(&event)) break;              if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break;
413              pEvents->alloc_assign(event);              *pEvents->allocAppend() = event;
414          }          }
415    
416    
# Line 417  namespace LinuxSampler { namespace gig { Line 419  namespace LinuxSampler { namespace gig {
419    
420    
421          // process events          // process events
422          Event* pNextEvent = pEvents->first();          {
423          while (pNextEvent) {              RTList<Event>::Iterator itEvent = pEvents->first();
424              Event* pEvent = pNextEvent;              RTList<Event>::Iterator end     = pEvents->end();
425              pEvents->set_current(pEvent);              for (; itEvent != end; ++itEvent) {
426              pNextEvent = pEvents->next();                  switch (itEvent->Type) {
427              switch (pEvent->Type) {                      case Event::type_note_on:
428                  case Event::type_note_on:                          dmsg(5,("Engine: Note on received\n"));
429                      dmsg(5,("Engine: Note on received\n"));                          ProcessNoteOn(itEvent);
430                      ProcessNoteOn(pEvent);                          break;
431                      break;                      case Event::type_note_off:
432                  case Event::type_note_off:                          dmsg(5,("Engine: Note off received\n"));
433                      dmsg(5,("Engine: Note off received\n"));                          ProcessNoteOff(itEvent);
434                      ProcessNoteOff(pEvent);                          break;
435                      break;                      case Event::type_control_change:
436                  case Event::type_control_change:                          dmsg(5,("Engine: MIDI CC received\n"));
437                      dmsg(5,("Engine: MIDI CC received\n"));                          ProcessControlChange(itEvent);
438                      ProcessControlChange(pEvent);                          break;
439                      break;                      case Event::type_pitchbend:
440                  case Event::type_pitchbend:                          dmsg(5,("Engine: Pitchbend received\n"));
441                      dmsg(5,("Engine: Pitchbend received\n"));                          ProcessPitchbend(itEvent);
442                      ProcessPitchbend(pEvent);                          break;
443                      break;                      case Event::type_sysex:
444                  case Event::type_sysex:                          dmsg(5,("Engine: Sysex received\n"));
445                      dmsg(5,("Engine: Sysex received\n"));                          ProcessSysex(itEvent);
446                      ProcessSysex(pEvent);                          break;
447                      break;                  }
448              }              }
449          }          }
450    
451    
         // render audio from all active voices  
452          int active_voices = 0;          int active_voices = 0;
453          uint* piKey = pActiveKeys->first();  
454          while (piKey) { // iterate through all active keys          // render audio from all active voices
455              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];          {
456              pActiveKeys->set_current(piKey);              RTList<uint>::Iterator iuiKey = pActiveKeys->first();
457              piKey = pActiveKeys->next();              RTList<uint>::Iterator end    = pActiveKeys->end();
458                while (iuiKey != end) { // iterate through all active keys
459              Voice* pVoiceNext = pKey->pActiveVoices->first();                  midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
460              while (pVoiceNext) { // iterate through all voices on this key                  ++iuiKey;
461                  // already get next voice on key  
462                  Voice* pVoice = pVoiceNext;                  RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
463                  pKey->pActiveVoices->set_current(pVoice);                  RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
464                  pVoiceNext = pKey->pActiveVoices->next();                  for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
465                        // now render current voice
466                  // now render current voice                      itVoice->Render(Samples);
467                  pVoice->Render(Samples);                      if (itVoice->IsActive()) active_voices++; // still active
468                  if (pVoice->IsActive()) active_voices++; // still active                      else { // voice reached end, is now inactive
469                  else { // voice reached end, is now inactive                          KillVoiceImmediately(itVoice); // remove voice from the list of active voices
470                      KillVoiceImmediately(pVoice); // remove voice from the list of active voices                      }
471                  }                  }
472              }              }
473          }          }
474    
475    
476          // now render all postponed voices from voice stealing          // now render all postponed voices from voice stealing
477          Event* pVoiceStealEvent = pVoiceStealingQueue->first();          {
478          while (pVoiceStealEvent) {              RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first();
479              Voice* pNewVoice = LaunchVoice(pVoiceStealEvent, pVoiceStealEvent->Param.Note.Layer, pVoiceStealEvent->Param.Note.ReleaseTrigger, false);              RTList<Event>::Iterator end               = pVoiceStealingQueue->end();
480              if (pNewVoice) {              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
481                  pNewVoice->Render(Samples);                  Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);
482                  if (pNewVoice->IsActive()) active_voices++; // still active                  if (itNewVoice) {
483                  else { // voice reached end, is now inactive                      itNewVoice->Render(Samples);
484                      KillVoiceImmediately(pNewVoice); // remove voice from the list of active voices                      if (itNewVoice->IsActive()) active_voices++; // still active
485                        else { // voice reached end, is now inactive
486                            KillVoiceImmediately(itNewVoice); // remove voice from the list of active voices
487                        }
488                  }                  }
489                    else dmsg(1,("Ouch, voice stealing didn't work out!\n"));
490              }              }
             else dmsg(1,("Ouch, voice stealing didn't work out!\n"));  
             pVoiceStealEvent = pVoiceStealingQueue->next();  
491          }          }
492          // reset voice stealing for the new fragment          // reset voice stealing for the new fragment
493          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
494          pLastStolenVoice = NULL;          itLastStolenVoice = RTList<Voice>::Iterator();
495          puiLastStolenKey = NULL;          iuiLastStolenKey  = RTList<uint>::Iterator();
496    
497    
498          // 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 600  namespace LinuxSampler { namespace gig {
600      /**      /**
601       *  Assigns and triggers a new voice for the respective MIDI key.       *  Assigns and triggers a new voice for the respective MIDI key.
602       *       *
603       *  @param pNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
604       */       */
605      void Engine::ProcessNoteOn(Event* pNoteOnEvent) {      void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) {
606          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
607    
608          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
609    
610          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
611          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !SustainPedal) {
612              Event* pCancelReleaseEvent = pKey->pEvents->alloc();              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
613              if (pCancelReleaseEvent) {              if (itCancelReleaseEvent) {
614                  *pCancelReleaseEvent = *pNoteOnEvent;                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event
615                  pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type
616              }              }
617              else dmsg(1,("Event pool emtpy!\n"));              else dmsg(1,("Event pool emtpy!\n"));
618          }          }
619    
620          // allocate and trigger a new voice for the key          // move note on event to the key's own event list
621          LaunchVoice(pNoteOnEvent);          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
622    
623          // finally move note on event to the key's own event list          // allocate and trigger a new voice for the key
624          pEvents->move(pNoteOnEvent, pKey->pEvents);          LaunchVoice(itNoteOnEventOnKeyList);
625      }      }
626    
627      /**      /**
# Line 627  namespace LinuxSampler { namespace gig { Line 630  namespace LinuxSampler { namespace gig {
630       *  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.
631       *  due to completion of sample playback).       *  due to completion of sample playback).
632       *       *
633       *  @param pNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
634       */       */
635      void Engine::ProcessNoteOff(Event* pNoteOffEvent) {      void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) {
636          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
637    
638          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
639    
640          // release voices on this key if needed          // release voices on this key if needed
641          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !SustainPedal) {
642              pNoteOffEvent->Type = Event::type_release; // transform event type              itNoteOffEvent->Type = Event::type_release; // transform event type
643          }          }
644    
645            // move event to the key's own event list
646            RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
647    
648          // spawn release triggered voice(s) if needed          // spawn release triggered voice(s) if needed
649          if (pKey->ReleaseTrigger) {          if (pKey->ReleaseTrigger) {
650              LaunchVoice(pNoteOffEvent, 0, true);              LaunchVoice(itNoteOffEventOnKeyList, 0, true);
651              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
652          }          }
   
         // move event to the key's own event list  
         pEvents->move(pNoteOffEvent, pKey->pEvents);  
653      }      }
654    
655      /**      /**
656       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the pitch
657       *  event list.       *  event list.
658       *       *
659       *  @param pPitchbendEvent - absolute pitch value and time stamp of the event       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
660       */       */
661      void Engine::ProcessPitchbend(Event* pPitchbendEvent) {      void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) {
662          this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value          this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
663          pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]);          itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]);
664      }      }
665    
666      /**      /**
# Line 665  namespace LinuxSampler { namespace gig { Line 668  namespace LinuxSampler { namespace gig {
668       *  called by the ProcessNoteOn() method and by the voices itself       *  called by the ProcessNoteOn() method and by the voices itself
669       *  (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).
670       *       *
671       *  @param pNoteOnEvent        - key, velocity and time stamp of the event       *  @param itNoteOnEvent       - key, velocity and time stamp of the event
672       *  @param iLayer              - layer index for the new voice (optional - only       *  @param iLayer              - layer index for the new voice (optional - only
673       *                               in case of layered sounds of course)       *                               in case of layered sounds of course)
674       *  @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 679  namespace LinuxSampler { namespace gig {
679       *  @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
680       *           if an error occured while trying to trigger the new voice       *           if an error occured while trying to trigger the new voice
681       */       */
682      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) {
683          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
684    
685          // allocate a new voice for the key          // allocate a new voice for the key
686          Voice* pNewVoice = pKey->pActiveVoices->alloc();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
687          if (pNewVoice) {          if (itNewVoice) {
688              // launch the new voice              // launch the new voice
689              if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {
690                  dmsg(1,("Triggering new voice failed!\n"));                  dmsg(1,("Triggering new voice failed!\n"));
691                  pKey->pActiveVoices->free(pNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
692              }              }
693              else { // on success              else { // on success
694                  uint** ppKeyGroup = NULL;                  uint** ppKeyGroup = NULL;
695                  if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group                  if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group
696                      ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup];                      ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup];
697                      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
698                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];
699                          // kill all voices on the (other) key                          // kill all voices on the (other) key
700                          Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first();                          RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
701                          while (pVoiceToBeKilled) {                          RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
702                              Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next();                          for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
703                              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;  
704                          }                          }
705                      }                      }
706                  }                  }
707                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
708                      pKey->Active = true;                      pKey->Active = true;
709                      pKey->pSelf  = pActiveKeys->alloc();                      pKey->itSelf = pActiveKeys->allocAppend();
710                      *pKey->pSelf = pNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
711                  }                  }
712                  if (pNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
713                      *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
714                  }                  }
715                  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)
716                  return pNewVoice; // success                  return itNewVoice; // success
717              }              }
718          }          }
719          else if (VoiceStealing) StealVoice(pNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one          else if (VoiceStealing) StealVoice(itNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one
720    
721          return NULL; // no free voice or error          return Pool<Voice>::Iterator(); // no free voice or error
722      }      }
723    
724      /**      /**
# Line 726  namespace LinuxSampler { namespace gig { Line 727  namespace LinuxSampler { namespace gig {
727       *  voice stealing and postpone the note-on event until the selected       *  voice stealing and postpone the note-on event until the selected
728       *  voice actually died.       *  voice actually died.
729       *       *
730       *  @param pNoteOnEvent        - key, velocity and time stamp of the event       *  @param itNoteOnEvent       - key, velocity and time stamp of the event
731       *  @param iLayer              - layer index for the new voice       *  @param iLayer              - layer index for the new voice
732       *  @param ReleaseTriggerVoice - if new voice is a release triggered voice       *  @param ReleaseTriggerVoice - if new voice is a release triggered voice
733       */       */
734      void Engine::StealVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) {      void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) {
735          if (!pEventPool->pool_is_empty()) {          if (!pEventPool->poolIsEmpty()) {
736    
737              uint*  puiOldestKey;              RTList<uint>::Iterator  iuiOldestKey;
738              Voice* pOldestVoice;              RTList<Voice>::Iterator itOldestVoice;
739    
740              // Select one voice for voice stealing              // Select one voice for voice stealing
741              switch (VOICE_STEAL_ALGORITHM) {              switch (VOICE_STEAL_ALGORITHM) {
# Line 744  namespace LinuxSampler { namespace gig { Line 745  namespace LinuxSampler { namespace gig {
745                  // key, or no voice left to kill there, then procceed with                  // key, or no voice left to kill there, then procceed with
746                  // 'oldestkey' algorithm                  // 'oldestkey' algorithm
747                  case voice_steal_algo_keymask: {                  case voice_steal_algo_keymask: {
748                      midi_key_info_t* pOldestKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
749                      if (pLastStolenVoice) {                      if (itLastStolenVoice) {
750                          pOldestKey->pActiveVoices->set_current(pLastStolenVoice);                          itOldestVoice = itLastStolenVoice;
751                          pOldestVoice = pOldestKey->pActiveVoices->next();                          ++itOldestVoice;
752                      }                      }
753                      else { // no voice stolen in this audio fragment cycle yet                      else { // no voice stolen in this audio fragment cycle yet
754                          pOldestVoice = pOldestKey->pActiveVoices->first();                          itOldestVoice = pOldestKey->pActiveVoices->first();
755                      }                      }
756                      if (pOldestVoice) {                      if (itOldestVoice) {
757                          puiOldestKey = pOldestKey->pSelf;                          iuiOldestKey = pOldestKey->itSelf;
758                          break; // selection succeeded                          break; // selection succeeded
759                      }                      }
760                  } // no break - intentional !                  } // no break - intentional !
# Line 761  namespace LinuxSampler { namespace gig { Line 762  namespace LinuxSampler { namespace gig {
762                  // try to pick the oldest voice on the oldest active key                  // try to pick the oldest voice on the oldest active key
763                  // (caution: must stay after 'keymask' algorithm !)                  // (caution: must stay after 'keymask' algorithm !)
764                  case voice_steal_algo_oldestkey: {                  case voice_steal_algo_oldestkey: {
765                      if (pLastStolenVoice) {                      if (itLastStolenVoice) {
766                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiLastStolenKey];                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey];
767                          pOldestKey->pActiveVoices->set_current(pLastStolenVoice);                          itOldestVoice = itLastStolenVoice;
768                          pOldestVoice = pOldestKey->pActiveVoices->next();                          ++itOldestVoice;
769                          if (!pOldestVoice) {                          if (!itOldestVoice) {
770                              pActiveKeys->set_current(puiLastStolenKey);                              iuiOldestKey = iuiLastStolenKey;
771                              puiOldestKey = pActiveKeys->next();                              ++iuiOldestKey;
772                              if (puiOldestKey) {                              if (iuiOldestKey) {
773                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];
774                                  pOldestVoice = pOldestKey->pActiveVoices->first();                                  itOldestVoice = pOldestKey->pActiveVoices->first();
775                              }                              }
776                              else { // too less voices, even for voice stealing                              else { // too less voices, even for voice stealing
777                                  dmsg(1,("Voice overflow! - You might recompile with higher MAX_AUDIO_VOICES!\n"));                                  dmsg(1,("Voice overflow! - You might recompile with higher MAX_AUDIO_VOICES!\n"));
778                                  return;                                  return;
779                              }                              }
780                          }                          }
781                          else puiOldestKey = puiLastStolenKey;                          else iuiOldestKey = iuiLastStolenKey;
782                      }                      }
783                      else { // no voice stolen in this audio fragment cycle yet                      else { // no voice stolen in this audio fragment cycle yet
784                          puiOldestKey = pActiveKeys->first();                          iuiOldestKey = pActiveKeys->first();
785                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];                          midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];
786                          pOldestVoice = pOldestKey->pActiveVoices->first();                          itOldestVoice = pOldestKey->pActiveVoices->first();
787                      }                      }
788                      break;                      break;
789                  }                  }
# Line 796  namespace LinuxSampler { namespace gig { Line 797  namespace LinuxSampler { namespace gig {
797              }              }
798    
799              // now kill the selected voice              // now kill the selected voice
800              pOldestVoice->Kill(pNoteOnEvent);              itOldestVoice->Kill(itNoteOnEvent);
801              // 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
802              this->pLastStolenVoice = pOldestVoice;              this->itLastStolenVoice = itOldestVoice;
803              this->puiLastStolenKey = puiOldestKey;              this->iuiLastStolenKey = iuiOldestKey;
804              // 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
805              Event* pStealEvent = pVoiceStealingQueue->alloc();              RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
806              *pStealEvent = *pNoteOnEvent;              if (itStealEvent) {
807              pStealEvent->Param.Note.Layer = iLayer;                  *itStealEvent = *itNoteOnEvent; // copy event
808              pStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;                  itStealEvent->Param.Note.Layer = iLayer;
809                    itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
810                }
811                else dmsg(1,("Voice stealing queue full!\n"));
812          }          }
813          else dmsg(1,("Event pool emtpy!\n"));          else dmsg(1,("Event pool emtpy!\n"));
814      }      }
# Line 814  namespace LinuxSampler { namespace gig { Line 818  namespace LinuxSampler { namespace gig {
818       *  pressed or not) and removes it from the MIDI key's list of active voice.       *  pressed or not) and removes it from the MIDI key's list of active voice.
819       *  This method will e.g. be called if a voice went inactive by itself.       *  This method will e.g. be called if a voice went inactive by itself.
820       *       *
821       *  @param pVoice - points to the voice to be killed       *  @param itVoice - points to the voice to be killed
822       */       */
823      void Engine::KillVoiceImmediately(Voice* pVoice) {      void Engine::KillVoiceImmediately(Pool<Voice>::Iterator& itVoice) {
824          if (pVoice) {          if (itVoice) {
825              if (pVoice->IsActive()) pVoice->KillImmediately();              if (itVoice->IsActive()) itVoice->KillImmediately();
826    
827                midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];
828    
829              midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey];              uint keygroup = itVoice->KeyGroup;
830    
831              // free the voice object              // free the voice object
832              pVoicePool->free(pVoice);              pVoicePool->free(itVoice);
833    
834              // 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
835              if (pKey->pActiveVoices->is_empty()) {              if (pKey->pActiveVoices->isEmpty()) {
836                  if (pVoice->KeyGroup) { // if voice / key belongs to a key group                  if (keygroup) { // if voice / key belongs to a key group
837                      uint** ppKeyGroup = &ActiveKeyGroups[pVoice->KeyGroup];                      uint** ppKeyGroup = &ActiveKeyGroups[keygroup];
838                      if (*ppKeyGroup == pKey->pSelf) *ppKeyGroup = NULL; // remove key from key group                      if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
839                  }                  }
840                  pKey->Active = false;                  pKey->Active = false;
841                  pActiveKeys->free(pKey->pSelf); // remove key from list of active keys                  pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
842                  pKey->pSelf = NULL;                  pKey->itSelf = RTList<uint>::Iterator();
843                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
844                  pKey->pEvents->clear();                  pKey->pEvents->clear();
845                  dmsg(3,("Key has no more voices now\n"));                  dmsg(3,("Key has no more voices now\n"));
# Line 846  namespace LinuxSampler { namespace gig { Line 852  namespace LinuxSampler { namespace gig {
852       *  Reacts on supported control change commands (e.g. pitch bend wheel,       *  Reacts on supported control change commands (e.g. pitch bend wheel,
853       *  modulation wheel, aftertouch).       *  modulation wheel, aftertouch).
854       *       *
855       *  @param pControlChangeEvent - controller, value and time stamp of the event       *  @param itControlChangeEvent - controller, value and time stamp of the event
856       */       */
857      void Engine::ProcessControlChange(Event* pControlChangeEvent) {      void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) {
858          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));
859    
860          switch (pControlChangeEvent->Param.CC.Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
861              case 64: {              case 64: {
862                  if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {
863                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
864                      SustainPedal = true;                      SustainPedal = true;
865    
866                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
867                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();
868                      if (piKey) {                      if (iuiKey) {
869                          pControlChangeEvent->Type = Event::type_cancel_release; // transform event type                          itControlChangeEvent->Type = Event::type_cancel_release; // transform event type
870                          while (piKey) {                          while (iuiKey) {
871                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
872                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
873                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
874                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
875                                  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
876                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
877                              }                              }
878                          }                          }
879                      }                      }
880                  }                  }
881                  if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {
882                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
883                      SustainPedal = false;                      SustainPedal = false;
884    
885                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
886                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pActiveKeys->first();
887                      if (piKey) {                      if (iuiKey) {
888                          pControlChangeEvent->Type = Event::type_release; // transform event type                          itControlChangeEvent->Type = Event::type_release; // transform event type
889                          while (piKey) {                          while (iuiKey) {
890                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
891                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
892                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
893                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
894                                  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
895                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
896                              }                              }
897                          }                          }
# Line 898  namespace LinuxSampler { namespace gig { Line 902  namespace LinuxSampler { namespace gig {
902          }          }
903    
904          // update controller value in the engine's controller table          // update controller value in the engine's controller table
905          ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value;          ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
906    
907          // 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
908          pEvents->move(pControlChangeEvent, pCCEvents);          itControlChangeEvent.moveToEndOf(pCCEvents);
909      }      }
910    
911      /**      /**
912       *  Reacts on MIDI system exclusive messages.       *  Reacts on MIDI system exclusive messages.
913       *       *
914       *  @param pSysexEvent - sysex data size and time stamp of the sysex event       *  @param itSysexEvent - sysex data size and time stamp of the sysex event
915       */       */
916      void Engine::ProcessSysex(Event* pSysexEvent) {      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {
917          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
918    
919          uint8_t exclusive_status, id;          uint8_t exclusive_status, id;
# Line 957  namespace LinuxSampler { namespace gig { Line 961  namespace LinuxSampler { namespace gig {
961          }          }
962    
963          free_sysex_data: // finally free sysex data          free_sysex_data: // finally free sysex data
964          pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size);          pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size);
965      }      }
966    
967      /**      /**
# Line 1091  namespace LinuxSampler { namespace gig { Line 1095  namespace LinuxSampler { namespace gig {
1095      }      }
1096    
1097      String Engine::Version() {      String Engine::Version() {
1098          String s = "$Revision: 1.14 $";          String s = "$Revision: 1.15 $";
1099          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
1100      }      }
1101    

Legend:
Removed from v.270  
changed lines
  Added in v.271

  ViewVC Help
Powered by ViewVC