/[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 239 by schoenebeck, Sun Sep 12 14:48:19 2004 UTC revision 250 by schoenebeck, Mon Sep 20 00:31:13 2004 UTC
# Line 37  namespace LinuxSampler { namespace gig { Line 37  namespace LinuxSampler { namespace gig {
37          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
38          pDiskThread        = NULL;          pDiskThread        = NULL;
39          pEventGenerator    = NULL;          pEventGenerator    = NULL;
40          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT);          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);
41            pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
42          pEventPool         = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT);
43          pVoicePool         = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES);          pVoicePool         = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES);
44          pActiveKeys        = new RTELMemoryPool<uint>(128);          pActiveKeys        = new RTELMemoryPool<uint>(128);
45            pVoiceStealingQueue = new RTEList<Event>(pEventPool);
46          pEvents            = new RTEList<Event>(pEventPool);          pEvents            = new RTEList<Event>(pEventPool);
47          pCCEvents          = new RTEList<Event>(pEventPool);          pCCEvents          = new RTEList<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 RTEList<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 RTEList<Voice>(pVoicePool);
53              pMIDIKeyInfo[i].KeyPressed    = false;              pMIDIKeyInfo[i].KeyPressed     = false;
54              pMIDIKeyInfo[i].Active        = false;              pMIDIKeyInfo[i].Active         = false;
55              pMIDIKeyInfo[i].pSelf         = NULL;              pMIDIKeyInfo[i].ReleaseTrigger = false;
56              pMIDIKeyInfo[i].pEvents       = new RTEList<Event>(pEventPool);              pMIDIKeyInfo[i].pSelf          = NULL;
57                pMIDIKeyInfo[i].pEvents        = new RTEList<Event>(pEventPool);
58          }          }
59          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {
60              pVoice->SetEngine(this);              pVoice->SetEngine(this);
# Line 92  namespace LinuxSampler { namespace gig { Line 95  namespace LinuxSampler { namespace gig {
95          if (pEventPool)  delete pEventPool;          if (pEventPool)  delete pEventPool;
96          if (pVoicePool)  delete pVoicePool;          if (pVoicePool)  delete pVoicePool;
97          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
98            if (pSysexBuffer) delete pSysexBuffer;
99          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
100          if (pMainFilterParameters) delete[] pMainFilterParameters;          if (pMainFilterParameters) delete[] pMainFilterParameters;
101          if (pBasicFilterParameters) delete[] pBasicFilterParameters;          if (pBasicFilterParameters) delete[] pBasicFilterParameters;
102          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];
103            if (pVoiceStealingQueue) delete pVoiceStealingQueue;
104      }      }
105    
106      void Engine::Enable() {      void Engine::Enable() {
# Line 154  namespace LinuxSampler { namespace gig { Line 159  namespace LinuxSampler { namespace gig {
159          ActiveVoiceCountMax = 0;          ActiveVoiceCountMax = 0;
160          GlobalVolume        = 1.0;          GlobalVolume        = 1.0;
161    
162            // reset voice stealing parameters
163            pLastStolenVoice = NULL;
164            puiLastStolenKey = NULL;
165            pVoiceStealingQueue->clear();
166    
167            // reset to normal chromatic scale (means equal temper)
168            memset(&ScaleTuning[0], 0x00, 12);
169    
170          // set all MIDI controller values to zero          // set all MIDI controller values to zero
171          memset(ControllerTable, 0x00, 128);          memset(ControllerTable, 0x00, 128);
172    
# Line 161  namespace LinuxSampler { namespace gig { Line 174  namespace LinuxSampler { namespace gig {
174          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
175              pMIDIKeyInfo[i].pActiveVoices->clear();              pMIDIKeyInfo[i].pActiveVoices->clear();
176              pMIDIKeyInfo[i].pEvents->clear();              pMIDIKeyInfo[i].pEvents->clear();
177              pMIDIKeyInfo[i].KeyPressed = false;              pMIDIKeyInfo[i].KeyPressed     = false;
178              pMIDIKeyInfo[i].Active     = false;              pMIDIKeyInfo[i].Active         = false;
179              pMIDIKeyInfo[i].pSelf      = NULL;              pMIDIKeyInfo[i].ReleaseTrigger = false;
180                pMIDIKeyInfo[i].pSelf          = NULL;
181          }          }
182    
183          // reset all key groups          // reset all key groups
# Line 385  namespace LinuxSampler { namespace gig { Line 399  namespace LinuxSampler { namespace gig {
399          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
400              pSynthesisEvents[i]->clear();              pSynthesisEvents[i]->clear();
401          }          }
402            for (uint* puiKey = pActiveKeys->first(); puiKey; puiKey = pActiveKeys->next()) {
403                midi_key_info_t* pKey = &pMIDIKeyInfo[*puiKey];
404                pKey->pEvents->clear(); // free all events on the key
405            }
406    
407          // read and copy events from input queue          // read and copy events from input queue
408          Event event = pEventGenerator->CreateEvent();          Event event = pEventGenerator->CreateEvent();
# Line 406  namespace LinuxSampler { namespace gig { Line 424  namespace LinuxSampler { namespace gig {
424              pNextEvent = pEvents->next();              pNextEvent = pEvents->next();
425              switch (pEvent->Type) {              switch (pEvent->Type) {
426                  case Event::type_note_on:                  case Event::type_note_on:
427                      dmsg(5,("Audio Thread: Note on received\n"));                      dmsg(5,("Engine: Note on received\n"));
428                      ProcessNoteOn(pEvent);                      ProcessNoteOn(pEvent);
429                      break;                      break;
430                  case Event::type_note_off:                  case Event::type_note_off:
431                      dmsg(5,("Audio Thread: Note off received\n"));                      dmsg(5,("Engine: Note off received\n"));
432                      ProcessNoteOff(pEvent);                      ProcessNoteOff(pEvent);
433                      break;                      break;
434                  case Event::type_control_change:                  case Event::type_control_change:
435                      dmsg(5,("Audio Thread: MIDI CC received\n"));                      dmsg(5,("Engine: MIDI CC received\n"));
436                      ProcessControlChange(pEvent);                      ProcessControlChange(pEvent);
437                      break;                      break;
438                  case Event::type_pitchbend:                  case Event::type_pitchbend:
439                      dmsg(5,("Audio Thread: Pitchbend received\n"));                      dmsg(5,("Engine: Pitchbend received\n"));
440                      ProcessPitchbend(pEvent);                      ProcessPitchbend(pEvent);
441                      break;                      break;
442                    case Event::type_sysex:
443                        dmsg(5,("Engine: Sysex received\n"));
444                        ProcessSysex(pEvent);
445                        break;
446              }              }
447          }          }
448    
# Line 447  namespace LinuxSampler { namespace gig { Line 469  namespace LinuxSampler { namespace gig {
469                      KillVoiceImmediately(pVoice); // remove voice from the list of active voices                      KillVoiceImmediately(pVoice); // remove voice from the list of active voices
470                  }                  }
471              }              }
             pKey->pEvents->clear(); // free all events on the key  
472          }          }
473    
474    
475            // now render all postponed voices from voice stealing
476            Event* pVoiceStealEvent = pVoiceStealingQueue->first();
477            while (pVoiceStealEvent) {
478                Voice* pNewVoice = LaunchVoice(pVoiceStealEvent, pVoiceStealEvent->Param.Note.Layer, pVoiceStealEvent->Param.Note.ReleaseTrigger, false);
479                if (pNewVoice) {
480                    pNewVoice->Render(Samples);
481                    if (pNewVoice->IsActive()) active_voices++; // still active
482                    else { // voice reached end, is now inactive
483                        KillVoiceImmediately(pNewVoice); // remove voice from the list of active voices
484                    }
485                }
486                else dmsg(1,("Ouch, voice stealing didn't work out!\n"));
487                pVoiceStealEvent = pVoiceStealingQueue->next();
488            }
489            // reset voice stealing for the new fragment
490            pVoiceStealingQueue->clear();
491            pLastStolenVoice = NULL;
492            puiLastStolenKey = NULL;
493    
494    
495          // 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
496          // on the console for debugging purposes          // on the console for debugging purposes
497          ActiveVoiceCount = active_voices;          ActiveVoiceCount = active_voices;
# Line 468  namespace LinuxSampler { namespace gig { Line 509  namespace LinuxSampler { namespace gig {
509       *  @param Velocity - MIDI velocity value of the triggered key       *  @param Velocity - MIDI velocity value of the triggered key
510       */       */
511      void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) {      void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) {
512          Event event    = pEventGenerator->CreateEvent();          Event event               = pEventGenerator->CreateEvent();
513          event.Type     = Event::type_note_on;          event.Type                = Event::type_note_on;
514          event.Key      = Key;          event.Param.Note.Key      = Key;
515          event.Velocity = Velocity;          event.Param.Note.Velocity = Velocity;
516          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
517          else dmsg(1,("Engine: Input event queue full!"));          else dmsg(1,("Engine: Input event queue full!"));
518      }      }
# Line 484  namespace LinuxSampler { namespace gig { Line 525  namespace LinuxSampler { namespace gig {
525       *  @param Velocity - MIDI release velocity value of the released key       *  @param Velocity - MIDI release velocity value of the released key
526       */       */
527      void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) {      void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) {
528          Event event    = pEventGenerator->CreateEvent();          Event event               = pEventGenerator->CreateEvent();
529          event.Type     = Event::type_note_off;          event.Type                = Event::type_note_off;
530          event.Key      = Key;          event.Param.Note.Key      = Key;
531          event.Velocity = Velocity;          event.Param.Note.Velocity = Velocity;
532          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
533          else dmsg(1,("Engine: Input event queue full!"));          else dmsg(1,("Engine: Input event queue full!"));
534      }      }
# Line 499  namespace LinuxSampler { namespace gig { Line 540  namespace LinuxSampler { namespace gig {
540       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
541       */       */
542      void Engine::SendPitchbend(int Pitch) {      void Engine::SendPitchbend(int Pitch) {
543          Event event = pEventGenerator->CreateEvent();          Event event             = pEventGenerator->CreateEvent();
544          event.Type  = Event::type_pitchbend;          event.Type              = Event::type_pitchbend;
545          event.Pitch = Pitch;          event.Param.Pitch.Pitch = Pitch;
546          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
547          else dmsg(1,("Engine: Input event queue full!"));          else dmsg(1,("Engine: Input event queue full!"));
548      }      }
# Line 514  namespace LinuxSampler { namespace gig { Line 555  namespace LinuxSampler { namespace gig {
555       *  @param Value      - value of the control change       *  @param Value      - value of the control change
556       */       */
557      void Engine::SendControlChange(uint8_t Controller, uint8_t Value) {      void Engine::SendControlChange(uint8_t Controller, uint8_t Value) {
558          Event event      = pEventGenerator->CreateEvent();          Event event               = pEventGenerator->CreateEvent();
559          event.Type       = Event::type_control_change;          event.Type                = Event::type_control_change;
560          event.Controller = Controller;          event.Param.CC.Controller = Controller;
561          event.Value      = Value;          event.Param.CC.Value      = Value;
562          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
563          else dmsg(1,("Engine: Input event queue full!"));          else dmsg(1,("Engine: Input event queue full!"));
564      }      }
565    
566      /**      /**
567         *  Will be called by the MIDI input device whenever a MIDI system
568         *  exclusive message has arrived.
569         *
570         *  @param pData - pointer to sysex data
571         *  @param Size  - lenght of sysex data (in bytes)
572         */
573        void Engine::SendSysex(void* pData, uint Size) {
574            Event event             = pEventGenerator->CreateEvent();
575            event.Type              = Event::type_sysex;
576            event.Param.Sysex.Size  = Size;
577            if (pEventQueue->write_space() > 0) {
578                if (pSysexBuffer->write_space() >= Size) {
579                    // copy sysex data to input buffer
580                    uint toWrite = Size;
581                    uint8_t* pPos = (uint8_t*) pData;
582                    while (toWrite) {
583                        const uint writeNow = RTMath::Min(toWrite, pSysexBuffer->write_space_to_end());
584                        pSysexBuffer->write(pPos, writeNow);
585                        toWrite -= writeNow;
586                        pPos    += writeNow;
587    
588                    }
589                    // finally place sysex event into input event queue
590                    pEventQueue->push(&event);
591                }
592                else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,SYSEX_BUFFER_SIZE));
593            }
594            else dmsg(1,("Engine: Input event queue full!"));
595        }
596    
597        /**
598       *  Assigns and triggers a new voice for the respective MIDI key.       *  Assigns and triggers a new voice for the respective MIDI key.
599       *       *
600       *  @param pNoteOnEvent - key, velocity and time stamp of the event       *  @param pNoteOnEvent - key, velocity and time stamp of the event
601       */       */
602      void Engine::ProcessNoteOn(Event* pNoteOnEvent) {      void Engine::ProcessNoteOn(Event* pNoteOnEvent) {
603          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];
604    
605          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
606    
# Line 558  namespace LinuxSampler { namespace gig { Line 630  namespace LinuxSampler { namespace gig {
630       *  @param pNoteOffEvent - key, velocity and time stamp of the event       *  @param pNoteOffEvent - key, velocity and time stamp of the event
631       */       */
632      void Engine::ProcessNoteOff(Event* pNoteOffEvent) {      void Engine::ProcessNoteOff(Event* pNoteOffEvent) {
633          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key];
634    
635          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
636    
637          // release voices on this key if needed          // release voices on this key if needed
638          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !SustainPedal) {
639              pNoteOffEvent->Type = Event::type_release; // transform event type              pNoteOffEvent->Type = Event::type_release; // transform event type
             pEvents->move(pNoteOffEvent, pKey->pEvents); // move event to the key's own event list  
640          }          }
641    
642            // spawn release triggered voice(s) if needed
643            if (pKey->ReleaseTrigger) {
644                LaunchVoice(pNoteOffEvent, 0, true);
645                pKey->ReleaseTrigger = false;
646            }
647    
648            // move event to the key's own event list
649            pEvents->move(pNoteOffEvent, pKey->pEvents);
650      }      }
651    
652      /**      /**
# Line 576  namespace LinuxSampler { namespace gig { Line 656  namespace LinuxSampler { namespace gig {
656       *  @param pPitchbendEvent - absolute pitch value and time stamp of the event       *  @param pPitchbendEvent - absolute pitch value and time stamp of the event
657       */       */
658      void Engine::ProcessPitchbend(Event* pPitchbendEvent) {      void Engine::ProcessPitchbend(Event* pPitchbendEvent) {
659          this->Pitch = pPitchbendEvent->Pitch; // store current pitch value          this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
660          pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]);          pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]);
661      }      }
662    
# Line 585  namespace LinuxSampler { namespace gig { Line 665  namespace LinuxSampler { namespace gig {
665       *  called by the ProcessNoteOn() method and by the voices itself       *  called by the ProcessNoteOn() method and by the voices itself
666       *  (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).
667       *       *
668       *  @param pNoteOnEvent - key, velocity and time stamp of the event       *  @param pNoteOnEvent        - key, velocity and time stamp of the event
669       *  @param iLayer       - layer index for the new voice (optional - only       *  @param iLayer              - layer index for the new voice (optional - only
670       *                        in case of layered sounds of course)       *                               in case of layered sounds of course)
671         *  @param ReleaseTriggerVoice - if new voice is a release triggered voice
672         *                               (optional, default = false)
673         *  @param VoiceStealing       - if voice stealing should be performed
674         *                               when there is no free voice
675         *                               (optional, default = true)
676         *  @returns pointer to new voice or NULL if there was no free voice or
677         *           if an error occured while trying to trigger the new voice
678       */       */
679      void Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer) {      Voice* Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {
680          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key];          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];
681    
682          // allocate a new voice for the key          // allocate a new voice for the key
683          Voice* pNewVoice = pKey->pActiveVoices->alloc();          Voice* pNewVoice = pKey->pActiveVoices->alloc();
684          if (pNewVoice) {          if (pNewVoice) {
685              // launch the new voice              // launch the new voice
686              if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer) < 0) {              if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {
687                  dmsg(1,("Triggering new voice failed!\n"));                  dmsg(1,("Triggering new voice failed!\n"));
688                  pKey->pActiveVoices->free(pNewVoice);                  pKey->pActiveVoices->free(pNewVoice);
689              }              }
# Line 608  namespace LinuxSampler { namespace gig { Line 695  namespace LinuxSampler { namespace gig {
695                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];                          midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup];
696                          // kill all voices on the (other) key                          // kill all voices on the (other) key
697                          Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first();                          Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first();
698                          for (; pVoiceToBeKilled; pVoiceToBeKilled = pOtherKey->pActiveVoices->next())                          while (pVoiceToBeKilled) {
699                              if (pVoiceToBeKilled != pNewVoice) pVoiceToBeKilled->Kill(pNoteOnEvent);                              Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next();
700                                if (pVoiceToBeKilled->Type != Voice::type_release_trigger) pVoiceToBeKilled->Kill(pNoteOnEvent);
701                                pOtherKey->pActiveVoices->set_current(pVoiceToBeKilled);
702                                pVoiceToBeKilled = pVoiceToBeKilledNext;
703                            }
704                      }                      }
705                  }                  }
706                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
707                      pKey->Active = true;                      pKey->Active = true;
708                      pKey->pSelf  = pActiveKeys->alloc();                      pKey->pSelf  = pActiveKeys->alloc();
709                      *pKey->pSelf = pNoteOnEvent->Key;                      *pKey->pSelf = pNoteOnEvent->Param.Note.Key;
710                  }                  }
711                  if (pNewVoice->KeyGroup) {                  if (pNewVoice->KeyGroup) {
712                      *ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group                      *ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group
713                  }                  }
714                    if (pNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)
715                    return pNewVoice; // success
716                }
717            }
718            else if (VoiceStealing) StealVoice(pNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one
719    
720            return NULL; // no free voice or error
721        }
722    
723        /**
724         *  Will be called by LaunchVoice() method in case there are no free
725         *  voices left. This method will select and kill one old voice for
726         *  voice stealing and postpone the note-on event until the selected
727         *  voice actually died.
728         *
729         *  @param pNoteOnEvent        - key, velocity and time stamp of the event
730         *  @param iLayer              - layer index for the new voice
731         *  @param ReleaseTriggerVoice - if new voice is a release triggered voice
732         */
733        void Engine::StealVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) {
734            if (!pEventPool->pool_is_empty()) {
735    
736                uint*  puiOldestKey;
737                Voice* pOldestVoice;
738    
739                // Select one voice for voice stealing
740                switch (VOICE_STEAL_ALGORITHM) {
741    
742                    // try to pick the oldest voice on the key where the new
743                    // voice should be spawned, if there is no voice on that
744                    // key, or no voice left to kill there, then procceed with
745                    // 'oldestkey' algorithm
746                    case voice_steal_algo_keymask: {
747                        midi_key_info_t* pOldestKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key];
748                        if (pLastStolenVoice) {
749                            pOldestKey->pActiveVoices->set_current(pLastStolenVoice);
750                            pOldestVoice = pOldestKey->pActiveVoices->next();
751                        }
752                        else { // no voice stolen in this audio fragment cycle yet
753                            pOldestVoice = pOldestKey->pActiveVoices->first();
754                        }
755                        if (pOldestVoice) {
756                            puiOldestKey = pOldestKey->pSelf;
757                            break; // selection succeeded
758                        }
759                    } // no break - intentional !
760    
761                    // try to pick the oldest voice on the oldest active key
762                    // (caution: must stay after 'keymask' algorithm !)
763                    case voice_steal_algo_oldestkey: {
764                        if (pLastStolenVoice) {
765                            midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiLastStolenKey];
766                            pOldestKey->pActiveVoices->set_current(pLastStolenVoice);
767                            pOldestVoice = pOldestKey->pActiveVoices->next();
768                            if (!pOldestVoice) {
769                                pActiveKeys->set_current(puiLastStolenKey);
770                                puiOldestKey = pActiveKeys->next();
771                                if (puiOldestKey) {
772                                    midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];
773                                    pOldestVoice = pOldestKey->pActiveVoices->first();
774                                }
775                                else { // too less voices, even for voice stealing
776                                    dmsg(1,("Voice overflow! - You might recompile with higher MAX_AUDIO_VOICES!\n"));
777                                    return;
778                                }
779                            }
780                            else puiOldestKey = puiLastStolenKey;
781                        }
782                        else { // no voice stolen in this audio fragment cycle yet
783                            puiOldestKey = pActiveKeys->first();
784                            midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey];
785                            pOldestVoice = pOldestKey->pActiveVoices->first();
786                        }
787                        break;
788                    }
789    
790                    // don't steal anything
791                    case voice_steal_algo_none:
792                    default: {
793                        dmsg(1,("No free voice (voice stealing disabled)!\n"));
794                        return;
795                    }
796              }              }
797    
798                // now kill the selected voice
799                pOldestVoice->Kill(pNoteOnEvent);
800                // remember which voice on which key we stole, so we can simply proceed for the next voice stealing
801                this->pLastStolenVoice = pOldestVoice;
802                this->puiLastStolenKey = puiOldestKey;
803                // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
804                Event* pStealEvent = pVoiceStealingQueue->alloc();
805                *pStealEvent = *pNoteOnEvent;
806                pStealEvent->Param.Note.Layer = iLayer;
807                pStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
808          }          }
809          else std::cerr << "No free voice!" << std::endl << std::flush;          else dmsg(1,("Event pool emtpy!\n"));
810      }      }
811    
812      /**      /**
# Line 650  namespace LinuxSampler { namespace gig { Line 834  namespace LinuxSampler { namespace gig {
834                  pKey->Active = false;                  pKey->Active = false;
835                  pActiveKeys->free(pKey->pSelf); // remove key from list of active keys                  pActiveKeys->free(pKey->pSelf); // remove key from list of active keys
836                  pKey->pSelf = NULL;                  pKey->pSelf = NULL;
837                    pKey->ReleaseTrigger = false;
838                    pKey->pEvents->clear();
839                  dmsg(3,("Key has no more voices now\n"));                  dmsg(3,("Key has no more voices now\n"));
840              }              }
841          }          }
# Line 663  namespace LinuxSampler { namespace gig { Line 849  namespace LinuxSampler { namespace gig {
849       *  @param pControlChangeEvent - controller, value and time stamp of the event       *  @param pControlChangeEvent - controller, value and time stamp of the event
850       */       */
851      void Engine::ProcessControlChange(Event* pControlChangeEvent) {      void Engine::ProcessControlChange(Event* pControlChangeEvent) {
852          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Controller, pControlChangeEvent->Value));          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Param.CC.Controller, pControlChangeEvent->Param.CC.Value));
853    
854          switch (pControlChangeEvent->Controller) {          switch (pControlChangeEvent->Param.CC.Controller) {
855              case 64: {              case 64: {
856                  if (pControlChangeEvent->Value >= 64 && !SustainPedal) {                  if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) {
857                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
858                      SustainPedal = true;                      SustainPedal = true;
859    
# Line 687  namespace LinuxSampler { namespace gig { Line 873  namespace LinuxSampler { namespace gig {
873                          }                          }
874                      }                      }
875                  }                  }
876                  if (pControlChangeEvent->Value < 64 && SustainPedal) {                  if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) {
877                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
878                      SustainPedal = false;                      SustainPedal = false;
879    
# Line 712  namespace LinuxSampler { namespace gig { Line 898  namespace LinuxSampler { namespace gig {
898          }          }
899    
900          // update controller value in the engine's controller table          // update controller value in the engine's controller table
901          ControllerTable[pControlChangeEvent->Controller] = pControlChangeEvent->Value;          ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value;
902    
903          // 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
904          pEvents->move(pControlChangeEvent, pCCEvents);          pEvents->move(pControlChangeEvent, pCCEvents);
905      }      }
906    
907      /**      /**
908         *  Reacts on MIDI system exclusive messages.
909         *
910         *  @param pSysexEvent - sysex data size and time stamp of the sysex event
911         */
912        void Engine::ProcessSysex(Event* pSysexEvent) {
913            RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
914    
915            uint8_t exclusive_status, id;
916            if (!reader.pop(&exclusive_status)) goto free_sysex_data;
917            if (!reader.pop(&id))               goto free_sysex_data;
918            if (exclusive_status != 0xF0)       goto free_sysex_data;
919    
920            switch (id) {
921                case 0x41: { // Roland
922                    uint8_t device_id, model_id, cmd_id;
923                    if (!reader.pop(&device_id)) goto free_sysex_data;
924                    if (!reader.pop(&model_id))  goto free_sysex_data;
925                    if (!reader.pop(&cmd_id))    goto free_sysex_data;
926                    if (model_id != 0x42 /*GS*/) goto free_sysex_data;
927                    if (cmd_id != 0x12 /*DT1*/)  goto free_sysex_data;
928    
929                    // command address
930                    uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)
931                    const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later
932                    if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;
933                    if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters
934                    }
935                    else if (addr[0] == 0x40 && addr[1] == 0x01) { // Common Parameters
936                    }
937                    else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x10) { // Part Parameters (1)
938                        switch (addr[3]) {
939                            case 0x40: { // scale tuning
940                                uint8_t scale_tunes[12]; // detuning of all 12 semitones of an octave
941                                if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data;
942                                uint8_t checksum;
943                                if (!reader.pop(&checksum))                      goto free_sysex_data;
944                                if (GSCheckSum(checksum_reader, 12) != checksum) goto free_sysex_data;
945                                for (int i = 0; i < 12; i++) scale_tunes[i] -= 64;
946                                AdjustScale((int8_t*) scale_tunes);
947                                break;
948                            }
949                        }
950                    }
951                    else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2)
952                    }
953                    else if (addr[0] == 0x41) { // Drum Setup Parameters
954                    }
955                    break;
956                }
957            }
958    
959            free_sysex_data: // finally free sysex data
960            pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size);
961        }
962    
963        /**
964         * Calculates the Roland GS sysex check sum.
965         *
966         * @param AddrReader - reader which currently points to the first GS
967         *                     command address byte of the GS sysex message in
968         *                     question
969         * @param DataSize   - size of the GS message data (in bytes)
970         */
971        uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) {
972            RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader;
973            uint bytes = 3 /*addr*/ + DataSize;
974            uint8_t addr_and_data[bytes];
975            reader.read(&addr_and_data[0], bytes);
976            uint8_t sum = 0;
977            for (uint i = 0; i < bytes; i++) sum += addr_and_data[i];
978            return 128 - sum % 128;
979        }
980    
981        /**
982         * Allows to tune each of the twelve semitones of an octave.
983         *
984         * @param ScaleTunes - detuning of all twelve semitones (in cents)
985         */
986        void Engine::AdjustScale(int8_t ScaleTunes[12]) {
987            memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate
988        }
989    
990        /**
991       * Initialize the parameter sequence for the modulation destination given by       * Initialize the parameter sequence for the modulation destination given by
992       * by 'dst' with the constant value given by val.       * by 'dst' with the constant value given by val.
993       */       */
# Line 822  namespace LinuxSampler { namespace gig { Line 1091  namespace LinuxSampler { namespace gig {
1091      }      }
1092    
1093      String Engine::Version() {      String Engine::Version() {
1094          String s = "$Revision: 1.10 $";          String s = "$Revision: 1.14 $";
1095          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
1096      }      }
1097    

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

  ViewVC Help
Powered by ViewVC