1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2011 Grigor Iliev * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include "SF2SignalUnitRack.h" |
24 |
#include "Voice.h" |
25 |
|
26 |
namespace LinuxSampler { namespace sf2 { |
27 |
|
28 |
SFSignalUnit::SFSignalUnit(SF2SignalUnitRack* rack): SignalUnit(rack), pVoice(NULL) { |
29 |
|
30 |
} |
31 |
|
32 |
void EGUnit::EnterReleaseStage() { |
33 |
update(EG::event_release, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
34 |
} |
35 |
|
36 |
void EGUnit::CancelRelease() { |
37 |
update(EG::event_cancel_release, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
38 |
} |
39 |
|
40 |
|
41 |
void VolEGUnit::Trigger() { |
42 |
// set the delay trigger |
43 |
double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
44 |
uiDelayTrigger = pVoice->pRegion->GetEG1PreAttackDelay(pVoice->pPresetRegion) * d; |
45 |
//////////// |
46 |
|
47 |
// GetEG1Sustain gets the decrease in level in centibels |
48 |
uint sustain = ::sf2::ToRatio(-1 * pVoice->pRegion->GetEG1Sustain(pVoice->pPresetRegion)) * 1000; // in permille |
49 |
|
50 |
trigger ( |
51 |
0, // should be in permille |
52 |
pVoice->pRegion->GetEG1Attack(pVoice->pPresetRegion), |
53 |
pVoice->pRegion->GetEG1Hold(pVoice->pPresetRegion), |
54 |
pVoice->pRegion->GetEG1Decay(pVoice->pPresetRegion), |
55 |
sustain, |
56 |
pVoice->pRegion->GetEG1Release(pVoice->pPresetRegion), |
57 |
pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE |
58 |
); |
59 |
} |
60 |
|
61 |
void VolEGUnit::Increment() { |
62 |
if (DelayStage()) return; |
63 |
|
64 |
EGUnit::Increment(); |
65 |
if (!active()) return; |
66 |
|
67 |
switch (getSegmentType()) { |
68 |
case EG::segment_lin: |
69 |
processLin(); |
70 |
break; |
71 |
case EG::segment_exp: |
72 |
processExp(); |
73 |
break; |
74 |
case EG::segment_end: |
75 |
getLevel(); |
76 |
break; // noop |
77 |
case EG::segment_pow: |
78 |
processPow(); |
79 |
break; |
80 |
} |
81 |
|
82 |
if (active()) { |
83 |
|
84 |
// if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage |
85 |
/*if (pVoice->SmplInfo.HasLoops && pVoice->Pos <= pVoice->SmplInfo.LoopStart && pVoice->SmplInfo.LoopStart < newPos) { |
86 |
update(EG::event_hold_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
87 |
}*/ |
88 |
// TODO: ^^^ |
89 |
|
90 |
increment(1); |
91 |
if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
92 |
} |
93 |
} |
94 |
|
95 |
void ModEGUnit::Trigger() { |
96 |
double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
97 |
uiDelayTrigger = pVoice->pRegion->GetEG2PreAttackDelay(pVoice->pPresetRegion) * d; |
98 |
|
99 |
trigger ( |
100 |
0, // should be in permille |
101 |
pVoice->pRegion->GetEG2Attack(pVoice->pPresetRegion), |
102 |
pVoice->pRegion->GetEG2Hold(pVoice->pPresetRegion), |
103 |
pVoice->pRegion->GetEG2Decay(pVoice->pPresetRegion), |
104 |
uint(1000 - pVoice->pRegion->GetEG2Sustain(pVoice->pPresetRegion)), |
105 |
pVoice->pRegion->GetEG2Release(pVoice->pPresetRegion), |
106 |
pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE |
107 |
); |
108 |
} |
109 |
|
110 |
void ModEGUnit::Increment() { |
111 |
if (DelayStage()) return; |
112 |
|
113 |
EGUnit::Increment(); |
114 |
if (!active()) return; |
115 |
|
116 |
switch (getSegmentType()) { |
117 |
case EG::segment_lin: |
118 |
processLin(); |
119 |
break; |
120 |
case EG::segment_exp: |
121 |
processExp(); |
122 |
break; |
123 |
case EG::segment_end: |
124 |
getLevel(); |
125 |
break; // noop |
126 |
case EG::segment_pow: |
127 |
processPow(); |
128 |
break; |
129 |
} |
130 |
|
131 |
if (active()) { |
132 |
increment(1); |
133 |
if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
134 |
} |
135 |
} |
136 |
|
137 |
|
138 |
void ModLfoUnit::Trigger() { |
139 |
//reset |
140 |
Level = 0; |
141 |
|
142 |
// set the delay trigger |
143 |
double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
144 |
uiDelayTrigger = pVoice->pRegion->GetDelayModLfo(pVoice->pPresetRegion) * samplerate; |
145 |
//////////// |
146 |
|
147 |
trigger ( |
148 |
pVoice->pRegion->GetFreqModLfo(pVoice->pPresetRegion), |
149 |
start_level_min, |
150 |
1, 0, false, samplerate |
151 |
); |
152 |
update(0); |
153 |
} |
154 |
|
155 |
void ModLfoUnit::Increment() { |
156 |
if (DelayStage()) return; |
157 |
|
158 |
SignalUnit::Increment(); |
159 |
|
160 |
Level = render(); |
161 |
} |
162 |
|
163 |
|
164 |
void VibLfoUnit::Trigger() { |
165 |
//reset |
166 |
Level = 0; |
167 |
|
168 |
// set the delay trigger |
169 |
double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
170 |
uiDelayTrigger = pVoice->pRegion->GetDelayVibLfo(pVoice->pPresetRegion) * samplerate; |
171 |
//////////// |
172 |
|
173 |
trigger ( |
174 |
pVoice->pRegion->GetFreqVibLfo(pVoice->pPresetRegion), |
175 |
start_level_min, |
176 |
pVoice->pRegion->GetVibLfoToPitch(pVoice->pPresetRegion), |
177 |
0, false, samplerate |
178 |
); |
179 |
update(0); |
180 |
} |
181 |
|
182 |
void VibLfoUnit::Increment() { |
183 |
if (DelayStage()) return; |
184 |
|
185 |
SignalUnit::Increment(); |
186 |
|
187 |
Level = render(); |
188 |
} |
189 |
|
190 |
|
191 |
EndpointUnit::EndpointUnit(SF2SignalUnitRack* rack): EndpointSignalUnit(rack), pVoice(rack->pVoice) { |
192 |
|
193 |
} |
194 |
|
195 |
void EndpointUnit::Trigger() { |
196 |
prmModEgPitch->Coeff = pVoice->pRegion->GetModEnvToPitch(pVoice->pPresetRegion); |
197 |
if (prmModEgPitch->Coeff == ::sf2::NONE) prmModEgPitch->Coeff = 0; |
198 |
|
199 |
prmModEgCutoff->Coeff = pVoice->pRegion->GetModEnvToFilterFc(pVoice->pPresetRegion); // cents |
200 |
if (prmModEgCutoff->Coeff == ::sf2::NONE) prmModEgCutoff->Coeff = 0; |
201 |
|
202 |
prmModLfoVol->Coeff = pVoice->pRegion->GetModLfoToVolume(pVoice->pPresetRegion); // centibels |
203 |
if (prmModLfoVol->Coeff == ::sf2::NONE) prmModLfoVol->Coeff = 0; |
204 |
|
205 |
prmModLfoCutoff->Coeff = pVoice->pRegion->GetModLfoToFilterFc(pVoice->pPresetRegion); |
206 |
if (prmModLfoCutoff->Coeff == ::sf2::NONE) prmModLfoCutoff->Coeff = 0; |
207 |
|
208 |
prmModLfoPitch->Coeff = pVoice->pRegion->GetModLfoToPitch(pVoice->pPresetRegion); |
209 |
if (prmModLfoPitch->Coeff == ::sf2::NONE) prmModLfoPitch->Coeff = 0; |
210 |
} |
211 |
|
212 |
bool EndpointUnit::Active() { |
213 |
if (Params.size() < 1) return false; |
214 |
return prmVolEg->pUnit->Active(); // volEGUnit |
215 |
} |
216 |
|
217 |
float EndpointUnit::GetVolume() { |
218 |
if (!prmVolEg->pUnit->Active()) return 0; |
219 |
return prmVolEg->GetValue() * |
220 |
::sf2::ToRatio(prmModLfoVol->GetValue() /*logarithmically modified */); |
221 |
} |
222 |
|
223 |
float EndpointUnit::GetFilterCutoff() { |
224 |
double modEg, modLfo; |
225 |
modEg = prmModEgCutoff->pUnit->Active() ? prmModEgCutoff->GetValue() : 0; |
226 |
modEg = RTMath::CentsToFreqRatioUnlimited(modEg); |
227 |
|
228 |
modLfo = prmModLfoCutoff->pUnit->Active() ? prmModLfoCutoff->GetValue() : 0; |
229 |
modLfo = RTMath::CentsToFreqRatioUnlimited(modLfo); |
230 |
|
231 |
return modEg * modLfo; |
232 |
} |
233 |
|
234 |
float EndpointUnit::GetPitch() { |
235 |
double modEg, modLfo, vibLfo; |
236 |
modEg = prmModEgPitch->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmModEgPitch->GetValue()) : 1; |
237 |
modLfo = prmModLfoPitch->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmModLfoPitch->GetValue()) : 1; |
238 |
vibLfo = prmVibLfo->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmVibLfo->GetValue()) : 1; |
239 |
|
240 |
return modEg * modLfo * vibLfo; |
241 |
} |
242 |
|
243 |
float EndpointUnit::GetResonance() { |
244 |
return 1; |
245 |
} |
246 |
|
247 |
SF2SignalUnitRack::SF2SignalUnitRack(Voice* voice) |
248 |
: pVoice(voice), suVolEG(this), suModEG(this), suModLfo(this), suVibLfo(this), suEndpoint(this) { |
249 |
|
250 |
suVolEG.pVoice = suModEG.pVoice = suModLfo.pVoice = suVibLfo.pVoice = suEndpoint.pVoice = voice; |
251 |
Units.add(&suVolEG); |
252 |
Units.add(&suModEG); |
253 |
Units.add(&suModLfo); |
254 |
Units.add(&suVibLfo); |
255 |
Units.add(&suEndpoint); |
256 |
|
257 |
// Volume envelope |
258 |
suEndpoint.Params.add(SignalUnit::Parameter(&suVolEG)); // Volume |
259 |
// Modulation envelope |
260 |
suEndpoint.Params.add(SignalUnit::Parameter(&suModEG)); // Pitch |
261 |
suEndpoint.Params.add(SignalUnit::Parameter(&suModEG)); // Filter cutoff |
262 |
// Modulation LFO |
263 |
suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Volume |
264 |
suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Pitch |
265 |
suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Filter cutoff |
266 |
// Vibrato LFO |
267 |
suEndpoint.Params.add(SignalUnit::Parameter(&suVibLfo)); // Pitch |
268 |
|
269 |
/* This should be done at the end because when a parameter is added to |
270 |
ArrayList a new copy is made for all parameters */ |
271 |
suEndpoint.prmVolEg = &suEndpoint.Params[0]; |
272 |
suEndpoint.prmModEgPitch = &suEndpoint.Params[1]; |
273 |
suEndpoint.prmModEgCutoff = &suEndpoint.Params[2]; |
274 |
suEndpoint.prmModLfoVol = &suEndpoint.Params[3]; |
275 |
suEndpoint.prmModLfoPitch = &suEndpoint.Params[4]; |
276 |
suEndpoint.prmModLfoCutoff = &suEndpoint.Params[5]; |
277 |
suEndpoint.prmVibLfo = &suEndpoint.Params[6]; |
278 |
} |
279 |
|
280 |
EndpointSignalUnit* SF2SignalUnitRack::GetEndpointUnit() { |
281 |
return static_cast<EndpointSignalUnit*> (&suEndpoint); |
282 |
} |
283 |
|
284 |
}} // namespace LinuxSampler::sf2 |