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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2298 - (show annotations) (download)
Fri Dec 9 17:04:24 2011 UTC (12 years, 3 months ago) by iliev
File size: 12819 byte(s)
* use different EQ effect instance for every voice

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

  ViewVC Help
Powered by ViewVC