/[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 285 by schoenebeck, Thu Oct 14 21:31:26 2004 UTC revision 329 by senkov, Tue Dec 28 09:43:04 2004 UTC
# Line 26  Line 26 
26  #include "EGADSR.h"  #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 76  namespace LinuxSampler { namespace gig { Line 77  namespace LinuxSampler { namespace gig {
77    
78      Engine::~Engine() {      Engine::~Engine() {
79          if (pDiskThread) {          if (pDiskThread) {
80                dmsg(1,("Stopping disk thread..."));
81              pDiskThread->StopThread();              pDiskThread->StopThread();
82              delete pDiskThread;              delete pDiskThread;
83                dmsg(1,("OK\n"));
84          }          }
85          if (pGig)  delete pGig;          if (pGig)  delete pGig;
86          if (pRIFF) delete pRIFF;          if (pRIFF) delete pRIFF;
# Line 88  namespace LinuxSampler { namespace gig { Line 91  namespace LinuxSampler { namespace gig {
91          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
92              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];
93          }          }
         delete[] pSynthesisEvents;  
94          if (pEvents)     delete pEvents;          if (pEvents)     delete pEvents;
95          if (pCCEvents)   delete pCCEvents;          if (pCCEvents)   delete pCCEvents;
96          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
97          if (pEventPool)  delete pEventPool;          if (pEventPool)  delete pEventPool;
98          if (pVoicePool)  delete pVoicePool;          if (pVoicePool) {
99                    pVoicePool->clear();
100                    delete pVoicePool;
101            }
102          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
103          if (pSysexBuffer) delete pSysexBuffer;          if (pSysexBuffer) delete pSysexBuffer;
104          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
105          if (pMainFilterParameters) delete[] pMainFilterParameters;          if (pMainFilterParameters) delete[] pMainFilterParameters;
106          if (pBasicFilterParameters) delete[] pBasicFilterParameters;          if (pBasicFilterParameters) delete[] pBasicFilterParameters;
107          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
108          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
109      }      }
110    
# Line 321  namespace LinuxSampler { namespace gig { Line 326  namespace LinuxSampler { namespace gig {
326    
327          // (re)create disk thread          // (re)create disk thread
328          if (this->pDiskThread) {          if (this->pDiskThread) {
329                dmsg(1,("Stopping disk thread..."));
330              this->pDiskThread->StopThread();              this->pDiskThread->StopThread();
331              delete this->pDiskThread;              delete this->pDiskThread;
332                dmsg(1,("OK\n"));
333          }          }
334          this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo          this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo
335          if (!pDiskThread) {          if (!pDiskThread) {
# Line 341  namespace LinuxSampler { namespace gig { Line 348  namespace LinuxSampler { namespace gig {
348          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());
349    
350          // (re)allocate synthesis parameter matrix          // (re)allocate synthesis parameter matrix
351          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
352          pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()];          pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()));
353          for (int dst = 1; dst < Event::destination_count; dst++)          for (int dst = 1; dst < Event::destination_count; dst++)
354              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();
355    
# Line 398  namespace LinuxSampler { namespace gig { Line 405  namespace LinuxSampler { namespace gig {
405          }          }
406    
407    
408            // update time of start and end of this audio fragment (as events' time stamps relate to this)
409            pEventGenerator->UpdateFragmentTime(Samples);
410    
411    
412          // empty the event lists for the new fragment          // empty the event lists for the new fragment
413          pEvents->clear();          pEvents->clear();
414          pCCEvents->clear();          pCCEvents->clear();
# Line 412  namespace LinuxSampler { namespace gig { Line 423  namespace LinuxSampler { namespace gig {
423              }              }
424          }          }
425    
         // read and copy events from input queue  
         Event event = pEventGenerator->CreateEvent();  
         while (true) {  
             if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break;  
             *pEvents->allocAppend() = event;  
         }  
   
426    
427          // 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
428          pEventGenerator->UpdateFragmentTime(Samples);          {
429                RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
430                Event* pEvent;
431                while (true) {
432                    // get next event from input event queue
433                    if (!(pEvent = eventQueueReader.pop())) break;
434                    // if younger event reached, ignore that and all subsequent ones for now
435                    if (pEvent->FragmentPos() >= Samples) {
436                        eventQueueReader--;
437                        dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
438                        pEvent->ResetFragmentPos();
439                        break;
440                    }
441                    // copy event to internal event list
442                    if (pEvents->poolIsEmpty()) {
443                        dmsg(1,("Event pool emtpy!\n"));
444                        break;
445                    }
446                    *pEvents->allocAppend() = *pEvent;
447                }
448                eventQueueReader.free(); // free all copied events from input queue
449            }
450    
451    
452          // process events          // process events
# Line 494  namespace LinuxSampler { namespace gig { Line 519  namespace LinuxSampler { namespace gig {
519                          }                          }
520                      }                      }
521                  }                  }
522                  else dmsg(1,("Ouch, voice stealing didn't work out!\n"));                  else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n"));
523              }              }
524          }          }
525          // reset voice stealing for the new fragment          // reset voice stealing for the new fragment
# Line 503  namespace LinuxSampler { namespace gig { Line 528  namespace LinuxSampler { namespace gig {
528          iuiLastStolenKey  = RTList<uint>::Iterator();          iuiLastStolenKey  = RTList<uint>::Iterator();
529    
530    
531            // free all keys which have no active voices left
532            {
533                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
534                RTList<uint>::Iterator end    = pActiveKeys->end();
535                while (iuiKey != end) { // iterate through all active keys
536                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
537                    ++iuiKey;
538                    if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey);
539                    #if DEVMODE
540                    else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
541                        RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
542                        RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
543                        for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
544                            if (itVoice->itKillEvent) {
545                                dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n"));
546                            }
547                        }
548                    }
549                    #endif // DEVMODE
550                }
551            }
552    
553    
554          // 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
555          // on the console for debugging purposes          // on the console for debugging purposes
556          ActiveVoiceCount = active_voices;          ActiveVoiceCount = active_voices;
# Line 629  namespace LinuxSampler { namespace gig { Line 677  namespace LinuxSampler { namespace gig {
677          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);          RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
678    
679          // allocate and trigger a new voice for the key          // allocate and trigger a new voice for the key
680          LaunchVoice(itNoteOnEventOnKeyList);          LaunchVoice(itNoteOnEventOnKeyList, 0, false, true);
681      }      }
682    
683      /**      /**
# Line 655  namespace LinuxSampler { namespace gig { Line 703  namespace LinuxSampler { namespace gig {
703    
704          // spawn release triggered voice(s) if needed          // spawn release triggered voice(s) if needed
705          if (pKey->ReleaseTrigger) {          if (pKey->ReleaseTrigger) {
706              LaunchVoice(itNoteOffEventOnKeyList, 0, true);              LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
707              pKey->ReleaseTrigger = false;              pKey->ReleaseTrigger = false;
708          }          }
709      }      }
# Line 694  namespace LinuxSampler { namespace gig { Line 742  namespace LinuxSampler { namespace gig {
742          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
743          if (itNewVoice) {          if (itNewVoice) {
744              // launch the new voice              // launch the new voice
745              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) {              if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {
746                  dmsg(1,("Triggering new voice failed!\n"));                  dmsg(1,("Triggering new voice failed!\n"));
747                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
748              }              }
# Line 820  namespace LinuxSampler { namespace gig { Line 868  namespace LinuxSampler { namespace gig {
868                  }                  }
869              }              }
870    
871                //FIXME: can be removed, just a sanity check for debugging
872                if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));
873    
874              // now kill the selected voice              // now kill the selected voice
875              itOldestVoice->Kill(itNoteOnEvent);              itOldestVoice->Kill(itNoteOnEvent);
876              // 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
# Line 846  namespace LinuxSampler { namespace gig { Line 897  namespace LinuxSampler { namespace gig {
897              // free the voice object              // free the voice object
898              pVoicePool->free(itVoice);              pVoicePool->free(itVoice);
899    
900              // 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
901              if (pKey->pActiveVoices->isEmpty()) {              if (pKey->pActiveVoices->isEmpty() && keygroup) {
902                  if (keygroup) { // if voice / key belongs to a key group                  uint** ppKeyGroup = &ActiveKeyGroups[keygroup];
903                      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"));  
904              }              }
905          }          }
906          else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;          else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;
907      }      }
908    
909      /**      /**
910         *  Called when there's no more voice left on a key, this call will
911         *  update the key info respectively.
912         *
913         *  @param pKey - key which is now inactive
914         */
915        void Engine::FreeKey(midi_key_info_t* pKey) {
916            if (pKey->pActiveVoices->isEmpty()) {
917                pKey->Active = false;
918                pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
919                pKey->itSelf = RTList<uint>::Iterator();
920                pKey->ReleaseTrigger = false;
921                pKey->pEvents->clear();
922                dmsg(3,("Key has no more voices now\n"));
923            }
924            else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n"));
925        }
926    
927        /**
928       *  Reacts on supported control change commands (e.g. pitch bend wheel,       *  Reacts on supported control change commands (e.g. pitch bend wheel,
929       *  modulation wheel, aftertouch).       *  modulation wheel, aftertouch).
930       *       *
# Line 1110  namespace LinuxSampler { namespace gig { Line 1171  namespace LinuxSampler { namespace gig {
1171      }      }
1172    
1173      String Engine::Version() {      String Engine::Version() {
1174          String s = "$Revision: 1.16 $";          String s = "$Revision: 1.20 $";
1175          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
1176      }      }
1177    

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

  ViewVC Help
Powered by ViewVC