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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2327 - (show annotations) (download)
Sat Mar 10 16:16:14 2012 UTC (12 years, 1 month ago) by persson
File size: 14141 byte(s)
* sfz/sf2 engine: fixed crash when using small audio fragment size

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 - 2012 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 namespace LinuxSampler { namespace sf2 {
31
32 typedef LinuxSampler::VoiceBase<EngineChannel, ::sf2::Region, ::sf2::Sample, DiskThread> SF2Voice;
33 Voice::Voice(): SignalRack(this), SF2Voice(&SignalRack) {
34 pEngine = NULL;
35 pEG1 = NULL;
36 pEG2 = NULL;
37 }
38
39 Voice::~Voice() {
40
41 }
42
43 void Voice::AboutToTrigger() {
44
45 }
46
47 EngineChannel* Voice::GetSf2EngineChannel() {
48 return static_cast<EngineChannel*>(pEngineChannel);
49 }
50
51 void Voice::SetEngine(LinuxSampler::Engine* pEngine) {
52 Engine* engine = static_cast<Engine*>(pEngine);
53 this->pEngine = engine;
54 this->pDiskThread = engine->pDiskThread;
55 dmsg(6,("Voice::SetEngine()\n"));
56 }
57
58 Voice::SampleInfo Voice::GetSampleInfo() {
59 SampleInfo si;
60 si.SampleRate = pSample->SampleRate;
61 si.ChannelCount = pSample->GetChannelCount();
62 si.FrameSize = pSample->GetFrameSize();
63 si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
64 si.TotalFrameCount = pSample->GetTotalFrameCount();
65
66 si.HasLoops = pRegion->HasLoop;
67 si.LoopStart = (si.HasLoops) ? pRegion->LoopStart : 0;
68 si.LoopLength = (si.HasLoops) ? ((pRegion->LoopEnd) - pRegion->LoopStart): 0;
69 si.LoopPlayCount = 0; // TODO:
70 si.Unpitched = pSample->IsUnpitched();
71
72 return si;
73 }
74
75 Voice::RegionInfo Voice::GetRegionInfo() {
76 ::sf2::Region* reg = NULL;
77 ::sf2::Preset* preset = GetSf2EngineChannel()->pInstrument;
78 for (int i = 0; i < preset->GetRegionCount(); i++) { // TODO: some optimization?
79 if (preset->GetRegion(i)->pInstrument == pRegion->GetParentInstrument()) {
80 reg = preset->GetRegion(i); // TODO: Can the instrument belong to more than one preset region?
81 break;
82 }
83 }
84 pPresetRegion = reg;
85
86 RegionInfo ri;
87 ri.UnityNote = pRegion->GetUnityNote();
88 ri.FineTune = pRegion->GetFineTune(reg) + (pRegion->GetCoarseTune(reg) * 100);
89 ri.Pan = pRegion->GetPan(reg);
90 ri.SampleStartOffset = pRegion->startAddrsOffset + pRegion->startAddrsCoarseOffset;
91
92 // sample pitch
93 ri.VCFEnabled = true; // TODO:
94 ri.VCFType = Filter::vcf_type_2p_lowpass; // TODO:
95 ri.VCFResonance = 0; // TODO:
96
97 ri.ReleaseTriggerDecay = 0; // TODO:
98
99 return ri;
100 }
101
102 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
103 InstrumentInfo ii;
104 ii.FineTune = 0; // TODO:
105 ii.PitchbendRange = 2; // TODO:
106
107 return ii;
108 }
109
110 double Voice::GetSampleAttenuation() {
111 return 1.0; // TODO:
112 }
113
114 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
115 return double(MIDIKeyVelocity) / 127.0f; // TODO:
116 }
117
118 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
119 return 0.9; // TODO:
120 }
121
122 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
123 /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
124 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
125 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
126 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
127 }
128 }*/ // TODO: ^^^
129 }
130
131 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
132 /*int ccvalue = itEvent->Param.CC.Value;
133 if (VCFCutoffCtrl.value == ccvalue) return;
134 VCFCutoffCtrl.value == ccvalue;
135 if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue;
136 if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;
137 float cutoff = CutoffBase * float(ccvalue);
138 if (cutoff > 127.0f) cutoff = 127.0f;
139
140 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
141 fFinalCutoff = cutoff;*/ // TODO: ^^^
142 }
143
144 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
145 /*float crossfadeVolume;
146 switch (pRegion->AttenuationController.type) {
147 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
148 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSf2EngineChannel()->ControllerTable[128])];
149 break;
150 case ::gig::attenuation_ctrl_t::type_velocity:
151 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
152 break;
153 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
154 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSf2EngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
155 break;
156 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
157 default:
158 crossfadeVolume = 1.0f;
159 }
160
161 return crossfadeVolume;*/ // TODO: ^^^
162 return 1.0f;
163 }
164
165 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
166 /*double eg1controllervalue = 0;
167 switch (pRegion->EG1Controller.type) {
168 case ::gig::eg1_ctrl_t::type_none: // no controller defined
169 eg1controllervalue = 0;
170 break;
171 case ::gig::eg1_ctrl_t::type_channelaftertouch:
172 eg1controllervalue = GetSf2EngineChannel()->ControllerTable[128];
173 break;
174 case ::gig::eg1_ctrl_t::type_velocity:
175 eg1controllervalue = MIDIKeyVelocity;
176 break;
177 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
178 eg1controllervalue = GetSf2EngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
179 break;
180 }
181 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
182
183 return eg1controllervalue;*/ // TODO: ^^^
184 return 0;
185 }
186
187 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
188 /*EGInfo eg;
189 // (eg1attack is different from the others)
190 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
191 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
192 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
193 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
194 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
195
196 return eg;*/ // TODO: ^^^
197 EGInfo eg;
198 eg.Attack = 1.0;
199 eg.Decay = 1.0;
200 eg.Release = 1.0;
201 return eg;
202 }
203
204 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
205 /*double eg2controllervalue = 0;
206 switch (pRegion->EG2Controller.type) {
207 case ::gig::eg2_ctrl_t::type_none: // no controller defined
208 eg2controllervalue = 0;
209 break;
210 case ::gig::eg2_ctrl_t::type_channelaftertouch:
211 eg2controllervalue = GetSf2EngineChannel()->ControllerTable[128];
212 break;
213 case ::gig::eg2_ctrl_t::type_velocity:
214 eg2controllervalue = MIDIKeyVelocity;
215 break;
216 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
217 eg2controllervalue = GetSf2EngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
218 break;
219 }
220 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
221
222 return eg2controllervalue;*/ // TODO: ^^^
223 return 0;
224 }
225
226 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
227 /*EGInfo eg;
228 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
229 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
230 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 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 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
241 float cutoff = pRegion->GetInitialFilterFc(pPresetRegion);
242 if (MIDIKeyVelocity == 0) return cutoff;
243
244 cutoff *= RTMath::CentsToFreqRatioUnlimited (
245 ((127 - MIDIKeyVelocity) / 127.0) * -2400 // 8.4.2 MIDI Note-On Velocity to Filter Cutoff
246 );
247
248 return cutoff;
249 }
250
251 float Voice::CalculateFinalCutoff(float cutoffBase) {
252 /*int cvalue;
253 if (VCFCutoffCtrl.controller) {
254 cvalue = GetSf2EngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
255 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
256 // VCFVelocityScale in this case means Minimum cutoff
257 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
258 }
259 else {
260 cvalue = pRegion->VCFCutoff;
261 }
262 float fco = cutoffBase * float(cvalue);
263 if (fco > 127.0f) fco = 127.0f;
264
265 return fco;*/ // TODO: ^^^
266 return cutoffBase;
267 }
268
269 uint8_t Voice::GetVCFCutoffCtrl() {
270 /*uint8_t ctrl;
271 switch (pRegion->VCFCutoffController) {
272 case ::gig::vcf_cutoff_ctrl_modwheel:
273 ctrl = 1;
274 break;
275 case ::gig::vcf_cutoff_ctrl_effect1:
276 ctrl = 12;
277 break;
278 case ::gig::vcf_cutoff_ctrl_effect2:
279 ctrl = 13;
280 break;
281 case ::gig::vcf_cutoff_ctrl_breath:
282 ctrl = 2;
283 break;
284 case ::gig::vcf_cutoff_ctrl_foot:
285 ctrl = 4;
286 break;
287 case ::gig::vcf_cutoff_ctrl_sustainpedal:
288 ctrl = 64;
289 break;
290 case ::gig::vcf_cutoff_ctrl_softpedal:
291 ctrl = 67;
292 break;
293 case ::gig::vcf_cutoff_ctrl_genpurpose7:
294 ctrl = 82;
295 break;
296 case ::gig::vcf_cutoff_ctrl_genpurpose8:
297 ctrl = 83;
298 break;
299 case ::gig::vcf_cutoff_ctrl_aftertouch:
300 ctrl = 128;
301 break;
302 case ::gig::vcf_cutoff_ctrl_none:
303 default:
304 ctrl = 0;
305 break;
306 }
307
308 return ctrl;*/ // TODO: ^^^
309 return 0;
310 }
311
312 uint8_t Voice::GetVCFResonanceCtrl() {
313 /*uint8_t ctrl;
314 switch (pRegion->VCFResonanceController) {
315 case ::gig::vcf_res_ctrl_genpurpose3:
316 ctrl = 18;
317 break;
318 case ::gig::vcf_res_ctrl_genpurpose4:
319 ctrl = 19;
320 break;
321 case ::gig::vcf_res_ctrl_genpurpose5:
322 ctrl = 80;
323 break;
324 case ::gig::vcf_res_ctrl_genpurpose6:
325 ctrl = 81;
326 break;
327 case ::gig::vcf_res_ctrl_none:
328 default:
329 ctrl = 0;
330 }
331
332 return ctrl;*/ // TODO: ^^^
333 return 0;
334 }
335
336 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
337 if (itEvent->Param.Note.Key != MIDIKey) {
338 // kill the voice fast
339 SignalRack.EnterFadeOutStage();
340 }
341 }
342
343 void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) {
344 SignalRack.CalculateFadeOutCoeff(FadeOutTime, SampleRate);
345 }
346
347 }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC