1 |
iliev |
2012 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
|
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
persson |
2317 |
* Copyright (C) 2009-2012 Grigor Iliev * |
8 |
iliev |
2012 |
* * |
9 |
|
|
* This program is free software; you can redistribute it and/or modify * |
10 |
|
|
* it under the terms of the GNU General Public License as published by * |
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
12 |
|
|
* (at your option) any later version. * |
13 |
|
|
* * |
14 |
|
|
* This program is distributed in the hope that it will be useful, * |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
17 |
|
|
* GNU General Public License for more details. * |
18 |
|
|
* * |
19 |
|
|
* You should have received a copy of the GNU General Public License * |
20 |
|
|
* along with this program; if not, write to the Free Software * |
21 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
22 |
|
|
* MA 02111-1307 USA * |
23 |
|
|
***************************************************************************/ |
24 |
|
|
|
25 |
|
|
#include "Engine.h" |
26 |
|
|
#include "EngineChannel.h" |
27 |
|
|
|
28 |
|
|
namespace LinuxSampler { namespace sfz { |
29 |
|
|
Engine::Format Engine::GetEngineFormat() { return SFZ; } |
30 |
iliev |
2244 |
|
31 |
|
|
Engine::Engine() { |
32 |
|
|
pCCPool = new Pool<CCSignalUnit::CC>(GLOBAL_MAX_VOICES * MaxCCPerVoice); |
33 |
|
|
pSmootherPool = new Pool<Smoother>(GLOBAL_MAX_VOICES * MaxCCPerVoice); |
34 |
|
|
for (VoiceIterator iterVoice = GetVoicePool()->allocAppend(); iterVoice == GetVoicePool()->last(); iterVoice = GetVoicePool()->allocAppend()) { |
35 |
|
|
(static_cast<SfzSignalUnitRack*>(iterVoice->pSignalUnitRack))->InitRTLists(); |
36 |
|
|
} |
37 |
|
|
GetVoicePool()->clear(); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
Engine::~Engine() { |
41 |
|
|
if (pCCPool) { |
42 |
|
|
pCCPool->clear(); |
43 |
|
|
delete pCCPool; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
if (pSmootherPool) { |
47 |
|
|
pSmootherPool->clear(); |
48 |
|
|
delete pSmootherPool; |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
void Engine::PostSetMaxVoices(int iVoices) { |
53 |
|
|
pCCPool->resizePool(iVoices * MaxCCPerVoice); |
54 |
|
|
pSmootherPool->resizePool(iVoices * MaxCCPerVoice); |
55 |
|
|
|
56 |
|
|
for (VoiceIterator iterVoice = GetVoicePool()->allocAppend(); iterVoice == GetVoicePool()->last(); iterVoice = GetVoicePool()->allocAppend()) { |
57 |
|
|
(static_cast<SfzSignalUnitRack*>(iterVoice->pSignalUnitRack))->InitRTLists(); |
58 |
|
|
} |
59 |
|
|
GetVoicePool()->clear(); |
60 |
|
|
} |
61 |
iliev |
2012 |
|
62 |
|
|
/** |
63 |
|
|
* Reacts on supported control change commands (e.g. pitch bend wheel, |
64 |
|
|
* modulation wheel, aftertouch). |
65 |
|
|
* |
66 |
|
|
* @param pEngineChannel - engine channel on which this event occured on |
67 |
|
|
* @param itControlChangeEvent - controller, value and time stamp of the event |
68 |
|
|
*/ |
69 |
|
|
void Engine::ProcessControlChange ( |
70 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
71 |
|
|
Pool<Event>::Iterator& itControlChangeEvent |
72 |
|
|
) { |
73 |
persson |
2115 |
uint8_t cc = itControlChangeEvent->Param.CC.Controller; |
74 |
|
|
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", cc, itControlChangeEvent->Param.CC.Value)); |
75 |
iliev |
2012 |
|
76 |
persson |
2106 |
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
77 |
iliev |
2012 |
|
78 |
|
|
// update controller value in the engine channel's controller table |
79 |
persson |
2115 |
pChannel->ControllerTable[cc] = itControlChangeEvent->Param.CC.Value; |
80 |
iliev |
2012 |
|
81 |
|
|
ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent); |
82 |
|
|
|
83 |
|
|
// handle FX send controllers |
84 |
|
|
ProcessFxSendControllers(pChannel, itControlChangeEvent); |
85 |
persson |
2115 |
|
86 |
|
|
// handle control triggered regions: a control change event |
87 |
|
|
// can trigger a new voice |
88 |
|
|
if (pChannel->pInstrument && cc < 128) { |
89 |
|
|
|
90 |
|
|
::sfz::Query q; |
91 |
persson |
2317 |
q.chan = itControlChangeEvent->Param.CC.Channel + 1; |
92 |
persson |
2115 |
q.key = 60; |
93 |
|
|
q.vel = 127; |
94 |
|
|
q.bend = pChannel->Pitch; |
95 |
|
|
q.bpm = 0; |
96 |
|
|
q.chanaft = pChannel->ControllerTable[128]; |
97 |
|
|
q.polyaft = 0; |
98 |
|
|
q.prog = 0; |
99 |
|
|
q.rand = Random(); |
100 |
|
|
q.cc = pChannel->ControllerTable; |
101 |
|
|
q.timer = 0; |
102 |
|
|
q.sw = pChannel->PressedKeys; |
103 |
|
|
q.last_sw_key = pChannel->LastKeySwitch; |
104 |
|
|
q.prev_sw_key = pChannel->LastKey; |
105 |
|
|
q.trig = TRIGGER_ATTACK | TRIGGER_FIRST; |
106 |
|
|
|
107 |
|
|
q.search(pChannel->pInstrument, cc); |
108 |
|
|
|
109 |
schoenebeck |
2879 |
NoteIterator itNewNote; |
110 |
|
|
|
111 |
persson |
2115 |
int i = 0; |
112 |
|
|
while (::sfz::Region* region = q.next()) { |
113 |
|
|
if (!RegionSuspended(region)) { |
114 |
|
|
itControlChangeEvent->Param.Note.Key = 60; |
115 |
|
|
itControlChangeEvent->Param.Note.Velocity = 127; |
116 |
|
|
itControlChangeEvent->Param.Note.pRegion = region; |
117 |
schoenebeck |
2879 |
if (!itNewNote) { |
118 |
|
|
const note_id_t noteID = LaunchNewNote(pEngineChannel, &*itControlChangeEvent); |
119 |
|
|
itNewNote = GetNotePool()->fromID(noteID); |
120 |
|
|
if (!itNewNote) { |
121 |
|
|
dmsg(1,("sfz::Engine: Note pool empty!\n")); |
122 |
|
|
return; |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
VoiceIterator itNewVoice = |
126 |
|
|
LaunchVoice(pChannel, itControlChangeEvent, i, false, false, true); |
127 |
|
|
if (itNewVoice) |
128 |
|
|
itNewVoice.moveToEndOf(itNewNote->pActiveVoices); |
129 |
persson |
2115 |
} |
130 |
|
|
i++; |
131 |
|
|
} |
132 |
|
|
} |
133 |
iliev |
2012 |
} |
134 |
|
|
|
135 |
schoenebeck |
2559 |
void Engine::ProcessChannelPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itChannelPressureEvent) { |
136 |
schoenebeck |
3015 |
// forward this to the CC routine, so it updates the current aftertouch value |
137 |
|
|
ProcessControlChange(pEngineChannel, itChannelPressureEvent); |
138 |
|
|
|
139 |
schoenebeck |
2559 |
// if required: engine global aftertouch handling (apart from the per voice handling) |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
void Engine::ProcessPolyphonicKeyPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNotePressureEvent) { |
143 |
|
|
// if required: engine global aftertouch handling (apart from the per voice handling) |
144 |
|
|
} |
145 |
|
|
|
146 |
iliev |
2012 |
DiskThread* Engine::CreateDiskThread() { |
147 |
|
|
return new DiskThread ( |
148 |
|
|
iMaxDiskStreams, |
149 |
|
|
((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo |
150 |
|
|
&instruments |
151 |
|
|
); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void Engine::TriggerNewVoices ( |
155 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
156 |
|
|
RTList<Event>::Iterator& itNoteOnEvent, |
157 |
|
|
bool HandleKeyGroupConflicts |
158 |
|
|
) { |
159 |
|
|
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
160 |
schoenebeck |
3034 |
//MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
161 |
persson |
2106 |
::sfz::Query q; |
162 |
persson |
2317 |
q.chan = itNoteOnEvent->Param.Note.Channel + 1; |
163 |
persson |
2101 |
q.key = itNoteOnEvent->Param.Note.Key; |
164 |
|
|
q.vel = itNoteOnEvent->Param.Note.Velocity; |
165 |
|
|
q.bend = pChannel->Pitch; |
166 |
|
|
q.bpm = 0; |
167 |
|
|
q.chanaft = pChannel->ControllerTable[128]; |
168 |
|
|
q.polyaft = 0; |
169 |
|
|
q.prog = 0; |
170 |
|
|
q.rand = Random(); |
171 |
|
|
q.cc = pChannel->ControllerTable; |
172 |
|
|
q.timer = 0; |
173 |
|
|
q.sw = pChannel->PressedKeys; |
174 |
|
|
q.last_sw_key = pChannel->LastKeySwitch; |
175 |
|
|
q.prev_sw_key = pChannel->LastKey; |
176 |
|
|
q.trig = TRIGGER_ATTACK | |
177 |
|
|
((pChannel->LastKey != -1 && |
178 |
|
|
pChannel->PressedKeys[pChannel->LastKey] && |
179 |
|
|
pChannel->LastKey != q.key) ? |
180 |
|
|
TRIGGER_LEGATO : TRIGGER_FIRST); |
181 |
iliev |
2012 |
|
182 |
persson |
2106 |
q.search(pChannel->pInstrument); |
183 |
|
|
|
184 |
schoenebeck |
2879 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID); |
185 |
|
|
if (!itNote) { |
186 |
|
|
dmsg(1,("sfz::Engine: No Note object for triggering new voices!\n")); |
187 |
|
|
return; |
188 |
|
|
} |
189 |
|
|
|
190 |
persson |
2101 |
int i = 0; |
191 |
|
|
while (::sfz::Region* region = q.next()) { |
192 |
|
|
if (!RegionSuspended(region)) { |
193 |
|
|
itNoteOnEvent->Param.Note.pRegion = region; |
194 |
schoenebeck |
2879 |
VoiceIterator itNewVoice = |
195 |
|
|
LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts); |
196 |
|
|
if (itNewVoice) |
197 |
|
|
itNewVoice.moveToEndOf(itNote->pActiveVoices); |
198 |
iliev |
2012 |
} |
199 |
persson |
2101 |
i++; |
200 |
iliev |
2012 |
} |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
void Engine::TriggerReleaseVoices ( |
204 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
205 |
|
|
RTList<Event>::Iterator& itNoteOffEvent |
206 |
|
|
) { |
207 |
|
|
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
208 |
persson |
2106 |
::sfz::Query q; |
209 |
persson |
2317 |
q.chan = itNoteOffEvent->Param.Note.Channel + 1; |
210 |
persson |
2101 |
q.key = itNoteOffEvent->Param.Note.Key; |
211 |
persson |
2061 |
|
212 |
|
|
// MIDI note-on velocity is used instead of note-off velocity |
213 |
persson |
2101 |
q.vel = pChannel->pMIDIKeyInfo[q.key].Velocity; |
214 |
|
|
itNoteOffEvent->Param.Note.Velocity = q.vel; |
215 |
persson |
2061 |
|
216 |
persson |
2101 |
q.bend = pChannel->Pitch; |
217 |
|
|
q.bpm = 0; |
218 |
|
|
q.chanaft = pChannel->ControllerTable[128]; |
219 |
|
|
q.polyaft = 0; |
220 |
|
|
q.prog = 0; |
221 |
|
|
q.rand = Random(); |
222 |
|
|
q.cc = pChannel->ControllerTable; |
223 |
|
|
q.timer = 0; |
224 |
|
|
q.sw = pChannel->PressedKeys; |
225 |
|
|
q.last_sw_key = pChannel->LastKeySwitch; |
226 |
|
|
q.prev_sw_key = pChannel->LastKey; |
227 |
|
|
q.trig = TRIGGER_RELEASE; |
228 |
iliev |
2012 |
|
229 |
persson |
2106 |
q.search(pChannel->pInstrument); |
230 |
|
|
|
231 |
schoenebeck |
2879 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOffEvent->Param.Note.ID); |
232 |
|
|
if (!itNote) { |
233 |
|
|
dmsg(1,("sfz::Engine: No Note object for triggering new release voices!\n")); |
234 |
|
|
return; |
235 |
|
|
} |
236 |
|
|
|
237 |
iliev |
2012 |
// now launch the required amount of voices |
238 |
persson |
2101 |
int i = 0; |
239 |
|
|
while (::sfz::Region* region = q.next()) { |
240 |
|
|
itNoteOffEvent->Param.Note.pRegion = region; |
241 |
schoenebeck |
2879 |
VoiceIterator itNewVoice = |
242 |
|
|
LaunchVoice(pChannel, itNoteOffEvent, i, true, false, true); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
243 |
|
|
if (itNewVoice) |
244 |
|
|
itNewVoice.moveToEndOf(itNote->pActiveVoices); |
245 |
persson |
2101 |
i++; |
246 |
iliev |
2012 |
} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
Pool<Voice>::Iterator Engine::LaunchVoice ( |
250 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
251 |
|
|
Pool<Event>::Iterator& itNoteOnEvent, |
252 |
|
|
int iLayer, |
253 |
|
|
bool ReleaseTriggerVoice, |
254 |
|
|
bool VoiceStealing, |
255 |
|
|
bool HandleKeyGroupConflicts |
256 |
|
|
) { |
257 |
|
|
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
258 |
schoenebeck |
3034 |
//int key = itNoteOnEvent->Param.Note.Key; |
259 |
schoenebeck |
2879 |
//EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key]; |
260 |
persson |
2115 |
::sfz::Region* pRgn = static_cast< ::sfz::Region*>(itNoteOnEvent->Param.Note.pRegion); |
261 |
iliev |
2012 |
|
262 |
persson |
2115 |
Voice::type_t VoiceType = |
263 |
|
|
itNoteOnEvent->Type == Event::type_control_change ? Voice::type_controller_triggered : |
264 |
|
|
ReleaseTriggerVoice ? Voice::type_release_trigger : |
265 |
|
|
iLayer == 0 ? Voice::type_release_trigger_required : |
266 |
|
|
Voice::type_normal; |
267 |
|
|
if (pRgn->loop_mode == ::sfz::ONE_SHOT) VoiceType |= Voice::type_one_shot; |
268 |
|
|
|
269 |
iliev |
2012 |
Pool<Voice>::Iterator itNewVoice; |
270 |
|
|
|
271 |
persson |
2317 |
if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(pRgn->group, itNoteOnEvent); |
272 |
|
|
|
273 |
iliev |
2012 |
// no need to process if sample is silent |
274 |
persson |
2317 |
if (!pRgn->GetSample(false) || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator(); |
275 |
iliev |
2012 |
|
276 |
|
|
// allocate a new voice for the key |
277 |
schoenebeck |
2879 |
itNewVoice = GetVoicePool()->allocAppend(); |
278 |
iliev |
2027 |
int res = InitNewVoice ( |
279 |
|
|
pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer, |
280 |
persson |
2114 |
pRgn->off_by, ReleaseTriggerVoice, VoiceStealing, itNewVoice |
281 |
iliev |
2027 |
); |
282 |
|
|
if (!res) return itNewVoice; |
283 |
iliev |
2012 |
|
284 |
|
|
// return if this is a release triggered voice and there is no |
285 |
|
|
// releasetrigger dimension (could happen if an instrument |
286 |
|
|
// change has occured between note on and off) |
287 |
|
|
//if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator(); |
288 |
|
|
|
289 |
iliev |
2027 |
return Pool<Voice>::Iterator(); // no free voice or error |
290 |
iliev |
2012 |
} |
291 |
|
|
|
292 |
|
|
bool Engine::DiskStreamSupported() { |
293 |
|
|
return true; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
String Engine::Description() { |
297 |
|
|
return "SFZ Format Engine"; |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
String Engine::Version() { |
301 |
schoenebeck |
2494 |
String s = "$Revision$"; |
302 |
iliev |
2012 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
}} // namespace LinuxSampler::sfz |