/[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 2237 - (show annotations) (download)
Fri Aug 12 13:07:05 2011 UTC (12 years, 8 months ago) by iliev
File size: 14728 byte(s)
* sfz engine: implemented opcodes pan_onccN, pan_smoothccN,
  pan_curveccN, egN_pan, egN_pan_curve, egN_pan_onccX,
  egN_pan_curveccX, pitch_veltrack
* sfz engine: when failed to parse a sfz file
  print the line number on which the error occurs

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.VCFEnabled = pRegion->cutoff;
78 switch (pRegion->fil_type) {
79 case ::sfz::LPF_1P:
80 ri.VCFType = Filter::vcf_type_1p_lowpass;
81 break;
82 case ::sfz::LPF_2P:
83 ri.VCFType = Filter::vcf_type_2p_lowpass;
84 break;
85 case ::sfz::LPF_4P:
86 ri.VCFType = Filter::vcf_type_4p_lowpass;
87 break;
88 case ::sfz::LPF_6P:
89 ri.VCFType = Filter::vcf_type_6p_lowpass;
90 break;
91 case ::sfz::HPF_1P:
92 ri.VCFType = Filter::vcf_type_1p_highpass;
93 break;
94 case ::sfz::HPF_2P:
95 ri.VCFType = Filter::vcf_type_2p_highpass;
96 break;
97 case ::sfz::HPF_4P:
98 ri.VCFType = Filter::vcf_type_4p_highpass;
99 break;
100 case ::sfz::HPF_6P:
101 ri.VCFType = Filter::vcf_type_6p_highpass;
102 break;
103 case ::sfz::BPF_1P:
104 case ::sfz::BPF_2P:
105 ri.VCFType = Filter::vcf_type_2p_bandpass;
106 break;
107 case ::sfz::BRF_1P:
108 case ::sfz::BRF_2P:
109 ri.VCFType = Filter::vcf_type_2p_bandreject;
110 break;
111 case ::sfz::APF_1P:
112 case ::sfz::PKF_2P:
113 default:
114 ri.VCFEnabled = false;
115 break;
116 }
117
118 ri.VCFResonance = pRegion->resonance;
119
120 // rt_decay is in dB. Precalculate a suitable value for exp in
121 // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
122 ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
123
124 return ri;
125 }
126
127 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
128 InstrumentInfo ii;
129 ii.FineTune = 0; // TODO:
130 ii.PitchbendRange = 2; // TODO:
131
132 return ii;
133 }
134
135 double Voice::GetSampleAttenuation() {
136 return exp(LN_10_DIV_20 * pRegion->volume);
137 }
138
139 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
140 return pRegion->amp_velcurve[MIDIKeyVelocity];
141 }
142
143 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
144 return 127.0 / MIDIKeyVelocity;
145 }
146
147 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
148 /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
149 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
150 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
151 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
152 }
153 }*/ // TODO: ^^^
154 }
155
156 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
157 int ccvalue = itEvent->Param.CC.Value;
158 if (VCFCutoffCtrl.value == ccvalue) return;
159 VCFCutoffCtrl.value = ccvalue;
160
161 float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
162 ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
163 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
164
165 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
166 fFinalCutoff = cutoff;
167 }
168
169 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
170 /*float crossfadeVolume;
171 switch (pRegion->AttenuationController.type) {
172 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
173 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
174 break;
175 case ::gig::attenuation_ctrl_t::type_velocity:
176 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
177 break;
178 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
179 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
180 break;
181 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
182 default:
183 crossfadeVolume = 1.0f;
184 }
185
186 return crossfadeVolume;*/ // TODO: ^^^
187 return 1.0f;
188 }
189
190 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
191 /*double eg1controllervalue = 0;
192 switch (pRegion->EG1Controller.type) {
193 case ::gig::eg1_ctrl_t::type_none: // no controller defined
194 eg1controllervalue = 0;
195 break;
196 case ::gig::eg1_ctrl_t::type_channelaftertouch:
197 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
198 break;
199 case ::gig::eg1_ctrl_t::type_velocity:
200 eg1controllervalue = MIDIKeyVelocity;
201 break;
202 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
203 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
204 break;
205 }
206 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
207
208 return eg1controllervalue;*/ // TODO: ^^^
209 return 0;
210 }
211
212 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
213 /*EGInfo eg;
214 // (eg1attack is different from the others)
215 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
216 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
217 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
218 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
219 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
220
221 return eg;*/ // TODO: ^^^
222 EGInfo eg;
223 eg.Attack = 1.0;
224 eg.Decay = 1.0;
225 eg.Release = 1.0;
226 return eg;
227 }
228
229 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
230 /*double eg2controllervalue = 0;
231 switch (pRegion->EG2Controller.type) {
232 case ::gig::eg2_ctrl_t::type_none: // no controller defined
233 eg2controllervalue = 0;
234 break;
235 case ::gig::eg2_ctrl_t::type_channelaftertouch:
236 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
237 break;
238 case ::gig::eg2_ctrl_t::type_velocity:
239 eg2controllervalue = MIDIKeyVelocity;
240 break;
241 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
242 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
243 break;
244 }
245 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
246
247 return eg2controllervalue;*/ // TODO: ^^^
248 return 0;
249 }
250
251 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
252 /*EGInfo eg;
253 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
254 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
255 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
256
257 return eg;*/ // TODO: ^^^
258 EGInfo eg;
259 eg.Attack = 1.0;
260 eg.Decay = 1.0;
261 eg.Release = 1.0;
262 return eg;
263 }
264
265 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
266 float cutoff = *pRegion->cutoff;
267 cutoff *= RTMath::CentsToFreqRatioUnlimited(
268 MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
269 (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);
270 return cutoff;
271 }
272
273 float Voice::CalculateFinalCutoff(float cutoffBase) {
274 float cutoff;
275 if (VCFCutoffCtrl.controller) {
276 int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
277 cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
278 ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
279 } else {
280 cutoff = cutoffBase;
281 }
282 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
283 return cutoff;
284 }
285
286 uint8_t Voice::GetVCFCutoffCtrl() {
287 // TODO: the sfz format allows several CC for the same
288 // modulation destination. The Voice interface needs to be
289 // changed to support that.
290 if (pRegion->cutoff_cc) return pRegion->cutoff_cc;
291 else if (pRegion->cutoff_chanaft) return 128;
292 return 0;
293 }
294
295 uint8_t Voice::GetVCFResonanceCtrl() {
296 /*uint8_t ctrl;
297 switch (pRegion->VCFResonanceController) {
298 case ::gig::vcf_res_ctrl_genpurpose3:
299 ctrl = 18;
300 break;
301 case ::gig::vcf_res_ctrl_genpurpose4:
302 ctrl = 19;
303 break;
304 case ::gig::vcf_res_ctrl_genpurpose5:
305 ctrl = 80;
306 break;
307 case ::gig::vcf_res_ctrl_genpurpose6:
308 ctrl = 81;
309 break;
310 case ::gig::vcf_res_ctrl_none:
311 default:
312 ctrl = 0;
313 }
314
315 return ctrl;*/ // TODO: ^^^
316 return 0;
317 }
318
319 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
320 // pow(10, -rt_decay * noteLength / 20):
321 return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
322 }
323
324 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
325 dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
326 if (itEvent->Type == Event::type_control_change ||
327 (Type & Voice::type_controller_triggered) ||
328 itEvent->Param.Note.Key != MIDIKey) {
329 dmsg(4,("Voice %x - kill", this));
330 if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
331 // turn off the voice by entering release envelope stage
332 EnterReleaseStage();
333 } else {
334 // kill the voice fast
335 SignalRack.EnterFadeOutStage();
336 }
337 }
338 }
339
340 void Voice::SetSampleStartOffset() {
341 if (DiskVoice && RgnInfo.SampleStartOffset > pSample->MaxOffset) {
342 // The offset is applied to the RAM buffer
343 finalSynthesisParameters.dPos = 0;
344 Pos = 0;
345 } else {
346 finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample
347 Pos = RgnInfo.SampleStartOffset;
348 }
349 }
350
351 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC