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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2175 - (show annotations) (download)
Mon Apr 25 08:12:36 2011 UTC (12 years, 11 months ago) by persson
File size: 21907 byte(s)
* sfz engine: implemeted filters. Filter types: lowpass, bandpass,
  bandreject and highpass. 1, 2, 4 and 6 pole filters. Opcodes:
  fil_type, cutoff, resonance, fil_veltrack, fil_keytrack,
  fil_keycenter, cutoff_cc, cutoff_chanaft.
* sfz engine: bugfix: zero ampeg_sustain didn't work
* gig engine: bugfix: pitch LFO controller "internal+aftertouch" was broken
* gig engine: bugfix: filter keyboard tracking was broken
* gig engine: filter performance fix (an unnecessary copy was made of
  the filter parameters in each sub fragment)
* ASIO driver: fixes for newer gcc versions (fix from PortAudio)

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 - 2011 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 "../../common/Features.h"
26 #include "Synthesizer.h"
27 #include "Profiler.h"
28 #include "Engine.h"
29 #include "EngineChannel.h"
30
31 #include "Voice.h"
32
33 namespace LinuxSampler { namespace gig {
34
35 Voice::Voice() {
36 pEngine = NULL;
37 pEG1 = &EG1;
38 pEG2 = &EG2;
39 }
40
41 Voice::~Voice() {
42 }
43
44 EngineChannel* Voice::GetGigEngineChannel() {
45 return static_cast<EngineChannel*>(pEngineChannel);
46 }
47
48 void Voice::SetEngine(LinuxSampler::Engine* pEngine) {
49 Engine* engine = static_cast<Engine*>(pEngine);
50 this->pEngine = engine;
51 this->pDiskThread = engine->pDiskThread;
52 dmsg(6,("Voice::SetEngine()\n"));
53 }
54
55 Voice::SampleInfo Voice::GetSampleInfo() {
56 SampleInfo si;
57 si.SampleRate = pSample->SamplesPerSecond;
58 si.ChannelCount = pSample->Channels;
59 si.FrameSize = pSample->FrameSize;
60 si.BitDepth = pSample->BitDepth;
61 si.TotalFrameCount = pSample->SamplesTotal;
62
63 si.HasLoops = pRegion->SampleLoops;
64 si.LoopStart = (si.HasLoops) ? pRegion->pSampleLoops[0].LoopStart : 0;
65 si.LoopLength = (si.HasLoops) ? pRegion->pSampleLoops[0].LoopLength : 0;
66 si.LoopPlayCount = pSample->LoopPlayCount;
67 si.Unpitched = !pRegion->PitchTrack;
68
69 return si;
70 }
71
72 Voice::RegionInfo Voice::GetRegionInfo() {
73 RegionInfo ri;
74 ri.UnityNote = pRegion->UnityNote;
75 ri.FineTune = pRegion->FineTune;
76 ri.Pan = pRegion->Pan;
77 ri.SampleStartOffset = pRegion->SampleStartOffset;
78
79 ri.EG1PreAttack = pRegion->EG1PreAttack;
80 ri.EG1Attack = pRegion->EG1Attack;
81 ri.EG1Hold = pRegion->EG1Hold;
82 ri.EG1Decay1 = pRegion->EG1Decay1;
83 ri.EG1Decay2 = pRegion->EG1Decay2;
84 ri.EG1Sustain = pRegion->EG1Sustain;
85 ri.EG1InfiniteSustain = pRegion->EG1InfiniteSustain;
86 ri.EG1Release = pRegion->EG1Release;
87
88 ri.EG2PreAttack = pRegion->EG2PreAttack;
89 ri.EG2Attack = pRegion->EG2Attack;
90 ri.EG2Decay1 = pRegion->EG2Decay1;
91 ri.EG2Decay2 = pRegion->EG2Decay2;
92 ri.EG2Sustain = pRegion->EG2Sustain;
93 ri.EG2InfiniteSustain = pRegion->EG2InfiniteSustain;
94 ri.EG2Release = pRegion->EG2Release;
95
96 ri.EG3Attack = pRegion->EG3Attack;
97 ri.EG3Depth = pRegion->EG3Depth;
98 ri.VCFEnabled = pRegion->VCFEnabled;
99 ri.VCFType = Filter::vcf_type_t(pRegion->VCFType);
100 ri.VCFResonance = pRegion->VCFResonance;
101
102 ri.ReleaseTriggerDecay = 0.01053 * (256 >> pRegion->ReleaseTriggerDecay);
103
104 return ri;
105 }
106
107 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
108 InstrumentInfo ii;
109 ii.FineTune = GetGigEngineChannel()->pInstrument->FineTune;
110 ii.PitchbendRange = GetGigEngineChannel()->pInstrument->PitchbendRange;
111
112 return ii;
113 }
114
115 double Voice::GetSampleAttenuation() {
116 return pRegion->SampleAttenuation;
117 }
118
119 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
120 return pRegion->GetVelocityAttenuation(MIDIKeyVelocity);
121 }
122
123 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
124 return pRegion->GetVelocityRelease(MIDIKeyVelocity);
125 }
126
127 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
128 if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
129 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
130 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
131 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
132 }
133 }
134 }
135
136 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
137 int ccvalue = itEvent->Param.CC.Value;
138 if (VCFCutoffCtrl.value == ccvalue) return;
139 VCFCutoffCtrl.value = ccvalue;
140 if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue;
141 if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;
142 float cutoff = CutoffBase * float(ccvalue);
143 if (cutoff > 127.0f) cutoff = 127.0f;
144
145 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
146 fFinalCutoff = cutoff;
147 }
148
149 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
150 float crossfadeVolume;
151 switch (pRegion->AttenuationController.type) {
152 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
153 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetGigEngineChannel()->ControllerTable[128])];
154 break;
155 case ::gig::attenuation_ctrl_t::type_velocity:
156 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
157 break;
158 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
159 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetGigEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
160 break;
161 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
162 default:
163 crossfadeVolume = 1.0f;
164 }
165
166 return crossfadeVolume;
167 }
168
169 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
170 double eg1controllervalue = 0;
171 switch (pRegion->EG1Controller.type) {
172 case ::gig::eg1_ctrl_t::type_none: // no controller defined
173 eg1controllervalue = 0;
174 break;
175 case ::gig::eg1_ctrl_t::type_channelaftertouch:
176 eg1controllervalue = GetGigEngineChannel()->ControllerTable[128];
177 break;
178 case ::gig::eg1_ctrl_t::type_velocity:
179 eg1controllervalue = MIDIKeyVelocity;
180 break;
181 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
182 eg1controllervalue = GetGigEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
183 break;
184 }
185 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
186
187 return eg1controllervalue;
188 }
189
190 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
191 EGInfo eg;
192 // (eg1attack is different from the others)
193 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
194 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
195 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
196 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
197 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
198
199 return eg;
200 }
201
202 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
203 double eg2controllervalue = 0;
204 switch (pRegion->EG2Controller.type) {
205 case ::gig::eg2_ctrl_t::type_none: // no controller defined
206 eg2controllervalue = 0;
207 break;
208 case ::gig::eg2_ctrl_t::type_channelaftertouch:
209 eg2controllervalue = GetGigEngineChannel()->ControllerTable[128];
210 break;
211 case ::gig::eg2_ctrl_t::type_velocity:
212 eg2controllervalue = MIDIKeyVelocity;
213 break;
214 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
215 eg2controllervalue = GetGigEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
216 break;
217 }
218 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
219
220 return eg2controllervalue;
221 }
222
223 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
224 EGInfo eg;
225 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
226 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
227 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
228
229 return eg;
230 }
231
232 void Voice::InitLFO1() {
233 uint16_t lfo1_internal_depth;
234 switch (pRegion->LFO1Controller) {
235 case ::gig::lfo1_ctrl_internal:
236 lfo1_internal_depth = pRegion->LFO1InternalDepth;
237 pLFO1->ExtController = 0; // no external controller
238 bLFO1Enabled = (lfo1_internal_depth > 0);
239 break;
240 case ::gig::lfo1_ctrl_modwheel:
241 lfo1_internal_depth = 0;
242 pLFO1->ExtController = 1; // MIDI controller 1
243 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
244 break;
245 case ::gig::lfo1_ctrl_breath:
246 lfo1_internal_depth = 0;
247 pLFO1->ExtController = 2; // MIDI controller 2
248 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
249 break;
250 case ::gig::lfo1_ctrl_internal_modwheel:
251 lfo1_internal_depth = pRegion->LFO1InternalDepth;
252 pLFO1->ExtController = 1; // MIDI controller 1
253 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
254 break;
255 case ::gig::lfo1_ctrl_internal_breath:
256 lfo1_internal_depth = pRegion->LFO1InternalDepth;
257 pLFO1->ExtController = 2; // MIDI controller 2
258 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
259 break;
260 default:
261 lfo1_internal_depth = 0;
262 pLFO1->ExtController = 0; // no external controller
263 bLFO1Enabled = false;
264 }
265 if (bLFO1Enabled) {
266 pLFO1->trigger(pRegion->LFO1Frequency,
267 start_level_min,
268 lfo1_internal_depth,
269 pRegion->LFO1ControlDepth,
270 pRegion->LFO1FlipPhase,
271 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
272 pLFO1->update(pLFO1->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO1->ExtController] : 0);
273 }
274 }
275
276 void Voice::InitLFO2() {
277 uint16_t lfo2_internal_depth;
278 switch (pRegion->LFO2Controller) {
279 case ::gig::lfo2_ctrl_internal:
280 lfo2_internal_depth = pRegion->LFO2InternalDepth;
281 pLFO2->ExtController = 0; // no external controller
282 bLFO2Enabled = (lfo2_internal_depth > 0);
283 break;
284 case ::gig::lfo2_ctrl_modwheel:
285 lfo2_internal_depth = 0;
286 pLFO2->ExtController = 1; // MIDI controller 1
287 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
288 break;
289 case ::gig::lfo2_ctrl_foot:
290 lfo2_internal_depth = 0;
291 pLFO2->ExtController = 4; // MIDI controller 4
292 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
293 break;
294 case ::gig::lfo2_ctrl_internal_modwheel:
295 lfo2_internal_depth = pRegion->LFO2InternalDepth;
296 pLFO2->ExtController = 1; // MIDI controller 1
297 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
298 break;
299 case ::gig::lfo2_ctrl_internal_foot:
300 lfo2_internal_depth = pRegion->LFO2InternalDepth;
301 pLFO2->ExtController = 4; // MIDI controller 4
302 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
303 break;
304 default:
305 lfo2_internal_depth = 0;
306 pLFO2->ExtController = 0; // no external controller
307 bLFO2Enabled = false;
308 }
309 if (bLFO2Enabled) {
310 pLFO2->trigger(pRegion->LFO2Frequency,
311 start_level_max,
312 lfo2_internal_depth,
313 pRegion->LFO2ControlDepth,
314 pRegion->LFO2FlipPhase,
315 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
316 pLFO2->update(pLFO2->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO2->ExtController] : 0);
317 }
318 }
319
320 void Voice::InitLFO3() {
321 uint16_t lfo3_internal_depth;
322 switch (pRegion->LFO3Controller) {
323 case ::gig::lfo3_ctrl_internal:
324 lfo3_internal_depth = pRegion->LFO3InternalDepth;
325 pLFO3->ExtController = 0; // no external controller
326 bLFO3Enabled = (lfo3_internal_depth > 0);
327 break;
328 case ::gig::lfo3_ctrl_modwheel:
329 lfo3_internal_depth = 0;
330 pLFO3->ExtController = 1; // MIDI controller 1
331 bLFO3Enabled = (pRegion->LFO3ControlDepth > 0);
332 break;
333 case ::gig::lfo3_ctrl_aftertouch:
334 lfo3_internal_depth = 0;
335 pLFO3->ExtController = 128;
336 bLFO3Enabled = true;
337 break;
338 case ::gig::lfo3_ctrl_internal_modwheel:
339 lfo3_internal_depth = pRegion->LFO3InternalDepth;
340 pLFO3->ExtController = 1; // MIDI controller 1
341 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
342 break;
343 case ::gig::lfo3_ctrl_internal_aftertouch:
344 lfo3_internal_depth = pRegion->LFO3InternalDepth;
345 pLFO3->ExtController = 128;
346 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
347 break;
348 default:
349 lfo3_internal_depth = 0;
350 pLFO3->ExtController = 0; // no external controller
351 bLFO3Enabled = false;
352 }
353 if (bLFO3Enabled) {
354 pLFO3->trigger(pRegion->LFO3Frequency,
355 start_level_mid,
356 lfo3_internal_depth,
357 pRegion->LFO3ControlDepth,
358 false,
359 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
360 pLFO3->update(pLFO3->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO3->ExtController] : 0);
361 }
362 }
363
364 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
365 float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);
366 if (pRegion->VCFKeyboardTracking) {
367 cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey - pRegion->VCFKeyboardTrackingBreakpoint) * 100);
368 }
369 return cutoff;
370 }
371
372 float Voice::CalculateFinalCutoff(float cutoffBase) {
373 int cvalue;
374 if (VCFCutoffCtrl.controller) {
375 cvalue = GetGigEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
376 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
377 // VCFVelocityScale in this case means Minimum cutoff
378 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
379 }
380 else {
381 cvalue = pRegion->VCFCutoff;
382 }
383 float fco = cutoffBase * float(cvalue);
384 if (fco > 127.0f) fco = 127.0f;
385
386 return fco;
387 }
388
389 uint8_t Voice::GetVCFCutoffCtrl() {
390 uint8_t ctrl;
391 switch (pRegion->VCFCutoffController) {
392 case ::gig::vcf_cutoff_ctrl_modwheel:
393 ctrl = 1;
394 break;
395 case ::gig::vcf_cutoff_ctrl_effect1:
396 ctrl = 12;
397 break;
398 case ::gig::vcf_cutoff_ctrl_effect2:
399 ctrl = 13;
400 break;
401 case ::gig::vcf_cutoff_ctrl_breath:
402 ctrl = 2;
403 break;
404 case ::gig::vcf_cutoff_ctrl_foot:
405 ctrl = 4;
406 break;
407 case ::gig::vcf_cutoff_ctrl_sustainpedal:
408 ctrl = 64;
409 break;
410 case ::gig::vcf_cutoff_ctrl_softpedal:
411 ctrl = 67;
412 break;
413 case ::gig::vcf_cutoff_ctrl_genpurpose7:
414 ctrl = 82;
415 break;
416 case ::gig::vcf_cutoff_ctrl_genpurpose8:
417 ctrl = 83;
418 break;
419 case ::gig::vcf_cutoff_ctrl_aftertouch:
420 ctrl = 128;
421 break;
422 case ::gig::vcf_cutoff_ctrl_none:
423 default:
424 ctrl = 0;
425 break;
426 }
427
428 return ctrl;
429 }
430
431 uint8_t Voice::GetVCFResonanceCtrl() {
432 uint8_t ctrl;
433 switch (pRegion->VCFResonanceController) {
434 case ::gig::vcf_res_ctrl_genpurpose3:
435 ctrl = 18;
436 break;
437 case ::gig::vcf_res_ctrl_genpurpose4:
438 ctrl = 19;
439 break;
440 case ::gig::vcf_res_ctrl_genpurpose5:
441 ctrl = 80;
442 break;
443 case ::gig::vcf_res_ctrl_genpurpose6:
444 ctrl = 81;
445 break;
446 case ::gig::vcf_res_ctrl_none:
447 default:
448 ctrl = 0;
449 }
450
451 return ctrl;
452 }
453
454 void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
455 EG1.trigger(uint(RgnInfo.EG1PreAttack),
456 RgnInfo.EG1Attack * egInfo.Attack,
457 RgnInfo.EG1Hold,
458 RgnInfo.EG1Decay1 * egInfo.Decay * velrelease,
459 RgnInfo.EG1Decay2 * egInfo.Decay * velrelease,
460 RgnInfo.EG1InfiniteSustain,
461 uint(RgnInfo.EG1Sustain),
462 RgnInfo.EG1Release * egInfo.Release * velrelease,
463 velocityAttenuation,
464 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
465 }
466
467 void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
468 EG2.trigger(uint(RgnInfo.EG2PreAttack),
469 RgnInfo.EG2Attack * egInfo.Attack,
470 false,
471 RgnInfo.EG2Decay1 * egInfo.Decay * velrelease,
472 RgnInfo.EG2Decay2 * egInfo.Decay * velrelease,
473 RgnInfo.EG2InfiniteSustain,
474 uint(RgnInfo.EG2Sustain),
475 RgnInfo.EG2Release * egInfo.Release * velrelease,
476 velocityAttenuation,
477 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
478 }
479
480 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
481 dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
482
483 // TODO: The SustainPedal condition could be wrong, maybe the
484 // check should be if this Voice is in release stage or is a
485 // release sample instead. Need to test this in GSt.
486 if (itEvent->Param.Note.Key != MIDIKey ||
487 !GetGigEngineChannel()->SustainPedal) {
488 dmsg(4,("Voice %x - kill", this));
489
490 // kill the voice fast
491 pEG1->enterFadeOutStage();
492 }
493 }
494
495 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC