/[svn]/linuxsampler/trunk/src/engines/gig/Engine.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2879 - (show annotations) (download)
Tue Apr 19 14:07:53 2016 UTC (8 years ago) by schoenebeck
File size: 18868 byte(s)
* All engines: Active voices are now internally grouped to "Note" objects,
  instead of being directly assigned to a keyboard key. This allows more
  fine graded processing of voices, which is i.e. required for certain
  instrument script features.
* Built-in script function "play_note()": Added support for passing
  special value -1 for "duration-us" argument, which will cause the
  triggered note to be released once the original note was released.
* Bumped version (2.0.0.svn3).

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 // if required: engine global aftertouch handling (apart from the per voice handling)
121 }
122
123 void Engine::ProcessPolyphonicKeyPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNotePressureEvent) {
124 // if required: engine global aftertouch handling (apart from the per voice handling)
125 }
126
127 DiskThread* Engine::CreateDiskThread() {
128 return new DiskThread (
129 iMaxDiskStreams,
130 ((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo
131 &instruments
132 );
133 }
134
135 void Engine::TriggerNewVoices (
136 LinuxSampler::EngineChannel* pEngineChannel,
137 RTList<Event>::Iterator& itNoteOnEvent,
138 bool HandleKeyGroupConflicts
139 ) {
140 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
141 // first, get total amount of required voices (dependant on amount of layers)
142 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
143 if (!pRegion || RegionSuspended(pRegion))
144 return;
145 const int voicesRequired = pRegion->Layers;
146 if (voicesRequired <= 0)
147 return;
148
149 NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID);
150 if (!itNote) {
151 dmsg(1,("gig::Engine: No Note object for triggering new voices!\n"));
152 return;
153 }
154
155 // now launch the required amount of voices
156 for (int i = 0; i < voicesRequired; i++) {
157 VoiceIterator itNewVoice =
158 LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts);
159 if (!itNewVoice) continue;
160 itNewVoice.moveToEndOf(itNote->pActiveVoices);
161 }
162 }
163
164 void Engine::TriggerReleaseVoices (
165 LinuxSampler::EngineChannel* pEngineChannel,
166 RTList<Event>::Iterator& itNoteOffEvent
167 ) {
168 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
169 MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
170 // first, get total amount of required voices (dependant on amount of layers)
171 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNoteOffEvent->Param.Note.Key);
172 if (!pRegion)
173 return;
174 const int voicesRequired = pRegion->Layers;
175 if (voicesRequired <= 0)
176 return;
177
178 NoteIterator itNote = GetNotePool()->fromID(itNoteOffEvent->Param.Note.ID);
179 if (!itNote) {
180 dmsg(1,("gig::Engine: No Note object for triggering new release voices!\n"));
181 return;
182 }
183
184 // MIDI note-on velocity is used instead of note-off velocity
185 itNoteOffEvent->Param.Note.Velocity = pKey->Velocity;
186
187 // now launch the required amount of voices
188 for (int i = 0; i < voicesRequired; i++) {
189 VoiceIterator itNewVoice =
190 LaunchVoice(pChannel, itNoteOffEvent, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
191 if (!itNewVoice) continue;
192 itNewVoice.moveToEndOf(itNote->pActiveVoices);
193 }
194 }
195
196 Pool<Voice>::Iterator Engine::LaunchVoice (
197 LinuxSampler::EngineChannel* pEngineChannel,
198 Pool<Event>::Iterator& itNoteOnEvent,
199 int iLayer,
200 bool ReleaseTriggerVoice,
201 bool VoiceStealing,
202 bool HandleKeyGroupConflicts
203 ) {
204 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
205 int MIDIKey = itNoteOnEvent->Param.Note.Key;
206 //EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[MIDIKey];
207 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(MIDIKey);
208
209 // if nothing defined for this key
210 if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
211
212 int iKeyGroup = pRegion->KeyGroup;
213 // only need to send a group event from the first voice in a layered region,
214 // as all layers in a region always belongs to the same key group
215 if (HandleKeyGroupConflicts && iLayer == 0) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent);
216
217 Voice::type_t VoiceType = Voice::type_normal;
218
219 // get current dimension values to select the right dimension region
220 //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
221 //FIXME: controller values for selecting the dimension region here are currently not sample accurate
222 uint DimValues[8] = { 0 };
223 for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
224 switch (pRegion->pDimensionDefinitions[i].dimension) {
225 case ::gig::dimension_samplechannel:
226 DimValues[i] = 0; //TODO: we currently ignore this dimension
227 break;
228 case ::gig::dimension_layer:
229 DimValues[i] = iLayer;
230 break;
231 case ::gig::dimension_velocity:
232 DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
233 break;
234 case ::gig::dimension_channelaftertouch:
235 DimValues[i] = pChannel->ControllerTable[128];
236 break;
237 case ::gig::dimension_releasetrigger:
238 VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
239 DimValues[i] = (uint) ReleaseTriggerVoice;
240 break;
241 case ::gig::dimension_keyboard:
242 DimValues[i] = (uint) (pChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones);
243 break;
244 case ::gig::dimension_roundrobin:
245 DimValues[i] = uint(*pChannel->pMIDIKeyInfo[MIDIKey].pRoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on in this Region
246 break;
247 case ::gig::dimension_roundrobinkeyboard:
248 DimValues[i] = uint(pChannel->RoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on
249 break;
250 case ::gig::dimension_random:
251 DimValues[i] = uint(Random() * pRegion->pDimensionDefinitions[i].zones);
252 break;
253 case ::gig::dimension_smartmidi:
254 DimValues[i] = 0;
255 break;
256 case ::gig::dimension_modwheel:
257 DimValues[i] = pChannel->ControllerTable[1];
258 break;
259 case ::gig::dimension_breath:
260 DimValues[i] = pChannel->ControllerTable[2];
261 break;
262 case ::gig::dimension_foot:
263 DimValues[i] = pChannel->ControllerTable[4];
264 break;
265 case ::gig::dimension_portamentotime:
266 DimValues[i] = pChannel->ControllerTable[5];
267 break;
268 case ::gig::dimension_effect1:
269 DimValues[i] = pChannel->ControllerTable[12];
270 break;
271 case ::gig::dimension_effect2:
272 DimValues[i] = pChannel->ControllerTable[13];
273 break;
274 case ::gig::dimension_genpurpose1:
275 DimValues[i] = pChannel->ControllerTable[16];
276 break;
277 case ::gig::dimension_genpurpose2:
278 DimValues[i] = pChannel->ControllerTable[17];
279 break;
280 case ::gig::dimension_genpurpose3:
281 DimValues[i] = pChannel->ControllerTable[18];
282 break;
283 case ::gig::dimension_genpurpose4:
284 DimValues[i] = pChannel->ControllerTable[19];
285 break;
286 case ::gig::dimension_sustainpedal:
287 DimValues[i] = pChannel->ControllerTable[64];
288 break;
289 case ::gig::dimension_portamento:
290 DimValues[i] = pChannel->ControllerTable[65];
291 break;
292 case ::gig::dimension_sostenutopedal:
293 DimValues[i] = pChannel->ControllerTable[66];
294 break;
295 case ::gig::dimension_softpedal:
296 DimValues[i] = pChannel->ControllerTable[67];
297 break;
298 case ::gig::dimension_genpurpose5:
299 DimValues[i] = pChannel->ControllerTable[80];
300 break;
301 case ::gig::dimension_genpurpose6:
302 DimValues[i] = pChannel->ControllerTable[81];
303 break;
304 case ::gig::dimension_genpurpose7:
305 DimValues[i] = pChannel->ControllerTable[82];
306 break;
307 case ::gig::dimension_genpurpose8:
308 DimValues[i] = pChannel->ControllerTable[83];
309 break;
310 case ::gig::dimension_effect1depth:
311 DimValues[i] = pChannel->ControllerTable[91];
312 break;
313 case ::gig::dimension_effect2depth:
314 DimValues[i] = pChannel->ControllerTable[92];
315 break;
316 case ::gig::dimension_effect3depth:
317 DimValues[i] = pChannel->ControllerTable[93];
318 break;
319 case ::gig::dimension_effect4depth:
320 DimValues[i] = pChannel->ControllerTable[94];
321 break;
322 case ::gig::dimension_effect5depth:
323 DimValues[i] = pChannel->ControllerTable[95];
324 break;
325 case ::gig::dimension_none:
326 std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
327 break;
328 default:
329 std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
330 }
331 }
332
333 // return if this is a release triggered voice and there is no
334 // releasetrigger dimension (could happen if an instrument
335 // change has occured between note on and off)
336 if (ReleaseTriggerVoice && !(VoiceType & Voice::type_release_trigger)) return Pool<Voice>::Iterator();
337
338 NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID);
339
340 ::gig::DimensionRegion* pDimRgn;
341 if (!itNote->Format.Gig.DimMask) { // normal case ...
342 pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
343 } else { // some dimension zones were overridden (i.e. by instrument script) ...
344 dmsg(3,("trigger with dim mask=%d val=%d\n", itNote->Format.Gig.DimMask, itNote->Format.Gig.DimBits));
345 int index = pRegion->GetDimensionRegionIndexByValue(DimValues);
346 index &= ~itNote->Format.Gig.DimMask;
347 index |= itNote->Format.Gig.DimBits & itNote->Format.Gig.DimMask;
348 pDimRgn = pRegion->pDimensionRegions[index & 255];
349 }
350 if (!pDimRgn) return Pool<Voice>::Iterator(); // error (could not resolve dimension region)
351
352 // no need to continue if sample is silent
353 if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
354
355 // allocate a new voice for the key
356 Pool<Voice>::Iterator itNewVoice = GetVoicePool()->allocAppend();
357
358 int res = InitNewVoice (
359 pChannel, pDimRgn, itNoteOnEvent, VoiceType, iLayer,
360 iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice
361 );
362 if (!res) return itNewVoice;
363
364 return Pool<Voice>::Iterator(); // no free voice or error
365 }
366
367 bool Engine::DiskStreamSupported() {
368 return true;
369 }
370
371 String Engine::Description() {
372 return "GigaSampler Format Engine";
373 }
374
375 String Engine::Version() {
376 String s = "$Revision$";
377 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
378 }
379
380 }} // namespace LinuxSampler::gig

Properties

Name Value
svn:keywords Revision

  ViewVC Help
Powered by ViewVC