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, 2006 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 * |
35 |
uiSampleRate = SampleRate; |
uiSampleRate = SampleRate; |
36 |
uiSamplesProcessed = 0; |
uiSamplesProcessed = 0; |
37 |
FragmentTime.end = RTMath::CreateTimeStamp(); |
FragmentTime.end = RTMath::CreateTimeStamp(); |
38 |
|
uiTotalSamplesProcessed = 0; |
39 |
} |
} |
40 |
|
|
41 |
/** |
/** |
47 |
* audio fragment cycle |
* audio fragment cycle |
48 |
*/ |
*/ |
49 |
void EventGenerator::UpdateFragmentTime(uint SamplesToProcess) { |
void EventGenerator::UpdateFragmentTime(uint SamplesToProcess) { |
50 |
|
// update total amount of sample points being processed since this object was created |
51 |
|
uiTotalSamplesProcessed += uiSamplesProcessed; |
52 |
// update time stamp for this audio fragment cycle |
// update time stamp for this audio fragment cycle |
53 |
FragmentTime.begin = FragmentTime.end; |
FragmentTime.begin = FragmentTime.end; |
54 |
FragmentTime.end = RTMath::CreateTimeStamp(); |
FragmentTime.end = RTMath::CreateTimeStamp(); |
58 |
// store amount of samples to process for the next cycle |
// store amount of samples to process for the next cycle |
59 |
uiSamplesProcessed = SamplesToProcess; |
uiSamplesProcessed = SamplesToProcess; |
60 |
} |
} |
61 |
|
|
62 |
|
/** |
63 |
|
* Get the next scheduled MIDI event (the one with the lowest time value) |
64 |
|
* for the current audio fragment cycle and remove it from the queue. This |
65 |
|
* method will not return any event scheduled past the current audio |
66 |
|
* fragment boundary. |
67 |
|
* |
68 |
|
* @param queue - where the MIDI events are scheduled on |
69 |
|
* @param pool - used to allocate and deallocate ScheduledEvent objects |
70 |
|
* @param end - you @b MUST always pass EventGenerator::schedTimeAtCurrentFragmentEnd() |
71 |
|
* here reflecting the current audio fragment's scheduler end time |
72 |
|
*/ |
73 |
|
RTList<ScheduledEvent>::Iterator EventGenerator::popNextScheduledEvent(RTAVLTree<ScheduledEvent>& queue, Pool<ScheduledEvent>& pool, sched_time_t end) { |
74 |
|
if (queue.isEmpty()) |
75 |
|
return RTList<ScheduledEvent>::Iterator(); // return invalid iterator |
76 |
|
ScheduledEvent& e = queue.lowest(); |
77 |
|
if (e.scheduleTime >= end) |
78 |
|
return RTList<ScheduledEvent>::Iterator(); // no event scheduled before 'end' |
79 |
|
RTList<ScheduledEvent>::Iterator itEvent = pool.fromPtr(&e); |
80 |
|
queue.erase(e); |
81 |
|
if (!itEvent || !itEvent->itEvent) { |
82 |
|
dmsg(1,("EventGenerator::popNextScheduledEvent(): !itEvent\n")); |
83 |
|
return itEvent; // should never happen at this point, but just to be sure |
84 |
|
} |
85 |
|
if (!itEvent->itEvent) { |
86 |
|
dmsg(1,("EventGenerator::popNextScheduledEvent(): !itEvent->itEvent\n")); |
87 |
|
return itEvent; // should never happen at this point, but just to be sure |
88 |
|
} |
89 |
|
|
90 |
|
// update position of this event in the current audio fragment |
91 |
|
// (since calling scheduleAheadMicroSec() will relate to this) |
92 |
|
itEvent->itEvent->iFragmentPos = uiSamplesProcessed - (end - itEvent->scheduleTime); |
93 |
|
// safety first: fragment boundary sanity checks |
94 |
|
if (itEvent->itEvent->iFragmentPos < 0) |
95 |
|
itEvent->itEvent->iFragmentPos = 0; |
96 |
|
if (itEvent->itEvent->iFragmentPos >= uiSamplesProcessed) |
97 |
|
itEvent->itEvent->iFragmentPos = uiSamplesProcessed - 1; |
98 |
|
|
99 |
|
return itEvent; |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Get the next instrument script event (the one with the lowest time value) |
104 |
|
* for the current audio fragment cycle and remove it from the queue. This |
105 |
|
* method will not return any event scheduled past the current audio |
106 |
|
* fragment boundary. |
107 |
|
* |
108 |
|
* @param queue - where the instrument script events are scheduled on |
109 |
|
* @param pool - used to allocate and deallocate ScriptEvent objects |
110 |
|
* @param end - you @b MUST always pass EventGenerator::schedTimeAtCurrentFragmentEnd() |
111 |
|
* here reflecting the current audio fragment's scheduler end time |
112 |
|
*/ |
113 |
|
RTList<ScriptEvent>::Iterator EventGenerator::popNextScheduledScriptEvent(RTAVLTree<ScriptEvent>& queue, Pool<ScriptEvent>& pool, sched_time_t end) { |
114 |
|
if (queue.isEmpty()) |
115 |
|
return RTList<ScriptEvent>::Iterator(); // return invalid iterator |
116 |
|
ScriptEvent& e = queue.lowest(); |
117 |
|
if (e.scheduleTime >= end) |
118 |
|
return RTList<ScriptEvent>::Iterator(); // no event scheduled before 'end' |
119 |
|
RTList<ScriptEvent>::Iterator itEvent = pool.fromPtr(&e); |
120 |
|
queue.erase(e); |
121 |
|
if (!itEvent) { // should never happen at this point, but just to be sure |
122 |
|
dmsg(1,("EventGenerator::popNextScheduledScriptEvent(): !itEvent\n")); |
123 |
|
return itEvent; |
124 |
|
} |
125 |
|
|
126 |
|
// update position of this event in the current audio fragment |
127 |
|
// (since calling scheduleAheadMicroSec() will relate to this) |
128 |
|
itEvent->cause.iFragmentPos = uiSamplesProcessed - (end - itEvent->scheduleTime); |
129 |
|
// safety first: fragment boundary sanity checks |
130 |
|
if (itEvent->cause.iFragmentPos < 0) |
131 |
|
itEvent->cause.iFragmentPos = 0; |
132 |
|
if (itEvent->cause.iFragmentPos >= uiSamplesProcessed) |
133 |
|
itEvent->cause.iFragmentPos = uiSamplesProcessed - 1; |
134 |
|
|
135 |
|
return itEvent; |
136 |
|
} |
137 |
|
|
138 |
/** |
/** |
139 |
* Create a new event with the current time as time stamp. |
* Create a new event with the current time as time stamp. |