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

Diff of /linuxsampler/trunk/src/engines/common/Event.h

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

revision 2871 by schoenebeck, Sun Apr 10 18:22:23 2016 UTC revision 3246 by schoenebeck, Sun May 28 22:22:56 2017 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 - 2016 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2017 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   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 29  Line 29 
29  #include "../../common/RTAVLTree.h"  #include "../../common/RTAVLTree.h"
30  #include "../../common/Pool.h"  #include "../../common/Pool.h"
31  #include "../EngineChannel.h"  #include "../EngineChannel.h"
32    #include "../../scriptvm/common.h"
33    
34  namespace LinuxSampler {  namespace LinuxSampler {
35    
# Line 67  namespace LinuxSampler { Line 68  namespace LinuxSampler {
68              RTList<ScriptEvent>::Iterator popNextScheduledScriptEvent(RTAVLTree<ScriptEvent>& queue, Pool<ScriptEvent>& pool, sched_time_t end);              RTList<ScriptEvent>::Iterator popNextScheduledScriptEvent(RTAVLTree<ScriptEvent>& queue, Pool<ScriptEvent>& pool, sched_time_t end);
69    
70              /**              /**
71                 * Returns the scheduler time for the first sample point of the
72                 * current audio fragment cycle.
73                 */
74                sched_time_t schedTimeAtCurrentFragmentStart() const {
75                    return uiTotalSamplesProcessed;
76                }
77    
78                /**
79               * Returns the scheduler time for the first sample point of the next               * Returns the scheduler time for the first sample point of the next
80               * audio fragment cycle.               * audio fragment cycle.
81               */               */
# Line 92  namespace LinuxSampler { Line 101  namespace LinuxSampler {
101      };      };
102    
103      /**      /**
104         * Unique numeric ID of an event which can be used to retrieve access to
105         * the actual @c Event object. Once the event associated with a certain ID
106         * was released (back to its event pool), this numeric ID becomes invalid
107         * and Pool< Event >::fromID() will detect this circumstance and will
108         * return an invalid Iterator, and thus will prevent you from misusing an
109         * event which no longer "exists".
110         *
111         * Note that an @c Event object usually just "exists" for exactly on audio
112         * fragment cycle: that is it exists right from the beginning of the audio
113         * fragment cycle where it was caused (i.e. where its MIDI data was
114         * received by the respective engine channel) and will disappear
115         * automatically at the end of that audio fragment cycle.
116         */
117        typedef pool_element_id_t event_id_t;
118    
119        /**
120         * Unique numeric ID of a note which can be used to retrieve access to the
121         * actual @c Note object. Once the note associated with a certain ID was
122         * released (back to its note pool), this numeric ID becomes invalid and
123         * Pool< Note >::fromID() will detect this circumstance and will return
124         * an invalid Iterator, and thus will prevent you from misusing a note
125         * which no longer is "alive".
126         *
127         * A @c Note object exists right when the respective MIDI note-on event
128         * was received by the respective engine channel, and remains existent
129         * until the caused note and all its voices were finally freed (which might
130         * even be long time after the respective note-off event was received,
131         * depending on the duration of the voice's release stages etc.).
132         */
133        typedef pool_element_id_t note_id_t;
134    
135        /**
136         * Unique numeric ID of a script callback ID instance which can be used to
137         * retrieve access to the actual @c ScriptEvent object. Once the script
138         * callback instance associated with a certain ID stopped its execution
139         * (that is completely stopped, not just suspended) then this numeric ID
140         * becomes invalid and Pool< ScriptEvent >::fromID() will detect this
141         * circumstance and will return an invalid Iterator, and thus will prevent
142         * you from misusing a script callback instance which no longer "exists".
143         */
144        typedef pool_element_id_t script_callback_id_t;
145    
146        /**
147       * Events are usually caused by a MIDI source or an internal modulation       * Events are usually caused by a MIDI source or an internal modulation
148       * controller like LFO or EG. An event should only be created by an       * controller like LFO or EG. An event should only be created by an
149       * EventGenerator!       * EventGenerator!
# Line 102  namespace LinuxSampler { Line 154  namespace LinuxSampler {
154          public:          public:
155              Event(){}              Event(){}
156              enum type_t {              enum type_t {
157                  type_note_on,                  type_note_on, ///< (real) MIDI note-on event
158                  type_note_off,                  type_note_off, ///< (real) MIDI note-off event
159                  type_pitchbend,                  type_pitchbend, ///< MIDI pitch bend wheel change event
160                  type_control_change,                  type_control_change, ///< MIDI CC event
161                  type_sysex,           ///< MIDI system exclusive message                  type_sysex,           ///< MIDI system exclusive message
162                  type_cancel_release,  ///< transformed either from a note-on or sustain-pedal-down event                  type_cancel_release_key, ///< transformed either from a (real) MIDI note-on or sustain-pedal-down event
163                  type_release,         ///< transformed either from a note-off or sustain-pedal-up event                  type_release_key,     ///< transformed either from a (real) MIDI note-off or sustain-pedal-up event
164                    type_release_note,    ///< transformed from a type_stop_note event
165                  type_channel_pressure, ///< a.k.a. aftertouch                  type_channel_pressure, ///< a.k.a. aftertouch
166                  type_note_pressure, ///< polyphonic key pressure (aftertouch)                  type_note_pressure, ///< polyphonic key pressure (aftertouch)
167                    type_play_note, ///< caused by a call to built-in instrument script function play_note()
168                    type_stop_note, ///< caused by a call to built-in instrument script function note_off()
169                    type_kill_note, ///< caused by a call to built-in instrument script function fade_out()
170                    type_note_synth_param, ///< change a note's synthesis parameters (upon real-time instrument script function calls, i.e. change_vol(), change_tune(), change_pan(), etc.)
171              } Type;              } Type;
172                enum synth_param_t {
173                    synth_param_volume,
174                    synth_param_volume_time,
175                    synth_param_volume_curve,
176                    synth_param_pitch,
177                    synth_param_pitch_time,
178                    synth_param_pitch_curve,
179                    synth_param_pan,
180                    synth_param_cutoff,
181                    synth_param_resonance,
182                    synth_param_attack,
183                    synth_param_decay,
184                    synth_param_release,
185                    synth_param_amp_lfo_depth,
186                    synth_param_amp_lfo_freq,
187                    synth_param_pitch_lfo_depth,
188                    synth_param_pitch_lfo_freq,
189                };
190              union {              union {
191                  /// Note-on and note-off event specifics                  /// Note-on and note-off event specifics
192                  struct _Note {                  struct _Note {
# Line 120  namespace LinuxSampler { Line 195  namespace LinuxSampler {
195                      uint8_t Velocity;    ///< Trigger or release velocity of note-on / note-off event.                      uint8_t Velocity;    ///< Trigger or release velocity of note-on / note-off event.
196                      int8_t  Layer;       ///< Layer index (usually only used if a note-on event has to be postponed, e.g. due to shortage of free voices).                      int8_t  Layer;       ///< Layer index (usually only used if a note-on event has to be postponed, e.g. due to shortage of free voices).
197                      int8_t  ReleaseTrigger; ///< If new voice should be a release triggered voice (actually boolean field and usually only used if a note-on event has to be postponed, e.g. due to shortage of free voices).                      int8_t  ReleaseTrigger; ///< If new voice should be a release triggered voice (actually boolean field and usually only used if a note-on event has to be postponed, e.g. due to shortage of free voices).
198                        note_id_t ID;        ///< Unique numeric ID of the @c Note object associated with this note event.
199                        note_id_t ParentNoteID; ///< If not zero: Unique numeric ID of the parent @c Note object that shall become parent of resulting new Note object of this Event. So this is used to associate a new note with a previous note, i.e. to release the new note once the parent note was released.
200                      void*   pRegion;     ///< Engine specific pointer to instrument region                      void*   pRegion;     ///< Engine specific pointer to instrument region
201                  } Note;                  } Note;
202                  /// Control change event specifics                  /// Control change event specifics
# Line 149  namespace LinuxSampler { Line 226  namespace LinuxSampler {
226                      uint8_t Key;     ///< MIDI note number where key pressure (polyphonic aftertouch) changed.                      uint8_t Key;     ///< MIDI note number where key pressure (polyphonic aftertouch) changed.
227                      uint8_t Value;   ///< New pressure value for note.                      uint8_t Value;   ///< New pressure value for note.
228                  } NotePressure;                  } NotePressure;
229                    ///< Note synthesis parameter change event's specifics (used for real-time instrument script built-in functions which may alter synthesis parameters on note level).
230                    struct _NoteSynthParam {
231                        note_id_t     NoteID;   ///< ID of Note whose voices shall be modified.
232                        synth_param_t Type;     ///< Synthesis parameter which is to be changed.
233                        float         Delta;    ///< The value change that should be applied against the note's current synthesis parameter value.
234                        bool          Relative; ///< Whether @c Delta should be applied relatively against the note's current synthesis parameter value (false means the paramter's current value is simply replaced by Delta).
235                        float         AbsValue; ///< New current absolute value of synthesis parameter (that is after @c Delta being applied).
236                    } NoteSynthParam;
237              } Param;              } Param;
             /// Sampler format specific informations and variables.  
             union {  
                 /// Gigasampler/GigaStudio format specifics.  
                 struct _Gig {  
                     uint8_t DimMask; ///< May be used to override the Dimension zone to be selected for a new voice: each 1 bit means that respective bit shall be overridden by taking the respective bit from DimBits instead.  
                     uint8_t DimBits; ///< Used only in conjunction with DimMask: Dimension bits that shall be selected.  
                 } Gig;  
             } Format;  
238              EngineChannel* pEngineChannel; ///< Pointer to the EngineChannel where this event occured on, NULL means Engine global event (e.g. SysEx message).              EngineChannel* pEngineChannel; ///< Pointer to the EngineChannel where this event occured on, NULL means Engine global event (e.g. SysEx message).
239              MidiInputPort* pMidiInputPort; ///< Pointer to the MIDI input port on which this event occured (NOTE: currently only for global events, that is SysEx messages)              MidiInputPort* pMidiInputPort; ///< Pointer to the MIDI input port on which this event occured (NOTE: currently only for global events, that is SysEx messages)
240    
241                inline void Init() {
242                    Param.Note.ID = 0;
243                    Param.Note.ParentNoteID = 0;
244                    Param.NoteSynthParam.NoteID = 0;
245                }
246              inline int32_t FragmentPos() {              inline int32_t FragmentPos() {
247                  if (iFragmentPos >= 0) return iFragmentPos;                  if (iFragmentPos >= 0) return iFragmentPos;
248                  iFragmentPos = pEventGenerator->ToFragmentPos(TimeStamp);                  iFragmentPos = pEventGenerator->ToFragmentPos(TimeStamp);
# Line 170  namespace LinuxSampler { Line 252  namespace LinuxSampler {
252              inline void ResetFragmentPos() {              inline void ResetFragmentPos() {
253                  iFragmentPos = -1;                  iFragmentPos = -1;
254              }              }
255                inline void CopyTimeFrom(const Event& other) {
256                    TimeStamp = other.TimeStamp;
257                    iFragmentPos = other.iFragmentPos;
258                }
259                inline sched_time_t SchedTime() {
260                    return pEventGenerator->schedTimeAtCurrentFragmentStart() + FragmentPos();
261                }
262          protected:          protected:
263              typedef EventGenerator::time_stamp_t time_stamp_t;              typedef EventGenerator::time_stamp_t time_stamp_t;
264              Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time);              Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time);
# Line 188  namespace LinuxSampler { Line 277  namespace LinuxSampler {
277       */       */
278      class SchedulerNode : public RTAVLNode {      class SchedulerNode : public RTAVLNode {
279      public:      public:
280            using RTAVLNode::reset; // make reset() method public
281    
282          sched_time_t scheduleTime; ///< Time ahead in future (in sample points) when this object shall be processed. This value is compared with EventGenerator's uiTotalSamplesProcessed member variable.          sched_time_t scheduleTime; ///< Time ahead in future (in sample points) when this object shall be processed. This value is compared with EventGenerator's uiTotalSamplesProcessed member variable.
283    
284          /// Required operator implementation for RTAVLTree class.          /// Required operator implementation for RTAVLTree class.
# Line 199  namespace LinuxSampler { Line 290  namespace LinuxSampler {
290          inline bool operator<(const SchedulerNode& other) const {          inline bool operator<(const SchedulerNode& other) const {
291              return this->scheduleTime < other.scheduleTime;              return this->scheduleTime < other.scheduleTime;
292          }          }
293    
294            /// This is actually just for code readability.
295            inline RTAVLTreeBase* currentSchedulerQueue() const { return rtavlTree(); }
296      };      };
297    
298      /**      /**
# Line 229  namespace LinuxSampler { Line 323  namespace LinuxSampler {
323       */       */
324      class ScriptEvent : public SchedulerNode {      class ScriptEvent : public SchedulerNode {
325      public:      public:
326          Event cause; ///< Original external event that triggered this script event (i.e. MIDI note on event, MIDI CC event, etc.).          Event cause; ///< Copy of original external @c Event that triggered this script event (i.e. MIDI note on event, MIDI CC event, etc.).
327          int id; ///< Unique ID of the external event that triggered this script event.          pool_element_id_t id; ///< Native representation of built-in script variable $EVENT_ID. For scripts' "note" event handler this will reflect the unique ID of the @c Note object, for all other event handlers the unique ID of the original external @c Event object that triggered this script event.
328          VMEventHandler** handlers; ///< The script's event handlers (callbacks) to be processed (NULL terminated list).          VMEventHandler** handlers; ///< The script's event handlers (callbacks) to be processed (NULL terminated list).
329          VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack).          VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack).
330          int currentHandler; ///< Current index in 'handlers' list above.          int currentHandler; ///< Current index in 'handlers' list above.
331          int executionSlices; ///< Amount of times this script event has been executed by the ScriptVM runner class.          int executionSlices; ///< Amount of times this script event has been executed by the ScriptVM runner class.
332            bool ignoreAllWaitCalls; ///< If true: calling any built-in wait*() script function should be ignored (this variable may be set with the 2nd argument of built-in script function stop_wait()).
333            VMEventHandlerType_t handlerType; ///< Native representation of built-in script variable $NI_CALLBACK_TYPE, reflecting the script event type of this script event.
334      };      };
335    
336      /**      /**
337       * Insert given @a node into the supplied timing @a queue with a scheduled       * Insert given @a node into the supplied timing @a queue with a scheduled
338       * timing position given by @a fragmentPosBase and @a microseconds, where       * timing position given by @a fragmentPosBase and @a microseconds, where
339       * @a microseconds reflects the amount microseconds in future from "now"       * @a microseconds reflects the amount of microseconds in future from "now"
340       * where the node shall be scheduled, and @a fragmentPos identifies the       * where the node shall be scheduled, and @a fragmentPos identifies the
341       * sample point within the current audio fragment cycle which shall be       * sample point within the current audio fragment cycle which shall be
342       * interpreted by this method to be "now".       * interpreted by this method to be "now".
# Line 256  namespace LinuxSampler { Line 352  namespace LinuxSampler {
352       */       */
353      template<typename T>      template<typename T>
354      void EventGenerator::scheduleAheadMicroSec(RTAVLTree<T>& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds) {      void EventGenerator::scheduleAheadMicroSec(RTAVLTree<T>& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds) {
355          node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + float(uiSampleRate) * (float(microseconds) / 1000000.f);          // round up (+1) if microseconds is not zero (i.e. because 44.1 kHz and
356            // 1 us would yield in < 1 and thus would be offset == 0)
357            const sched_time_t offset =
358                (microseconds != 0LL) ?
359                    1.f + (float(uiSampleRate) * (float(microseconds) / 1000000.f))
360                    : 0.f;
361            node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + offset;
362          queue.insert(node);          queue.insert(node);
363      }      }
364    

Legend:
Removed from v.2871  
changed lines
  Added in v.3246

  ViewVC Help
Powered by ViewVC