3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009 Grigor Iliev * |
* Copyright (C) 2009 - 2011 Christian Schoenebeck and Grigor Iliev * |
8 |
* * |
* * |
9 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
27 |
#include "Engine.h" |
#include "Engine.h" |
28 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
29 |
|
|
30 |
|
#define LN_10_DIV_20 0.115129254649702 |
31 |
|
|
32 |
namespace LinuxSampler { namespace sfz { |
namespace LinuxSampler { namespace sfz { |
33 |
|
|
34 |
Voice::Voice() { |
Voice::Voice() { |
58 |
si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8; |
si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8; |
59 |
si.TotalFrameCount = pSample->GetTotalFrameCount(); |
si.TotalFrameCount = pSample->GetTotalFrameCount(); |
60 |
|
|
61 |
si.HasLoops = false; // TODO: |
si.HasLoops = pRegion->HasLoop(); |
62 |
si.LoopStart = 0; // TODO: |
si.LoopStart = pRegion->GetLoopStart(); |
63 |
si.LoopLength = 0; // TODO: |
si.LoopLength = pRegion->GetLoopEnd() - pRegion->GetLoopStart(); |
64 |
si.LoopPlayCount = 0; // TODO: |
si.LoopPlayCount = pRegion->GetLoopCount(); |
65 |
si.Unpitched = false; // TODO: |
si.Unpitched = pRegion->pitch_keytrack == 0; |
66 |
return si; |
return si; |
67 |
} |
} |
68 |
|
|
69 |
Voice::RegionInfo Voice::GetRegionInfo() { |
Voice::RegionInfo Voice::GetRegionInfo() { |
70 |
RegionInfo ri; |
RegionInfo ri; |
71 |
ri.UnityNote = pRegion->pitch_keycenter; |
ri.UnityNote = pRegion->pitch_keycenter; |
72 |
ri.FineTune = pRegion->tune; |
ri.FineTune = pRegion->tune + pRegion->transpose * 100; |
73 |
ri.Pan = pRegion->pan; |
ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63 |
74 |
ri.SampleStartOffset = 0; // TODO: |
ri.SampleStartOffset = 0; // TODO: |
75 |
|
|
76 |
ri.EG1PreAttack = pRegion->ampeg_start * 10; |
ri.EG1PreAttack = pRegion->ampeg_start * 10; |
78 |
ri.EG1Hold = pRegion->ampeg_hold; |
ri.EG1Hold = pRegion->ampeg_hold; |
79 |
ri.EG1Decay1 = pRegion->ampeg_decay; |
ri.EG1Decay1 = pRegion->ampeg_decay; |
80 |
ri.EG1Decay2 = pRegion->ampeg_decay; |
ri.EG1Decay2 = pRegion->ampeg_decay; |
81 |
ri.EG1Sustain = pRegion->ampeg_sustain; |
ri.EG1Sustain = pRegion->ampeg_sustain * 10; |
82 |
ri.EG1InfiniteSustain = true; |
ri.EG1InfiniteSustain = true; |
83 |
ri.EG1Release = pRegion->ampeg_release; |
ri.EG1Release = pRegion->ampeg_release; |
84 |
|
|
85 |
ri.EG2PreAttack = pRegion->fileg_start; |
ri.EG2PreAttack = pRegion->fileg_start * 10; |
86 |
ri.EG2Attack = pRegion->fileg_attack; |
ri.EG2Attack = pRegion->fileg_attack; |
87 |
//ri.EG2Hold = pRegion->fileg_hold; // TODO: |
//ri.EG2Hold = pRegion->fileg_hold; // TODO: |
88 |
ri.EG2Decay1 = pRegion->fileg_decay; |
ri.EG2Decay1 = pRegion->fileg_decay; |
89 |
ri.EG2Decay2 = pRegion->fileg_decay; |
ri.EG2Decay2 = pRegion->fileg_decay; |
90 |
ri.EG2Sustain = pRegion->fileg_sustain; |
ri.EG2Sustain = pRegion->fileg_sustain * 10; |
91 |
ri.EG2InfiniteSustain = true; |
ri.EG2InfiniteSustain = true; |
92 |
ri.EG2Release = pRegion->fileg_release; |
ri.EG2Release = pRegion->fileg_release; |
93 |
|
|
94 |
ri.EG3Attack = pRegion->pitcheg_attack; |
ri.EG3Attack = pRegion->pitcheg_attack; |
95 |
ri.EG3Depth = 0; // TODO: |
ri.EG3Depth = 0; // TODO: |
96 |
ri.VCFEnabled = false; // TODO: |
ri.VCFEnabled = pRegion->cutoff; |
97 |
ri.VCFType = ::gig::vcf_type_lowpass; // TODO: |
switch (pRegion->fil_type) { |
98 |
ri.VCFResonance = 0; // TODO: |
case ::sfz::LPF_1P: |
99 |
|
ri.VCFType = Filter::vcf_type_1p_lowpass; |
100 |
ri.ReleaseTriggerDecay = 0; |
break; |
101 |
|
case ::sfz::LPF_2P: |
102 |
|
ri.VCFType = Filter::vcf_type_2p_lowpass; |
103 |
|
break; |
104 |
|
case ::sfz::LPF_4P: |
105 |
|
ri.VCFType = Filter::vcf_type_4p_lowpass; |
106 |
|
break; |
107 |
|
case ::sfz::LPF_6P: |
108 |
|
ri.VCFType = Filter::vcf_type_6p_lowpass; |
109 |
|
break; |
110 |
|
case ::sfz::HPF_1P: |
111 |
|
ri.VCFType = Filter::vcf_type_1p_highpass; |
112 |
|
break; |
113 |
|
case ::sfz::HPF_2P: |
114 |
|
ri.VCFType = Filter::vcf_type_2p_highpass; |
115 |
|
break; |
116 |
|
case ::sfz::HPF_4P: |
117 |
|
ri.VCFType = Filter::vcf_type_4p_highpass; |
118 |
|
break; |
119 |
|
case ::sfz::HPF_6P: |
120 |
|
ri.VCFType = Filter::vcf_type_6p_highpass; |
121 |
|
break; |
122 |
|
case ::sfz::BPF_1P: |
123 |
|
case ::sfz::BPF_2P: |
124 |
|
ri.VCFType = Filter::vcf_type_2p_bandpass; |
125 |
|
break; |
126 |
|
case ::sfz::BRF_1P: |
127 |
|
case ::sfz::BRF_2P: |
128 |
|
ri.VCFType = Filter::vcf_type_2p_bandreject; |
129 |
|
break; |
130 |
|
case ::sfz::APF_1P: |
131 |
|
case ::sfz::PKF_2P: |
132 |
|
default: |
133 |
|
ri.VCFEnabled = false; |
134 |
|
break; |
135 |
|
} |
136 |
|
|
137 |
|
ri.VCFResonance = pRegion->resonance; |
138 |
|
|
139 |
|
// rt_decay is in dB. Precalculate a suitable value for exp in |
140 |
|
// GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay |
141 |
|
ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay; |
142 |
|
|
143 |
return ri; |
return ri; |
144 |
} |
} |
152 |
} |
} |
153 |
|
|
154 |
double Voice::GetSampleAttenuation() { |
double Voice::GetSampleAttenuation() { |
155 |
return 1.0; // TODO: |
return exp(LN_10_DIV_20 * pRegion->volume); |
156 |
} |
} |
157 |
|
|
158 |
double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
159 |
return double(MIDIKeyVelocity) / 127.0f; // TODO: |
return pRegion->amp_velcurve[MIDIKeyVelocity]; |
160 |
} |
} |
161 |
|
|
162 |
double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) { |
double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) { |
173 |
} |
} |
174 |
|
|
175 |
void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) { |
void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) { |
176 |
/*int ccvalue = itEvent->Param.CC.Value; |
int ccvalue = itEvent->Param.CC.Value; |
177 |
if (VCFCutoffCtrl.value == ccvalue) return; |
if (VCFCutoffCtrl.value == ccvalue) return; |
178 |
VCFCutoffCtrl.value == ccvalue; |
VCFCutoffCtrl.value = ccvalue; |
179 |
if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; |
|
180 |
if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale; |
float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited( |
181 |
float cutoff = CutoffBase * float(ccvalue); |
ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]); |
182 |
if (cutoff > 127.0f) cutoff = 127.0f; |
if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate; |
183 |
|
|
184 |
VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time |
VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time |
185 |
fFinalCutoff = cutoff;*/ // TODO: ^^^ |
fFinalCutoff = cutoff; |
186 |
} |
} |
187 |
|
|
188 |
double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) { |
double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) { |
245 |
return eg; |
return eg; |
246 |
} |
} |
247 |
|
|
248 |
double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) { |
void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
249 |
|
|
250 |
|
// TODO: controller modulation |
251 |
|
|
252 |
|
// first check if there is a v2 EG for amplitude |
253 |
|
for (int i = 0 ; i < pRegion->eg.size() ; i++) { |
254 |
|
if (pRegion->eg[i].amplitude > 0) { |
255 |
|
// TODO: actually use the value of the amplitude parameter |
256 |
|
pEG1 = &EG1; |
257 |
|
EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity); |
258 |
|
return; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
// otherwise use the v1 EGADSR |
263 |
|
pEG1 = &EGADSR1; |
264 |
|
EGADSR1.trigger(uint(RgnInfo.EG1PreAttack), |
265 |
|
RgnInfo.EG1Attack, |
266 |
|
RgnInfo.EG1Hold, |
267 |
|
RgnInfo.EG1Decay1, |
268 |
|
uint(RgnInfo.EG1Sustain), |
269 |
|
RgnInfo.EG1Release, |
270 |
|
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
271 |
|
} |
272 |
|
|
273 |
|
double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) { |
274 |
/*double eg2controllervalue = 0; |
/*double eg2controllervalue = 0; |
275 |
switch (pRegion->EG2Controller.type) { |
switch (pRegion->EG2Controller.type) { |
276 |
case ::gig::eg2_ctrl_t::type_none: // no controller defined |
case ::gig::eg2_ctrl_t::type_none: // no controller defined |
306 |
return eg; |
return eg; |
307 |
} |
} |
308 |
|
|
309 |
|
void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
310 |
|
|
311 |
|
// TODO: the sfz filter EG should modulate cents, not hertz, |
312 |
|
// so we can't use the EG or EGADSR as it is. Disable for now. |
313 |
|
|
314 |
|
pEG2 = &EGADSR2; |
315 |
|
EGADSR2.trigger(0, |
316 |
|
0, |
317 |
|
false, |
318 |
|
0, |
319 |
|
1000, |
320 |
|
0, |
321 |
|
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
322 |
|
} |
323 |
|
|
324 |
void Voice::InitLFO1() { |
void Voice::InitLFO1() { |
325 |
/*uint16_t lfo1_internal_depth; |
/*uint16_t lfo1_internal_depth; |
326 |
switch (pRegion->LFO1Controller) { |
switch (pRegion->LFO1Controller) { |
457 |
} |
} |
458 |
|
|
459 |
float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { |
float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { |
460 |
/*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity); |
float cutoff = *pRegion->cutoff; |
461 |
if (pRegion->VCFKeyboardTracking) { |
cutoff *= RTMath::CentsToFreqRatioUnlimited( |
462 |
cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12) |
MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack + |
463 |
} |
(MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack); |
464 |
return cutoff;*/ // TODO: ^^^ |
return cutoff; |
|
return 1.0f; |
|
465 |
} |
} |
466 |
|
|
467 |
float Voice::CalculateFinalCutoff(float cutoffBase) { |
float Voice::CalculateFinalCutoff(float cutoffBase) { |
468 |
/*int cvalue; |
float cutoff; |
469 |
if (VCFCutoffCtrl.controller) { |
if (VCFCutoffCtrl.controller) { |
470 |
cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller]; |
int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller]; |
471 |
if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue; |
cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited( |
472 |
// VCFVelocityScale in this case means Minimum cutoff |
ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]); |
473 |
if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale; |
} else { |
474 |
|
cutoff = cutoffBase; |
475 |
} |
} |
476 |
else { |
if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate; |
477 |
cvalue = pRegion->VCFCutoff; |
return cutoff; |
|
} |
|
|
float fco = cutoffBase * float(cvalue); |
|
|
if (fco > 127.0f) fco = 127.0f; |
|
|
|
|
|
return fco;*/ // TODO: ^^^ |
|
|
return 127.0f; |
|
478 |
} |
} |
479 |
|
|
480 |
uint8_t Voice::GetVCFCutoffCtrl() { |
uint8_t Voice::GetVCFCutoffCtrl() { |
481 |
/*uint8_t ctrl; |
// TODO: the sfz format allows several CC for the same |
482 |
switch (pRegion->VCFCutoffController) { |
// modulation destination. The Voice interface needs to be |
483 |
case ::gig::vcf_cutoff_ctrl_modwheel: |
// changed to support that. |
484 |
ctrl = 1; |
if (pRegion->cutoff_cc) return pRegion->cutoff_cc; |
485 |
break; |
else if (pRegion->cutoff_chanaft) return 128; |
|
case ::gig::vcf_cutoff_ctrl_effect1: |
|
|
ctrl = 12; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_effect2: |
|
|
ctrl = 13; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_breath: |
|
|
ctrl = 2; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_foot: |
|
|
ctrl = 4; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_sustainpedal: |
|
|
ctrl = 64; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_softpedal: |
|
|
ctrl = 67; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_genpurpose7: |
|
|
ctrl = 82; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_genpurpose8: |
|
|
ctrl = 83; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_aftertouch: |
|
|
ctrl = 128; |
|
|
break; |
|
|
case ::gig::vcf_cutoff_ctrl_none: |
|
|
default: |
|
|
ctrl = 0; |
|
|
break; |
|
|
} |
|
|
|
|
|
return ctrl;*/ // TODO: ^^^ |
|
486 |
return 0; |
return 0; |
487 |
} |
} |
488 |
|
|
510 |
return 0; |
return 0; |
511 |
} |
} |
512 |
|
|
513 |
|
float Voice::GetReleaseTriggerAttenuation(float noteLength) { |
514 |
|
// pow(10, -rt_decay * noteLength / 20): |
515 |
|
return expf(RgnInfo.ReleaseTriggerDecay * noteLength); |
516 |
|
} |
517 |
|
|
518 |
|
void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) { |
519 |
|
dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type)); |
520 |
|
if (itEvent->Type == Event::type_control_change || |
521 |
|
(Type & Voice::type_controller_triggered) || |
522 |
|
itEvent->Param.Note.Key != MIDIKey) { |
523 |
|
dmsg(4,("Voice %x - kill", this)); |
524 |
|
if (pRegion->off_mode == ::sfz::OFF_NORMAL) { |
525 |
|
// turn off the voice by entering release envelope stage |
526 |
|
EnterReleaseStage(); |
527 |
|
} else { |
528 |
|
// kill the voice fast |
529 |
|
pEG1->enterFadeOutStage(); |
530 |
|
} |
531 |
|
} |
532 |
|
} |
533 |
|
|
534 |
}} // namespace LinuxSampler::sfz |
}} // namespace LinuxSampler::sfz |