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