1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009-2010 Christian Schoenebeck and Grigor Iliev * |
8 |
* * |
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 |
#include "InstrumentScriptVM.h" |
28 |
|
29 |
namespace LinuxSampler { namespace gig { |
30 |
Engine::Format Engine::GetEngineFormat() { return GIG; } |
31 |
|
32 |
void Engine::CreateInstrumentScriptVM() { |
33 |
dmsg(2,("gig::Engine created Giga format scriptvm\n")); |
34 |
if (pScriptVM) return; |
35 |
pScriptVM = new InstrumentScriptVM; // gig format specific extended script runner |
36 |
} |
37 |
|
38 |
/** |
39 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
40 |
* modulation wheel, aftertouch). |
41 |
* |
42 |
* @param pEngineChannel - engine channel on which this event occured on |
43 |
* @param itControlChangeEvent - controller, value and time stamp of the event |
44 |
*/ |
45 |
void Engine::ProcessControlChange ( |
46 |
LinuxSampler::EngineChannel* pEngineChannel, |
47 |
Pool<Event>::Iterator& itControlChangeEvent |
48 |
) { |
49 |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
50 |
|
51 |
EngineChannel* pChannel = dynamic_cast<EngineChannel*>(pEngineChannel); |
52 |
// handle the "control triggered" MIDI rule: a control change |
53 |
// event can trigger a new note on or note off event |
54 |
if (pChannel->pInstrument) { |
55 |
|
56 |
::gig::MidiRule* rule; |
57 |
for (int i = 0 ; (rule = pChannel->pInstrument->GetMidiRule(i)) ; i++) { |
58 |
|
59 |
if (::gig::MidiRuleCtrlTrigger* ctrlTrigger = |
60 |
dynamic_cast< ::gig::MidiRuleCtrlTrigger*>(rule)) { |
61 |
if (itControlChangeEvent->Param.CC.Controller == |
62 |
ctrlTrigger->ControllerNumber) { |
63 |
|
64 |
uint8_t oldCCValue = pChannel->ControllerTable[ |
65 |
itControlChangeEvent->Param.CC.Controller]; |
66 |
uint8_t newCCValue = itControlChangeEvent->Param.CC.Value; |
67 |
|
68 |
for (int i = 0 ; i < ctrlTrigger->Triggers ; i++) { |
69 |
::gig::MidiRuleCtrlTrigger::trigger_t* pTrigger = |
70 |
&ctrlTrigger->pTriggers[i]; |
71 |
|
72 |
// check if the controller has passed the |
73 |
// trigger point in the right direction |
74 |
if ((pTrigger->Descending && |
75 |
oldCCValue > pTrigger->TriggerPoint && |
76 |
newCCValue <= pTrigger->TriggerPoint) || |
77 |
(!pTrigger->Descending && |
78 |
oldCCValue < pTrigger->TriggerPoint && |
79 |
newCCValue >= pTrigger->TriggerPoint)) { |
80 |
|
81 |
RTList<Event>::Iterator itNewEvent = pGlobalEvents->allocAppend(); |
82 |
if (itNewEvent) { |
83 |
*itNewEvent = *itControlChangeEvent; |
84 |
itNewEvent->Param.Note.Key = pTrigger->Key; |
85 |
|
86 |
if (pTrigger->NoteOff || pTrigger->Velocity == 0) { |
87 |
itNewEvent->Type = Event::type_note_off; |
88 |
itNewEvent->Param.Note.Velocity = 100; |
89 |
|
90 |
ProcessNoteOff(pEngineChannel, itNewEvent); |
91 |
} else { |
92 |
itNewEvent->Type = Event::type_note_on; |
93 |
//TODO: if Velocity is 255, the triggered velocity should |
94 |
// depend on how fast the controller is moving |
95 |
itNewEvent->Param.Note.Velocity = |
96 |
pTrigger->Velocity == 255 ? 100 : |
97 |
pTrigger->Velocity; |
98 |
|
99 |
ProcessNoteOn(pEngineChannel, itNewEvent); |
100 |
} |
101 |
} |
102 |
else dmsg(1,("Event pool emtpy!\n")); |
103 |
} |
104 |
} |
105 |
} |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
// update controller value in the engine channel's controller table |
111 |
pChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
112 |
|
113 |
ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent); |
114 |
|
115 |
// handle FX send controllers |
116 |
ProcessFxSendControllers(pChannel, itControlChangeEvent); |
117 |
} |
118 |
|
119 |
void Engine::ProcessChannelPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itChannelPressureEvent) { |
120 |
// forward this to the CC routine, so it updates the current aftertouch value and may handle aftertouch trigger rules |
121 |
ProcessControlChange(pEngineChannel, itChannelPressureEvent); |
122 |
|
123 |
// if required: engine global aftertouch handling (apart from the per voice handling) |
124 |
} |
125 |
|
126 |
void Engine::ProcessPolyphonicKeyPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNotePressureEvent) { |
127 |
// if required: engine global aftertouch handling (apart from the per voice handling) |
128 |
} |
129 |
|
130 |
DiskThread* Engine::CreateDiskThread() { |
131 |
return new DiskThread ( |
132 |
iMaxDiskStreams, |
133 |
((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo |
134 |
&instruments |
135 |
); |
136 |
} |
137 |
|
138 |
void Engine::TriggerNewVoices ( |
139 |
LinuxSampler::EngineChannel* pEngineChannel, |
140 |
RTList<Event>::Iterator& itNoteOnEvent, |
141 |
bool HandleKeyGroupConflicts |
142 |
) { |
143 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID); |
144 |
if (!itNote) { |
145 |
dmsg(1,("gig::Engine: No Note object for triggering new voices!\n")); |
146 |
return; |
147 |
} |
148 |
|
149 |
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
150 |
|
151 |
// first, get total amount of required voices (dependant on amount of layers) |
152 |
// (using the note's MIDI note number instead of the MIDI event's one, |
153 |
// because an instrument script might have modified the note number) |
154 |
::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNote->cause.Param.Note.Key); |
155 |
if (!pRegion || RegionSuspended(pRegion)) |
156 |
return; |
157 |
const int voicesRequired = pRegion->Layers; |
158 |
if (voicesRequired <= 0) |
159 |
return; |
160 |
|
161 |
// now launch the required amount of voices |
162 |
for (int i = 0; i < voicesRequired; i++) { |
163 |
VoiceIterator itNewVoice = |
164 |
LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts); |
165 |
if (!itNewVoice) continue; |
166 |
itNewVoice.moveToEndOf(itNote->pActiveVoices); |
167 |
} |
168 |
} |
169 |
|
170 |
void Engine::TriggerReleaseVoices ( |
171 |
LinuxSampler::EngineChannel* pEngineChannel, |
172 |
RTList<Event>::Iterator& itNoteOffEvent |
173 |
) { |
174 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOffEvent->Param.Note.ID); |
175 |
if (!itNote) { |
176 |
dmsg(1,("gig::Engine: No Note object for triggering new release voices!\n")); |
177 |
return; |
178 |
} |
179 |
|
180 |
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
181 |
MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
182 |
// first, get total amount of required voices (dependant on amount of layers) |
183 |
// (using the note's MIDI note number instead of the MIDI event's one, |
184 |
// because an instrument script might have modified the note number) |
185 |
::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNote->cause.Param.Note.Key); |
186 |
if (!pRegion) |
187 |
return; |
188 |
const int voicesRequired = pRegion->Layers; |
189 |
if (voicesRequired <= 0) |
190 |
return; |
191 |
|
192 |
// MIDI note-on velocity is used instead of note-off velocity |
193 |
itNoteOffEvent->Param.Note.Velocity = pKey->Velocity; |
194 |
|
195 |
// now launch the required amount of voices |
196 |
for (int i = 0; i < voicesRequired; i++) { |
197 |
VoiceIterator itNewVoice = |
198 |
LaunchVoice(pChannel, itNoteOffEvent, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
199 |
if (!itNewVoice) continue; |
200 |
itNewVoice.moveToEndOf(itNote->pActiveVoices); |
201 |
} |
202 |
} |
203 |
|
204 |
Pool<Voice>::Iterator Engine::LaunchVoice ( |
205 |
LinuxSampler::EngineChannel* pEngineChannel, |
206 |
Pool<Event>::Iterator& itNoteOnEvent, |
207 |
int iLayer, |
208 |
bool ReleaseTriggerVoice, |
209 |
bool VoiceStealing, |
210 |
bool HandleKeyGroupConflicts |
211 |
) { |
212 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID); |
213 |
if (!itNote) { |
214 |
dmsg(1,("gig::Engine: No Note object for launching voices!\n")); |
215 |
return Pool<Voice>::Iterator(); |
216 |
} |
217 |
|
218 |
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
219 |
// the note's MIDI note number might differ from the event's note number |
220 |
// because a script might have modified the note's note number |
221 |
int MIDIKey = itNoteOnEvent->Param.Note.Key; |
222 |
int NoteKey = itNote->cause.Param.Note.Key; |
223 |
//EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[MIDIKey]; |
224 |
::gig::Region* pRegion = pChannel->pInstrument->GetRegion(NoteKey); |
225 |
|
226 |
// if nothing defined for this key |
227 |
if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do |
228 |
|
229 |
int iKeyGroup = pRegion->KeyGroup; |
230 |
// only need to send a group event from the first voice in a layered region, |
231 |
// as all layers in a region always belongs to the same key group |
232 |
if (HandleKeyGroupConflicts && iLayer == 0) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent); |
233 |
|
234 |
Voice::type_t VoiceType = Voice::type_normal; |
235 |
|
236 |
// get current dimension values to select the right dimension region |
237 |
//TODO: for stolen voices this dimension region selection block is processed twice, this should be changed |
238 |
//FIXME: controller values for selecting the dimension region here are currently not sample accurate |
239 |
uint DimValues[8] = { 0 }; |
240 |
for (int i = pRegion->Dimensions - 1; i >= 0; i--) { |
241 |
switch (pRegion->pDimensionDefinitions[i].dimension) { |
242 |
case ::gig::dimension_samplechannel: |
243 |
DimValues[i] = 0; //TODO: we currently ignore this dimension |
244 |
break; |
245 |
case ::gig::dimension_layer: |
246 |
DimValues[i] = iLayer; |
247 |
break; |
248 |
case ::gig::dimension_velocity: |
249 |
DimValues[i] = itNote->cause.Param.Note.Velocity; |
250 |
break; |
251 |
case ::gig::dimension_channelaftertouch: |
252 |
DimValues[i] = pChannel->ControllerTable[128]; |
253 |
break; |
254 |
case ::gig::dimension_releasetrigger: |
255 |
VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal; |
256 |
DimValues[i] = (uint) ReleaseTriggerVoice; |
257 |
break; |
258 |
case ::gig::dimension_keyboard: |
259 |
DimValues[i] = (uint) (pChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones); |
260 |
break; |
261 |
case ::gig::dimension_roundrobin: |
262 |
DimValues[i] = uint(*pChannel->pMIDIKeyInfo[MIDIKey].pRoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on in this Region |
263 |
break; |
264 |
case ::gig::dimension_roundrobinkeyboard: |
265 |
DimValues[i] = uint(pChannel->RoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on |
266 |
break; |
267 |
case ::gig::dimension_random: |
268 |
DimValues[i] = uint(Random() * pRegion->pDimensionDefinitions[i].zones); |
269 |
break; |
270 |
case ::gig::dimension_smartmidi: |
271 |
DimValues[i] = 0; |
272 |
break; |
273 |
case ::gig::dimension_modwheel: |
274 |
DimValues[i] = pChannel->ControllerTable[1]; |
275 |
break; |
276 |
case ::gig::dimension_breath: |
277 |
DimValues[i] = pChannel->ControllerTable[2]; |
278 |
break; |
279 |
case ::gig::dimension_foot: |
280 |
DimValues[i] = pChannel->ControllerTable[4]; |
281 |
break; |
282 |
case ::gig::dimension_portamentotime: |
283 |
DimValues[i] = pChannel->ControllerTable[5]; |
284 |
break; |
285 |
case ::gig::dimension_effect1: |
286 |
DimValues[i] = pChannel->ControllerTable[12]; |
287 |
break; |
288 |
case ::gig::dimension_effect2: |
289 |
DimValues[i] = pChannel->ControllerTable[13]; |
290 |
break; |
291 |
case ::gig::dimension_genpurpose1: |
292 |
DimValues[i] = pChannel->ControllerTable[16]; |
293 |
break; |
294 |
case ::gig::dimension_genpurpose2: |
295 |
DimValues[i] = pChannel->ControllerTable[17]; |
296 |
break; |
297 |
case ::gig::dimension_genpurpose3: |
298 |
DimValues[i] = pChannel->ControllerTable[18]; |
299 |
break; |
300 |
case ::gig::dimension_genpurpose4: |
301 |
DimValues[i] = pChannel->ControllerTable[19]; |
302 |
break; |
303 |
case ::gig::dimension_sustainpedal: |
304 |
DimValues[i] = pChannel->ControllerTable[64]; |
305 |
break; |
306 |
case ::gig::dimension_portamento: |
307 |
DimValues[i] = pChannel->ControllerTable[65]; |
308 |
break; |
309 |
case ::gig::dimension_sostenutopedal: |
310 |
DimValues[i] = pChannel->ControllerTable[66]; |
311 |
break; |
312 |
case ::gig::dimension_softpedal: |
313 |
DimValues[i] = pChannel->ControllerTable[67]; |
314 |
break; |
315 |
case ::gig::dimension_genpurpose5: |
316 |
DimValues[i] = pChannel->ControllerTable[80]; |
317 |
break; |
318 |
case ::gig::dimension_genpurpose6: |
319 |
DimValues[i] = pChannel->ControllerTable[81]; |
320 |
break; |
321 |
case ::gig::dimension_genpurpose7: |
322 |
DimValues[i] = pChannel->ControllerTable[82]; |
323 |
break; |
324 |
case ::gig::dimension_genpurpose8: |
325 |
DimValues[i] = pChannel->ControllerTable[83]; |
326 |
break; |
327 |
case ::gig::dimension_effect1depth: |
328 |
DimValues[i] = pChannel->ControllerTable[91]; |
329 |
break; |
330 |
case ::gig::dimension_effect2depth: |
331 |
DimValues[i] = pChannel->ControllerTable[92]; |
332 |
break; |
333 |
case ::gig::dimension_effect3depth: |
334 |
DimValues[i] = pChannel->ControllerTable[93]; |
335 |
break; |
336 |
case ::gig::dimension_effect4depth: |
337 |
DimValues[i] = pChannel->ControllerTable[94]; |
338 |
break; |
339 |
case ::gig::dimension_effect5depth: |
340 |
DimValues[i] = pChannel->ControllerTable[95]; |
341 |
break; |
342 |
case ::gig::dimension_none: |
343 |
std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush; |
344 |
break; |
345 |
default: |
346 |
std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush; |
347 |
} |
348 |
} |
349 |
|
350 |
// return if this is a release triggered voice and there is no |
351 |
// releasetrigger dimension (could happen if an instrument |
352 |
// change has occured between note on and off) |
353 |
if (ReleaseTriggerVoice && !(VoiceType & Voice::type_release_trigger)) return Pool<Voice>::Iterator(); |
354 |
|
355 |
::gig::DimensionRegion* pDimRgn; |
356 |
if (!itNote->Format.Gig.DimMask) { // normal case ... |
357 |
pDimRgn = pRegion->GetDimensionRegionByValue(DimValues); |
358 |
} else { // some dimension zones were overridden (i.e. by instrument script) ... |
359 |
dmsg(3,("trigger with dim mask=%d val=%d\n", itNote->Format.Gig.DimMask, itNote->Format.Gig.DimBits)); |
360 |
int index = pRegion->GetDimensionRegionIndexByValue(DimValues); |
361 |
index &= ~itNote->Format.Gig.DimMask; |
362 |
index |= itNote->Format.Gig.DimBits & itNote->Format.Gig.DimMask; |
363 |
pDimRgn = pRegion->pDimensionRegions[index & 255]; |
364 |
} |
365 |
if (!pDimRgn) return Pool<Voice>::Iterator(); // error (could not resolve dimension region) |
366 |
|
367 |
// no need to continue if sample is silent |
368 |
if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator(); |
369 |
|
370 |
dmsg(2,("sample -> \"%s\"\n", pDimRgn->pSample->pInfo->Name.c_str())); |
371 |
|
372 |
// allocate a new voice for the key |
373 |
Pool<Voice>::Iterator itNewVoice = GetVoicePool()->allocAppend(); |
374 |
|
375 |
int res = InitNewVoice ( |
376 |
pChannel, pDimRgn, itNoteOnEvent, VoiceType, iLayer, |
377 |
iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice |
378 |
); |
379 |
if (!res) return itNewVoice; |
380 |
|
381 |
return Pool<Voice>::Iterator(); // no free voice or error |
382 |
} |
383 |
|
384 |
bool Engine::DiskStreamSupported() { |
385 |
return true; |
386 |
} |
387 |
|
388 |
String Engine::Description() { |
389 |
return "GigaSampler Format Engine"; |
390 |
} |
391 |
|
392 |
String Engine::Version() { |
393 |
String s = "$Revision$"; |
394 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
395 |
} |
396 |
|
397 |
}} // namespace LinuxSampler::gig |