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 - 2015 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> SfzVoiceBase; |
35 |
|
36 |
Voice::Voice(): SfzVoiceBase(&SignalRack), SignalRack(this) { |
37 |
pEngine = NULL; |
38 |
bEqSupport = true; |
39 |
} |
40 |
|
41 |
Voice::~Voice() { |
42 |
|
43 |
} |
44 |
|
45 |
EngineChannel* Voice::GetSfzEngineChannel() { |
46 |
return static_cast<EngineChannel*>(pEngineChannel); |
47 |
} |
48 |
|
49 |
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 |
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 |
si.TotalFrameCount = (uint)pSample->GetTotalFrameCount(); |
63 |
|
64 |
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 |
return si; |
70 |
} |
71 |
|
72 |
Voice::RegionInfo Voice::GetRegionInfo() { |
73 |
RegionInfo ri; |
74 |
ri.UnityNote = pRegion->pitch_keycenter; |
75 |
ri.FineTune = pRegion->tune + pRegion->transpose * 100; |
76 |
ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63 |
77 |
ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0; |
78 |
|
79 |
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 |
|
120 |
ri.VCFResonance = pRegion->resonance; |
121 |
|
122 |
// rt_decay is in dB. Precalculate a suitable value for exp in |
123 |
// GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay |
124 |
ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay; |
125 |
|
126 |
return ri; |
127 |
} |
128 |
|
129 |
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 |
return exp(LN_10_DIV_20 * pRegion->volume) * pRegion->amplitude / 100; |
139 |
} |
140 |
|
141 |
double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
142 |
float offset = -pRegion->amp_veltrack; |
143 |
if (offset <= 0) offset += 100; |
144 |
return (offset + pRegion->amp_veltrack * pRegion->amp_velcurve[MIDIKeyVelocity]) / 100; |
145 |
} |
146 |
|
147 |
double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) { |
148 |
return 127.0 / MIDIKeyVelocity; |
149 |
} |
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 |
void Voice::ProcessChannelPressureEvent(RTList<Event>::Iterator& itEvent) { |
161 |
//TODO: ... |
162 |
} |
163 |
|
164 |
void Voice::ProcessPolyphonicKeyPressureEvent(RTList<Event>::Iterator& itEvent) { |
165 |
//TODO: ... |
166 |
} |
167 |
|
168 |
double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) { |
169 |
/*float crossfadeVolume; |
170 |
switch (pRegion->AttenuationController.type) { |
171 |
case ::gig::attenuation_ctrl_t::type_channelaftertouch: |
172 |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])]; |
173 |
break; |
174 |
case ::gig::attenuation_ctrl_t::type_velocity: |
175 |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)]; |
176 |
break; |
177 |
case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate |
178 |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])]; |
179 |
break; |
180 |
case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined |
181 |
default: |
182 |
crossfadeVolume = 1.0f; |
183 |
} |
184 |
|
185 |
return crossfadeVolume;*/ // TODO: ^^^ |
186 |
return 1.0f; |
187 |
} |
188 |
|
189 |
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 |
} |
205 |
if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue; |
206 |
|
207 |
return eg1controllervalue;*/ // TODO: ^^^ |
208 |
return 0; |
209 |
} |
210 |
|
211 |
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 |
|
220 |
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 |
|
228 |
double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) { |
229 |
/*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 |
} |
244 |
if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue; |
245 |
|
246 |
return eg2controllervalue;*/ // TODO: ^^^ |
247 |
return 0; |
248 |
} |
249 |
|
250 |
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 |
|
256 |
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 |
|
264 |
float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { |
265 |
float cutoff = *pRegion->cutoff; |
266 |
cutoff *= RTMath::CentsToFreqRatioUnlimited( |
267 |
MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack + |
268 |
(MIDIKey() - pRegion->fil_keycenter) * pRegion->fil_keytrack); |
269 |
return cutoff; |
270 |
} |
271 |
|
272 |
float Voice::CalculateFinalCutoff(float cutoffBase) { |
273 |
float cutoff = cutoffBase; |
274 |
if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate; |
275 |
return cutoff; |
276 |
} |
277 |
|
278 |
float Voice::GetReleaseTriggerAttenuation(float noteLength) { |
279 |
// pow(10, -rt_decay * noteLength / 20): |
280 |
return expf(RgnInfo.ReleaseTriggerDecay * noteLength); |
281 |
} |
282 |
|
283 |
release_trigger_t Voice::GetReleaseTriggerFlags() { |
284 |
return release_trigger_noteoff; |
285 |
} |
286 |
|
287 |
void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) { |
288 |
dmsg(4,("Voice %p processGroupEvents event type=%d", (void*)this, itEvent->Type)); |
289 |
if (itEvent->Type == Event::type_control_change || |
290 |
(Type & Voice::type_controller_triggered) || |
291 |
itEvent->Param.Note.Key != HostKey()) { |
292 |
dmsg(4,("Voice %p - kill", (void*)this)); |
293 |
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 |
SignalRack.EnterFadeOutStage(); |
299 |
} |
300 |
} |
301 |
} |
302 |
|
303 |
void Voice::SetSampleStartOffset() { |
304 |
AbstractVoice::SetSampleStartOffset(); |
305 |
|
306 |
if (DiskVoice && Pos > pSample->MaxOffset) { |
307 |
// The offset is applied to the RAM buffer |
308 |
finalSynthesisParameters.dPos = 0; |
309 |
Pos = 0; |
310 |
} |
311 |
} |
312 |
|
313 |
void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) { |
314 |
SignalRack.CalculateFadeOutCoeff(FadeOutTime, SampleRate); |
315 |
} |
316 |
|
317 |
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 |
}} // namespace LinuxSampler::sfz |