/[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 2015 - (show annotations) (download)
Sun Oct 25 22:22:52 2009 UTC (14 years, 5 months ago) by iliev
File size: 19889 byte(s)
* Refactoring: moved the independent code from gig::Voice to base classes
* SoundFont format engine: implemented EG1 & EG2

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

  ViewVC Help
Powered by ViewVC