/[svn]/linuxsampler/trunk/src/engines/EngineBase.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/EngineBase.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2012 by iliev, Fri Oct 23 17:53:17 2009 UTC revision 2645 by schoenebeck, Wed Jun 18 00:14:57 2014 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck    *   *   Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck    *
6   *   Copyright (C) 2005-2009 Christian Schoenebeck                         *   *   Copyright (C) 2005-2008 Christian Schoenebeck                         *
7   *   Copyright (C) 2009 Grigor Iliev                                       *   *   Copyright (C) 2009-2013 Christian Schoenebeck and Grigor Iliev        *
8   *                                                                         *   *                                                                         *
9   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
10   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 104  namespace LinuxSampler { Line 104  namespace LinuxSampler {
104               *  @param Samples - number of sample points to be rendered               *  @param Samples - number of sample points to be rendered
105               *  @returns       0 on success               *  @returns       0 on success
106               */               */
107              virtual int RenderAudio(uint Samples) {              virtual int RenderAudio(uint Samples) OVERRIDE {
108                  dmsg(8,("RenderAudio(Samples=%d)\n", Samples));                  dmsg(8,("RenderAudio(Samples=%d)\n", Samples));
109    
110                  // return if engine disabled                  // return if engine disabled
# Line 143  namespace LinuxSampler { Line 143  namespace LinuxSampler {
143                          }                          }
144                      }                      }
145                  }                  }
146                    
147                    // In case scale tuning has been changed, recalculate pitch for
148                    // all active voices.
149                    ProcessScaleTuningChange();
150    
151                  // reset internal voice counter (just for statistic of active voices)                  // reset internal voice counter (just for statistic of active voices)
152                  ActiveVoiceCountTemp = 0;                  ActiveVoiceCountTemp = 0;
# Line 190  namespace LinuxSampler { Line 194  namespace LinuxSampler {
194                  // been deleted by the disk thread                  // been deleted by the disk thread
195                  if (iPendingStreamDeletions) ProcessPendingStreamDeletions();                  if (iPendingStreamDeletions) ProcessPendingStreamDeletions();
196    
197                    // Release the instrument change command. (This has to
198                    // be done after all voices have been rendered and not
199                    // in HandleInstrumentChanges, as the RegionsInUse
200                    // list has been built up by the voice renderers.)
201                    for (int i = 0; i < engineChannels.size(); i++) {
202                        EngineChannelBase<V, R, I>* channel =
203                            static_cast<EngineChannelBase<V, R, I>*>(engineChannels[i]);
204                        channel->InstrumentChangeCommandReader.Unlock();
205                    }
206                  FrameTime += Samples;                  FrameTime += Samples;
207    
208                  EngineDisabled.RttDone();                  EngineDisabled.RttDone();
209                  return 0;                  return 0;
210              }              }
211    
212              virtual int MaxVoices() { return pVoicePool->poolSize(); }              virtual int MaxVoices() OVERRIDE { return pVoicePool->poolSize(); }
213    
214              virtual void SetMaxVoices(int iVoices) throw (Exception) {              virtual void SetMaxVoices(int iVoices) throw (Exception) OVERRIDE {
215                  if (iVoices < 1)                  if (iVoices < 1)
216                      throw Exception("Maximum voices for an engine cannot be set lower than 1");                      throw Exception("Maximum voices for an engine cannot be set lower than 1");
217    
# Line 234  namespace LinuxSampler { Line 247  namespace LinuxSampler {
247                  }                  }
248                  pVoicePool->clear();                  pVoicePool->clear();
249    
250                    PostSetMaxVoices(iVoices);
251                  ResumeAll();                  ResumeAll();
252              }              }
253                
254                /** Called after the new max number of voices is set and before resuming the engine. */
255                virtual void PostSetMaxVoices(int iVoices) { }
256    
257              virtual uint DiskStreamCount() { return (pDiskThread) ? pDiskThread->GetActiveStreamCount() : 0; }              virtual uint DiskStreamCount() OVERRIDE { return (pDiskThread) ? pDiskThread->GetActiveStreamCount() : 0; }
258              virtual uint DiskStreamCountMax() { return (pDiskThread) ? pDiskThread->ActiveStreamCountMax : 0; }              virtual uint DiskStreamCountMax() OVERRIDE { return (pDiskThread) ? pDiskThread->ActiveStreamCountMax : 0; }
259              virtual int  MaxDiskStreams() { return iMaxDiskStreams; }              virtual int  MaxDiskStreams() OVERRIDE { return iMaxDiskStreams; }
260    
261              virtual void SetMaxDiskStreams(int iStreams) throw (Exception) {              virtual void SetMaxDiskStreams(int iStreams) throw (Exception) OVERRIDE {
262                  if (iStreams < 0)                  if (iStreams < 0)
263                      throw Exception("Maximum disk streams for an engine cannot be set lower than 0");                      throw Exception("Maximum disk streams for an engine cannot be set lower than 0");
264    
# Line 256  namespace LinuxSampler { Line 273  namespace LinuxSampler {
273                  ResumeAll();                  ResumeAll();
274              }              }
275    
276              virtual String DiskStreamBufferFillBytes() { return (pDiskThread) ? pDiskThread->GetBufferFillBytes() : ""; }              virtual String DiskStreamBufferFillBytes() OVERRIDE { return (pDiskThread) ? pDiskThread->GetBufferFillBytes() : ""; }
277              virtual String DiskStreamBufferFillPercentage() { return (pDiskThread) ? pDiskThread->GetBufferFillPercentage() : ""; }              virtual String DiskStreamBufferFillPercentage() OVERRIDE { return (pDiskThread) ? pDiskThread->GetBufferFillPercentage() : ""; }
278              virtual InstrumentManager* GetInstrumentManager() { return &instruments; }              virtual InstrumentManager* GetInstrumentManager() OVERRIDE { return &instruments; }
279    
280              /**              /**
281               * Connect this engine instance with the given audio output device.               * Connect this engine instance with the given audio output device.
# Line 269  namespace LinuxSampler { Line 286  namespace LinuxSampler {
286               *               *
287               * @param pAudioOut - audio output device to connect to               * @param pAudioOut - audio output device to connect to
288               */               */
289              virtual void Connect(AudioOutputDevice* pAudioOut) {              virtual void Connect(AudioOutputDevice* pAudioOut) OVERRIDE {
290                  // caution: don't ignore if connecting to the same device here,                  // caution: don't ignore if connecting to the same device here,
291                  // because otherwise SetMaxDiskStreams() implementation won't work anymore!                  // because otherwise SetMaxDiskStreams() implementation won't work anymore!
292    
# Line 299  namespace LinuxSampler { Line 316  namespace LinuxSampler {
316                      // lower minimum release time                      // lower minimum release time
317                      const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate;                      const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate;
318                      for (VoiceIterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {                      for (VoiceIterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
319                          iterVoice->EG1.CalculateFadeOutCoeff(minReleaseTime, SampleRate);                          iterVoice->CalculateFadeOutCoeff(minReleaseTime, SampleRate);
320                      }                      }
321                      pVoicePool->clear();                      pVoicePool->clear();
322                  }                  }
# Line 332  namespace LinuxSampler { Line 349  namespace LinuxSampler {
349                  pDiskThread->StartThread();                  pDiskThread->StartThread();
350                  dmsg(1,("OK\n"));                  dmsg(1,("OK\n"));
351    
352                    bool printEqInfo = true;
353                  for (VoiceIterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {                  for (VoiceIterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
354                      if (!iterVoice->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                      }                      }
358                        
359                        iterVoice->CreateEq();
360                        
361                        if(printEqInfo) {
362                            iterVoice->PrintEqInfo();
363                            printEqInfo = false;
364                        }
365                  }                  }
366                  pVoicePool->clear();                  pVoicePool->clear();
367                    
368                    // (re)create dedicated voice audio buffers
369                    //TODO: we could optimize resource usage a bit by just allocating these dedicated voice buffers when there is at least one engine channel with FX sends, because only in this case those special buffers are used actually, but since it would usually only save couple bytes in total, its probably not worth it
370                    if (pDedicatedVoiceChannelLeft)  delete pDedicatedVoiceChannelLeft;
371                    if (pDedicatedVoiceChannelRight) delete pDedicatedVoiceChannelRight;
372                    pDedicatedVoiceChannelLeft  = new AudioChannel(0, MaxSamplesPerCycle);
373                    pDedicatedVoiceChannelRight = new AudioChannel(1, MaxSamplesPerCycle);
374                }
375            
376                // Implementattion for abstract method derived from Engine.
377                virtual void ReconnectAudioOutputDevice() OVERRIDE {
378                    SuspendAll();
379                    if (pAudioOutputDevice) Connect(pAudioOutputDevice);
380                    ResumeAll();
381              }              }
382    
383              /**              /**
# Line 394  namespace LinuxSampler { Line 433  namespace LinuxSampler {
433               */               */
434              virtual void Suspend(RR* pRegion) {              virtual void Suspend(RR* pRegion) {
435                  dmsg(2,("EngineBase: Suspending Region %x ...\n",pRegion));                  dmsg(2,("EngineBase: Suspending Region %x ...\n",pRegion));
436                  SuspendedRegionsMutex.Lock();                  {
437                  SuspensionChangeOngoing.Set(true);                      LockGuard lock(SuspendedRegionsMutex);
438                  pPendingRegionSuspension = pRegion;                      SuspensionChangeOngoing.Set(true);
439                  SuspensionChangeOngoing.WaitAndUnlockIf(true);                      pPendingRegionSuspension = pRegion;
440                  SuspendedRegionsMutex.Unlock();                      SuspensionChangeOngoing.WaitAndUnlockIf(true);
441                    }
442                  dmsg(2,("EngineBase: Region %x suspended.",pRegion));                  dmsg(2,("EngineBase: Region %x suspended.",pRegion));
443              }              }
444    
# Line 410  namespace LinuxSampler { Line 450  namespace LinuxSampler {
450               */               */
451              virtual void Resume(RR* pRegion) {              virtual void Resume(RR* pRegion) {
452                  dmsg(2,("EngineBase: Resuming Region %x ...\n",pRegion));                  dmsg(2,("EngineBase: Resuming Region %x ...\n",pRegion));
453                  SuspendedRegionsMutex.Lock();                  {
454                  SuspensionChangeOngoing.Set(true);                      LockGuard lock(SuspendedRegionsMutex);
455                  pPendingRegionResumption = pRegion;                      SuspensionChangeOngoing.Set(true);
456                  SuspensionChangeOngoing.WaitAndUnlockIf(true);                      pPendingRegionResumption = pRegion;
457                  SuspendedRegionsMutex.Unlock();                      SuspensionChangeOngoing.WaitAndUnlockIf(true);
458                    }
459                  dmsg(2,("EngineBase: Region %x resumed.\n",pRegion));                  dmsg(2,("EngineBase: Region %x resumed.\n",pRegion));
460              }              }
461    
# Line 530  namespace LinuxSampler { Line 571  namespace LinuxSampler {
571    
572              //friend class EngineChannelBase<V, R, I>;              //friend class EngineChannelBase<V, R, I>;
573    
574                static IM instruments;
575    
576          protected:          protected:
577              class SuspensionVoiceHandler : public MidiKeyboardManager<V>::VoiceHandler {              class SuspensionVoiceHandler : public MidiKeyboardManager<V>::VoiceHandler {
578              public:              public:
579                  int PendingStreamDeletions;                  int PendingStreamDeletions;
580                  RR* pPendingRegionSuspension;                  RR* pPendingRegionSuspension;
581    
582                  SuspensionVoiceHandler(RR* pPendingRegionSuspension) {                  SuspensionVoiceHandler(RR* pPendingRegionSuspension) {
583                      PendingStreamDeletions = 0;                      PendingStreamDeletions = 0;
584                      this->pPendingRegionSuspension = pPendingRegionSuspension;                      this->pPendingRegionSuspension = pPendingRegionSuspension;
585                  }                  }
586    
587                  virtual bool Process(MidiKey* pMidiKey) {                  virtual bool Process(MidiKey* pMidiKey) OVERRIDE {
588                      VoiceIterator itVoice = pMidiKey->pActiveVoices->first();                      VoiceIterator itVoice = pMidiKey->pActiveVoices->first();
589                      // if current key is not associated with this region, skip this key                      // if current key is not associated with this region, skip this key
590                      if (itVoice->GetRegion()->GetParent() != pPendingRegionSuspension) return false;                      if (itVoice->GetRegion()->GetParent() != pPendingRegionSuspension) return false;
# Line 548  namespace LinuxSampler { Line 592  namespace LinuxSampler {
592                      return true;                      return true;
593                  }                  }
594    
595                  virtual void Process(VoiceIterator& itVoice) {                  virtual void Process(VoiceIterator& itVoice) OVERRIDE {
596                      // request a notification from disk thread side for stream deletion                      // request a notification from disk thread side for stream deletion
597                      const Stream::Handle hStream = itVoice->KillImmediately(true);                      const Stream::Handle hStream = itVoice->KillImmediately(true);
598                      if (hStream != Stream::INVALID_HANDLE) { // voice actually used a stream                      if (hStream != Stream::INVALID_HANDLE) { // voice actually used a stream
# Line 558  namespace LinuxSampler { Line 602  namespace LinuxSampler {
602                  }                  }
603              };              };
604    
             static IM instruments;  
   
605              Pool<R*>* pRegionPool[2]; ///< Double buffered pool, used by the engine channels to keep track of regions in use.              Pool<R*>* pRegionPool[2]; ///< Double buffered pool, used by the engine channels to keep track of regions in use.
606              int       MinFadeOutSamples;     ///< The number of samples needed to make an instant fade out (e.g. for voice stealing) without leading to clicks.              int       MinFadeOutSamples;     ///< The number of samples needed to make an instant fade out (e.g. for voice stealing) without leading to clicks.
607              D*        pDiskThread;              D*        pDiskThread;
608    
             int                          VoiceSpawnsLeft;       ///< We only allow CONFIG_MAX_VOICES voices to be spawned per audio fragment, we use this variable to ensure this limit.  
609              int                          ActiveVoiceCountTemp;  ///< number of currently active voices (for internal usage, will be used for incrementation)              int                          ActiveVoiceCountTemp;  ///< number of currently active voices (for internal usage, will be used for incrementation)
610              VoiceIterator                itLastStolenVoice;     ///< Only for voice stealing: points to the last voice which was theft in current audio fragment, NULL otherwise.              VoiceIterator                itLastStolenVoice;     ///< Only for voice stealing: points to the last voice which was theft in current audio fragment, NULL otherwise.
611              RTList<uint>::Iterator       iuiLastStolenKey;      ///< Only for voice stealing: key number of last key on which the last voice was theft in current audio fragment, NULL otherwise.              RTList<uint>::Iterator       iuiLastStolenKey;      ///< Only for voice stealing: key number of last key on which the last voice was theft in current audio fragment, NULL otherwise.
# Line 590  namespace LinuxSampler { Line 631  namespace LinuxSampler {
631                  AbstractEngineChannel* pChannel = static_cast<AbstractEngineChannel*>(pEngineChannel);                  AbstractEngineChannel* pChannel = static_cast<AbstractEngineChannel*>(pEngineChannel);
632                  pChannel->ImportEvents(Samples);                  pChannel->ImportEvents(Samples);
633    
634                  // process events                  // if a valid real-time instrument script is loaded, pre-process
635                    // the event list by running the script now, since the script
636                    // might filter events or add new ones for this cycle
637                    if (pChannel->pScript && pChannel->pScript->bHasValidScript) {
638                        // resume any suspended script executions still hanging
639                        // around of previous audio fragment cycles
640                        for (RTList<ScriptEvent>::Iterator itEvent = pChannel->pScript->pEvents->first(),
641                            end = pChannel->pScript->pEvents->end(); itEvent != end; ++itEvent)
642                        {
643                            ResumeScriptEvent(pChannel, itEvent); //TODO: implement support for actual suspension time (i.e. passed to a script's wait() function call)
644                        }
645    
646                        // spawn new script executions for the new MIDI events of
647                        // this audio fragment cycle
648                        for (RTList<Event>::Iterator itEvent = pChannel->pEvents->first(),
649                            end = pChannel->pEvents->end(); itEvent != end; ++itEvent)
650                        {
651                            switch (itEvent->Type) {
652                                case Event::type_note_on:
653                                    if (pChannel->pScript->handlerNote)
654                                        ProcessEventByScript(pChannel, itEvent, pChannel->pScript->handlerNote);
655                                    break;
656                                case Event::type_note_off:
657                                    if (pChannel->pScript->handlerRelease)
658                                        ProcessEventByScript(pChannel, itEvent, pChannel->pScript->handlerRelease);
659                                    break;
660                                case Event::type_control_change:
661                                case Event::type_channel_pressure:
662                                case Event::type_pitchbend:
663                                    if (pChannel->pScript->handlerController)
664                                        ProcessEventByScript(pChannel, itEvent, pChannel->pScript->handlerController);                            
665                                    break;
666                                case Event::type_note_pressure:
667                                    //TODO: ...
668                                    break;
669                            }
670                        }
671                    }
672    
673                    // now process all events regularly
674                  {                  {
675                      RTList<Event>::Iterator itEvent = pChannel->pEvents->first();                      RTList<Event>::Iterator itEvent = pChannel->pEvents->first();
676                      RTList<Event>::Iterator end     = pChannel->pEvents->end();                      RTList<Event>::Iterator end     = pChannel->pEvents->end();
# Line 608  namespace LinuxSampler { Line 688  namespace LinuxSampler {
688                                  dmsg(5,("Engine: MIDI CC received\n"));                                  dmsg(5,("Engine: MIDI CC received\n"));
689                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);
690                                  break;                                  break;
691                                case Event::type_channel_pressure:
692                                    dmsg(5,("Engine: MIDI Chan. Pressure received\n"));
693                                    ProcessChannelPressure((EngineChannel*)itEvent->pEngineChannel, itEvent);
694                                    break;
695                                case Event::type_note_pressure:
696                                    dmsg(5,("Engine: MIDI Note Pressure received\n"));
697                                    ProcessPolyphonicKeyPressure((EngineChannel*)itEvent->pEngineChannel, itEvent);
698                                    break;
699                              case Event::type_pitchbend:                              case Event::type_pitchbend:
700                                  dmsg(5,("Engine: Pitchbend received\n"));                                  dmsg(5,("Engine: Pitchbend received\n"));
701                                  ProcessPitchbend(static_cast<AbstractEngineChannel*>(itEvent->pEngineChannel), itEvent);                                  ProcessPitchbend(static_cast<AbstractEngineChannel*>(itEvent->pEngineChannel), itEvent);
# Line 624  namespace LinuxSampler { Line 712  namespace LinuxSampler {
712                  pLastStolenChannel        = NULL;                  pLastStolenChannel        = NULL;
713              }              }
714    
715                /** @brief Call instrument script's event handler for this event.
716                 *
717                 * Causes a new execution instance of the currently loaded real-time
718                 * instrument script's event handler (callback) to be spawned for
719                 * the given MIDI event.
720                 *
721                 * @param pChannel - engine channel on which the MIDI event occured
722                 * @param itEvent - MIDI event that causes this new script execution
723                 * @param pEventHandler - script's event handler to be executed
724                 */
725                void ProcessEventByScript(AbstractEngineChannel* pChannel, RTList<Event>::Iterator& itEvent, VMEventHandler* pEventHandler) {
726                    const int key = itEvent->Param.Note.Key; // even if this is not a note on/off event, accessing it does not mean any harm
727                    // check if polyphonic data is passed from "note" to "release"
728                    // script event handlers
729                    if (pEventHandler == pChannel->pScript->handlerRelease &&
730                        pChannel->pScript->handlerNote &&
731                        pChannel->pScript->handlerNote->isPolyphonic() &&
732                        pChannel->pScript->handlerRelease->isPolyphonic() &&
733                        !pChannel->pScript->pKeyEvents[key]->isEmpty())
734                    {
735                        // polyphonic variable data is used/passed from "note" to
736                        // "release" script callback, so we have to recycle the
737                        // original "note on" script event(s)
738                        RTList<ScriptEvent>::Iterator it  = pChannel->pScript->pKeyEvents[key]->first();
739                        RTList<ScriptEvent>::Iterator end = pChannel->pScript->pKeyEvents[key]->end();
740                        for (; it != end; ++it) {
741                            ProcessScriptEvent(
742                                pChannel, itEvent, pEventHandler, it
743                            );
744                        }
745                    } else {
746                        // no polyphonic data is used/passed from "note" to
747                        // "release" script callback, so just use a new fresh
748                        // script event object
749                        RTList<ScriptEvent>::Iterator itScriptEvent =
750                            pChannel->pScript->pEvents->allocAppend();
751                        ProcessScriptEvent(
752                            pChannel, itEvent, pEventHandler, itScriptEvent
753                        );
754                    }
755                }
756    
757                void ProcessScriptEvent(AbstractEngineChannel* pChannel, RTList<Event>::Iterator& itEvent, VMEventHandler* pEventHandler, RTList<ScriptEvent>::Iterator& itScriptEvent) {
758                    if (!itScriptEvent) return; // not a valid script event (i.e. because no free script event was left in the script event pool)
759    
760                    // fill the list of script handlers to be executed by this event
761                    int i = 0;
762                    itScriptEvent->handlers[i++] = pEventHandler; // actual event handler (i.e. note, controller)
763                    itScriptEvent->handlers[i] = NULL; // NULL termination of list
764    
765                    // initialize/reset other members
766                    itScriptEvent->cause = *itEvent;
767                    itScriptEvent->id = pEventPool->getID(itEvent);
768                    itScriptEvent->currentHandler = 0;
769                    itScriptEvent->executionSlices = 0;
770    
771                    // run script handler(s)
772                    VMExecStatus_t res = pScriptVM->exec(
773                        pChannel->pScript->parserContext, &*itScriptEvent
774                    );
775    
776                    // in case the script was suspended, keep it on the allocated
777                    // ScriptEvent list to be continued on the next audio cycle
778                    if (!(res & VM_EXEC_SUSPENDED)) { // script execution has finished without 'suspended' status ...
779                        // if "polyphonic" variable data is passed from script's
780                        // "note" event handler to its "release" event handler, then
781                        // the script event must be kept and recycled for the later
782                        // occuring "release" script event ...
783                        if (pEventHandler == pChannel->pScript->handlerNote &&
784                            pChannel->pScript->handlerRelease &&
785                            pChannel->pScript->handlerNote->isPolyphonic() &&
786                            pChannel->pScript->handlerRelease->isPolyphonic())
787                        {
788                            const int key = itEvent->Param.Note.Key;
789                            itScriptEvent.moveToEndOf(pChannel->pScript->pKeyEvents[key & 127]);
790                        } else {
791                            // ... otherwise if no polyphonic data is passed and
792                            // script's execution has finished without suspension
793                            // status, then free the script event for a new future
794                            // script event to be triggered from start
795                            pChannel->pScript->pEvents->free(itScriptEvent);
796                        }
797                    }
798                }
799    
800                /** @brief Resume execution of instrument script.
801                 *
802                 * Will be called to resume execution of a real-time instrument
803                 * script event which has been suspended in a previous audio
804                 * fragment cycle.
805                 *
806                 * Script execution might be suspended for various reasons. Usually
807                 * a script will be suspended if the script called the built-in
808                 * "wait()" function, but it might also be suspended automatically
809                 * if the script took too much execution time in an audio fragment
810                 * cycle. So in the latter case automatic suspension is performed in
811                 * order to avoid harm for the sampler's overall real-time
812                 * requirements.
813                 *
814                 * @param pChannel - engine channel this script is running for
815                 * @param itScriptEvent - script execution that shall be resumed
816                 */
817                void ResumeScriptEvent(AbstractEngineChannel* pChannel, RTList<ScriptEvent>::Iterator& itScriptEvent) {
818                    VMEventHandler* handler = itScriptEvent->handlers[itScriptEvent->currentHandler];
819    
820                    // run script
821                    VMExecStatus_t res = pScriptVM->exec(
822                        pChannel->pScript->parserContext, &*itScriptEvent
823                    );
824    
825                    // in case the script was suspended, keep it on the allocated
826                    // ScriptEvent list to be continued on the next audio cycle
827                    if (!(res & VM_EXEC_SUSPENDED)) { // script execution has finished without 'suspended' status ...
828                        // if "polyphonic" variable data is passed from script's
829                        // "note" event handler to its "release" event handler, then
830                        // the script event must be kept and recycled for the later
831                        // occuring "release" script event ...
832                        if (handler && handler == pChannel->pScript->handlerNote &&
833                            pChannel->pScript->handlerRelease &&
834                            pChannel->pScript->handlerNote->isPolyphonic() &&
835                            pChannel->pScript->handlerRelease->isPolyphonic())
836                        {
837                            const int key = itScriptEvent->cause.Param.Note.Key;
838                            itScriptEvent.moveToEndOf(pChannel->pScript->pKeyEvents[key & 127]);
839                        } else {
840                            // ... otherwise if no polyphonic data is passed and
841                            // script's execution has finished without suspension
842                            // status, then free the script event for a new future
843                            // script event to be triggered from start
844                            pChannel->pScript->pEvents->free(itScriptEvent);
845                        }
846                    }
847                }
848    
849              /**              /**
850               *  Will be called by LaunchVoice() method in case there are no free               *  Will be called by LaunchVoice() method in case there are no free
851               *  voices left. This method will select and kill one old voice for               *  voices left. This method will select and kill one old voice for
# Line 756  namespace LinuxSampler { Line 978  namespace LinuxSampler {
978                          dmsg(5,("Engine: instrument change command received\n"));                          dmsg(5,("Engine: instrument change command received\n"));
979                          cmd.bChangeInstrument = false;                          cmd.bChangeInstrument = false;
980                          pEngineChannel->pInstrument = cmd.pInstrument;                          pEngineChannel->pInstrument = cmd.pInstrument;
981                            pEngineChannel->pScript = cmd.pScript;
982                          instrumentChanged = true;                          instrumentChanged = true;
983    
984                          pEngineChannel->MarkAllActiveVoicesAsOrphans();                          pEngineChannel->MarkAllActiveVoicesAsOrphans();
985    
986                            // the script's "init" event handler is only executed
987                            // once (when the script is loaded or reloaded)
988                            if (pEngineChannel->pScript && pEngineChannel->pScript->handlerInit) {
989                                RTList<ScriptEvent>::Iterator itScriptEvent =
990                                    pEngineChannel->pScript->pEvents->allocAppend();
991    
992                                itScriptEvent->cause.pEngineChannel = pEngineChannel;
993                                itScriptEvent->handlers[0] = pEngineChannel->pScript->handlerInit;
994                                itScriptEvent->handlers[1] = NULL;
995    
996                                VMExecStatus_t res = pScriptVM->exec(
997                                    pEngineChannel->pScript->parserContext, &*itScriptEvent
998                                );
999    
1000                                pEngineChannel->pScript->pEvents->free(itScriptEvent);
1001                            }
1002                      }                      }
1003                  }                  }
1004    
# Line 766  namespace LinuxSampler { Line 1006  namespace LinuxSampler {
1006                      //TODO: this is a lazy solution ATM and not safe in case somebody is currently editing the instrument we're currently switching to (we should store all suspended regions on instrument manager side and when switching to another instrument copy that list to the engine's local list of suspensions                      //TODO: this is a lazy solution ATM and not safe in case somebody is currently editing the instrument we're currently switching to (we should store all suspended regions on instrument manager side and when switching to another instrument copy that list to the engine's local list of suspensions
1007                      ResetSuspendedRegions();                      ResetSuspendedRegions();
1008                  }                  }
   
                 for (int i = 0; i < engineChannels.size(); i++) {  
                     EngineChannelBase<V, R, I>* channel =  
                         static_cast<EngineChannelBase<V, R, I>*>(engineChannels[i]);  
                     channel->InstrumentChangeCommandReader.Unlock();  
                 }  
1009              }              }
1010    
1011              /**              /**
# Line 858  namespace LinuxSampler { Line 1092  namespace LinuxSampler {
1092                  pChannel->ClearEventLists();                  pChannel->ClearEventLists();
1093              }              }
1094    
1095                /**
1096                 * Process MIDI control change events with hard coded behavior,
1097                 * that is controllers whose behavior is defined independently
1098                 * of the actual sampler engine type and instrument.
1099                 *
1100                 * @param pEngineChannel - engine channel on which the MIDI CC event was received
1101                 * @param itControlChangeEvent - the actual MIDI CC event
1102                 */
1103              void ProcessHardcodedControllers (              void ProcessHardcodedControllers (
1104                  EngineChannel*          pEngineChannel,                  EngineChannel*          pEngineChannel,
1105                  Pool<Event>::Iterator&  itControlChangeEvent                  Pool<Event>::Iterator&  itControlChangeEvent
# Line 870  namespace LinuxSampler { Line 1112  namespace LinuxSampler {
1112                          pChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;                          pChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;
1113                          break;                          break;
1114                      }                      }
1115                      case 6: { // data entry (currently only used for RPN controllers)                      case 6: { // data entry (currently only used for RPN and NRPN controllers)
1116                          if (pChannel->GetMidiRpnController() == 2) { // coarse tuning in half tones                          //dmsg(1,("DATA ENTRY %d\n", itControlChangeEvent->Param.CC.Value));
1117                              int transpose = (int) itControlChangeEvent->Param.CC.Value - 64;                          if (pChannel->GetMidiRpnController() >= 0) { // RPN controller number was sent previously ...
1118                              // limit to +- two octaves for now                              dmsg(4,("Guess it's an RPN ...\n"));
1119                              transpose = RTMath::Min(transpose,  24);                              if (pChannel->GetMidiRpnController() == 2) { // coarse tuning in half tones
1120                              transpose = RTMath::Max(transpose, -24);                                  int transpose = (int) itControlChangeEvent->Param.CC.Value - 64;
1121                              pChannel->GlobalTranspose = transpose;                                  // limit to +- two octaves for now
1122                              // workaround, so we won't have hanging notes                                  transpose = RTMath::Min(transpose,  24);
1123                              pChannel->ReleaseAllVoices(itControlChangeEvent);                                  transpose = RTMath::Max(transpose, -24);
1124                                    pChannel->GlobalTranspose = transpose;
1125                                    // workaround, so we won't have hanging notes
1126                                    pChannel->ReleaseAllVoices(itControlChangeEvent);
1127                                }
1128                                // to prevent other MIDI CC #6 messages to be misenterpreted as RPN controller data
1129                                pChannel->ResetMidiRpnController();
1130                            } else if (pChannel->GetMidiNrpnController() >= 0) { // NRPN controller number was sent previously ...
1131                                dmsg(4,("Guess it's an NRPN ...\n"));
1132                                const int NrpnCtrlMSB = pChannel->GetMidiNrpnController() >> 8;
1133                                const int NrpnCtrlLSB = pChannel->GetMidiNrpnController() & 0xff;
1134                                dmsg(4,("NRPN MSB=%d LSB=%d Data=%d\n", NrpnCtrlMSB, NrpnCtrlLSB, itControlChangeEvent->Param.CC.Value));
1135                                switch (NrpnCtrlMSB) {
1136                                    case 0x1a: { // volume level of note (Roland GS NRPN)
1137                                        const uint note = NrpnCtrlLSB;
1138                                        const uint vol  = itControlChangeEvent->Param.CC.Value;
1139                                        dmsg(4,("Note Volume NRPN received (note=%d,vol=%d).\n", note, vol));
1140                                        if (note < 128 && vol < 128)
1141                                            pChannel->pMIDIKeyInfo[note].Volume = VolumeCurve[vol];
1142                                        break;
1143                                    }
1144                                    case 0x1c: { // panpot of note (Roland GS NRPN)
1145                                        const uint note = NrpnCtrlLSB;
1146                                        const uint pan  = itControlChangeEvent->Param.CC.Value;
1147                                        dmsg(4,("Note Pan NRPN received (note=%d,pan=%d).\n", note, pan));
1148                                        if (note < 128 && pan < 128) {
1149                                            pChannel->pMIDIKeyInfo[note].PanLeft  = PanCurve[128 - pan];
1150                                            pChannel->pMIDIKeyInfo[note].PanRight = PanCurve[pan];
1151                                        }
1152                                        break;
1153                                    }
1154                                    case 0x1d: { // reverb send of note (Roland GS NRPN)
1155                                        const uint note = NrpnCtrlLSB;
1156                                        const float reverb = float(itControlChangeEvent->Param.CC.Value) / 127.0f;
1157                                        dmsg(4,("Note Reverb Send NRPN received (note=%d,send=%d).\n", note, reverb));
1158                                        if (note < 128)
1159                                            pChannel->pMIDIKeyInfo[note].ReverbSend = reverb;
1160                                        break;
1161                                    }
1162                                    case 0x1e: { // chorus send of note (Roland GS NRPN)
1163                                        const uint note = NrpnCtrlLSB;
1164                                        const float chorus = float(itControlChangeEvent->Param.CC.Value) / 127.0f;
1165                                        dmsg(4,("Note Chorus Send NRPN received (note=%d,send=%d).\n", note, chorus));
1166                                        if (note < 128)
1167                                            pChannel->pMIDIKeyInfo[note].ChorusSend = chorus;
1168                                        break;
1169                                    }
1170                                }
1171                                // to prevent other MIDI CC #6 messages to be misenterpreted as NRPN controller data
1172                                pChannel->ResetMidiNrpnController();
1173                          }                          }
                         // to avoid other MIDI CC #6 messages to be misenterpreted as RPN controller data  
                         pChannel->ResetMidiRpnController();  
1174                          break;                          break;
1175                      }                      }
1176                      case 7: { // volume                      case 7: { // volume
# Line 892  namespace LinuxSampler { Line 1181  namespace LinuxSampler {
1181                      }                      }
1182                      case 10: { // panpot                      case 10: { // panpot
1183                          //TODO: not sample accurate yet                          //TODO: not sample accurate yet
                         pChannel->GlobalPanLeft  = PanCurve[128 - itControlChangeEvent->Param.CC.Value];  
                         pChannel->GlobalPanRight = PanCurve[itControlChangeEvent->Param.CC.Value];  
1184                          pChannel->iLastPanRequest = itControlChangeEvent->Param.CC.Value;                          pChannel->iLastPanRequest = itControlChangeEvent->Param.CC.Value;
1185                          break;                          break;
1186                      }                      }
# Line 970  namespace LinuxSampler { Line 1257  namespace LinuxSampler {
1257                          }                          }
1258                          break;                          break;
1259                      }                      }
1260                        case 98: { // NRPN controller LSB
1261                            dmsg(4,("NRPN LSB %d\n", itControlChangeEvent->Param.CC.Value));
1262                            pEngineChannel->SetMidiNrpnControllerLsb(itControlChangeEvent->Param.CC.Value);
1263                            break;
1264                        }
1265                        case 99: { // NRPN controller MSB
1266                            dmsg(4,("NRPN MSB %d\n", itControlChangeEvent->Param.CC.Value));
1267                            pEngineChannel->SetMidiNrpnControllerMsb(itControlChangeEvent->Param.CC.Value);
1268                            break;
1269                        }
1270                      case 100: { // RPN controller LSB                      case 100: { // RPN controller LSB
1271                            dmsg(4,("RPN LSB %d\n", itControlChangeEvent->Param.CC.Value));
1272                          pEngineChannel->SetMidiRpnControllerLsb(itControlChangeEvent->Param.CC.Value);                          pEngineChannel->SetMidiRpnControllerLsb(itControlChangeEvent->Param.CC.Value);
1273                          break;                          break;
1274                      }                      }
1275                      case 101: { // RPN controller MSB                      case 101: { // RPN controller MSB
1276                            dmsg(4,("RPN MSB %d\n", itControlChangeEvent->Param.CC.Value));
1277                          pEngineChannel->SetMidiRpnControllerMsb(itControlChangeEvent->Param.CC.Value);                          pEngineChannel->SetMidiRpnControllerMsb(itControlChangeEvent->Param.CC.Value);
1278                          break;                          break;
1279                      }                      }
# Line 1065  namespace LinuxSampler { Line 1364  namespace LinuxSampler {
1364                              VoiceIterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();                              VoiceIterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
1365                              VoiceIterator end               = pOtherKey->pActiveVoices->end();                              VoiceIterator end               = pOtherKey->pActiveVoices->end();
1366                              for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {                              for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
1367                                  if (itVoiceToBeKilled->Type != Voice::type_release_trigger)                                  if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))
1368                                      itVoiceToBeKilled->Kill(itNoteOnEventOnKeyList);                                      itVoiceToBeKilled->Kill(itNoteOnEventOnKeyList);
1369                              }                              }
1370                          }                          }
# Line 1077  namespace LinuxSampler { Line 1376  namespace LinuxSampler {
1376                  pChannel->ProcessKeySwitchChange(key);                  pChannel->ProcessKeySwitchChange(key);
1377    
1378                  pKey->KeyPressed = true; // the MIDI key was now pressed down                  pKey->KeyPressed = true; // the MIDI key was now pressed down
1379                    pChannel->KeyDown[key] = true; // just used as built-in %KEY_DOWN script variable
1380                  pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;                  pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;
1381                  pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length                  pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length
1382    
# Line 1097  namespace LinuxSampler { Line 1397  namespace LinuxSampler {
1397                      pKey->pEvents->free(itNoteOnEventOnKeyList);                      pKey->pEvents->free(itNoteOnEventOnKeyList);
1398    
1399                  if (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f) pChannel->PortamentoPos = (float) key;                  if (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f) pChannel->PortamentoPos = (float) key;
1400                  pKey->RoundRobinIndex++;                  if (pKey->pRoundRobinIndex) {
1401                        (*pKey->pRoundRobinIndex)++; // counter specific for the key or region
1402                        pChannel->RoundRobinIndex++; // common counter for the channel
1403                    }
1404                  pChannel->listeners.PostProcessNoteOn(key, vel);                  pChannel->listeners.PostProcessNoteOn(key, vel);
1405              }              }
1406    
# Line 1150  namespace LinuxSampler { Line 1453  namespace LinuxSampler {
1453                  #endif                  #endif
1454    
1455                  pKey->KeyPressed = false; // the MIDI key was now released                  pKey->KeyPressed = false; // the MIDI key was now released
1456                    pChannel->KeyDown[iKey] = false; // just used as built-in %KEY_DOWN script variable
1457    
1458                  // move event to the key's own event list                  // move event to the key's own event list
1459                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
# Line 1199  namespace LinuxSampler { Line 1503  namespace LinuxSampler {
1503                              VoiceIterator itVoiceToBeKilled = pKey->pActiveVoices->first();                              VoiceIterator itVoiceToBeKilled = pKey->pActiveVoices->first();
1504                              VoiceIterator end               = pKey->pActiveVoices->end();                              VoiceIterator end               = pKey->pActiveVoices->end();
1505                              for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {                              for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
1506                                  if (itVoiceToBeKilled->Type != Voice::type_release_trigger)                                  if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))
1507                                      itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);                                      itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);
1508                              }                              }
1509                          }                          }
# Line 1229  namespace LinuxSampler { Line 1533  namespace LinuxSampler {
1533               *  control and status variables. This method is protected by a mutex.               *  control and status variables. This method is protected by a mutex.
1534               */               */
1535              virtual void ResetInternal() {              virtual void ResetInternal() {
1536                  ResetInternalMutex.Lock();                  LockGuard lock(ResetInternalMutex);
1537    
1538                  // make sure that the engine does not get any sysex messages                  // make sure that the engine does not get any sysex messages
1539                  // while it's reseting                  // while it's reseting
# Line 1258  namespace LinuxSampler { Line 1562  namespace LinuxSampler {
1562                  pEventQueue->init();                  pEventQueue->init();
1563                  pSysexBuffer->init();                  pSysexBuffer->init();
1564                  if (sysexDisabled) MidiInputPort::AddSysexListener(this);                  if (sysexDisabled) MidiInputPort::AddSysexListener(this);
                 ResetInternalMutex.Unlock();  
1565              }              }
1566    
1567              /**              /**
# Line 1304  namespace LinuxSampler { Line 1607  namespace LinuxSampler {
1607                  bool                    HandleKeyGroupConflicts                  bool                    HandleKeyGroupConflicts
1608              ) = 0;              ) = 0;
1609    
1610                virtual int GetMinFadeOutSamples() { return MinFadeOutSamples; }
1611    
1612                int InitNewVoice (
1613                    EngineChannelBase<V, R, I>*  pChannel,
1614                    R*                           pRegion,
1615                    Pool<Event>::Iterator&       itNoteOnEvent,
1616                    Voice::type_t                VoiceType,
1617                    int                          iLayer,
1618                    int                          iKeyGroup,
1619                    bool                         ReleaseTriggerVoice,
1620                    bool                         VoiceStealing,
1621                    typename Pool<V>::Iterator&  itNewVoice
1622                ) {
1623                    int key = itNoteOnEvent->Param.Note.Key;
1624                    typename MidiKeyboardManager<V>::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
1625                    if (itNewVoice) {
1626                        // launch the new voice
1627                        if (itNewVoice->Trigger(pChannel, itNoteOnEvent, pChannel->Pitch, pRegion, VoiceType, iKeyGroup) < 0) {
1628                            dmsg(4,("Voice not triggered\n"));
1629                            pKey->pActiveVoices->free(itNewVoice);
1630                        }
1631                        else { // on success
1632                            --VoiceSpawnsLeft;
1633                            if (!pKey->Active) { // mark as active key
1634                                pKey->Active = true;
1635                                pKey->itSelf = pChannel->pActiveKeys->allocAppend();
1636                                *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
1637                            }
1638                            if (itNewVoice->Type & Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)
1639                            return 0; // success
1640                        }
1641                    }
1642                    else if (VoiceStealing) {
1643                        // try to steal one voice
1644                        int result = StealVoice(pChannel, itNoteOnEvent);
1645                        if (!result) { // voice stolen successfully
1646                            // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
1647                            RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
1648                            if (itStealEvent) {
1649                                *itStealEvent = *itNoteOnEvent; // copy event
1650                                itStealEvent->Param.Note.Layer = iLayer;
1651                                itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
1652                                pKey->VoiceTheftsQueued++;
1653                            }
1654                            else dmsg(1,("Voice stealing queue full!\n"));
1655                        }
1656                    }
1657    
1658                    return -1;
1659                }
1660                
1661                /**
1662                 * Checks whether scale tuning setting has been changed since last
1663                 * time this method was called, if yes, it recalculates the pitch
1664                 * for all active voices.
1665                 */
1666                void ProcessScaleTuningChange() {
1667                    const bool changed = ScaleTuningChanged.readAndReset();
1668                    if (!changed) return;
1669                    
1670                    for (int i = 0; i < engineChannels.size(); i++) {
1671                        EngineChannelBase<V, R, I>* channel =
1672                            static_cast<EngineChannelBase<V, R, I>*>(engineChannels[i]);
1673                        channel->OnScaleTuningChanged();
1674                    }
1675                }
1676    
1677          private:          private:
1678              Pool<V>*    pVoicePool;            ///< Contains all voices that can be activated.              Pool<V>*    pVoicePool;            ///< Contains all voices that can be activated.
1679              Pool<RR*>   SuspendedRegions;              Pool<RR*>   SuspendedRegions;

Legend:
Removed from v.2012  
changed lines
  Added in v.2645

  ViewVC Help
Powered by ViewVC