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 - 2017 Christian Schoenebeck * |
* Copyright (C) 2005 - 2020 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 * |
31 |
#include "../EngineChannel.h" |
#include "../EngineChannel.h" |
32 |
#include "../../scriptvm/common.h" |
#include "../../scriptvm/common.h" |
33 |
|
|
34 |
|
// On Windows RELATIVE might be defined as macro in wingdi.h, which would |
35 |
|
// cause a compiler error of the same token used in this header file below. |
36 |
|
// So we undefine that macro here for now (if present). |
37 |
|
#ifdef RELATIVE |
38 |
|
# warning Preprocessor conflict detected: Macro RELATIVE was declared by system headers; undefining it here. |
39 |
|
# undef RELATIVE |
40 |
|
#endif |
41 |
|
|
42 |
namespace LinuxSampler { |
namespace LinuxSampler { |
43 |
|
|
44 |
// just symbol prototyping |
// just symbol prototyping |
66 |
public: |
public: |
67 |
EventGenerator(uint SampleRate); |
EventGenerator(uint SampleRate); |
68 |
void UpdateFragmentTime(uint SamplesToProcess); |
void UpdateFragmentTime(uint SamplesToProcess); |
69 |
|
void SetSampleRate(uint SampleRate); |
70 |
Event CreateEvent(); |
Event CreateEvent(); |
71 |
Event CreateEvent(int32_t FragmentPos); |
Event CreateEvent(int32_t FragmentPos); |
72 |
|
|
175 |
type_note_pressure, ///< polyphonic key pressure (aftertouch) |
type_note_pressure, ///< polyphonic key pressure (aftertouch) |
176 |
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() |
177 |
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() |
178 |
|
type_kill_note, ///< caused by a call to built-in instrument script function fade_out() |
179 |
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.) |
180 |
} Type; |
} Type; |
181 |
enum synth_param_t { |
enum synth_param_t { |
182 |
synth_param_volume, |
synth_param_volume, |
183 |
|
synth_param_volume_time, |
184 |
|
synth_param_volume_curve, |
185 |
synth_param_pitch, |
synth_param_pitch, |
186 |
|
synth_param_pitch_time, |
187 |
|
synth_param_pitch_curve, |
188 |
synth_param_pan, |
synth_param_pan, |
189 |
|
synth_param_pan_time, |
190 |
|
synth_param_pan_curve, |
191 |
synth_param_cutoff, |
synth_param_cutoff, |
192 |
synth_param_resonance, |
synth_param_resonance, |
193 |
synth_param_attack, |
synth_param_attack, |
194 |
synth_param_decay, |
synth_param_decay, |
195 |
|
synth_param_sustain, |
196 |
synth_param_release, |
synth_param_release, |
197 |
|
synth_param_cutoff_attack, |
198 |
|
synth_param_cutoff_decay, |
199 |
|
synth_param_cutoff_sustain, |
200 |
|
synth_param_cutoff_release, |
201 |
synth_param_amp_lfo_depth, |
synth_param_amp_lfo_depth, |
202 |
synth_param_amp_lfo_freq, |
synth_param_amp_lfo_freq, |
203 |
|
synth_param_cutoff_lfo_depth, |
204 |
|
synth_param_cutoff_lfo_freq, |
205 |
synth_param_pitch_lfo_depth, |
synth_param_pitch_lfo_depth, |
206 |
synth_param_pitch_lfo_freq, |
synth_param_pitch_lfo_freq, |
207 |
}; |
}; |
208 |
|
enum class ValueScope : unsigned char { |
209 |
|
/** |
210 |
|
* The new synthesis parameter value should be applied |
211 |
|
* relatively to itself (as normalized value range), and then |
212 |
|
* applied relatively against other sources (i.e. LFOs, EGs) |
213 |
|
* for the same synthesis parameter. |
214 |
|
*/ |
215 |
|
SELF_RELATIVE = 1, |
216 |
|
/** |
217 |
|
* The new synthesis paramater value of itself should be |
218 |
|
* replaced, and then applied relatively to other sources |
219 |
|
* (i.e. LFOs, EGs) for the same synthesis parameter. |
220 |
|
*/ |
221 |
|
RELATIVE = 0, //IMPORANT: must remain 0 because of the union structure below which would otherwise i.e. assign invalid pointers/IDs to Param.Note structure in Init() |
222 |
|
/** |
223 |
|
* The new synthesis parameter value should be applied |
224 |
|
* relatively to itself (as normalized value range), and then |
225 |
|
* applied directly (as normalized value range) as final value |
226 |
|
* of this synthesis chain, thus all other sources (i.e. LFOs, |
227 |
|
* EGs) should entirely be ignored. |
228 |
|
*/ |
229 |
|
FINAL_SELF_RELATIVE = 2, |
230 |
|
/** |
231 |
|
* The new synthesis parameter value of itself should be |
232 |
|
* replaced, and then applied directly (as normalized value |
233 |
|
* range) as final value of this synthesis chain, thus all other |
234 |
|
* sources (i.e. LFOs, EGs) should entirely be ignored. |
235 |
|
*/ |
236 |
|
FINAL_NORM = 3, |
237 |
|
/** |
238 |
|
* Same as @c FINAL_NORM, but this one is already in the native |
239 |
|
* unit (i.e. seconds, Hz) of this synthesis parameter. |
240 |
|
*/ |
241 |
|
FINAL_NATIVE = 4, |
242 |
|
}; |
243 |
union { |
union { |
244 |
/// Note-on and note-off event specifics |
/// Note-on and note-off event specifics |
245 |
struct _Note { |
struct _Note { |
284 |
note_id_t NoteID; ///< ID of Note whose voices shall be modified. |
note_id_t NoteID; ///< ID of Note whose voices shall be modified. |
285 |
synth_param_t Type; ///< Synthesis parameter which is to be changed. |
synth_param_t Type; ///< Synthesis parameter which is to be changed. |
286 |
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). |
|
287 |
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). |
288 |
|
ValueScope Scope; ///< How @c Delta should be applied against @c AbsValue, and how @c AbsValue should then actually be applied to the synthesis chain. |
289 |
|
|
290 |
|
inline bool isFinal() const { return Scope >= ValueScope::FINAL_SELF_RELATIVE; } |
291 |
} NoteSynthParam; |
} NoteSynthParam; |
292 |
} Param; |
} Param; |
293 |
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). |
294 |
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) |
295 |
|
|
296 |
inline void Init() { |
inline void Init() { |
297 |
Param.Note.ID = 0; |
memset(&Param, 0, sizeof(Param)); |
|
Param.Note.ParentNoteID = 0; |
|
|
Param.NoteSynthParam.NoteID = 0; |
|
298 |
} |
} |
299 |
inline int32_t FragmentPos() { |
inline int32_t FragmentPos() { |
300 |
if (iFragmentPos >= 0) return iFragmentPos; |
if (iFragmentPos >= 0) return iFragmentPos; |
312 |
inline sched_time_t SchedTime() { |
inline sched_time_t SchedTime() { |
313 |
return pEventGenerator->schedTimeAtCurrentFragmentStart() + FragmentPos(); |
return pEventGenerator->schedTimeAtCurrentFragmentStart() + FragmentPos(); |
314 |
} |
} |
315 |
|
inline static ValueScope scopeBy_FinalRelativeUnit(bool bFinal, bool bRelative, bool bNativeUnit) { |
316 |
|
if (!bFinal && bRelative) |
317 |
|
return ValueScope::SELF_RELATIVE; |
318 |
|
if (!bFinal) |
319 |
|
return ValueScope::RELATIVE; |
320 |
|
if (bRelative) |
321 |
|
return ValueScope::FINAL_SELF_RELATIVE; |
322 |
|
if (bNativeUnit) |
323 |
|
return ValueScope::FINAL_NATIVE; |
324 |
|
return ValueScope::FINAL_NORM; |
325 |
|
} |
326 |
protected: |
protected: |
327 |
typedef EventGenerator::time_stamp_t time_stamp_t; |
typedef EventGenerator::time_stamp_t time_stamp_t; |
328 |
Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time); |
Event(EventGenerator* pGenerator, EventGenerator::time_stamp_t Time); |
372 |
class VMEventHandler; |
class VMEventHandler; |
373 |
class VMExecContext; |
class VMExecContext; |
374 |
|
|
375 |
|
/** |
376 |
|
* Maximum amount of child script handler instances one script handler is |
377 |
|
* allowed to create by calling built-in script function fork(). |
378 |
|
*/ |
379 |
|
#define MAX_FORK_PER_SCRIPT_HANDLER 8 |
380 |
|
|
381 |
/** @brief Real-time instrument script event. |
/** @brief Real-time instrument script event. |
382 |
* |
* |
383 |
* Encapsulates one execution instance of a real-time instrument script for |
* Encapsulates one execution instance of a real-time instrument script for |
401 |
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. |
402 |
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()). |
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()). |
403 |
VMEventHandlerType_t handlerType; ///< Native representation of built-in script variable $NI_CALLBACK_TYPE, reflecting the script event type of this script event. |
VMEventHandlerType_t handlerType; ///< Native representation of built-in script variable $NI_CALLBACK_TYPE, reflecting the script event type of this script event. |
404 |
|
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). |
405 |
|
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). |
406 |
|
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. |
407 |
|
int forkIndex; ///< Only for fork() calls: distinguishment feature which is 0 for parent, 1 for 1st child, 2 for 2nd child, etc. |
408 |
|
|
409 |
|
void forkTo(ScriptEvent* e, bool bAutoAbort) const; |
410 |
|
int countChildHandlers() const; |
411 |
|
void addChildHandlerID(script_callback_id_t childID); |
412 |
}; |
}; |
413 |
|
|
414 |
/** |
/** |
420 |
* interpreted by this method to be "now". |
* interpreted by this method to be "now". |
421 |
* |
* |
422 |
* The meaning of @a fragmentPosBase becomes more important the larger |
* The meaning of @a fragmentPosBase becomes more important the larger |
423 |
* the audio fragment size, and vice versa it bcomes less important the |
* the audio fragment size, and vice versa it becomes less important the |
424 |
* smaller the audio fragment size. |
* smaller the audio fragment size. |
425 |
* |
* |
426 |
* @param queue - destination scheduler queue |
* @param queue - destination scheduler queue |
430 |
*/ |
*/ |
431 |
template<typename T> |
template<typename T> |
432 |
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) { |
433 |
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 |
434 |
|
// 1 us would yield in < 1 and thus would be offset == 0) |
435 |
|
const sched_time_t offset = |
436 |
|
(microseconds != 0LL) ? |
437 |
|
1.f + (float(uiSampleRate) * (float(microseconds) / 1000000.f)) |
438 |
|
: 0.f; |
439 |
|
node.scheduleTime = uiTotalSamplesProcessed + fragmentPosBase + offset; |
440 |
queue.insert(node); |
queue.insert(node); |
441 |
} |
} |
442 |
|
|