/[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 3444 - (hide annotations) (download)
Sun Dec 23 19:32:11 2018 UTC (5 years, 3 months ago) by schoenebeck
File size: 13516 byte(s)
* Only play release trigger samples on sustain pedal up if this behaviour
  was explicitly requested by the instrument (otherwise only on note-off).
* Bumped version (2.1.0.svn2).

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 2837 * Copyright (C) 2009 - 2015 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 schoenebeck 3034 typedef LinuxSampler::VoiceBase<EngineChannel, ::sfz::Region, Sample, DiskThread> SfzVoiceBase;
35    
36     Voice::Voice(): SfzVoiceBase(&SignalRack), SignalRack(this) {
37 iliev 2012 pEngine = NULL;
38 iliev 2298 bEqSupport = true;
39 iliev 2012 }
40    
41     Voice::~Voice() {
42 iliev 2015
43 iliev 2012 }
44    
45 iliev 2015 EngineChannel* Voice::GetSfzEngineChannel() {
46     return static_cast<EngineChannel*>(pEngineChannel);
47     }
48    
49 iliev 2012 void Voice::SetEngine(LinuxSampler::Engine* pEngine) {
50     Engine* engine = static_cast<Engine*>(pEngine);
51     this->pEngine = engine;
52     this->pDiskThread = engine->pDiskThread;
53     dmsg(6,("Voice::SetEngine()\n"));
54     }
55    
56 iliev 2015 Voice::SampleInfo Voice::GetSampleInfo() {
57     SampleInfo si;
58     si.SampleRate = pSample->GetSampleRate();
59     si.ChannelCount = pSample->GetChannelCount();
60     si.FrameSize = pSample->GetFrameSize();
61     si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
62 schoenebeck 3054 si.TotalFrameCount = (uint)pSample->GetTotalFrameCount();
63 iliev 2012
64 iliev 2021 si.HasLoops = pRegion->HasLoop();
65     si.LoopStart = pRegion->GetLoopStart();
66     si.LoopLength = pRegion->GetLoopEnd() - pRegion->GetLoopStart();
67     si.LoopPlayCount = pRegion->GetLoopCount();
68     si.Unpitched = pRegion->pitch_keytrack == 0;
69 iliev 2015 return si;
70     }
71 iliev 2012
72 iliev 2015 Voice::RegionInfo Voice::GetRegionInfo() {
73     RegionInfo ri;
74 iliev 2018 ri.UnityNote = pRegion->pitch_keycenter;
75 persson 2086 ri.FineTune = pRegion->tune + pRegion->transpose * 100;
76 persson 2045 ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63
77 iliev 2216 ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;
78 iliev 2012
79 persson 2175 ri.VCFEnabled = pRegion->cutoff;
80     switch (pRegion->fil_type) {
81     case ::sfz::LPF_1P:
82     ri.VCFType = Filter::vcf_type_1p_lowpass;
83     break;
84     case ::sfz::LPF_2P:
85     ri.VCFType = Filter::vcf_type_2p_lowpass;
86     break;
87     case ::sfz::LPF_4P:
88     ri.VCFType = Filter::vcf_type_4p_lowpass;
89     break;
90     case ::sfz::LPF_6P:
91     ri.VCFType = Filter::vcf_type_6p_lowpass;
92     break;
93     case ::sfz::HPF_1P:
94     ri.VCFType = Filter::vcf_type_1p_highpass;
95     break;
96     case ::sfz::HPF_2P:
97     ri.VCFType = Filter::vcf_type_2p_highpass;
98     break;
99     case ::sfz::HPF_4P:
100     ri.VCFType = Filter::vcf_type_4p_highpass;
101     break;
102     case ::sfz::HPF_6P:
103     ri.VCFType = Filter::vcf_type_6p_highpass;
104     break;
105     case ::sfz::BPF_1P:
106     case ::sfz::BPF_2P:
107     ri.VCFType = Filter::vcf_type_2p_bandpass;
108     break;
109     case ::sfz::BRF_1P:
110     case ::sfz::BRF_2P:
111     ri.VCFType = Filter::vcf_type_2p_bandreject;
112     break;
113     case ::sfz::APF_1P:
114     case ::sfz::PKF_2P:
115     default:
116     ri.VCFEnabled = false;
117     break;
118     }
119 iliev 2012
120 persson 2175 ri.VCFResonance = pRegion->resonance;
121    
122 persson 2061 // rt_decay is in dB. Precalculate a suitable value for exp in
123     // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
124 persson 2072 ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
125 iliev 2012
126 iliev 2015 return ri;
127     }
128 iliev 2012
129 iliev 2015 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
130     InstrumentInfo ii;
131     ii.FineTune = 0; // TODO:
132     ii.PitchbendRange = 2; // TODO:
133    
134     return ii;
135     }
136    
137     double Voice::GetSampleAttenuation() {
138 persson 2403 return exp(LN_10_DIV_20 * pRegion->volume) * pRegion->amplitude / 100;
139 iliev 2015 }
140    
141     double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
142 persson 2315 float offset = -pRegion->amp_veltrack;
143     if (offset <= 0) offset += 100;
144     return (offset + pRegion->amp_veltrack * pRegion->amp_velcurve[MIDIKeyVelocity]) / 100;
145 iliev 2015 }
146    
147     double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
148 persson 2176 return 127.0 / MIDIKeyVelocity;
149 iliev 2015 }
150    
151     void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
152     /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
153     if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
154     itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
155     CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
156     }
157     }*/ // TODO: ^^^
158     }
159    
160 schoenebeck 2559 void Voice::ProcessChannelPressureEvent(RTList<Event>::Iterator& itEvent) {
161     //TODO: ...
162     }
163    
164     void Voice::ProcessPolyphonicKeyPressureEvent(RTList<Event>::Iterator& itEvent) {
165     //TODO: ...
166     }
167    
168 iliev 2015 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
169 iliev 2012 /*float crossfadeVolume;
170     switch (pRegion->AttenuationController.type) {
171     case ::gig::attenuation_ctrl_t::type_channelaftertouch:
172 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
173 iliev 2012 break;
174     case ::gig::attenuation_ctrl_t::type_velocity:
175 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
176 iliev 2012 break;
177     case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
178 iliev 2015 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
179 iliev 2012 break;
180     case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
181     default:
182     crossfadeVolume = 1.0f;
183     }
184    
185 iliev 2015 return crossfadeVolume;*/ // TODO: ^^^
186     return 1.0f;
187     }
188 iliev 2012
189 iliev 2015 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
190     /*double eg1controllervalue = 0;
191     switch (pRegion->EG1Controller.type) {
192     case ::gig::eg1_ctrl_t::type_none: // no controller defined
193     eg1controllervalue = 0;
194     break;
195     case ::gig::eg1_ctrl_t::type_channelaftertouch:
196     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
197     break;
198     case ::gig::eg1_ctrl_t::type_velocity:
199     eg1controllervalue = MIDIKeyVelocity;
200     break;
201     case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
202     eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
203     break;
204 iliev 2012 }
205 iliev 2015 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
206 iliev 2012
207 iliev 2015 return eg1controllervalue;*/ // TODO: ^^^
208     return 0;
209     }
210 iliev 2012
211 iliev 2015 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
212     /*EGInfo eg;
213     // (eg1attack is different from the others)
214     eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
215     1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
216     1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
217     eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
218     eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
219 iliev 2012
220 iliev 2015 return eg;*/ // TODO: ^^^
221     EGInfo eg;
222     eg.Attack = 1.0;
223     eg.Decay = 1.0;
224     eg.Release = 1.0;
225     return eg;
226     }
227 iliev 2012
228 persson 2176 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
229 iliev 2015 /*double eg2controllervalue = 0;
230     switch (pRegion->EG2Controller.type) {
231     case ::gig::eg2_ctrl_t::type_none: // no controller defined
232     eg2controllervalue = 0;
233     break;
234     case ::gig::eg2_ctrl_t::type_channelaftertouch:
235     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
236     break;
237     case ::gig::eg2_ctrl_t::type_velocity:
238     eg2controllervalue = MIDIKeyVelocity;
239     break;
240     case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
241     eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
242     break;
243 iliev 2012 }
244 iliev 2015 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
245 iliev 2012
246 iliev 2015 return eg2controllervalue;*/ // TODO: ^^^
247     return 0;
248     }
249 iliev 2012
250 iliev 2015 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
251     /*EGInfo eg;
252     eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
253     eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
254     eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
255 iliev 2012
256 iliev 2015 return eg;*/ // TODO: ^^^
257     EGInfo eg;
258     eg.Attack = 1.0;
259     eg.Decay = 1.0;
260     eg.Release = 1.0;
261     return eg;
262     }
263 iliev 2012
264 iliev 2015 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
265 persson 2175 float cutoff = *pRegion->cutoff;
266     cutoff *= RTMath::CentsToFreqRatioUnlimited(
267     MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
268 schoenebeck 2879 (MIDIKey() - pRegion->fil_keycenter) * pRegion->fil_keytrack);
269 persson 2175 return cutoff;
270 iliev 2012 }
271    
272 iliev 2015 float Voice::CalculateFinalCutoff(float cutoffBase) {
273 iliev 2252 float cutoff = cutoffBase;
274 persson 2175 if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
275     return cutoff;
276 iliev 2012 }
277    
278 persson 2061 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
279     // pow(10, -rt_decay * noteLength / 20):
280     return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
281     }
282    
283 schoenebeck 3444 release_trigger_t Voice::GetReleaseTriggerFlags() {
284     return release_trigger_noteoff;
285     }
286    
287 persson 2114 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
288 persson 2837 dmsg(4,("Voice %p processGroupEvents event type=%d", (void*)this, itEvent->Type));
289 persson 2115 if (itEvent->Type == Event::type_control_change ||
290     (Type & Voice::type_controller_triggered) ||
291 schoenebeck 2879 itEvent->Param.Note.Key != HostKey()) {
292 persson 2837 dmsg(4,("Voice %p - kill", (void*)this));
293 persson 2114 if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
294     // turn off the voice by entering release envelope stage
295     EnterReleaseStage();
296     } else {
297     // kill the voice fast
298 iliev 2218 SignalRack.EnterFadeOutStage();
299 persson 2114 }
300     }
301     }
302 schoenebeck 3251
303 iliev 2216 void Voice::SetSampleStartOffset() {
304 schoenebeck 3251 AbstractVoice::SetSampleStartOffset();
305    
306     if (DiskVoice && Pos > pSample->MaxOffset) {
307 iliev 2216 // The offset is applied to the RAM buffer
308     finalSynthesisParameters.dPos = 0;
309     Pos = 0;
310     }
311     }
312 persson 2114
313 persson 2327 void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) {
314     SignalRack.CalculateFadeOutCoeff(FadeOutTime, SampleRate);
315     }
316    
317 persson 2382 int Voice::CalculatePan(uint8_t pan) {
318     // the value isn't limited to [0, 127] here, as this is done
319     // later in SignalUnit.CalculatePan
320     return pan + RgnInfo.Pan;
321     }
322    
323 iliev 2012 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC