/[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 2252 - (hide annotations) (download)
Sat Aug 20 14:01:36 2011 UTC (12 years, 8 months ago) by iliev
File size: 12792 byte(s)
* sfz engine: implemented opcodes resonance_onccN, resonance_smoothccN,
  resonance_curveccN, cutoff_smoothccN, cutoff_curveccN

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 2175 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 iliev 2012
118 persson 2175 ri.VCFResonance = pRegion->resonance;
119    
120 persson 2061 // rt_decay is in dB. Precalculate a suitable value for exp in
121     // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
122 persson 2072 ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
123 iliev 2012
124 iliev 2015 return ri;
125     }
126 iliev 2012
127 iliev 2015 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 persson 2072 return exp(LN_10_DIV_20 * pRegion->volume);
137 iliev 2015 }
138    
139     double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
140 persson 2082 return pRegion->amp_velcurve[MIDIKeyVelocity];
141 iliev 2015 }
142    
143     double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
144 persson 2176 return 127.0 / MIDIKeyVelocity;
145 iliev 2015 }
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     double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
157 iliev 2012 /*float crossfadeVolume;
158     switch (pRegion->AttenuationController.type) {
159     case ::gig::attenuation_ctrl_t::type_channelaftertouch:
160 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
161 iliev 2012 break;
162     case ::gig::attenuation_ctrl_t::type_velocity:
163 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
164 iliev 2012 break;
165     case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
166 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
167 iliev 2012 break;
168     case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
169     default:
170     crossfadeVolume = 1.0f;
171     }
172    
173 iliev 2015 return crossfadeVolume;*/ // TODO: ^^^
174     return 1.0f;
175     }
176 iliev 2012
177 iliev 2015 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
178     /*double eg1controllervalue = 0;
179     switch (pRegion->EG1Controller.type) {
180     case ::gig::eg1_ctrl_t::type_none: // no controller defined
181     eg1controllervalue = 0;
182     break;
183     case ::gig::eg1_ctrl_t::type_channelaftertouch:
184     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
185     break;
186     case ::gig::eg1_ctrl_t::type_velocity:
187     eg1controllervalue = MIDIKeyVelocity;
188     break;
189     case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
190     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
191     break;
192 iliev 2012 }
193 iliev 2015 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
194 iliev 2012
195 iliev 2015 return eg1controllervalue;*/ // TODO: ^^^
196     return 0;
197     }
198 iliev 2012
199 iliev 2015 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
200     /*EGInfo eg;
201     // (eg1attack is different from the others)
202     eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
203     1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
204     1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
205     eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
206     eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
207 iliev 2012
208 iliev 2015 return eg;*/ // TODO: ^^^
209     EGInfo eg;
210     eg.Attack = 1.0;
211     eg.Decay = 1.0;
212     eg.Release = 1.0;
213     return eg;
214     }
215 iliev 2012
216 persson 2176 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
217 iliev 2015 /*double eg2controllervalue = 0;
218     switch (pRegion->EG2Controller.type) {
219     case ::gig::eg2_ctrl_t::type_none: // no controller defined
220     eg2controllervalue = 0;
221     break;
222     case ::gig::eg2_ctrl_t::type_channelaftertouch:
223     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
224     break;
225     case ::gig::eg2_ctrl_t::type_velocity:
226     eg2controllervalue = MIDIKeyVelocity;
227     break;
228     case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
229     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
230     break;
231 iliev 2012 }
232 iliev 2015 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
233 iliev 2012
234 iliev 2015 return eg2controllervalue;*/ // TODO: ^^^
235     return 0;
236     }
237 iliev 2012
238 iliev 2015 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
239     /*EGInfo eg;
240     eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
241     eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
242     eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
243 iliev 2012
244 iliev 2015 return eg;*/ // TODO: ^^^
245     EGInfo eg;
246     eg.Attack = 1.0;
247     eg.Decay = 1.0;
248     eg.Release = 1.0;
249     return eg;
250     }
251 iliev 2012
252 iliev 2015 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
253 persson 2175 float cutoff = *pRegion->cutoff;
254     cutoff *= RTMath::CentsToFreqRatioUnlimited(
255     MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
256     (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);
257     return cutoff;
258 iliev 2012 }
259    
260 iliev 2015 float Voice::CalculateFinalCutoff(float cutoffBase) {
261 iliev 2252 float cutoff = cutoffBase;
262 persson 2175 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
263     return cutoff;
264 iliev 2012 }
265    
266 persson 2061 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
267     // pow(10, -rt_decay * noteLength / 20):
268     return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
269     }
270    
271 persson 2114 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
272     dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
273 persson 2115 if (itEvent->Type == Event::type_control_change ||
274     (Type & Voice::type_controller_triggered) ||
275     itEvent->Param.Note.Key != MIDIKey) {
276 persson 2114 dmsg(4,("Voice %x - kill", this));
277     if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
278     // turn off the voice by entering release envelope stage
279     EnterReleaseStage();
280     } else {
281     // kill the voice fast
282 iliev 2218 SignalRack.EnterFadeOutStage();
283 persson 2114 }
284     }
285     }
286 iliev 2216
287     void Voice::SetSampleStartOffset() {
288     if (DiskVoice && RgnInfo.SampleStartOffset > pSample->MaxOffset) {
289     // The offset is applied to the RAM buffer
290     finalSynthesisParameters.dPos = 0;
291     Pos = 0;
292     } else {
293     finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample
294     Pos = RgnInfo.SampleStartOffset;
295     }
296     }
297 persson 2114
298 iliev 2012 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC