--- linuxsampler/trunk/src/engines/common/Event.h 2014/06/09 19:20:37 2611 +++ linuxsampler/trunk/src/engines/common/Event.h 2016/04/10 18:22:23 2871 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2014 Christian Schoenebeck * + * Copyright (C) 2005 - 2016 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -26,12 +26,28 @@ #include "../../common/global.h" #include "../../common/RTMath.h" +#include "../../common/RTAVLTree.h" +#include "../../common/Pool.h" #include "../EngineChannel.h" namespace LinuxSampler { // just symbol prototyping class Event; + class SchedulerNode; + class ScriptEvent; + class ScheduledEvent; + + /** + * Data type used to schedule events sample point accurately both within, as + * well as beyond the scope of the current audio fragment cycle. The timing + * reflected by this data type is consecutively running for a very long + * time. Even with a sample rate of 96 kHz a scheduler time of this data + * type will not wrap before 6 million years. So in practice such time + * stamps are unique and will not repeat (unless the EventGenerator is + * reset). + */ + typedef uint64_t sched_time_t; /** * Generates Event objects and is responsible for resolving the position @@ -43,6 +59,21 @@ void UpdateFragmentTime(uint SamplesToProcess); Event CreateEvent(); Event CreateEvent(int32_t FragmentPos); + + template + void scheduleAheadMicroSec(RTAVLTree& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds); + + RTList::Iterator popNextScheduledEvent(RTAVLTree& queue, Pool& pool, sched_time_t end); + RTList::Iterator popNextScheduledScriptEvent(RTAVLTree& queue, Pool& pool, sched_time_t end); + + /** + * Returns the scheduler time for the first sample point of the next + * audio fragment cycle. + */ + sched_time_t schedTimeAtCurrentFragmentEnd() const { + return uiTotalSamplesProcessed + uiSamplesProcessed; + } + protected: typedef RTMath::time_stamp_t time_stamp_t; inline int32_t ToFragmentPos(time_stamp_t TimeStamp) { @@ -57,6 +88,7 @@ time_stamp_t end; ///< Real time stamp of the end of this audio fragment cycle. float sample_ratio; ///< (Samples per cycle) / (Real time duration of cycle) } FragmentTime; + 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). }; /** @@ -149,6 +181,36 @@ int32_t iFragmentPos; ///< Position in the current fragment this event refers to. }; + /** + * Used to sort timing relevant objects (i.e. events) into timing/scheduler + * queue. This class is just intended as base class and should be derived + * for its actual purpose (for the precise data type being scheduled). + */ + class SchedulerNode : public RTAVLNode { + public: + 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. + + /// Required operator implementation for RTAVLTree class. + inline bool operator==(const SchedulerNode& other) const { + return this->scheduleTime == other.scheduleTime; + } + + /// Required operator implementation for RTAVLTree class. + inline bool operator<(const SchedulerNode& other) const { + return this->scheduleTime < other.scheduleTime; + } + }; + + /** + * Used to sort delayed MIDI events into a timing/scheduler queue. This + * object just contains the timing informations, the actual MIDI event is + * pointed by member variable @c itEvent. + */ + class ScheduledEvent : public SchedulerNode { + public: + Pool::Iterator itEvent; ///< Points to the actual Event object being scheduled. + }; + class VMEventHandler; class VMExecContext; @@ -156,17 +218,48 @@ * * Encapsulates one execution instance of a real-time instrument script for * exactly one script event handler (script event callback). + * + * This class derives from SchedulerNode for being able to be sorted efficiently + * by the script scheduler if the script was either a) calling the wait() + * script function or b) the script was auto suspended by the ScriptVM + * because the script was executing for too long. In both cases the + * scheduler has to sort the ScriptEvents in its execution queue according + * to the precise time the respective script execution instance needs to be + * resumed. */ - class ScriptEvent { + class ScriptEvent : public SchedulerNode { public: Event cause; ///< Original external event that triggered this script event (i.e. MIDI note on event, MIDI CC event, etc.). - 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. VMEventHandler** handlers; ///< The script's event handlers (callbacks) to be processed (NULL terminated list). VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack). int currentHandler; ///< Current index in 'handlers' list above. int executionSlices; ///< Amount of times this script event has been executed by the ScriptVM runner class. }; + /** + * Insert given @a node into the supplied timing @a queue with a scheduled + * timing position given by @a fragmentPosBase and @a microseconds, where + * @a microseconds reflects the amount microseconds in future from "now" + * where the node shall be scheduled, and @a fragmentPos identifies the + * sample point within the current audio fragment cycle which shall be + * interpreted by this method to be "now". + * + * The meaning of @a fragmentPosBase becomes more important the larger + * the audio fragment size, and vice versa it bcomes less important the + * smaller the audio fragment size. + * + * @param queue - destination scheduler queue + * @param node - node (i.e. event) to be inserted into the queue + * @param fragmentPosBase - sample point in current audio fragment to be "now" + * @param microseconds - timing of node from "now" (in microseconds) + */ + template + void EventGenerator::scheduleAheadMicroSec(RTAVLTree& queue, T& node, int32_t fragmentPosBase, uint64_t microseconds) { + node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + float(uiSampleRate) * (float(microseconds) / 1000000.f); + queue.insert(node); + } + } // namespace LinuxSampler #endif // __LS_EVENT_H__