/[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 2935 by schoenebeck, Sun Jul 10 14:24:13 2016 UTC revision 3283 by schoenebeck, Wed Jun 21 20:59:06 2017 UTC
# Line 5  Line 5 
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-2008 Christian Schoenebeck                         *   *   Copyright (C) 2005-2008 Christian Schoenebeck                         *
7   *   Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev        *   *   Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev        *
8   *   Copyright (C) 2012-2016 Christian Schoenebeck and Andreas Persson     *   *   Copyright (C) 2012-2017 Christian Schoenebeck and Andreas Persson     *
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   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 58  namespace LinuxSampler { Line 58  namespace LinuxSampler {
58              typedef typename RTList<RR*>::Iterator RootRegionIterator;              typedef typename RTList<RR*>::Iterator RootRegionIterator;
59              typedef typename MidiKeyboardManager<V>::MidiKey MidiKey;              typedef typename MidiKeyboardManager<V>::MidiKey MidiKey;
60                            
61              EngineBase() : SuspendedRegions(128), noteIDPool(GLOBAL_MAX_NOTES) {              EngineBase() : noteIDPool(GLOBAL_MAX_NOTES), SuspendedRegions(128) {
62                  pDiskThread          = NULL;                  pDiskThread          = NULL;
63                  pNotePool            = new Pool< Note<V> >(GLOBAL_MAX_NOTES);                  pNotePool            = new Pool< Note<V> >(GLOBAL_MAX_NOTES);
64                  pNotePool->setPoolElementIDsReservedBits(INSTR_SCRIPT_EVENT_ID_RESERVED_BITS);                  pNotePool->setPoolElementIDsReservedBits(INSTR_SCRIPT_EVENT_ID_RESERVED_BITS);
# Line 163  namespace LinuxSampler { Line 163  namespace LinuxSampler {
163                                  dmsg(5,("Engine: Sysex received\n"));                                  dmsg(5,("Engine: Sysex received\n"));
164                                  ProcessSysex(itEvent);                                  ProcessSysex(itEvent);
165                                  break;                                  break;
166                                default: ; // noop
167                          }                          }
168                      }                      }
169                  }                  }
# Line 378  namespace LinuxSampler { Line 379  namespace LinuxSampler {
379                  }                  }
380                  pVoicePool->clear();                  pVoicePool->clear();
381    
382                  // (re)create event generator                  // update event generator
383                  if (pEventGenerator) delete pEventGenerator;                  pEventGenerator->SetSampleRate(pAudioOut->SampleRate());
                 pEventGenerator = new EventGenerator(pAudioOut->SampleRate());  
384    
385                  dmsg(1,("Starting disk thread..."));                  dmsg(1,("Starting disk thread..."));
386                  pDiskThread->StartThread();                  pDiskThread->StartThread();
# Line 596  namespace LinuxSampler { Line 596  namespace LinuxSampler {
596              }              }
597    
598              // implementation of abstract method derived from class 'LinuxSampler::RegionPools'              // implementation of abstract method derived from class 'LinuxSampler::RegionPools'
599              virtual Pool<R*>* GetRegionPool(int index) {              virtual Pool<R*>* GetRegionPool(int index) OVERRIDE {
600                  if (index < 0 || index > 1) throw Exception("Index out of bounds");                  if (index < 0 || index > 1) throw Exception("Index out of bounds");
601                  return pRegionPool[index];                  return pRegionPool[index];
602              }              }
# Line 604  namespace LinuxSampler { Line 604  namespace LinuxSampler {
604              // implementation of abstract methods derived from class 'LinuxSampler::NotePool'              // implementation of abstract methods derived from class 'LinuxSampler::NotePool'
605              virtual Pool<V>* GetVoicePool() OVERRIDE { return pVoicePool; }              virtual Pool<V>* GetVoicePool() OVERRIDE { return pVoicePool; }
606              virtual Pool< Note<V> >* GetNotePool() OVERRIDE { return pNotePool; }              virtual Pool< Note<V> >* GetNotePool() OVERRIDE { return pNotePool; }
607              virtual Pool<note_id_t>* GetNodeIDPool() OVERRIDE { return &noteIDPool; }              virtual Pool<note_id_t>* GetNoteIDPool() OVERRIDE { return &noteIDPool; }
608    
609              D* GetDiskThread() { return pDiskThread; }              D* GetDiskThread() { return pDiskThread; }
610    
# Line 675  namespace LinuxSampler { Line 675  namespace LinuxSampler {
675               * @param pNoteOnEvent - event which caused this               * @param pNoteOnEvent - event which caused this
676               * @returns new note's unique ID (or zero on error)               * @returns new note's unique ID (or zero on error)
677               */               */
678              note_id_t LaunchNewNote(LinuxSampler::EngineChannel* pEngineChannel, Event* pNoteOnEvent) OVERRIDE {              note_id_t LaunchNewNote(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) OVERRIDE {
679                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
680                  Pool< Note<V> >* pNotePool = GetNotePool();                  Pool< Note<V> >* pNotePool = GetNotePool();
681    
# Line 689  namespace LinuxSampler { Line 689  namespace LinuxSampler {
689                  NoteIterator itNewNote = pNotePool->allocAppend();                  NoteIterator itNewNote = pNotePool->allocAppend();
690                  const note_id_t newNoteID = pNotePool->getID(itNewNote);                  const note_id_t newNoteID = pNotePool->getID(itNewNote);
691    
692                    // remember the engine's time when this note was triggered exactly
693                    itNewNote->triggerSchedTime = itNoteOnEvent->SchedTime();
694    
695                  // usually the new note (and its subsequent voices) will be                  // usually the new note (and its subsequent voices) will be
696                  // allocated on the key provided by the event's note number,                  // allocated on the key provided by the event's note number,
697                  // however if this new note is requested not to be a regular                  // however if this new note is requested not to be a regular
698                  // note, but rather a child note, then this new note will be                  // note, but rather a child note, then this new note will be
699                  // allocated on the parent note's key instead in order to                  // allocated on the parent note's key instead in order to
700                  // release the child note simultaniously with its parent note                  // release the child note simultaniously with its parent note
701                  itNewNote->hostKey = pNoteOnEvent->Param.Note.Key;                  itNewNote->hostKey = itNoteOnEvent->Param.Note.Key;
702    
703                  // in case this new note was requested to be a child note,                  // in case this new note was requested to be a child note,
704                  // then retrieve its parent note and link them with each other                  // then retrieve its parent note and link them with each other
705                  const note_id_t parentNoteID = pNoteOnEvent->Param.Note.ParentNoteID;                  const note_id_t parentNoteID = itNoteOnEvent->Param.Note.ParentNoteID;
706                  if (parentNoteID) {                  if (parentNoteID) {
707                      NoteIterator itParentNote = pNotePool->fromID(parentNoteID);                                              NoteIterator itParentNote = pNotePool->fromID(parentNoteID);                        
708                      if (itParentNote) {                      if (itParentNote) {
# Line 727  namespace LinuxSampler { Line 730  namespace LinuxSampler {
730                  dmsg(2,("Launched new note on host key %d\n", itNewNote->hostKey));                  dmsg(2,("Launched new note on host key %d\n", itNewNote->hostKey));
731    
732                  // copy event which caused this note                  // copy event which caused this note
733                  itNewNote->cause = *pNoteOnEvent;                  itNewNote->cause = *itNoteOnEvent;
734                  itNewNote->eventID = pEventPool->getID(pNoteOnEvent);                  itNewNote->eventID = pEventPool->getID(itNoteOnEvent);
735                    if (!itNewNote->eventID) {
736                        dmsg(0,("Engine: No valid event ID resolved for note. This is a bug!!!\n"));
737                    }
738    
739                  // move new note to its host key                  // move new note to its host key
740                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNewNote->hostKey];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNewNote->hostKey];
741                  itNewNote.moveToEndOf(pKey->pActiveNotes);                  itNewNote.moveToEndOf(pKey->pActiveNotes);
742    
743                  // assign unique note ID of this new note to the original note on event                  // assign unique note ID of this new note to the original note on event
744                  pNoteOnEvent->Param.Note.ID = newNoteID;                  itNoteOnEvent->Param.Note.ID = newNoteID;
745    
746                  return newNoteID; // success                  return newNoteID; // success
747              }              }
# Line 795  namespace LinuxSampler { Line 801  namespace LinuxSampler {
801                              case Event::type_note_pressure:                              case Event::type_note_pressure:
802                                  //TODO: ...                                  //TODO: ...
803                                  break;                                  break;
804    
805                                case Event::type_sysex:
806                                    //TODO: ...
807                                    break;
808    
809                                case Event::type_cancel_release_key:
810                                case Event::type_release_key:
811                                case Event::type_release_note:
812                                case Event::type_play_note:
813                                case Event::type_stop_note:
814                                case Event::type_kill_note:
815                                case Event::type_note_synth_param:
816                                    break; // noop
817                          }                          }
818    
819                          // see HACK comment above                          // see HACK comment above
# Line 853  namespace LinuxSampler { Line 872  namespace LinuxSampler {
872                                  dmsg(5,("Engine: Note on received\n"));                                  dmsg(5,("Engine: Note on received\n"));
873                                  ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);
874                                  break;                                  break;
875                                case Event::type_play_note:
876                                    dmsg(5,("Engine: Play Note received\n"));
877                                    ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);
878                                    break;
879                              case Event::type_note_off:                              case Event::type_note_off:
880                                  dmsg(5,("Engine: Note off received\n"));                                  dmsg(5,("Engine: Note off received\n"));
881                                  ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);
882                                  break;                                  break;
883                                case Event::type_stop_note:
884                                    dmsg(5,("Engine: Stop Note received\n"));
885                                    ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);
886                                    break;
887                                case Event::type_kill_note:
888                                    dmsg(5,("Engine: Kill Note received\n"));
889                                    ProcessKillNote((EngineChannel*)itEvent->pEngineChannel, itEvent);
890                                    break;
891                              case Event::type_control_change:                              case Event::type_control_change:
892                                  dmsg(5,("Engine: MIDI CC received\n"));                                  dmsg(5,("Engine: MIDI CC received\n"));
893                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);
# Line 877  namespace LinuxSampler { Line 908  namespace LinuxSampler {
908                                  dmsg(5,("Engine: Note Synth Param received\n"));                                  dmsg(5,("Engine: Note Synth Param received\n"));
909                                  ProcessNoteSynthParam(itEvent->pEngineChannel, itEvent);                                  ProcessNoteSynthParam(itEvent->pEngineChannel, itEvent);
910                                  break;                                  break;
911                                case Event::type_sysex:
912                                    break; // TODO ...
913    
914                                case Event::type_cancel_release_key:
915                                case Event::type_release_key:
916                                case Event::type_release_note:
917                                    break; // noop
918                          }                          }
919                      }                      }
920                  }                  }
# Line 945  namespace LinuxSampler { Line 983  namespace LinuxSampler {
983                      // script event object                      // script event object
984                      RTList<ScriptEvent>::Iterator itScriptEvent =                      RTList<ScriptEvent>::Iterator itScriptEvent =
985                          pChannel->pScript->pEvents->allocAppend();                          pChannel->pScript->pEvents->allocAppend();
986                        // if event handler uses polyphonic variables, reset them
987                        // to zero values before starting to execute the handler
988                        if (pEventHandler->isPolyphonic())
989                            itScriptEvent->execCtx->resetPolyphonicData();
990                      ProcessScriptEvent(                      ProcessScriptEvent(
991                          pChannel, itEvent, pEventHandler, itScriptEvent                          pChannel, itEvent, pEventHandler, itScriptEvent
992                      );                      );
# Line 977  namespace LinuxSampler { Line 1019  namespace LinuxSampler {
1019    
1020                  // initialize/reset other members                  // initialize/reset other members
1021                  itScriptEvent->cause = *itEvent;                  itScriptEvent->cause = *itEvent;
1022                    itScriptEvent->scheduleTime = itEvent->SchedTime();
1023                  itScriptEvent->currentHandler = 0;                  itScriptEvent->currentHandler = 0;
1024                  itScriptEvent->executionSlices = 0;                  itScriptEvent->executionSlices = 0;
1025                    itScriptEvent->ignoreAllWaitCalls = false;
1026                    itScriptEvent->handlerType = pEventHandler->eventHandlerType();
1027                  // this is the native representation of the $EVENT_ID script variable                  // this is the native representation of the $EVENT_ID script variable
1028                  itScriptEvent->id =                  itScriptEvent->id =
1029                      (itEvent->Type == Event::type_note_on)                      (itEvent->Type == Event::type_note_on)
# Line 1246  namespace LinuxSampler { Line 1291  namespace LinuxSampler {
1291                          // the script's "init" event handler is only executed                          // the script's "init" event handler is only executed
1292                          // once (when the script is loaded or reloaded)                          // once (when the script is loaded or reloaded)
1293                          if (pEngineChannel->pScript && pEngineChannel->pScript->handlerInit) {                          if (pEngineChannel->pScript && pEngineChannel->pScript->handlerInit) {
1294                                dmsg(5,("Engine: exec handlerInit %p\n", pEngineChannel->pScript->handlerInit));
1295                              RTList<ScriptEvent>::Iterator itScriptEvent =                              RTList<ScriptEvent>::Iterator itScriptEvent =
1296                                  pEngineChannel->pScript->pEvents->allocAppend();                                  pEngineChannel->pScript->pEvents->allocAppend();
1297    
1298                                itScriptEvent->cause = pEventGenerator->CreateEvent(0);
1299                                itScriptEvent->cause.Type = (Event::type_t) -1; // some invalid type to avoid random event processing
1300                              itScriptEvent->cause.pEngineChannel = pEngineChannel;                              itScriptEvent->cause.pEngineChannel = pEngineChannel;
1301                                itScriptEvent->cause.pMidiInputPort = pEngineChannel->GetMidiInputPort();
1302                              itScriptEvent->handlers[0] = pEngineChannel->pScript->handlerInit;                              itScriptEvent->handlers[0] = pEngineChannel->pScript->handlerInit;
1303                              itScriptEvent->handlers[1] = NULL;                              itScriptEvent->handlers[1] = NULL;
1304                                itScriptEvent->currentHandler = 0;
1305                              VMExecStatus_t res = pScriptVM->exec(                              itScriptEvent->executionSlices = 0;
1306                                  pEngineChannel->pScript->parserContext, &*itScriptEvent                              itScriptEvent->ignoreAllWaitCalls = false;
1307                              );                              itScriptEvent->handlerType = VM_EVENT_HANDLER_INIT;
1308    
1309                                VMExecStatus_t res;
1310                                size_t instructionsCount = 0;
1311                                const size_t maxInstructions = 200000; // aiming approx. 1 second max. (based on very roughly 5us / instruction)
1312                                bool bWarningShown = false;
1313                                do {
1314                                    res = pScriptVM->exec(
1315                                        pEngineChannel->pScript->parserContext, &*itScriptEvent
1316                                    );
1317                                    instructionsCount += itScriptEvent->execCtx->instructionsPerformed();
1318                                    if (instructionsCount > maxInstructions && !bWarningShown) {
1319                                        bWarningShown = true;
1320                                        dmsg(0,("[ScriptVM] WARNING: \"init\" event handler of instrument script executing for long time!\n"));
1321                                    }
1322                                } while (res & VM_EXEC_SUSPENDED && !(res & VM_EXEC_ERROR));
1323    
1324                              pEngineChannel->pScript->pEvents->free(itScriptEvent);                              pEngineChannel->pScript->pEvents->free(itScriptEvent);
1325                          }                          }
# Line 1316  namespace LinuxSampler { Line 1380  namespace LinuxSampler {
1380                          // usually there should already be a new Note object                          // usually there should already be a new Note object
1381                          NoteIterator itNote = GetNotePool()->fromID(itVoiceStealEvent->Param.Note.ID);                          NoteIterator itNote = GetNotePool()->fromID(itVoiceStealEvent->Param.Note.ID);
1382                          if (!itNote) { // should not happen, but just to be sure ...                          if (!itNote) { // should not happen, but just to be sure ...
1383                              const note_id_t noteID = LaunchNewNote(pEngineChannel, &*itVoiceStealEvent);                              const note_id_t noteID = LaunchNewNote(pEngineChannel, itVoiceStealEvent);
1384                              if (!noteID) {                              if (!noteID) {
1385                                  dmsg(1,("Engine: Voice stealing failed; No Note object and Note pool empty!\n"));                                  dmsg(1,("Engine: Voice stealing failed; No Note object and Note pool empty!\n"));
1386                                  continue;                                  continue;
# Line 1593  namespace LinuxSampler { Line 1657  namespace LinuxSampler {
1657               *  @param pEngineChannel - engine channel on which this event occurred on               *  @param pEngineChannel - engine channel on which this event occurred on
1658               *  @param itNoteOnEvent - key, velocity and time stamp of the event               *  @param itNoteOnEvent - key, velocity and time stamp of the event
1659               */               */
1660              virtual void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {              virtual void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) OVERRIDE {
1661                  EngineChannelBase<V, R, I>* pChannel =                  EngineChannelBase<V, R, I>* pChannel =
1662                          static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                          static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1663    
# Line 1603  namespace LinuxSampler { Line 1667  namespace LinuxSampler {
1667    
1668                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
1669    
1670                  pChannel->listeners.PreProcessNoteOn(key, vel);                  // There are real MIDI note-on events (Event::type_note_on) and
1671                    // programmatically spawned notes (Event::type_play_note). We have
1672                    // to distinguish between them, since certain processing below
1673                    // must only be done on real MIDI note-on events (i.e. for
1674                    // correctly updating which MIDI keys are currently pressed down).
1675                    const bool isRealMIDINoteOnEvent = itNoteOnEvent->Type == Event::type_note_on;
1676    
1677                    if (isRealMIDINoteOnEvent)
1678                        pChannel->listeners.PreProcessNoteOn(key, vel);
1679    
1680                  #if !CONFIG_PROCESS_MUTED_CHANNELS                  #if !CONFIG_PROCESS_MUTED_CHANNELS
1681                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted
1682                      pChannel->listeners.PostProcessNoteOn(key, vel);                      if (isRealMIDINoteOnEvent)
1683                            pChannel->listeners.PostProcessNoteOn(key, vel);
1684                      return;                      return;
1685                  }                  }
1686                  #endif                  #endif
1687    
1688                  if (!pChannel->pInstrument) {                  if (!pChannel->pInstrument) {
1689                      pChannel->listeners.PostProcessNoteOn(key, vel);                      if (isRealMIDINoteOnEvent)
1690                            pChannel->listeners.PostProcessNoteOn(key, vel);
1691                      return; // ignore if no instrument loaded                      return; // ignore if no instrument loaded
1692                  }                  }
1693    
# Line 1620  namespace LinuxSampler { Line 1695  namespace LinuxSampler {
1695                  RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);                  RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
1696    
1697                  // if Solo Mode then kill all already active voices                  // if Solo Mode then kill all already active voices
1698                  if (pChannel->SoloMode) {                  if (pChannel->SoloMode && isRealMIDINoteOnEvent) {
1699                      Pool<uint>::Iterator itYoungestKey = pChannel->pActiveKeys->last();                      Pool<uint>::Iterator itYoungestKey = pChannel->pActiveKeys->last();
1700                      if (itYoungestKey) {                      if (itYoungestKey) {
1701                          const int iYoungestKey = *itYoungestKey;                          const int iYoungestKey = *itYoungestKey;
# Line 1649  namespace LinuxSampler { Line 1724  namespace LinuxSampler {
1724                      pChannel->SoloKey = key;                      pChannel->SoloKey = key;
1725                  }                  }
1726    
1727                  pChannel->ProcessKeySwitchChange(key);                  if (isRealMIDINoteOnEvent) {
1728                        pChannel->ProcessKeySwitchChange(key);
1729    
1730                  pKey->KeyPressed = true; // the MIDI key was now pressed down                      pKey->KeyPressed = true; // the MIDI key was now pressed down
1731                  pChannel->KeyDown[key] = true; // just used as built-in %KEY_DOWN script variable                      pChannel->KeyDown[key] = true; // just used as built-in %KEY_DOWN script variable
1732                  pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;                      pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;
1733                  pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length                      pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length
1734                    }
1735    
1736                  // cancel release process of voices on this key if needed                  // cancel release process of voices on this key if needed
1737                  if (pKey->Active && !pChannel->SustainPedal) {                  if (pKey->Active && !pChannel->SustainPedal && isRealMIDINoteOnEvent) {
1738                      RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();                      RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
1739                      if (itCancelReleaseEvent) {                      if (itCancelReleaseEvent) {
1740                          *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event                          *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event
1741                          itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                          itCancelReleaseEvent->Type = Event::type_cancel_release_key; // transform event type
1742                      }                      }
1743                      else dmsg(1,("Event pool emtpy!\n"));                      else dmsg(1,("Event pool emtpy!\n"));
1744                  }                  }
# Line 1672  namespace LinuxSampler { Line 1749  namespace LinuxSampler {
1749                  if (!pKey->Active && !pKey->VoiceTheftsQueued)                  if (!pKey->Active && !pKey->VoiceTheftsQueued)
1750                      pKey->pEvents->free(itNoteOnEventOnKeyList);                      pKey->pEvents->free(itNoteOnEventOnKeyList);
1751    
1752                  if (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f) pChannel->PortamentoPos = (float) key;                  if (isRealMIDINoteOnEvent && (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f))
1753                        pChannel->PortamentoPos = (float) key;
1754    
1755                    //NOTE: Hmm, I guess its a matter of taste whether round robin should be advanced only on real MIDI note-on events, isn't it?
1756                  if (pKey->pRoundRobinIndex) {                  if (pKey->pRoundRobinIndex) {
1757                      (*pKey->pRoundRobinIndex)++; // counter specific for the key or region                      (*pKey->pRoundRobinIndex)++; // counter specific for the key or region
1758                      pChannel->RoundRobinIndex++; // common counter for the channel                      pChannel->RoundRobinIndex++; // common counter for the channel
1759                  }                  }
1760                  pChannel->listeners.PostProcessNoteOn(key, vel);  
1761                    if (isRealMIDINoteOnEvent)
1762                        pChannel->listeners.PostProcessNoteOn(key, vel);
1763              }              }
1764    
1765              /**              /**
# Line 1706  namespace LinuxSampler { Line 1788  namespace LinuxSampler {
1788               *  @param pEngineChannel - engine channel on which this event occurred on               *  @param pEngineChannel - engine channel on which this event occurred on
1789               *  @param itNoteOffEvent - key, velocity and time stamp of the event               *  @param itNoteOffEvent - key, velocity and time stamp of the event
1790               */               */
1791              virtual void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {              virtual void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) OVERRIDE {
1792                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1793    
1794                  const int iKey = itNoteOffEvent->Param.Note.Key;                  const int iKey = itNoteOffEvent->Param.Note.Key;
# Line 1715  namespace LinuxSampler { Line 1797  namespace LinuxSampler {
1797    
1798                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[iKey];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[iKey];
1799    
1800                  pChannel->listeners.PreProcessNoteOff(iKey, vel);                  // There are real MIDI note-off events (Event::type_note_off) and
1801                    // programmatically spawned notes (Event::type_stop_note). We have
1802                    // to distinguish between them, since certain processing below
1803                    // must only be done on real MIDI note-off events (i.e. for
1804                    // correctly updating which MIDI keys are currently pressed down),
1805                    // plus a stop-note event just releases voices of one particular
1806                    // note, whereas a note-off event releases all voices on a
1807                    // particular MIDI key instead.
1808                    const bool isRealMIDINoteOffEvent = itNoteOffEvent->Type == Event::type_note_off;
1809    
1810                    if (isRealMIDINoteOffEvent)
1811                        pChannel->listeners.PreProcessNoteOff(iKey, vel);
1812    
1813                  #if !CONFIG_PROCESS_MUTED_CHANNELS                  #if !CONFIG_PROCESS_MUTED_CHANNELS
1814                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted
1815                      pChannel->listeners.PostProcessNoteOff(iKey, vel);                      if (isRealMIDINoteOffEvent)
1816                            pChannel->listeners.PostProcessNoteOff(iKey, vel);
1817                      return;                      return;
1818                  }                  }
1819                  #endif                  #endif
1820    
1821                  pKey->KeyPressed = false; // the MIDI key was now released                  if (isRealMIDINoteOffEvent) {
1822                  pChannel->KeyDown[iKey] = false; // just used as built-in %KEY_DOWN script variable                      pKey->KeyPressed = false; // the MIDI key was now released
1823                        pChannel->KeyDown[iKey] = false; // just used as built-in %KEY_DOWN script variable
1824                    }
1825    
1826                  // move event to the key's own event list                  // move event to the key's own event list
1827                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
1828    
1829                  bool bShouldRelease = pKey->Active && pChannel->ShouldReleaseVoice(itNoteOffEventOnKeyList->Param.Note.Key);                  if (isRealMIDINoteOffEvent) {
1830                        bool bShouldRelease = pKey->Active && pChannel->ShouldReleaseVoice(itNoteOffEventOnKeyList->Param.Note.Key);
1831    
1832                  // in case Solo Mode is enabled, kill all voices on this key and respawn a voice on the highest pressed key (if any)                      // in case Solo Mode is enabled, kill all voices on this key and respawn a voice on the highest pressed key (if any)
1833                  if (pChannel->SoloMode && pChannel->pInstrument) { //TODO: this feels like too much code just for handling solo mode :P                      if (pChannel->SoloMode && pChannel->pInstrument) { //TODO: this feels like too much code just for handling solo mode :P
1834                      bool bOtherKeysPressed = false;                          bool bOtherKeysPressed = false;
1835                      if (iKey == pChannel->SoloKey) {                          if (iKey == pChannel->SoloKey) {
1836                          pChannel->SoloKey = -1;                              pChannel->SoloKey = -1;
1837                          // if there's still a key pressed down, respawn a voice (group) on the highest key                              // if there's still a key pressed down, respawn a voice (group) on the highest key
1838                          for (int i = 127; i > 0; i--) {                              for (int i = 127; i > 0; i--) {
1839                              MidiKey* pOtherKey = &pChannel->pMIDIKeyInfo[i];                                  MidiKey* pOtherKey = &pChannel->pMIDIKeyInfo[i];
1840                              if (pOtherKey->KeyPressed) {                                  if (pOtherKey->KeyPressed) {
1841                                  bOtherKeysPressed = true;                                      bOtherKeysPressed = true;
1842                                  // make the other key the new 'currently active solo key'                                      // make the other key the new 'currently active solo key'
1843                                  pChannel->SoloKey = i;                                      pChannel->SoloKey = i;
1844                                  // get final portamento position of currently active voice                                      // get final portamento position of currently active voice
1845                                  if (pChannel->PortamentoMode) {                                      if (pChannel->PortamentoMode) {
1846                                      NoteIterator itNote = pKey->pActiveNotes->first();                                          NoteIterator itNote = pKey->pActiveNotes->first();
1847                                      VoiceIterator itVoice = itNote->pActiveVoices->first();                                          VoiceIterator itVoice = itNote->pActiveVoices->first();
1848                                      if (itVoice) itVoice->UpdatePortamentoPos(itNoteOffEventOnKeyList);                                          if (itVoice) itVoice->UpdatePortamentoPos(itNoteOffEventOnKeyList);
                                 }  
                                 // create a pseudo note on event  
                                 RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend();  
                                 if (itPseudoNoteOnEvent) {  
                                     // copy event  
                                     *itPseudoNoteOnEvent = *itNoteOffEventOnKeyList;  
                                     // transform event to a note on event  
                                     itPseudoNoteOnEvent->Type                = Event::type_note_on;  
                                     itPseudoNoteOnEvent->Param.Note.Key      = i;  
                                     itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity;  
                                     // assign a new note to this note-on event  
                                     if (LaunchNewNote(pChannel, &*itPseudoNoteOnEvent)) {  
                                         // allocate and trigger new voice(s) for the other key  
                                         TriggerNewVoices(pChannel, itPseudoNoteOnEvent, false);  
1849                                      }                                      }
1850                                      // if neither a voice was spawned or postponed then remove note on event from key again                                      // create a pseudo note on event
1851                                      if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)                                      RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend();
1852                                          pOtherKey->pEvents->free(itPseudoNoteOnEvent);                                      if (itPseudoNoteOnEvent) {
1853                                            // copy event
1854                                            *itPseudoNoteOnEvent = *itNoteOffEventOnKeyList;
1855                                            // transform event to a note on event
1856                                            itPseudoNoteOnEvent->Type                = Event::type_note_on; //FIXME: should probably use Event::type_play_note instead (to avoid i.e. hanging notes)
1857                                            itPseudoNoteOnEvent->Param.Note.Key      = i;
1858                                            itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity;
1859                                            // assign a new note to this note-on event
1860                                            if (LaunchNewNote(pChannel, itPseudoNoteOnEvent)) {
1861                                                // allocate and trigger new voice(s) for the other key
1862                                                TriggerNewVoices(pChannel, itPseudoNoteOnEvent, false);
1863                                            }
1864                                            // if neither a voice was spawned or postponed then remove note on event from key again
1865                                            if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)
1866                                                pOtherKey->pEvents->free(itPseudoNoteOnEvent);
1867    
1868                                  } else dmsg(1,("Could not respawn voice, no free event left\n"));                                      } else dmsg(1,("Could not respawn voice, no free event left\n"));
1869                                  break; // done                                      break; // done
1870                                    }
1871                              }                              }
1872                          }                          }
1873                      }                          if (bOtherKeysPressed) {
1874                      if (bOtherKeysPressed) {                              if (pKey->Active) { // kill all voices on this key
1875                          if (pKey->Active) { // kill all voices on this key                                  bShouldRelease = false; // no need to release, as we kill it here
1876                              bShouldRelease = false; // no need to release, as we kill it here                                  for (NoteIterator itNote = pKey->pActiveNotes->first(); itNote; ++itNote) {
1877                              for (NoteIterator itNote = pKey->pActiveNotes->first(); itNote; ++itNote) {                                      VoiceIterator itVoiceToBeKilled = itNote->pActiveVoices->first();
1878                                  VoiceIterator itVoiceToBeKilled = itNote->pActiveVoices->first();                                      VoiceIterator end               = itNote->pActiveVoices->end();
1879                                  VoiceIterator end               = itNote->pActiveVoices->end();                                      for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
1880                                  for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {                                          if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))
1881                                      if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))                                              itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);
1882                                          itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);                                      }
1883                                  }                                  }
1884                              }                              }
1885                          }                          } else pChannel->PortamentoPos = -1.0f;
1886                      } else pChannel->PortamentoPos = -1.0f;                      }
                 }  
1887    
1888                  // if no solo mode (the usual case) or if solo mode and no other key pressed, then release voices on this key if needed                      // if no solo mode (the usual case) or if solo mode and no other key pressed, then release voices on this key if needed
1889                  if (bShouldRelease) {                      if (bShouldRelease) {
1890                      itNoteOffEventOnKeyList->Type = Event::type_release; // transform event type                          itNoteOffEventOnKeyList->Type = Event::type_release_key; // transform event type
1891                      // spawn release triggered voice(s) if needed                          // spawn release triggered voice(s) if needed
1892                      ProcessReleaseTrigger(pChannel, itNoteOffEventOnKeyList, pKey);                          ProcessReleaseTrigger(pChannel, itNoteOffEventOnKeyList, pKey);
1893                        }
1894                    } else if (itNoteOffEventOnKeyList->Type == Event::type_stop_note) {
1895                        // This programmatically caused event is caused by a call to
1896                        // the built-in instrument script function note_off(). In
1897                        // contrast to a real MIDI note-off event the stop-note
1898                        // event just intends to release voices of one particular note.
1899                        NoteBase* pNote = pChannel->pEngine->NoteByID( itNoteOffEventOnKeyList->Param.Note.ID );
1900                        if (pNote) { // the requested note is still alive ...
1901                            itNoteOffEventOnKeyList->Type = Event::type_release_note; // transform event type
1902                        } else { // note is dead and gone ..
1903                            pKey->pEvents->free(itNoteOffEventOnKeyList); // remove stop-note event from key again
1904                            return; // prevent event to be removed a 2nd time below
1905                        }
1906                  }                  }
1907    
1908                  // if neither a voice was spawned or postponed on this key then remove note off event from key again                  // if neither a voice was spawned or postponed on this key then remove note off event from key again
1909                  if (!pKey->Active && !pKey->VoiceTheftsQueued)                  if (!pKey->Active && !pKey->VoiceTheftsQueued)
1910                      pKey->pEvents->free(itNoteOffEventOnKeyList);                      pKey->pEvents->free(itNoteOffEventOnKeyList);
1911    
1912                  pChannel->listeners.PostProcessNoteOff(iKey, vel);                  if (isRealMIDINoteOffEvent)
1913                        pChannel->listeners.PostProcessNoteOff(iKey, vel);
1914              }              }
1915    
1916              /**              /**
# Line 1833  namespace LinuxSampler { Line 1944  namespace LinuxSampler {
1944                  // spawn release triggered voice(s) if needed                  // spawn release triggered voice(s) if needed
1945                  if (pKey->ReleaseTrigger && pChannel->pInstrument) {                  if (pKey->ReleaseTrigger && pChannel->pInstrument) {
1946                      // assign a new note to this release event                      // assign a new note to this release event
1947                      if (LaunchNewNote(pChannel, &*itEvent)) {                      if (LaunchNewNote(pChannel, itEvent)) {
1948                          // allocate and trigger new release voice(s)                          // allocate and trigger new release voice(s)
1949                          TriggerReleaseVoices(pChannel, itEvent);                          TriggerReleaseVoices(pChannel, itEvent);
1950                      }                      }
# Line 1842  namespace LinuxSampler { Line 1953  namespace LinuxSampler {
1953              }              }
1954    
1955              /**              /**
1956                 * Called on "kill note" events, which currently only happens on
1957                 * built-in real-time instrument script function fade_out(). This
1958                 * method only fulfills one task: moving the even to the Note's own
1959                 * event list so that its voices can process the kill event sample
1960                 * accurately.
1961                 */
1962                void ProcessKillNote(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) {
1963                    EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1964    
1965                    NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.Note.ID );
1966                    if (!pNote || pNote->hostKey < 0 || pNote->hostKey >= 128) return;
1967    
1968                    // move note kill event to its MIDI key
1969                    MidiKey* pKey = &pChannel->pMIDIKeyInfo[pNote->hostKey];
1970                    itEvent.moveToEndOf(pKey->pEvents);
1971                }
1972    
1973                /**
1974               * Called on note synthesis parameter change events. These are               * Called on note synthesis parameter change events. These are
1975               * internal events caused by calling built-in real-time instrument               * internal events caused by calling built-in real-time instrument
1976               * script functions like change_vol(), change_pitch(), etc.               * script functions like change_vol(), change_tune(), etc.
1977               *               *
1978               * This method performs two tasks:               * This method performs two tasks:
1979               *               *
# Line 1859  namespace LinuxSampler { Line 1988  namespace LinuxSampler {
1988               * @param pEngineChannel - engine channel on which this event occurred on               * @param pEngineChannel - engine channel on which this event occurred on
1989               * @param itEvent - note synthesis parameter change event               * @param itEvent - note synthesis parameter change event
1990               */               */
1991              virtual void ProcessNoteSynthParam(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) OVERRIDE {              virtual void ProcessNoteSynthParam(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) {
1992                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1993    
1994                  NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.NoteSynthParam.NoteID );                  NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.NoteSynthParam.NoteID );
# Line 1875  namespace LinuxSampler { Line 2004  namespace LinuxSampler {
2004                              pNote->Override.Volume = itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Volume = itEvent->Param.NoteSynthParam.Delta;
2005                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Volume;                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Volume;
2006                          break;                          break;
2007                        case Event::synth_param_volume_time:
2008                            pNote->Override.VolumeTime = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2009                            break;
2010                        case Event::synth_param_volume_curve:
2011                            itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2012                            pNote->Override.VolumeCurve = (fade_curve_t) itEvent->Param.NoteSynthParam.AbsValue;
2013                            break;
2014                      case Event::synth_param_pitch:                      case Event::synth_param_pitch:
2015                          if (relative)                          if (relative)
2016                              pNote->Override.Pitch *= itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Pitch *= itEvent->Param.NoteSynthParam.Delta;
# Line 1882  namespace LinuxSampler { Line 2018  namespace LinuxSampler {
2018                              pNote->Override.Pitch = itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Pitch = itEvent->Param.NoteSynthParam.Delta;
2019                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pitch;                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pitch;
2020                          break;                          break;
2021                        case Event::synth_param_pitch_time:
2022                            pNote->Override.PitchTime = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2023                            break;
2024                        case Event::synth_param_pitch_curve:
2025                            itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2026                            pNote->Override.PitchCurve = (fade_curve_t) itEvent->Param.NoteSynthParam.AbsValue;
2027                            break;
2028                      case Event::synth_param_pan:                      case Event::synth_param_pan:
2029                          if (relative) {                          if (relative) {
2030                              pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, itEvent->Param.NoteSynthParam.Delta, ++pNote->Override.PanSources);                              pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, itEvent->Param.NoteSynthParam.Delta, ++pNote->Override.PanSources);
# Line 1897  namespace LinuxSampler { Line 2040  namespace LinuxSampler {
2040                      case Event::synth_param_resonance:                      case Event::synth_param_resonance:
2041                          pNote->Override.Resonance = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;                          pNote->Override.Resonance = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2042                          break;                          break;
2043                        case Event::synth_param_attack:
2044                            pNote->Override.Attack = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2045                            break;
2046                        case Event::synth_param_decay:
2047                            pNote->Override.Decay = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2048                            break;
2049                        case Event::synth_param_release:
2050                            pNote->Override.Release = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2051                            break;
2052                        case Event::synth_param_amp_lfo_depth:
2053                            pNote->Override.AmpLFODepth = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2054                            break;
2055                        case Event::synth_param_amp_lfo_freq:
2056                            pNote->Override.AmpLFOFreq = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2057                            break;
2058                        case Event::synth_param_pitch_lfo_depth:
2059                            pNote->Override.PitchLFODepth = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2060                            break;
2061                        case Event::synth_param_pitch_lfo_freq:
2062                            pNote->Override.PitchLFOFreq = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2063                            break;
2064                  }                  }
2065    
2066                  // move note parameter event to its MIDI key                  // move note parameter event to its MIDI key
# Line 1908  namespace LinuxSampler { Line 2072  namespace LinuxSampler {
2072               *  Reset all voices and disk thread and clear input event queue and all               *  Reset all voices and disk thread and clear input event queue and all
2073               *  control and status variables. This method is protected by a mutex.               *  control and status variables. This method is protected by a mutex.
2074               */               */
2075              virtual void ResetInternal() {              virtual void ResetInternal() OVERRIDE {
2076                  LockGuard lock(ResetInternalMutex);                  LockGuard lock(ResetInternalMutex);
2077    
2078                  // make sure that the engine does not get any sysex messages                  // make sure that the engine does not get any sysex messages
# Line 1967  namespace LinuxSampler { Line 2131  namespace LinuxSampler {
2131               * @param pEngineChannel - engine channel on which all voices should be killed               * @param pEngineChannel - engine channel on which all voices should be killed
2132               * @param itKillEvent    - event which caused this killing of all voices               * @param itKillEvent    - event which caused this killing of all voices
2133               */               */
2134              virtual void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent) {              virtual void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent) OVERRIDE {
2135                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
2136                  int count = pChannel->KillAllVoices(itKillEvent);                  int count = pChannel->KillAllVoices(itKillEvent);
2137                  VoiceSpawnsLeft -= count; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead                  VoiceSpawnsLeft -= count; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead
# Line 2002  namespace LinuxSampler { Line 2166  namespace LinuxSampler {
2166                  bool                    HandleKeyGroupConflicts                  bool                    HandleKeyGroupConflicts
2167              ) = 0;              ) = 0;
2168    
2169              virtual int GetMinFadeOutSamples() { return MinFadeOutSamples; }              virtual int GetMinFadeOutSamples() OVERRIDE { return MinFadeOutSamples; }
2170    
2171              int InitNewVoice (              int InitNewVoice (
2172                  EngineChannelBase<V, R, I>*  pChannel,                  EngineChannelBase<V, R, I>*  pChannel,

Legend:
Removed from v.2935  
changed lines
  Added in v.3283

  ViewVC Help
Powered by ViewVC