/[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 271 by schoenebeck, Fri Oct 8 20:51:39 2004 UTC revision 319 by schoenebeck, Mon Dec 13 00:46:42 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    #include <malloc.h>
30    
31  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
32    
# Line 98  namespace LinuxSampler { namespace gig { Line 100  namespace LinuxSampler { namespace gig {
100          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
101          if (pMainFilterParameters) delete[] pMainFilterParameters;          if (pMainFilterParameters) delete[] pMainFilterParameters;
102          if (pBasicFilterParameters) delete[] pBasicFilterParameters;          if (pBasicFilterParameters) delete[] pBasicFilterParameters;
103          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
104          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
105      }      }
106    
# Line 313  namespace LinuxSampler { namespace gig { Line 315  namespace LinuxSampler { namespace gig {
315          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();          this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();
316          this->SampleRate              = pAudioOutputDevice->SampleRate();          this->SampleRate              = pAudioOutputDevice->SampleRate();
317    
318            // FIXME: audio drivers with varying fragment sizes might be a problem here
319            MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1;
320            if (MaxFadeOutPos < 0)
321                throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!");
322    
323          // (re)create disk thread          // (re)create disk thread
324          if (this->pDiskThread) {          if (this->pDiskThread) {
325              this->pDiskThread->StopThread();              this->pDiskThread->StopThread();
# Line 335  namespace LinuxSampler { namespace gig { Line 342  namespace LinuxSampler { namespace gig {
342          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());
343    
344          // (re)allocate synthesis parameter matrix          // (re)allocate synthesis parameter matrix
345          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
346          pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()];          pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()));
347          for (int dst = 1; dst < Event::destination_count; dst++)          for (int dst = 1; dst < Event::destination_count; dst++)
348              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();
349    
# Line 392  namespace LinuxSampler { namespace gig { Line 399  namespace LinuxSampler { namespace gig {
399          }          }
400    
401    
402            // update time of start and end of this audio fragment (as events' time stamps relate to this)
403            pEventGenerator->UpdateFragmentTime(Samples);
404    
405    
406          // empty the event lists for the new fragment          // empty the event lists for the new fragment
407          pEvents->clear();          pEvents->clear();
408          pCCEvents->clear();          pCCEvents->clear();
# Line 406  namespace LinuxSampler { namespace gig { Line 417  namespace LinuxSampler { namespace gig {
417              }              }
418          }          }
419    
         // read and copy events from input queue  
         Event event = pEventGenerator->CreateEvent();  
         while (true) {  
             if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break;  
             *pEvents->allocAppend() = event;  
         }  
   
420    
421          // update time of start and end of this audio fragment (as events' time stamps relate to this)          // get all events from the input event queue which belong to the current fragment
422          pEventGenerator->UpdateFragmentTime(Samples);          {
423                RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
424                Event* pEvent;
425                while (true) {
426                    // get next event from input event queue
427                    if (!(pEvent = eventQueueReader.pop())) break;
428                    // if younger event reached, ignore that and all subsequent ones for now
429                    if (pEvent->FragmentPos() >= Samples) {
430                        eventQueueReader--;
431                        dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
432                        pEvent->ResetFragmentPos();
433                        break;
434                    }
435                    // copy event to internal event list
436                    if (pEvents->poolIsEmpty()) {
437                        dmsg(1,("Event pool emtpy!\n"));
438                        break;
439                    }
440                    *pEvents->allocAppend() = *pEvent;
441                }
442                eventQueueReader.free(); // free all copied events from input queue
443            }
444    
445    
446          // process events          // process events
# Line 466  namespace LinuxSampler { namespace gig { Line 491  namespace LinuxSampler { namespace gig {
491                      itVoice->Render(Samples);                      itVoice->Render(Samples);
492                      if (itVoice->IsActive()) active_voices++; // still active                      if (itVoice->IsActive()) active_voices++; // still active
493                      else { // voice reached end, is now inactive                      else { // voice reached end, is now inactive
494                          KillVoiceImmediately(itVoice); // remove voice from the list of active voices                          FreeVoice(itVoice); // remove voice from the list of active voices
495                      }                      }
496                  }                  }
497              }              }
# Line 480  namespace LinuxSampler { namespace gig { Line 505  namespace LinuxSampler { namespace gig {
505              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {              for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
506                  Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);                  Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);
507                  if (itNewVoice) {                  if (itNewVoice) {
508                      itNewVoice->Render(Samples);                      for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) {
509                      if (itNewVoice->IsActive()) active_voices++; // still active                          itNewVoice->Render(Samples);
510                      else { // voice reached end, is now inactive                          if (itNewVoice->IsActive()) active_voices++; // still active
511                          KillVoiceImmediately(itNewVoice); // remove voice from the list of active voices                          else { // voice reached end, is now inactive
512                                FreeVoice(itNewVoice); // remove voice from the list of active voices
513                            }
514                      }                      }
515                  }                  }
516                  else dmsg(1,("Ouch, voice stealing didn't work out!\n"));                  else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n"));
517              }              }
518          }          }
519          // reset voice stealing for the new fragment          // reset voice stealing for the new fragment
# Line 495  namespace LinuxSampler { namespace gig { Line 522  namespace LinuxSampler { namespace gig {
522          iuiLastStolenKey  = RTList<uint>::Iterator();          iuiLastStolenKey  = RTList<uint>::Iterator();
523    
524    
525            // free all keys which have no active voices left
526            {
527                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
528                RTList<uint>::Iterator end    = pActiveKeys->end();
529                while (iuiKey != end) { // iterate through all active keys
530                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
531                    ++iuiKey;
532                    if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey);
533                    #if DEVMODE
534                    else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
535                        RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
536                        RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
537                        for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
538                            if (itVoice->itKillEvent) {
539                                dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n"));
540                            }
541                        }
542                    }
543                    #endif // DEVMODE
544                }
545            }
546    
547    
548          // 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
549          // on the console for debugging purposes          // on the console for debugging purposes
550          ActiveVoiceCount = active_voices;          ActiveVoiceCount = active_voices;
# Line 621  namespace LinuxSampler { namespace gig { Line 671  namespace LinuxSampler { namespace gig {
671          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
672    
673          // allocate and trigger a new voice for the key          // allocate and trigger a new voice for the key
674          LaunchVoice(itNoteOnEventOnKeyList);          LaunchVoice(itNoteOnEventOnKeyList, 0, false, true);
675      }      }
676    
677      /**      /**
# Line 647  namespace LinuxSampler { namespace gig { Line 697  namespace LinuxSampler { namespace gig {
697    
698          // spawn release triggered voice(s) if needed          // spawn release triggered voice(s) if needed
699          if (pKey->ReleaseTrigger) {          if (pKey->ReleaseTrigger) {
700              LaunchVoice(itNoteOffEventOnKeyList, 0, true);              LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
701              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
702          }          }
703      }      }
# Line 686  namespace LinuxSampler { namespace gig { Line 736  namespace LinuxSampler { namespace gig {
736          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
737          if (itNewVoice) {          if (itNewVoice) {
738              // launch the new voice              // launch the new voice
739              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {
740                  dmsg(1,("Triggering new voice failed!\n"));                  dmsg(1,("Triggering new voice failed!\n"));
741                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
742              }              }
# Line 716  namespace LinuxSampler { namespace gig { Line 766  namespace LinuxSampler { namespace gig {
766                  return itNewVoice; // success                  return itNewVoice; // success
767              }              }
768          }          }
769          else if (VoiceStealing) StealVoice(itNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one          else if (VoiceStealing) {
770                // first, get total amount of required voices (dependant on amount of layers)
771                ::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
772                if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed
773                int voicesRequired = pRegion->Layers;
774    
775                // now steal the (remaining) amount of voices
776                for (int i = iLayer; i < voicesRequired; i++)
777                    StealVoice(itNoteOnEvent);
778    
779                // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
780                RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
781                if (itStealEvent) {
782                    *itStealEvent = *itNoteOnEvent; // copy event
783                    itStealEvent->Param.Note.Layer = iLayer;
784                    itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
785                }
786                else dmsg(1,("Voice stealing queue full!\n"));
787            }
788    
789          return Pool<Voice>::Iterator(); // no free voice or error          return Pool<Voice>::Iterator(); // no free voice or error
790      }      }
# Line 727  namespace LinuxSampler { namespace gig { Line 795  namespace LinuxSampler { namespace gig {
795       *  voice stealing and postpone the note-on event until the selected       *  voice stealing and postpone the note-on event until the selected
796       *  voice actually died.       *  voice actually died.
797       *       *
798       *  @param itNoteOnEvent       - 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  
799       */       */
800      void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) {      void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) {
801          if (!pEventPool->poolIsEmpty()) {          if (!pEventPool->poolIsEmpty()) {
802    
803              RTList<uint>::Iterator  iuiOldestKey;              RTList<uint>::Iterator  iuiOldestKey;
# Line 773  namespace LinuxSampler { namespace gig { Line 839  namespace LinuxSampler { namespace gig {
839                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];                                  midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey];
840                                  itOldestVoice = pOldestKey->pActiveVoices->first();                                  itOldestVoice = pOldestKey->pActiveVoices->first();
841                              }                              }
842                              else { // too less voices, even for voice stealing                              else {
843                                  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"));
844                                  return;                                  return;
845                              }                              }
846                          }                          }
# Line 796  namespace LinuxSampler { namespace gig { Line 862  namespace LinuxSampler { namespace gig {
862                  }                  }
863              }              }
864    
865                //FIXME: can be removed, just a sanity check for debugging
866                if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));
867    
868              // now kill the selected voice              // now kill the selected voice
869              itOldestVoice->Kill(itNoteOnEvent);              itOldestVoice->Kill(itNoteOnEvent);
870              // 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
871              this->itLastStolenVoice = itOldestVoice;              this->itLastStolenVoice = itOldestVoice;
872              this->iuiLastStolenKey = iuiOldestKey;              this->iuiLastStolenKey = iuiOldestKey;
             // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died  
             RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();  
             if (itStealEvent) {  
                 *itStealEvent = *itNoteOnEvent; // copy event  
                 itStealEvent->Param.Note.Layer = iLayer;  
                 itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;  
             }  
             else dmsg(1,("Voice stealing queue full!\n"));  
873          }          }
874          else dmsg(1,("Event pool emtpy!\n"));          else dmsg(1,("Event pool emtpy!\n"));
875      }      }
876    
877      /**      /**
878       *  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.
879       *  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
880       *  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
881         *  just was killed.
882       *       *
883       *  @param itVoice - points to the voice to be killed       *  @param itVoice - points to the voice to be freed
884       */       */
885      void Engine::KillVoiceImmediately(Pool<Voice>::Iterator& itVoice) {      void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) {
886          if (itVoice) {          if (itVoice) {
             if (itVoice->IsActive()) itVoice->KillImmediately();  
   
887              midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];              midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];
888    
889              uint keygroup = itVoice->KeyGroup;              uint keygroup = itVoice->KeyGroup;
# Line 831  namespace LinuxSampler { namespace gig { Line 891  namespace LinuxSampler { namespace gig {
891              // free the voice object              // free the voice object
892              pVoicePool->free(itVoice);              pVoicePool->free(itVoice);
893    
894              // check if there are no voices left on the MIDI key and update the key info if so              // if no other voices left and member of a key group, remove from key group
895              if (pKey->pActiveVoices->isEmpty()) {              if (pKey->pActiveVoices->isEmpty() && keygroup) {
896                  if (keygroup) { // if voice / key belongs to a key group                  uint** ppKeyGroup = &ActiveKeyGroups[keygroup];
897                      uint** ppKeyGroup = &ActiveKeyGroups[keygroup];                  if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
                     if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group  
                 }  
                 pKey->Active = false;  
                 pActiveKeys->free(pKey->itSelf); // remove key from list of active keys  
                 pKey->itSelf = RTList<uint>::Iterator();  
                 pKey->ReleaseTrigger = false;  
                 pKey->pEvents->clear();  
                 dmsg(3,("Key has no more voices now\n"));  
898              }              }
899          }          }
900          else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush;          else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;
901        }
902    
903        /**
904         *  Called when there's no more voice left on a key, this call will
905         *  update the key info respectively.
906         *
907         *  @param pKey - key which is now inactive
908         */
909        void Engine::FreeKey(midi_key_info_t* pKey) {
910            if (pKey->pActiveVoices->isEmpty()) {
911                pKey->Active = false;
912                pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
913                pKey->itSelf = RTList<uint>::Iterator();
914                pKey->ReleaseTrigger = false;
915                pKey->pEvents->clear();
916                dmsg(3,("Key has no more voices now\n"));
917            }
918            else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n"));
919      }      }
920    
921      /**      /**
# Line 1095  namespace LinuxSampler { namespace gig { Line 1165  namespace LinuxSampler { namespace gig {
1165      }      }
1166    
1167      String Engine::Version() {      String Engine::Version() {
1168          String s = "$Revision: 1.15 $";          String s = "$Revision: 1.19 $";
1169          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
1170      }      }
1171    

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

  ViewVC Help
Powered by ViewVC