/[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 2611 by schoenebeck, Mon Jun 9 19:20:37 2014 UTC revision 2871 by schoenebeck, Sun Apr 10 18:22:23 2016 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 - 2014 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2016 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 26  Line 26 
26    
27  #include "../../common/global.h"  #include "../../common/global.h"
28  #include "../../common/RTMath.h"  #include "../../common/RTMath.h"
29    #include "../../common/RTAVLTree.h"
30    #include "../../common/Pool.h"
31  #include "../EngineChannel.h"  #include "../EngineChannel.h"
32    
33  namespace LinuxSampler {  namespace LinuxSampler {
34    
35      // just symbol prototyping      // just symbol prototyping
36      class Event;      class Event;
37        class SchedulerNode;
38        class ScriptEvent;
39        class ScheduledEvent;
40    
41        /**
42         * Data type used to schedule events sample point accurately both within, as
43         * well as beyond the scope of the current audio fragment cycle. The timing
44         * reflected by this data type is consecutively running for a very long
45         * time. Even with a sample rate of 96 kHz a scheduler time of this data
46         * type will not wrap before 6 million years. So in practice such time
47         * stamps are unique and will not repeat (unless the EventGenerator is
48         * reset).
49         */
50        typedef uint64_t sched_time_t;
51    
52      /**      /**
53       * Generates Event objects and is responsible for resolving the position       * Generates Event objects and is responsible for resolving the position
# Line 43  namespace LinuxSampler { Line 59  namespace LinuxSampler {
59              void UpdateFragmentTime(uint SamplesToProcess);              void UpdateFragmentTime(uint SamplesToProcess);
60              Event CreateEvent();              Event CreateEvent();
61              Event CreateEvent(int32_t FragmentPos);              Event CreateEvent(int32_t FragmentPos);
62    
63                template<typename T>
64                void scheduleAheadMicroSec(RTAVLTree<T>& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds);
65    
66                RTList<ScheduledEvent>::Iterator popNextScheduledEvent(RTAVLTree<ScheduledEvent>& queue, Pool<ScheduledEvent>& pool, sched_time_t end);
67                RTList<ScriptEvent>::Iterator popNextScheduledScriptEvent(RTAVLTree<ScriptEvent>& queue, Pool<ScriptEvent>& pool, sched_time_t end);
68    
69                /**
70                 * Returns the scheduler time for the first sample point of the next
71                 * audio fragment cycle.
72                 */
73                sched_time_t schedTimeAtCurrentFragmentEnd() const {
74                    return uiTotalSamplesProcessed + uiSamplesProcessed;
75                }
76    
77          protected:          protected:
78              typedef RTMath::time_stamp_t time_stamp_t;              typedef RTMath::time_stamp_t time_stamp_t;
79              inline int32_t ToFragmentPos(time_stamp_t TimeStamp) {              inline int32_t ToFragmentPos(time_stamp_t TimeStamp) {
# Line 57  namespace LinuxSampler { Line 88  namespace LinuxSampler {
88                  time_stamp_t end;          ///< Real time stamp of the end of this audio fragment cycle.                  time_stamp_t end;          ///< Real time stamp of the end of this audio fragment cycle.
89                  float        sample_ratio; ///< (Samples per cycle) / (Real time duration of cycle)                  float        sample_ratio; ///< (Samples per cycle) / (Real time duration of cycle)
90              } FragmentTime;              } FragmentTime;
91                sched_time_t uiTotalSamplesProcessed; ///< Total amount of sample points that have been processed since this EventGenerator object has been created. This is used to schedule instrument script events long time ahead in future (that is beyond the scope of the current audio fragment).
92      };      };
93    
94      /**      /**
# Line 149  namespace LinuxSampler { Line 181  namespace LinuxSampler {
181              int32_t         iFragmentPos;    ///< Position in the current fragment this event refers to.              int32_t         iFragmentPos;    ///< Position in the current fragment this event refers to.
182      };      };
183    
184        /**
185         * Used to sort timing relevant objects (i.e. events) into timing/scheduler
186         * queue. This class is just intended as base class and should be derived
187         * for its actual purpose (for the precise data type being scheduled).
188         */
189        class SchedulerNode : public RTAVLNode {
190        public:
191            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.
192    
193            /// Required operator implementation for RTAVLTree class.
194            inline bool operator==(const SchedulerNode& other) const {
195                return this->scheduleTime == other.scheduleTime;
196            }
197    
198            /// Required operator implementation for RTAVLTree class.
199            inline bool operator<(const SchedulerNode& other) const {
200                return this->scheduleTime < other.scheduleTime;
201            }
202        };
203    
204        /**
205         * Used to sort delayed MIDI events into a timing/scheduler queue. This
206         * object just contains the timing informations, the actual MIDI event is
207         * pointed by member variable @c itEvent.
208         */
209        class ScheduledEvent : public SchedulerNode {
210        public:
211            Pool<Event>::Iterator itEvent; ///< Points to the actual Event object being scheduled.
212        };
213    
214      class VMEventHandler;      class VMEventHandler;
215      class VMExecContext;      class VMExecContext;
216    
# Line 156  namespace LinuxSampler { Line 218  namespace LinuxSampler {
218       *       *
219       * Encapsulates one execution instance of a real-time instrument script for       * Encapsulates one execution instance of a real-time instrument script for
220       * exactly one script event handler (script event callback).       * exactly one script event handler (script event callback).
221         *
222         * This class derives from SchedulerNode for being able to be sorted efficiently
223         * by the script scheduler if the script was either a) calling the wait()
224         * script function or b) the script was auto suspended by the ScriptVM
225         * because the script was executing for too long. In both cases the
226         * scheduler has to sort the ScriptEvents in its execution queue according
227         * to the precise time the respective script execution instance needs to be
228         * resumed.
229       */       */
230      class ScriptEvent {      class ScriptEvent : public SchedulerNode {
231      public:      public:
232          Event cause; ///< Original external event that triggered this script event (i.e. MIDI note on event, MIDI CC event, etc.).          Event cause; ///< Original external event that triggered this script event (i.e. MIDI note on event, MIDI CC event, etc.).
233          int id; ///< Unique ID of the external event that triggered this cript event.          int id; ///< Unique ID of the external event that triggered this script event.
234          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).
235          VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack).          VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack).
236          int currentHandler; ///< Current index in 'handlers' list above.          int currentHandler; ///< Current index in 'handlers' list above.
237          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.
238      };      };
239    
240        /**
241         * Insert given @a node into the supplied timing @a queue with a scheduled
242         * timing position given by @a fragmentPosBase and @a microseconds, where
243         * @a microseconds reflects the amount microseconds in future from "now"
244         * where the node shall be scheduled, and @a fragmentPos identifies the
245         * sample point within the current audio fragment cycle which shall be
246         * interpreted by this method to be "now".
247         *
248         * The meaning of @a fragmentPosBase becomes more important the larger
249         * the audio fragment size, and vice versa it bcomes less important the
250         * smaller the audio fragment size.
251         *
252         * @param queue - destination scheduler queue
253         * @param node - node (i.e. event) to be inserted into the queue
254         * @param fragmentPosBase - sample point in current audio fragment to be "now"
255         * @param microseconds - timing of node from "now" (in microseconds)
256         */
257        template<typename T>
258        void EventGenerator::scheduleAheadMicroSec(RTAVLTree<T>& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds) {
259            node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + float(uiSampleRate) * (float(microseconds) / 1000000.f);
260            queue.insert(node);
261        }
262    
263  } // namespace LinuxSampler  } // namespace LinuxSampler
264    
265  #endif // __LS_EVENT_H__  #endif // __LS_EVENT_H__

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

  ViewVC Help
Powered by ViewVC