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