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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2224 - (show 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 /***************************************************************************
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 "Voice.h"
26
27 #include "Engine.h"
28 #include "EngineChannel.h"
29
30 #define LN_10_DIV_20 0.115129254649702
31
32 namespace LinuxSampler { namespace sfz {
33
34 typedef LinuxSampler::VoiceBase<EngineChannel, ::sfz::Region, Sample, DiskThread> SfzVoice;
35 Voice::Voice(): SignalRack(this), SfzVoice(&SignalRack) {
36 pEngine = NULL;
37 }
38
39 Voice::~Voice() {
40
41 }
42
43 EngineChannel* Voice::GetSfzEngineChannel() {
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->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
62 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 return si;
68 }
69
70 Voice::RegionInfo Voice::GetRegionInfo() {
71 RegionInfo ri;
72 ri.UnityNote = pRegion->pitch_keycenter;
73 ri.FineTune = pRegion->tune + pRegion->transpose * 100;
74 ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63
75 ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;
76
77 ri.EG2PreAttack = pRegion->fileg_start * 10;
78 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 ri.EG2Sustain = pRegion->fileg_sustain * 10;
83 ri.EG2InfiniteSustain = true;
84 ri.EG2Release = pRegion->fileg_release;
85
86 ri.EG3Attack = pRegion->pitcheg_attack;
87 ri.EG3Depth = 0; // TODO:
88 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
129 ri.VCFResonance = pRegion->resonance;
130
131 // rt_decay is in dB. Precalculate a suitable value for exp in
132 // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
133 ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
134
135 return ri;
136 }
137
138 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 return exp(LN_10_DIV_20 * pRegion->volume);
148 }
149
150 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
151 return pRegion->amp_velcurve[MIDIKeyVelocity];
152 }
153
154 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
155 return 127.0 / MIDIKeyVelocity;
156 }
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 int ccvalue = itEvent->Param.CC.Value;
169 if (VCFCutoffCtrl.value == ccvalue) return;
170 VCFCutoffCtrl.value = ccvalue;
171
172 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 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
177 fFinalCutoff = cutoff;
178 }
179
180 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
181 /*float crossfadeVolume;
182 switch (pRegion->AttenuationController.type) {
183 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
184 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
185 break;
186 case ::gig::attenuation_ctrl_t::type_velocity:
187 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
188 break;
189 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
190 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
191 break;
192 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
193 default:
194 crossfadeVolume = 1.0f;
195 }
196
197 return crossfadeVolume;*/ // TODO: ^^^
198 return 1.0f;
199 }
200
201 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 }
217 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
218
219 return eg1controllervalue;*/ // TODO: ^^^
220 return 0;
221 }
222
223 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
232 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
240 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
241 /*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 }
256 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
257
258 return eg2controllervalue;*/ // TODO: ^^^
259 return 0;
260 }
261
262 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
268 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
276 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
277 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 }
283
284 float Voice::CalculateFinalCutoff(float cutoffBase) {
285 float cutoff;
286 if (VCFCutoffCtrl.controller) {
287 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 }
293 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
294 return cutoff;
295 }
296
297 uint8_t Voice::GetVCFCutoffCtrl() {
298 // 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 return 0;
304 }
305
306 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 }
325
326 return ctrl;*/ // TODO: ^^^
327 return 0;
328 }
329
330 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
331 // pow(10, -rt_decay * noteLength / 20):
332 return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
333 }
334
335 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
336 dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
337 if (itEvent->Type == Event::type_control_change ||
338 (Type & Voice::type_controller_triggered) ||
339 itEvent->Param.Note.Key != MIDIKey) {
340 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 SignalRack.EnterFadeOutStage();
347 }
348 }
349 }
350
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
362 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC