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

Annotation of /linuxsampler/trunk/src/engines/sfz/Voice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2224 - (hide annotations) (download)
Mon Aug 1 19:08:09 2011 UTC (12 years, 8 months ago) by iliev
File size: 15270 byte(s)
* implemented opcode pitchlfo_depthccN

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 2055 * Copyright (C) 2005 - 2008 Christian Schoenebeck *
7 persson 2175 * Copyright (C) 2009 - 2011 Christian Schoenebeck and Grigor Iliev *
8 iliev 2012 * *
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 iliev 2015 #include "Voice.h"
26    
27 iliev 2012 #include "Engine.h"
28     #include "EngineChannel.h"
29    
30 persson 2072 #define LN_10_DIV_20 0.115129254649702
31    
32 iliev 2012 namespace LinuxSampler { namespace sfz {
33    
34 iliev 2218 typedef LinuxSampler::VoiceBase<EngineChannel, ::sfz::Region, Sample, DiskThread> SfzVoice;
35     Voice::Voice(): SignalRack(this), SfzVoice(&SignalRack) {
36 iliev 2012 pEngine = NULL;
37     }
38    
39     Voice::~Voice() {
40 iliev 2015
41 iliev 2012 }
42    
43 iliev 2015 EngineChannel* Voice::GetSfzEngineChannel() {
44     return static_cast<EngineChannel*>(pEngineChannel);
45     }
46    
47 iliev 2012 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 iliev 2015 Voice::SampleInfo Voice::GetSampleInfo() {
55     SampleInfo si;
56     si.SampleRate = pSample->GetSampleRate();
57     si.ChannelCount = pSample->GetChannelCount();
58     si.FrameSize = pSample->GetFrameSize();
59     si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
60     si.TotalFrameCount = pSample->GetTotalFrameCount();
61 iliev 2012
62 iliev 2021 si.HasLoops = pRegion->HasLoop();
63     si.LoopStart = pRegion->GetLoopStart();
64     si.LoopLength = pRegion->GetLoopEnd() - pRegion->GetLoopStart();
65     si.LoopPlayCount = pRegion->GetLoopCount();
66     si.Unpitched = pRegion->pitch_keytrack == 0;
67 iliev 2015 return si;
68     }
69 iliev 2012
70 iliev 2015 Voice::RegionInfo Voice::GetRegionInfo() {
71     RegionInfo ri;
72 iliev 2018 ri.UnityNote = pRegion->pitch_keycenter;
73 persson 2086 ri.FineTune = pRegion->tune + pRegion->transpose * 100;
74 persson 2045 ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63
75 iliev 2216 ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;
76 iliev 2012
77 persson 2055 ri.EG2PreAttack = pRegion->fileg_start * 10;
78 iliev 2018 ri.EG2Attack = pRegion->fileg_attack;
79     //ri.EG2Hold = pRegion->fileg_hold; // TODO:
80     ri.EG2Decay1 = pRegion->fileg_decay;
81     ri.EG2Decay2 = pRegion->fileg_decay;
82 persson 2055 ri.EG2Sustain = pRegion->fileg_sustain * 10;
83 iliev 2015 ri.EG2InfiniteSustain = true;
84 iliev 2018 ri.EG2Release = pRegion->fileg_release;
85 iliev 2012
86 iliev 2018 ri.EG3Attack = pRegion->pitcheg_attack;
87 iliev 2015 ri.EG3Depth = 0; // TODO:
88 persson 2175 ri.VCFEnabled = pRegion->cutoff;
89     switch (pRegion->fil_type) {
90     case ::sfz::LPF_1P:
91     ri.VCFType = Filter::vcf_type_1p_lowpass;
92     break;
93     case ::sfz::LPF_2P:
94     ri.VCFType = Filter::vcf_type_2p_lowpass;
95     break;
96     case ::sfz::LPF_4P:
97     ri.VCFType = Filter::vcf_type_4p_lowpass;
98     break;
99     case ::sfz::LPF_6P:
100     ri.VCFType = Filter::vcf_type_6p_lowpass;
101     break;
102     case ::sfz::HPF_1P:
103     ri.VCFType = Filter::vcf_type_1p_highpass;
104     break;
105     case ::sfz::HPF_2P:
106     ri.VCFType = Filter::vcf_type_2p_highpass;
107     break;
108     case ::sfz::HPF_4P:
109     ri.VCFType = Filter::vcf_type_4p_highpass;
110     break;
111     case ::sfz::HPF_6P:
112     ri.VCFType = Filter::vcf_type_6p_highpass;
113     break;
114     case ::sfz::BPF_1P:
115     case ::sfz::BPF_2P:
116     ri.VCFType = Filter::vcf_type_2p_bandpass;
117     break;
118     case ::sfz::BRF_1P:
119     case ::sfz::BRF_2P:
120     ri.VCFType = Filter::vcf_type_2p_bandreject;
121     break;
122     case ::sfz::APF_1P:
123     case ::sfz::PKF_2P:
124     default:
125     ri.VCFEnabled = false;
126     break;
127     }
128 iliev 2012
129 persson 2175 ri.VCFResonance = pRegion->resonance;
130    
131 persson 2061 // rt_decay is in dB. Precalculate a suitable value for exp in
132     // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
133 persson 2072 ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
134 iliev 2012
135 iliev 2015 return ri;
136     }
137 iliev 2012
138 iliev 2015 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
139     InstrumentInfo ii;
140     ii.FineTune = 0; // TODO:
141     ii.PitchbendRange = 2; // TODO:
142    
143     return ii;
144     }
145    
146     double Voice::GetSampleAttenuation() {
147 persson 2072 return exp(LN_10_DIV_20 * pRegion->volume);
148 iliev 2015 }
149    
150     double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
151 persson 2082 return pRegion->amp_velcurve[MIDIKeyVelocity];
152 iliev 2015 }
153    
154     double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
155 persson 2176 return 127.0 / MIDIKeyVelocity;
156 iliev 2015 }
157    
158     void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
159     /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
160     if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
161     itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
162     CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
163     }
164     }*/ // TODO: ^^^
165     }
166    
167     void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
168 persson 2175 int ccvalue = itEvent->Param.CC.Value;
169 iliev 2015 if (VCFCutoffCtrl.value == ccvalue) return;
170 persson 2175 VCFCutoffCtrl.value = ccvalue;
171 iliev 2015
172 persson 2175 float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
173     ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
174     if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
175    
176 iliev 2015 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
177 persson 2175 fFinalCutoff = cutoff;
178 iliev 2015 }
179    
180     double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
181 iliev 2012 /*float crossfadeVolume;
182     switch (pRegion->AttenuationController.type) {
183     case ::gig::attenuation_ctrl_t::type_channelaftertouch:
184 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
185 iliev 2012 break;
186     case ::gig::attenuation_ctrl_t::type_velocity:
187 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
188 iliev 2012 break;
189     case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
190 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
191 iliev 2012 break;
192     case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
193     default:
194     crossfadeVolume = 1.0f;
195     }
196    
197 iliev 2015 return crossfadeVolume;*/ // TODO: ^^^
198     return 1.0f;
199     }
200 iliev 2012
201 iliev 2015 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
202     /*double eg1controllervalue = 0;
203     switch (pRegion->EG1Controller.type) {
204     case ::gig::eg1_ctrl_t::type_none: // no controller defined
205     eg1controllervalue = 0;
206     break;
207     case ::gig::eg1_ctrl_t::type_channelaftertouch:
208     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
209     break;
210     case ::gig::eg1_ctrl_t::type_velocity:
211     eg1controllervalue = MIDIKeyVelocity;
212     break;
213     case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
214     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
215     break;
216 iliev 2012 }
217 iliev 2015 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
218 iliev 2012
219 iliev 2015 return eg1controllervalue;*/ // TODO: ^^^
220     return 0;
221     }
222 iliev 2012
223 iliev 2015 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
224     /*EGInfo eg;
225     // (eg1attack is different from the others)
226     eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
227     1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
228     1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
229     eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
230     eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
231 iliev 2012
232 iliev 2015 return eg;*/ // TODO: ^^^
233     EGInfo eg;
234     eg.Attack = 1.0;
235     eg.Decay = 1.0;
236     eg.Release = 1.0;
237     return eg;
238     }
239 iliev 2012
240 persson 2176 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
241 iliev 2015 /*double eg2controllervalue = 0;
242     switch (pRegion->EG2Controller.type) {
243     case ::gig::eg2_ctrl_t::type_none: // no controller defined
244     eg2controllervalue = 0;
245     break;
246     case ::gig::eg2_ctrl_t::type_channelaftertouch:
247     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
248     break;
249     case ::gig::eg2_ctrl_t::type_velocity:
250     eg2controllervalue = MIDIKeyVelocity;
251     break;
252     case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
253     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
254     break;
255 iliev 2012 }
256 iliev 2015 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
257 iliev 2012
258 iliev 2015 return eg2controllervalue;*/ // TODO: ^^^
259     return 0;
260     }
261 iliev 2012
262 iliev 2015 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
263     /*EGInfo eg;
264     eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
265     eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
266     eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
267 iliev 2012
268 iliev 2015 return eg;*/ // TODO: ^^^
269     EGInfo eg;
270     eg.Attack = 1.0;
271     eg.Decay = 1.0;
272     eg.Release = 1.0;
273     return eg;
274     }
275 iliev 2012
276 iliev 2015 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
277 persson 2175 float cutoff = *pRegion->cutoff;
278     cutoff *= RTMath::CentsToFreqRatioUnlimited(
279     MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
280     (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);
281     return cutoff;
282 iliev 2012 }
283    
284 iliev 2015 float Voice::CalculateFinalCutoff(float cutoffBase) {
285 persson 2175 float cutoff;
286 iliev 2015 if (VCFCutoffCtrl.controller) {
287 persson 2175 int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
288     cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
289     ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
290     } else {
291     cutoff = cutoffBase;
292 iliev 2012 }
293 persson 2175 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
294     return cutoff;
295 iliev 2012 }
296    
297 iliev 2015 uint8_t Voice::GetVCFCutoffCtrl() {
298 persson 2175 // TODO: the sfz format allows several CC for the same
299     // modulation destination. The Voice interface needs to be
300     // changed to support that.
301     if (pRegion->cutoff_cc) return pRegion->cutoff_cc;
302     else if (pRegion->cutoff_chanaft) return 128;
303 iliev 2015 return 0;
304 iliev 2012 }
305    
306 iliev 2015 uint8_t Voice::GetVCFResonanceCtrl() {
307     /*uint8_t ctrl;
308     switch (pRegion->VCFResonanceController) {
309     case ::gig::vcf_res_ctrl_genpurpose3:
310     ctrl = 18;
311     break;
312     case ::gig::vcf_res_ctrl_genpurpose4:
313     ctrl = 19;
314     break;
315     case ::gig::vcf_res_ctrl_genpurpose5:
316     ctrl = 80;
317     break;
318     case ::gig::vcf_res_ctrl_genpurpose6:
319     ctrl = 81;
320     break;
321     case ::gig::vcf_res_ctrl_none:
322     default:
323     ctrl = 0;
324 iliev 2012 }
325    
326 iliev 2015 return ctrl;*/ // TODO: ^^^
327     return 0;
328 iliev 2012 }
329    
330 persson 2061 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
331     // pow(10, -rt_decay * noteLength / 20):
332     return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
333     }
334    
335 persson 2114 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
336     dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
337 persson 2115 if (itEvent->Type == Event::type_control_change ||
338     (Type & Voice::type_controller_triggered) ||
339     itEvent->Param.Note.Key != MIDIKey) {
340 persson 2114 dmsg(4,("Voice %x - kill", this));
341     if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
342     // turn off the voice by entering release envelope stage
343     EnterReleaseStage();
344     } else {
345     // kill the voice fast
346 iliev 2218 SignalRack.EnterFadeOutStage();
347 persson 2114 }
348     }
349     }
350 iliev 2216
351     void Voice::SetSampleStartOffset() {
352     if (DiskVoice && RgnInfo.SampleStartOffset > pSample->MaxOffset) {
353     // The offset is applied to the RAM buffer
354     finalSynthesisParameters.dPos = 0;
355     Pos = 0;
356     } else {
357     finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample
358     Pos = RgnInfo.SampleStartOffset;
359     }
360     }
361 persson 2114
362 iliev 2012 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC