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 - 2019 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 * |
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 |
|
|
58 |
public: |
public: |
59 |
EventGenerator(uint SampleRate); |
EventGenerator(uint SampleRate); |
60 |
void UpdateFragmentTime(uint SamplesToProcess); |
void UpdateFragmentTime(uint SamplesToProcess); |
61 |
|
void SetSampleRate(uint SampleRate); |
62 |
Event CreateEvent(); |
Event CreateEvent(); |
63 |
Event CreateEvent(int32_t FragmentPos); |
Event CreateEvent(int32_t FragmentPos); |
64 |
|
|
69 |
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); |
70 |
|
|
71 |
/** |
/** |
72 |
|
* Returns the scheduler time for the first sample point of the |
73 |
|
* current audio fragment cycle. |
74 |
|
*/ |
75 |
|
sched_time_t schedTimeAtCurrentFragmentStart() const { |
76 |
|
return uiTotalSamplesProcessed; |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
* Returns the scheduler time for the first sample point of the next |
* Returns the scheduler time for the first sample point of the next |
81 |
* audio fragment cycle. |
* audio fragment cycle. |
82 |
*/ |
*/ |
134 |
typedef pool_element_id_t note_id_t; |
typedef pool_element_id_t note_id_t; |
135 |
|
|
136 |
/** |
/** |
137 |
|
* Unique numeric ID of a script callback ID instance which can be used to |
138 |
|
* retrieve access to the actual @c ScriptEvent object. Once the script |
139 |
|
* callback instance associated with a certain ID stopped its execution |
140 |
|
* (that is completely stopped, not just suspended) then this numeric ID |
141 |
|
* becomes invalid and Pool< ScriptEvent >::fromID() will detect this |
142 |
|
* circumstance and will return an invalid Iterator, and thus will prevent |
143 |
|
* you from misusing a script callback instance which no longer "exists". |
144 |
|
*/ |
145 |
|
typedef pool_element_id_t script_callback_id_t; |
146 |
|
|
147 |
|
/** |
148 |
* Events are usually caused by a MIDI source or an internal modulation |
* Events are usually caused by a MIDI source or an internal modulation |
149 |
* 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 |
150 |
* EventGenerator! |
* EventGenerator! |
167 |
type_note_pressure, ///< polyphonic key pressure (aftertouch) |
type_note_pressure, ///< polyphonic key pressure (aftertouch) |
168 |
type_play_note, ///< caused by a call to built-in instrument script function play_note() |
type_play_note, ///< caused by a call to built-in instrument script function play_note() |
169 |
type_stop_note, ///< caused by a call to built-in instrument script function note_off() |
type_stop_note, ///< caused by a call to built-in instrument script function note_off() |
170 |
|
type_kill_note, ///< caused by a call to built-in instrument script function fade_out() |
171 |
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.) |
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.) |
172 |
} Type; |
} Type; |
173 |
enum synth_param_t { |
enum synth_param_t { |
174 |
synth_param_volume, |
synth_param_volume, |
175 |
|
synth_param_volume_time, |
176 |
|
synth_param_volume_curve, |
177 |
synth_param_pitch, |
synth_param_pitch, |
178 |
|
synth_param_pitch_time, |
179 |
|
synth_param_pitch_curve, |
180 |
synth_param_pan, |
synth_param_pan, |
181 |
|
synth_param_pan_time, |
182 |
|
synth_param_pan_curve, |
183 |
synth_param_cutoff, |
synth_param_cutoff, |
184 |
synth_param_resonance, |
synth_param_resonance, |
185 |
|
synth_param_attack, |
186 |
|
synth_param_decay, |
187 |
|
synth_param_sustain, |
188 |
|
synth_param_release, |
189 |
|
synth_param_cutoff_attack, |
190 |
|
synth_param_cutoff_decay, |
191 |
|
synth_param_cutoff_sustain, |
192 |
|
synth_param_cutoff_release, |
193 |
|
synth_param_amp_lfo_depth, |
194 |
|
synth_param_amp_lfo_freq, |
195 |
|
synth_param_cutoff_lfo_depth, |
196 |
|
synth_param_cutoff_lfo_freq, |
197 |
|
synth_param_pitch_lfo_depth, |
198 |
|
synth_param_pitch_lfo_freq, |
199 |
|
}; |
200 |
|
enum class ValueScope : unsigned char { |
201 |
|
/** |
202 |
|
* The new synthesis parameter value should be applied |
203 |
|
* relatively to itself (as normalized value range), and then |
204 |
|
* applied relatively against other sources (i.e. LFOs, EGs) |
205 |
|
* for the same synthesis parameter. |
206 |
|
*/ |
207 |
|
SELF_RELATIVE, |
208 |
|
/** |
209 |
|
* The new synthesis paramater value of itself should be |
210 |
|
* replaced, and then applied relatively to other sources |
211 |
|
* (i.e. LFOs, EGs) for the same synthesis parameter. |
212 |
|
*/ |
213 |
|
RELATIVE, |
214 |
|
/** |
215 |
|
* The new synthesis parameter value should be applied |
216 |
|
* relatively to itself (as normalized value range), and then |
217 |
|
* applied directly (as normalized value range) as final value |
218 |
|
* of this synthesis chain, thus all other sources (i.e. LFOs, |
219 |
|
* EGs) should entirely be ignored. |
220 |
|
*/ |
221 |
|
FINAL_SELF_RELATIVE, |
222 |
|
/** |
223 |
|
* The new synthesis parameter value of itself should be |
224 |
|
* replaced, and then applied directly (as normalized value |
225 |
|
* range) as final value of this synthesis chain, thus all other |
226 |
|
* sources (i.e. LFOs, EGs) should entirely be ignored. |
227 |
|
*/ |
228 |
|
FINAL_NORM, |
229 |
|
/** |
230 |
|
* Same as @c FINAL_NORM, but this one is already in the native |
231 |
|
* unit (i.e. seconds, Hz) of this synthesis parameter. |
232 |
|
*/ |
233 |
|
FINAL_NATIVE, |
234 |
}; |
}; |
235 |
union { |
union { |
236 |
/// Note-on and note-off event specifics |
/// Note-on and note-off event specifics |
276 |
note_id_t NoteID; ///< ID of Note whose voices shall be modified. |
note_id_t NoteID; ///< ID of Note whose voices shall be modified. |
277 |
synth_param_t Type; ///< Synthesis parameter which is to be changed. |
synth_param_t Type; ///< Synthesis parameter which is to be changed. |
278 |
float Delta; ///< The value change that should be applied against the note's current synthesis parameter value. |
float Delta; ///< The value change that should be applied against the note's current synthesis parameter value. |
|
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). |
|
279 |
float AbsValue; ///< New current absolute value of synthesis parameter (that is after @c Delta being applied). |
float AbsValue; ///< New current absolute value of synthesis parameter (that is after @c Delta being applied). |
280 |
|
ValueScope Scope; ///< How @c Delta should be applied against @c AbsValue, and how @c AbsValue should then actually be applied to the synthesis chain. |
281 |
|
|
282 |
|
inline bool isFinal() const { return Scope >= ValueScope::FINAL_SELF_RELATIVE; } |
283 |
} NoteSynthParam; |
} NoteSynthParam; |
284 |
} Param; |
} Param; |
285 |
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). |
289 |
Param.Note.ID = 0; |
Param.Note.ID = 0; |
290 |
Param.Note.ParentNoteID = 0; |
Param.Note.ParentNoteID = 0; |
291 |
Param.NoteSynthParam.NoteID = 0; |
Param.NoteSynthParam.NoteID = 0; |
292 |
|
Param.NoteSynthParam.Scope = ValueScope::RELATIVE; |
293 |
} |
} |
294 |
inline int32_t FragmentPos() { |
inline int32_t FragmentPos() { |
295 |
if (iFragmentPos >= 0) return iFragmentPos; |
if (iFragmentPos >= 0) return iFragmentPos; |
304 |
TimeStamp = other.TimeStamp; |
TimeStamp = other.TimeStamp; |
305 |
iFragmentPos = other.iFragmentPos; |
iFragmentPos = other.iFragmentPos; |
306 |
} |
} |
307 |
|
inline sched_time_t SchedTime() { |
308 |
|
return pEventGenerator->schedTimeAtCurrentFragmentStart() + FragmentPos(); |
309 |
|
} |
310 |
|
inline static ValueScope scopeBy_FinalRelativeUnit(bool bFinal, bool bRelative, bool bNativeUnit) { |
311 |
|
if (!bFinal && bRelative) |
312 |
|
return ValueScope::SELF_RELATIVE; |
313 |
|
if (!bFinal) |
314 |
|
return ValueScope::RELATIVE; |
315 |
|
if (bRelative) |
316 |
|
return ValueScope::FINAL_SELF_RELATIVE; |
317 |
|
if (bNativeUnit) |
318 |
|
return ValueScope::FINAL_NATIVE; |
319 |
|
return ValueScope::FINAL_NORM; |
320 |
|
} |
321 |
protected: |
protected: |
322 |
typedef EventGenerator::time_stamp_t time_stamp_t; |
typedef EventGenerator::time_stamp_t time_stamp_t; |
323 |
Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time); |
Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time); |
336 |
*/ |
*/ |
337 |
class SchedulerNode : public RTAVLNode { |
class SchedulerNode : public RTAVLNode { |
338 |
public: |
public: |
339 |
|
using RTAVLNode::reset; // make reset() method public |
340 |
|
|
341 |
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. |
342 |
|
|
343 |
/// Required operator implementation for RTAVLTree class. |
/// Required operator implementation for RTAVLTree class. |
349 |
inline bool operator<(const SchedulerNode& other) const { |
inline bool operator<(const SchedulerNode& other) const { |
350 |
return this->scheduleTime < other.scheduleTime; |
return this->scheduleTime < other.scheduleTime; |
351 |
} |
} |
352 |
|
|
353 |
|
/// This is actually just for code readability. |
354 |
|
inline RTAVLTreeBase* currentSchedulerQueue() const { return rtavlTree(); } |
355 |
}; |
}; |
356 |
|
|
357 |
/** |
/** |
367 |
class VMEventHandler; |
class VMEventHandler; |
368 |
class VMExecContext; |
class VMExecContext; |
369 |
|
|
370 |
|
/** |
371 |
|
* Maximum amount of child script handler instances one script handler is |
372 |
|
* allowed to create by calling built-in script function fork(). |
373 |
|
*/ |
374 |
|
#define MAX_FORK_PER_SCRIPT_HANDLER 8 |
375 |
|
|
376 |
/** @brief Real-time instrument script event. |
/** @brief Real-time instrument script event. |
377 |
* |
* |
378 |
* Encapsulates one execution instance of a real-time instrument script for |
* Encapsulates one execution instance of a real-time instrument script for |
394 |
VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack). |
VMExecContext* execCtx; ///< Script's current execution state (polyphonic variables and execution stack). |
395 |
int currentHandler; ///< Current index in 'handlers' list above. |
int currentHandler; ///< Current index in 'handlers' list above. |
396 |
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. |
397 |
|
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()). |
398 |
|
VMEventHandlerType_t handlerType; ///< Native representation of built-in script variable $NI_CALLBACK_TYPE, reflecting the script event type of this script event. |
399 |
|
script_callback_id_t parentHandlerID; ///< Only in case this script handler instance was created by calling built-in script function fork(): callback ID of the parent event handler instance which created this child. For regular event handler instances which were not created by fork(), this variable reflects 0 (which is always considered an invalid handler ID). |
400 |
|
script_callback_id_t childHandlerID[MAX_FORK_PER_SCRIPT_HANDLER+1]; ///< In case built-in script function fork() was called by this script handler instance: A zero terminated ID list of all child event handler instances (note: children will not vanish from this list after they terminated). |
401 |
|
bool autoAbortByParent; ///< Only if this is a child event handler created by calling fork(): if this is true then this child will automatically aborted if the parent event handler terminates. |
402 |
|
int forkIndex; ///< Only for fork() calls: distinguishment feature which is 0 for parent, 1 for 1st child, 2 for 2nd child, etc. |
403 |
|
|
404 |
|
void forkTo(ScriptEvent* e, bool bAutoAbort) const; |
405 |
|
int countChildHandlers() const; |
406 |
|
void addChildHandlerID(script_callback_id_t childID); |
407 |
}; |
}; |
408 |
|
|
409 |
/** |
/** |
415 |
* interpreted by this method to be "now". |
* interpreted by this method to be "now". |
416 |
* |
* |
417 |
* The meaning of @a fragmentPosBase becomes more important the larger |
* The meaning of @a fragmentPosBase becomes more important the larger |
418 |
* the audio fragment size, and vice versa it bcomes less important the |
* the audio fragment size, and vice versa it becomes less important the |
419 |
* smaller the audio fragment size. |
* smaller the audio fragment size. |
420 |
* |
* |
421 |
* @param queue - destination scheduler queue |
* @param queue - destination scheduler queue |
425 |
*/ |
*/ |
426 |
template<typename T> |
template<typename T> |
427 |
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) { |
428 |
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 |
429 |
|
// 1 us would yield in < 1 and thus would be offset == 0) |
430 |
|
const sched_time_t offset = |
431 |
|
(microseconds != 0LL) ? |
432 |
|
1.f + (float(uiSampleRate) * (float(microseconds) / 1000000.f)) |
433 |
|
: 0.f; |
434 |
|
node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + offset; |
435 |
queue.insert(node); |
queue.insert(node); |
436 |
} |
} |
437 |
|
|