/[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 2931 by schoenebeck, Sat Jul 9 14:38:33 2016 UTC revision 3205 by schoenebeck, Wed May 24 20:05:38 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 596  namespace LinuxSampler { Line 597  namespace LinuxSampler {
597              }              }
598    
599              // implementation of abstract method derived from class 'LinuxSampler::RegionPools'              // implementation of abstract method derived from class 'LinuxSampler::RegionPools'
600              virtual Pool<R*>* GetRegionPool(int index) {              virtual Pool<R*>* GetRegionPool(int index) OVERRIDE {
601                  if (index < 0 || index > 1) throw Exception("Index out of bounds");                  if (index < 0 || index > 1) throw Exception("Index out of bounds");
602                  return pRegionPool[index];                  return pRegionPool[index];
603              }              }
# Line 604  namespace LinuxSampler { Line 605  namespace LinuxSampler {
605              // implementation of abstract methods derived from class 'LinuxSampler::NotePool'              // implementation of abstract methods derived from class 'LinuxSampler::NotePool'
606              virtual Pool<V>* GetVoicePool() OVERRIDE { return pVoicePool; }              virtual Pool<V>* GetVoicePool() OVERRIDE { return pVoicePool; }
607              virtual Pool< Note<V> >* GetNotePool() OVERRIDE { return pNotePool; }              virtual Pool< Note<V> >* GetNotePool() OVERRIDE { return pNotePool; }
608              virtual Pool<note_id_t>* GetNodeIDPool() OVERRIDE { return &noteIDPool; }              virtual Pool<note_id_t>* GetNoteIDPool() OVERRIDE { return &noteIDPool; }
609    
610              D* GetDiskThread() { return pDiskThread; }              D* GetDiskThread() { return pDiskThread; }
611    
# Line 675  namespace LinuxSampler { Line 676  namespace LinuxSampler {
676               * @param pNoteOnEvent - event which caused this               * @param pNoteOnEvent - event which caused this
677               * @returns new note's unique ID (or zero on error)               * @returns new note's unique ID (or zero on error)
678               */               */
679              note_id_t LaunchNewNote(LinuxSampler::EngineChannel* pEngineChannel, Event* pNoteOnEvent) OVERRIDE {              note_id_t LaunchNewNote(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) OVERRIDE {
680                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
681                  Pool< Note<V> >* pNotePool = GetNotePool();                  Pool< Note<V> >* pNotePool = GetNotePool();
682    
# Line 689  namespace LinuxSampler { Line 690  namespace LinuxSampler {
690                  NoteIterator itNewNote = pNotePool->allocAppend();                  NoteIterator itNewNote = pNotePool->allocAppend();
691                  const note_id_t newNoteID = pNotePool->getID(itNewNote);                  const note_id_t newNoteID = pNotePool->getID(itNewNote);
692    
693                    // remember the engine's time when this note was triggered exactly
694                    itNewNote->triggerSchedTime = itNoteOnEvent->SchedTime();
695    
696                  // usually the new note (and its subsequent voices) will be                  // usually the new note (and its subsequent voices) will be
697                  // allocated on the key provided by the event's note number,                  // allocated on the key provided by the event's note number,
698                  // however if this new note is requested not to be a regular                  // however if this new note is requested not to be a regular
699                  // note, but rather a child note, then this new note will be                  // note, but rather a child note, then this new note will be
700                  // allocated on the parent note's key instead in order to                  // allocated on the parent note's key instead in order to
701                  // release the child note simultaniously with its parent note                  // release the child note simultaniously with its parent note
702                  itNewNote->hostKey = pNoteOnEvent->Param.Note.Key;                  itNewNote->hostKey = itNoteOnEvent->Param.Note.Key;
703    
704                  // in case this new note was requested to be a child note,                  // in case this new note was requested to be a child note,
705                  // then retrieve its parent note and link them with each other                  // then retrieve its parent note and link them with each other
706                  const note_id_t parentNoteID = pNoteOnEvent->Param.Note.ParentNoteID;                  const note_id_t parentNoteID = itNoteOnEvent->Param.Note.ParentNoteID;
707                  if (parentNoteID) {                  if (parentNoteID) {
708                      NoteIterator itParentNote = pNotePool->fromID(parentNoteID);                                              NoteIterator itParentNote = pNotePool->fromID(parentNoteID);                        
709                      if (itParentNote) {                      if (itParentNote) {
# Line 727  namespace LinuxSampler { Line 731  namespace LinuxSampler {
731                  dmsg(2,("Launched new note on host key %d\n", itNewNote->hostKey));                  dmsg(2,("Launched new note on host key %d\n", itNewNote->hostKey));
732    
733                  // copy event which caused this note                  // copy event which caused this note
734                  itNewNote->cause = *pNoteOnEvent;                  itNewNote->cause = *itNoteOnEvent;
735                  itNewNote->eventID = pEventPool->getID(pNoteOnEvent);                  itNewNote->eventID = pEventPool->getID(itNoteOnEvent);
736                    if (!itNewNote->eventID) {
737                        dmsg(0,("Engine: No valid event ID resolved for note. This is a bug!!!\n"));
738                    }
739    
740                  // move new note to its host key                  // move new note to its host key
741                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNewNote->hostKey];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNewNote->hostKey];
742                  itNewNote.moveToEndOf(pKey->pActiveNotes);                  itNewNote.moveToEndOf(pKey->pActiveNotes);
743    
744                  // 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
745                  pNoteOnEvent->Param.Note.ID = newNoteID;                  itNoteOnEvent->Param.Note.ID = newNoteID;
746    
747                  return newNoteID; // success                  return newNoteID; // success
748              }              }
# Line 795  namespace LinuxSampler { Line 802  namespace LinuxSampler {
802                              case Event::type_note_pressure:                              case Event::type_note_pressure:
803                                  //TODO: ...                                  //TODO: ...
804                                  break;                                  break;
805    
806                                case Event::type_sysex:
807                                    //TODO: ...
808                                    break;
809    
810                                case Event::type_cancel_release_key:
811                                case Event::type_release_key:
812                                case Event::type_release_note:
813                                case Event::type_play_note:
814                                case Event::type_stop_note:
815                                case Event::type_kill_note:
816                                case Event::type_note_synth_param:
817                                    break; // noop
818                          }                          }
819    
820                          // see HACK comment above                          // see HACK comment above
# Line 853  namespace LinuxSampler { Line 873  namespace LinuxSampler {
873                                  dmsg(5,("Engine: Note on received\n"));                                  dmsg(5,("Engine: Note on received\n"));
874                                  ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);
875                                  break;                                  break;
876                                case Event::type_play_note:
877                                    dmsg(5,("Engine: Play Note received\n"));
878                                    ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);
879                                    break;
880                              case Event::type_note_off:                              case Event::type_note_off:
881                                  dmsg(5,("Engine: Note off received\n"));                                  dmsg(5,("Engine: Note off received\n"));
882                                  ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);
883                                  break;                                  break;
884                                case Event::type_stop_note:
885                                    dmsg(5,("Engine: Stop Note received\n"));
886                                    ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);
887                                    break;
888                                case Event::type_kill_note:
889                                    dmsg(5,("Engine: Kill Note received\n"));
890                                    ProcessKillNote((EngineChannel*)itEvent->pEngineChannel, itEvent);
891                                    break;
892                              case Event::type_control_change:                              case Event::type_control_change:
893                                  dmsg(5,("Engine: MIDI CC received\n"));                                  dmsg(5,("Engine: MIDI CC received\n"));
894                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);                                  ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);
# Line 877  namespace LinuxSampler { Line 909  namespace LinuxSampler {
909                                  dmsg(5,("Engine: Note Synth Param received\n"));                                  dmsg(5,("Engine: Note Synth Param received\n"));
910                                  ProcessNoteSynthParam(itEvent->pEngineChannel, itEvent);                                  ProcessNoteSynthParam(itEvent->pEngineChannel, itEvent);
911                                  break;                                  break;
912                                case Event::type_sysex:
913                                    break; // TODO ...
914    
915                                case Event::type_cancel_release_key:
916                                case Event::type_release_key:
917                                case Event::type_release_note:
918                                    break; // noop
919                          }                          }
920                      }                      }
921                  }                  }
# Line 977  namespace LinuxSampler { Line 1016  namespace LinuxSampler {
1016    
1017                  // initialize/reset other members                  // initialize/reset other members
1018                  itScriptEvent->cause = *itEvent;                  itScriptEvent->cause = *itEvent;
1019                    itScriptEvent->scheduleTime = itEvent->SchedTime();
1020                  itScriptEvent->currentHandler = 0;                  itScriptEvent->currentHandler = 0;
1021                  itScriptEvent->executionSlices = 0;                  itScriptEvent->executionSlices = 0;
1022                    itScriptEvent->ignoreAllWaitCalls = false;
1023                    itScriptEvent->handlerType = pEventHandler->eventHandlerType();
1024                  // this is the native representation of the $EVENT_ID script variable                  // this is the native representation of the $EVENT_ID script variable
1025                  itScriptEvent->id =                  itScriptEvent->id =
1026                      (itEvent->Type == Event::type_note_on)                      (itEvent->Type == Event::type_note_on)
# Line 1246  namespace LinuxSampler { Line 1288  namespace LinuxSampler {
1288                          // the script's "init" event handler is only executed                          // the script's "init" event handler is only executed
1289                          // once (when the script is loaded or reloaded)                          // once (when the script is loaded or reloaded)
1290                          if (pEngineChannel->pScript && pEngineChannel->pScript->handlerInit) {                          if (pEngineChannel->pScript && pEngineChannel->pScript->handlerInit) {
1291                                dmsg(5,("Engine: exec handlerInit %p\n", pEngineChannel->pScript->handlerInit));
1292                              RTList<ScriptEvent>::Iterator itScriptEvent =                              RTList<ScriptEvent>::Iterator itScriptEvent =
1293                                  pEngineChannel->pScript->pEvents->allocAppend();                                  pEngineChannel->pScript->pEvents->allocAppend();
1294    
1295                              itScriptEvent->cause.pEngineChannel = pEngineChannel;                              itScriptEvent->cause.pEngineChannel = pEngineChannel;
1296                              itScriptEvent->handlers[0] = pEngineChannel->pScript->handlerInit;                              itScriptEvent->handlers[0] = pEngineChannel->pScript->handlerInit;
1297                              itScriptEvent->handlers[1] = NULL;                              itScriptEvent->handlers[1] = NULL;
1298                                itScriptEvent->currentHandler = 0;
1299                                itScriptEvent->executionSlices = 0;
1300                                itScriptEvent->ignoreAllWaitCalls = false;
1301                                itScriptEvent->handlerType = VM_EVENT_HANDLER_INIT;
1302    
1303                              VMExecStatus_t res = pScriptVM->exec(                              /*VMExecStatus_t res = */ pScriptVM->exec(
1304                                  pEngineChannel->pScript->parserContext, &*itScriptEvent                                  pEngineChannel->pScript->parserContext, &*itScriptEvent
1305                              );                              );
1306    
# Line 1316  namespace LinuxSampler { Line 1363  namespace LinuxSampler {
1363                          // usually there should already be a new Note object                          // usually there should already be a new Note object
1364                          NoteIterator itNote = GetNotePool()->fromID(itVoiceStealEvent->Param.Note.ID);                          NoteIterator itNote = GetNotePool()->fromID(itVoiceStealEvent->Param.Note.ID);
1365                          if (!itNote) { // should not happen, but just to be sure ...                          if (!itNote) { // should not happen, but just to be sure ...
1366                              const note_id_t noteID = LaunchNewNote(pEngineChannel, &*itVoiceStealEvent);                              const note_id_t noteID = LaunchNewNote(pEngineChannel, itVoiceStealEvent);
1367                              if (!noteID) {                              if (!noteID) {
1368                                  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"));
1369                                  continue;                                  continue;
# Line 1593  namespace LinuxSampler { Line 1640  namespace LinuxSampler {
1640               *  @param pEngineChannel - engine channel on which this event occurred on               *  @param pEngineChannel - engine channel on which this event occurred on
1641               *  @param itNoteOnEvent - key, velocity and time stamp of the event               *  @param itNoteOnEvent - key, velocity and time stamp of the event
1642               */               */
1643              virtual void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {              virtual void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) OVERRIDE {
1644                  EngineChannelBase<V, R, I>* pChannel =                  EngineChannelBase<V, R, I>* pChannel =
1645                          static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                          static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1646    
# Line 1603  namespace LinuxSampler { Line 1650  namespace LinuxSampler {
1650    
1651                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
1652    
1653                  pChannel->listeners.PreProcessNoteOn(key, vel);                  // There are real MIDI note-on events (Event::type_note_on) and
1654                    // programmatically spawned notes (Event::type_play_note). We have
1655                    // to distinguish between them, since certain processing below
1656                    // must only be done on real MIDI note-on events (i.e. for
1657                    // correctly updating which MIDI keys are currently pressed down).
1658                    const bool isRealMIDINoteOnEvent = itNoteOnEvent->Type == Event::type_note_on;
1659    
1660                    if (isRealMIDINoteOnEvent)
1661                        pChannel->listeners.PreProcessNoteOn(key, vel);
1662    
1663                  #if !CONFIG_PROCESS_MUTED_CHANNELS                  #if !CONFIG_PROCESS_MUTED_CHANNELS
1664                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted
1665                      pChannel->listeners.PostProcessNoteOn(key, vel);                      if (isRealMIDINoteOnEvent)
1666                            pChannel->listeners.PostProcessNoteOn(key, vel);
1667                      return;                      return;
1668                  }                  }
1669                  #endif                  #endif
1670    
1671                  if (!pChannel->pInstrument) {                  if (!pChannel->pInstrument) {
1672                      pChannel->listeners.PostProcessNoteOn(key, vel);                      if (isRealMIDINoteOnEvent)
1673                            pChannel->listeners.PostProcessNoteOn(key, vel);
1674                      return; // ignore if no instrument loaded                      return; // ignore if no instrument loaded
1675                  }                  }
1676    
# Line 1620  namespace LinuxSampler { Line 1678  namespace LinuxSampler {
1678                  RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);                  RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
1679    
1680                  // if Solo Mode then kill all already active voices                  // if Solo Mode then kill all already active voices
1681                  if (pChannel->SoloMode) {                  if (pChannel->SoloMode && isRealMIDINoteOnEvent) {
1682                      Pool<uint>::Iterator itYoungestKey = pChannel->pActiveKeys->last();                      Pool<uint>::Iterator itYoungestKey = pChannel->pActiveKeys->last();
1683                      if (itYoungestKey) {                      if (itYoungestKey) {
1684                          const int iYoungestKey = *itYoungestKey;                          const int iYoungestKey = *itYoungestKey;
# Line 1649  namespace LinuxSampler { Line 1707  namespace LinuxSampler {
1707                      pChannel->SoloKey = key;                      pChannel->SoloKey = key;
1708                  }                  }
1709    
1710                  pChannel->ProcessKeySwitchChange(key);                  if (isRealMIDINoteOnEvent) {
1711                        pChannel->ProcessKeySwitchChange(key);
1712    
1713                  pKey->KeyPressed = true; // the MIDI key was now pressed down                      pKey->KeyPressed = true; // the MIDI key was now pressed down
1714                  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
1715                  pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;                      pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;
1716                  pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length                      pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length
1717                    }
1718    
1719                  // cancel release process of voices on this key if needed                  // cancel release process of voices on this key if needed
1720                  if (pKey->Active && !pChannel->SustainPedal) {                  if (pKey->Active && !pChannel->SustainPedal && isRealMIDINoteOnEvent) {
1721                      RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();                      RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
1722                      if (itCancelReleaseEvent) {                      if (itCancelReleaseEvent) {
1723                          *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event                          *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event
1724                          itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                          itCancelReleaseEvent->Type = Event::type_cancel_release_key; // transform event type
1725                      }                      }
1726                      else dmsg(1,("Event pool emtpy!\n"));                      else dmsg(1,("Event pool emtpy!\n"));
1727                  }                  }
# Line 1672  namespace LinuxSampler { Line 1732  namespace LinuxSampler {
1732                  if (!pKey->Active && !pKey->VoiceTheftsQueued)                  if (!pKey->Active && !pKey->VoiceTheftsQueued)
1733                      pKey->pEvents->free(itNoteOnEventOnKeyList);                      pKey->pEvents->free(itNoteOnEventOnKeyList);
1734    
1735                  if (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f) pChannel->PortamentoPos = (float) key;                  if (isRealMIDINoteOnEvent && (!pChannel->SoloMode || pChannel->PortamentoPos < 0.0f))
1736                        pChannel->PortamentoPos = (float) key;
1737    
1738                    //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?
1739                  if (pKey->pRoundRobinIndex) {                  if (pKey->pRoundRobinIndex) {
1740                      (*pKey->pRoundRobinIndex)++; // counter specific for the key or region                      (*pKey->pRoundRobinIndex)++; // counter specific for the key or region
1741                      pChannel->RoundRobinIndex++; // common counter for the channel                      pChannel->RoundRobinIndex++; // common counter for the channel
1742                  }                  }
1743                  pChannel->listeners.PostProcessNoteOn(key, vel);  
1744                    if (isRealMIDINoteOnEvent)
1745                        pChannel->listeners.PostProcessNoteOn(key, vel);
1746              }              }
1747    
1748              /**              /**
# Line 1706  namespace LinuxSampler { Line 1771  namespace LinuxSampler {
1771               *  @param pEngineChannel - engine channel on which this event occurred on               *  @param pEngineChannel - engine channel on which this event occurred on
1772               *  @param itNoteOffEvent - key, velocity and time stamp of the event               *  @param itNoteOffEvent - key, velocity and time stamp of the event
1773               */               */
1774              virtual void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {              virtual void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) OVERRIDE {
1775                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1776    
1777                  const int iKey = itNoteOffEvent->Param.Note.Key;                  const int iKey = itNoteOffEvent->Param.Note.Key;
# Line 1715  namespace LinuxSampler { Line 1780  namespace LinuxSampler {
1780    
1781                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[iKey];                  MidiKey* pKey = &pChannel->pMIDIKeyInfo[iKey];
1782    
1783                  pChannel->listeners.PreProcessNoteOff(iKey, vel);                  // There are real MIDI note-off events (Event::type_note_off) and
1784                    // programmatically spawned notes (Event::type_stop_note). We have
1785                    // to distinguish between them, since certain processing below
1786                    // must only be done on real MIDI note-off events (i.e. for
1787                    // correctly updating which MIDI keys are currently pressed down),
1788                    // plus a stop-note event just releases voices of one particular
1789                    // note, whereas a note-off event releases all voices on a
1790                    // particular MIDI key instead.
1791                    const bool isRealMIDINoteOffEvent = itNoteOffEvent->Type == Event::type_note_off;
1792    
1793                    if (isRealMIDINoteOffEvent)
1794                        pChannel->listeners.PreProcessNoteOff(iKey, vel);
1795    
1796                  #if !CONFIG_PROCESS_MUTED_CHANNELS                  #if !CONFIG_PROCESS_MUTED_CHANNELS
1797                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted                  if (pEngineChannel->GetMute()) { // skip if sampler channel is muted
1798                      pChannel->listeners.PostProcessNoteOff(iKey, vel);                      if (isRealMIDINoteOffEvent)
1799                            pChannel->listeners.PostProcessNoteOff(iKey, vel);
1800                      return;                      return;
1801                  }                  }
1802                  #endif                  #endif
1803    
1804                  pKey->KeyPressed = false; // the MIDI key was now released                  if (isRealMIDINoteOffEvent) {
1805                  pChannel->KeyDown[iKey] = false; // just used as built-in %KEY_DOWN script variable                      pKey->KeyPressed = false; // the MIDI key was now released
1806                        pChannel->KeyDown[iKey] = false; // just used as built-in %KEY_DOWN script variable
1807                    }
1808    
1809                  // move event to the key's own event list                  // move event to the key's own event list
1810                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);                  RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
1811    
1812                  bool bShouldRelease = pKey->Active && pChannel->ShouldReleaseVoice(itNoteOffEventOnKeyList->Param.Note.Key);                  if (isRealMIDINoteOffEvent) {
1813                        bool bShouldRelease = pKey->Active && pChannel->ShouldReleaseVoice(itNoteOffEventOnKeyList->Param.Note.Key);
1814    
1815                  // 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)
1816                  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
1817                      bool bOtherKeysPressed = false;                          bool bOtherKeysPressed = false;
1818                      if (iKey == pChannel->SoloKey) {                          if (iKey == pChannel->SoloKey) {
1819                          pChannel->SoloKey = -1;                              pChannel->SoloKey = -1;
1820                          // 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
1821                          for (int i = 127; i > 0; i--) {                              for (int i = 127; i > 0; i--) {
1822                              MidiKey* pOtherKey = &pChannel->pMIDIKeyInfo[i];                                  MidiKey* pOtherKey = &pChannel->pMIDIKeyInfo[i];
1823                              if (pOtherKey->KeyPressed) {                                  if (pOtherKey->KeyPressed) {
1824                                  bOtherKeysPressed = true;                                      bOtherKeysPressed = true;
1825                                  // make the other key the new 'currently active solo key'                                      // make the other key the new 'currently active solo key'
1826                                  pChannel->SoloKey = i;                                      pChannel->SoloKey = i;
1827                                  // get final portamento position of currently active voice                                      // get final portamento position of currently active voice
1828                                  if (pChannel->PortamentoMode) {                                      if (pChannel->PortamentoMode) {
1829                                      NoteIterator itNote = pKey->pActiveNotes->first();                                          NoteIterator itNote = pKey->pActiveNotes->first();
1830                                      VoiceIterator itVoice = itNote->pActiveVoices->first();                                          VoiceIterator itVoice = itNote->pActiveVoices->first();
1831                                      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);  
1832                                      }                                      }
1833                                      // if neither a voice was spawned or postponed then remove note on event from key again                                      // create a pseudo note on event
1834                                      if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)                                      RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend();
1835                                          pOtherKey->pEvents->free(itPseudoNoteOnEvent);                                      if (itPseudoNoteOnEvent) {
1836                                            // copy event
1837                                            *itPseudoNoteOnEvent = *itNoteOffEventOnKeyList;
1838                                            // transform event to a note on event
1839                                            itPseudoNoteOnEvent->Type                = Event::type_note_on; //FIXME: should probably use Event::type_play_note instead (to avoid i.e. hanging notes)
1840                                            itPseudoNoteOnEvent->Param.Note.Key      = i;
1841                                            itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity;
1842                                            // assign a new note to this note-on event
1843                                            if (LaunchNewNote(pChannel, itPseudoNoteOnEvent)) {
1844                                                // allocate and trigger new voice(s) for the other key
1845                                                TriggerNewVoices(pChannel, itPseudoNoteOnEvent, false);
1846                                            }
1847                                            // if neither a voice was spawned or postponed then remove note on event from key again
1848                                            if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)
1849                                                pOtherKey->pEvents->free(itPseudoNoteOnEvent);
1850    
1851                                  } else dmsg(1,("Could not respawn voice, no free event left\n"));                                      } else dmsg(1,("Could not respawn voice, no free event left\n"));
1852                                  break; // done                                      break; // done
1853                                    }
1854                              }                              }
1855                          }                          }
1856                      }                          if (bOtherKeysPressed) {
1857                      if (bOtherKeysPressed) {                              if (pKey->Active) { // kill all voices on this key
1858                          if (pKey->Active) { // kill all voices on this key                                  bShouldRelease = false; // no need to release, as we kill it here
1859                              bShouldRelease = false; // no need to release, as we kill it here                                  for (NoteIterator itNote = pKey->pActiveNotes->first(); itNote; ++itNote) {
1860                              for (NoteIterator itNote = pKey->pActiveNotes->first(); itNote; ++itNote) {                                      VoiceIterator itVoiceToBeKilled = itNote->pActiveVoices->first();
1861                                  VoiceIterator itVoiceToBeKilled = itNote->pActiveVoices->first();                                      VoiceIterator end               = itNote->pActiveVoices->end();
1862                                  VoiceIterator end               = itNote->pActiveVoices->end();                                      for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
1863                                  for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {                                          if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))
1864                                      if (!(itVoiceToBeKilled->Type & Voice::type_release_trigger))                                              itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);
1865                                          itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);                                      }
1866                                  }                                  }
1867                              }                              }
1868                          }                          } else pChannel->PortamentoPos = -1.0f;
1869                      } else pChannel->PortamentoPos = -1.0f;                      }
                 }  
1870    
1871                  // 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
1872                  if (bShouldRelease) {                      if (bShouldRelease) {
1873                      itNoteOffEventOnKeyList->Type = Event::type_release; // transform event type                          itNoteOffEventOnKeyList->Type = Event::type_release_key; // transform event type
1874                      // spawn release triggered voice(s) if needed                          // spawn release triggered voice(s) if needed
1875                      ProcessReleaseTrigger(pChannel, itNoteOffEventOnKeyList, pKey);                          ProcessReleaseTrigger(pChannel, itNoteOffEventOnKeyList, pKey);
1876                        }
1877                    } else if (itNoteOffEventOnKeyList->Type == Event::type_stop_note) {
1878                        // This programmatically caused event is caused by a call to
1879                        // the built-in instrument script function note_off(). In
1880                        // contrast to a real MIDI note-off event the stop-note
1881                        // event just intends to release voices of one particular note.
1882                        NoteBase* pNote = pChannel->pEngine->NoteByID( itNoteOffEventOnKeyList->Param.Note.ID );
1883                        if (pNote) { // the requested note is still alive ...
1884                            itNoteOffEventOnKeyList->Type = Event::type_release_note; // transform event type
1885                        } else { // note is dead and gone ..
1886                            pKey->pEvents->free(itNoteOffEventOnKeyList); // remove stop-note event from key again
1887                            return; // prevent event to be removed a 2nd time below
1888                        }
1889                  }                  }
1890    
1891                  // 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
1892                  if (!pKey->Active && !pKey->VoiceTheftsQueued)                  if (!pKey->Active && !pKey->VoiceTheftsQueued)
1893                      pKey->pEvents->free(itNoteOffEventOnKeyList);                      pKey->pEvents->free(itNoteOffEventOnKeyList);
1894    
1895                  pChannel->listeners.PostProcessNoteOff(iKey, vel);                  if (isRealMIDINoteOffEvent)
1896                        pChannel->listeners.PostProcessNoteOff(iKey, vel);
1897              }              }
1898    
1899              /**              /**
# Line 1833  namespace LinuxSampler { Line 1927  namespace LinuxSampler {
1927                  // spawn release triggered voice(s) if needed                  // spawn release triggered voice(s) if needed
1928                  if (pKey->ReleaseTrigger && pChannel->pInstrument) {                  if (pKey->ReleaseTrigger && pChannel->pInstrument) {
1929                      // assign a new note to this release event                      // assign a new note to this release event
1930                      if (LaunchNewNote(pChannel, &*itEvent)) {                      if (LaunchNewNote(pChannel, itEvent)) {
1931                          // allocate and trigger new release voice(s)                          // allocate and trigger new release voice(s)
1932                          TriggerReleaseVoices(pChannel, itEvent);                          TriggerReleaseVoices(pChannel, itEvent);
1933                      }                      }
# Line 1842  namespace LinuxSampler { Line 1936  namespace LinuxSampler {
1936              }              }
1937    
1938              /**              /**
1939                 * Called on "kill note" events, which currently only happens on
1940                 * built-in real-time instrument script function fade_out(). This
1941                 * method only fulfills one task: moving the even to the Note's own
1942                 * event list so that its voices can process the kill event sample
1943                 * accurately.
1944                 */
1945                void ProcessKillNote(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) {
1946                    EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1947    
1948                    NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.Note.ID );
1949                    if (!pNote || pNote->hostKey < 0 || pNote->hostKey >= 128) return;
1950    
1951                    // move note kill event to its MIDI key
1952                    MidiKey* pKey = &pChannel->pMIDIKeyInfo[pNote->hostKey];
1953                    itEvent.moveToEndOf(pKey->pEvents);
1954                }
1955    
1956                /**
1957               * Called on note synthesis parameter change events. These are               * Called on note synthesis parameter change events. These are
1958               * internal events caused by calling built-in real-time instrument               * internal events caused by calling built-in real-time instrument
1959               * script functions like change_vol(), change_pitch(), etc.               * script functions like change_vol(), change_tune(), etc.
1960               *               *
1961               * This method performs two tasks:               * This method performs two tasks:
1962               *               *
# Line 1859  namespace LinuxSampler { Line 1971  namespace LinuxSampler {
1971               * @param pEngineChannel - engine channel on which this event occurred on               * @param pEngineChannel - engine channel on which this event occurred on
1972               * @param itEvent - note synthesis parameter change event               * @param itEvent - note synthesis parameter change event
1973               */               */
1974              virtual void ProcessNoteSynthParam(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) OVERRIDE {              virtual void ProcessNoteSynthParam(EngineChannel* pEngineChannel, RTList<Event>::Iterator& itEvent) {
1975                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
1976    
1977                  NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.NoteSynthParam.NoteID );                  NoteBase* pNote = pChannel->pEngine->NoteByID( itEvent->Param.NoteSynthParam.NoteID );
# Line 1875  namespace LinuxSampler { Line 1987  namespace LinuxSampler {
1987                              pNote->Override.Volume = itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Volume = itEvent->Param.NoteSynthParam.Delta;
1988                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Volume;                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Volume;
1989                          break;                          break;
1990                        case Event::synth_param_volume_time:
1991                            pNote->Override.VolumeTime = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
1992                            break;
1993                      case Event::synth_param_pitch:                      case Event::synth_param_pitch:
1994                          if (relative)                          if (relative)
1995                              pNote->Override.Pitch *= itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Pitch *= itEvent->Param.NoteSynthParam.Delta;
# Line 1882  namespace LinuxSampler { Line 1997  namespace LinuxSampler {
1997                              pNote->Override.Pitch = itEvent->Param.NoteSynthParam.Delta;                              pNote->Override.Pitch = itEvent->Param.NoteSynthParam.Delta;
1998                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pitch;                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pitch;
1999                          break;                          break;
2000                        case Event::synth_param_pitch_time:
2001                            pNote->Override.PitchTime = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2002                            break;
2003                      case Event::synth_param_pan:                      case Event::synth_param_pan:
2004                          if (relative) {                          if (relative) {
2005                              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 1891  namespace LinuxSampler { Line 2009  namespace LinuxSampler {
2009                          }                          }
2010                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pan;                          itEvent->Param.NoteSynthParam.AbsValue = pNote->Override.Pan;
2011                          break;                          break;
2012                        case Event::synth_param_cutoff:
2013                            pNote->Override.Cutoff = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2014                            break;
2015                        case Event::synth_param_resonance:
2016                            pNote->Override.Resonance = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2017                            break;
2018                        case Event::synth_param_attack:
2019                            pNote->Override.Attack = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2020                            break;
2021                        case Event::synth_param_decay:
2022                            pNote->Override.Decay = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2023                            break;
2024                        case Event::synth_param_release:
2025                            pNote->Override.Release = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2026                            break;
2027                        case Event::synth_param_amp_lfo_depth:
2028                            pNote->Override.AmpLFODepth = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2029                            break;
2030                        case Event::synth_param_amp_lfo_freq:
2031                            pNote->Override.AmpLFOFreq = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2032                            break;
2033                        case Event::synth_param_pitch_lfo_depth:
2034                            pNote->Override.PitchLFODepth = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2035                            break;
2036                        case Event::synth_param_pitch_lfo_freq:
2037                            pNote->Override.PitchLFOFreq = itEvent->Param.NoteSynthParam.AbsValue = itEvent->Param.NoteSynthParam.Delta;
2038                            break;
2039                  }                  }
2040    
2041                  // move note parameter event to its MIDI key                  // move note parameter event to its MIDI key
# Line 1902  namespace LinuxSampler { Line 2047  namespace LinuxSampler {
2047               *  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
2048               *  control and status variables. This method is protected by a mutex.               *  control and status variables. This method is protected by a mutex.
2049               */               */
2050              virtual void ResetInternal() {              virtual void ResetInternal() OVERRIDE {
2051                  LockGuard lock(ResetInternalMutex);                  LockGuard lock(ResetInternalMutex);
2052    
2053                  // make sure that the engine does not get any sysex messages                  // make sure that the engine does not get any sysex messages
# Line 1961  namespace LinuxSampler { Line 2106  namespace LinuxSampler {
2106               * @param pEngineChannel - engine channel on which all voices should be killed               * @param pEngineChannel - engine channel on which all voices should be killed
2107               * @param itKillEvent    - event which caused this killing of all voices               * @param itKillEvent    - event which caused this killing of all voices
2108               */               */
2109              virtual void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent) {              virtual void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent) OVERRIDE {
2110                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);                  EngineChannelBase<V, R, I>* pChannel = static_cast<EngineChannelBase<V, R, I>*>(pEngineChannel);
2111                  int count = pChannel->KillAllVoices(itKillEvent);                  int count = pChannel->KillAllVoices(itKillEvent);
2112                  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 1996  namespace LinuxSampler { Line 2141  namespace LinuxSampler {
2141                  bool                    HandleKeyGroupConflicts                  bool                    HandleKeyGroupConflicts
2142              ) = 0;              ) = 0;
2143    
2144              virtual int GetMinFadeOutSamples() { return MinFadeOutSamples; }              virtual int GetMinFadeOutSamples() OVERRIDE { return MinFadeOutSamples; }
2145    
2146              int InitNewVoice (              int InitNewVoice (
2147                  EngineChannelBase<V, R, I>*  pChannel,                  EngineChannelBase<V, R, I>*  pChannel,

Legend:
Removed from v.2931  
changed lines
  Added in v.3205

  ViewVC Help
Powered by ViewVC