/[svn]/linuxsampler/trunk/src/engines/sf2/SF2SignalUnitRack.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sf2/SF2SignalUnitRack.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2311 - (show annotations) (download)
Sat Feb 11 11:08:09 2012 UTC (12 years, 2 months ago) by persson
File size: 11254 byte(s)
* more LV2 "state" extension support fixes (patch by David Robillard)
* sfz parser: allow double spaces in sample filenames
* sfz parser: allow absolute paths for sample filenames
* MME driver: fixed memory handling bug found with cppcheck
* sfz/sf2 engines: use linear decay and release for filter and pitch
  envelope generators

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2011 - 2012 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 false
59 );
60 }
61
62 void VolEGUnit::Increment() {
63 if (DelayStage()) return;
64
65 EGUnit::Increment();
66 if (!active()) return;
67
68 switch (getSegmentType()) {
69 case EG::segment_lin:
70 processLin();
71 break;
72 case EG::segment_exp:
73 processExp();
74 break;
75 case EG::segment_end:
76 getLevel();
77 break; // noop
78 case EG::segment_pow:
79 processPow();
80 break;
81 }
82
83 if (active()) {
84
85 // 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
86 /*if (pVoice->SmplInfo.HasLoops && pVoice->Pos <= pVoice->SmplInfo.LoopStart && pVoice->SmplInfo.LoopStart < newPos) {
87 update(EG::event_hold_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
88 }*/
89 // TODO: ^^^
90
91 increment(1);
92 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
93 }
94 }
95
96 void ModEGUnit::Trigger() {
97 double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
98 uiDelayTrigger = pVoice->pRegion->GetEG2PreAttackDelay(pVoice->pPresetRegion) * d;
99
100 trigger (
101 0, // should be in permille
102 pVoice->pRegion->GetEG2Attack(pVoice->pPresetRegion),
103 pVoice->pRegion->GetEG2Hold(pVoice->pPresetRegion),
104 pVoice->pRegion->GetEG2Decay(pVoice->pPresetRegion),
105 uint(1000 - pVoice->pRegion->GetEG2Sustain(pVoice->pPresetRegion)),
106 pVoice->pRegion->GetEG2Release(pVoice->pPresetRegion),
107 pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE,
108 true
109 );
110 }
111
112 void ModEGUnit::Increment() {
113 if (DelayStage()) return;
114
115 EGUnit::Increment();
116 if (!active()) return;
117
118 switch (getSegmentType()) {
119 case EG::segment_lin:
120 processLin();
121 break;
122 case EG::segment_exp:
123 processExp();
124 break;
125 case EG::segment_end:
126 getLevel();
127 break; // noop
128 case EG::segment_pow:
129 processPow();
130 break;
131 }
132
133 if (active()) {
134 increment(1);
135 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
136 }
137 }
138
139
140 void ModLfoUnit::Trigger() {
141 //reset
142 Level = 0;
143
144 // set the delay trigger
145 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
146 uiDelayTrigger = pVoice->pRegion->GetDelayModLfo(pVoice->pPresetRegion) * samplerate;
147 ////////////
148
149 trigger (
150 pVoice->pRegion->GetFreqModLfo(pVoice->pPresetRegion),
151 start_level_min,
152 1, 0, false, samplerate
153 );
154 update(0);
155 }
156
157 void ModLfoUnit::Increment() {
158 if (DelayStage()) return;
159
160 SignalUnit::Increment();
161
162 Level = render();
163 }
164
165
166 void VibLfoUnit::Trigger() {
167 //reset
168 Level = 0;
169
170 // set the delay trigger
171 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
172 uiDelayTrigger = pVoice->pRegion->GetDelayVibLfo(pVoice->pPresetRegion) * samplerate;
173 ////////////
174
175 trigger (
176 pVoice->pRegion->GetFreqVibLfo(pVoice->pPresetRegion),
177 start_level_min,
178 pVoice->pRegion->GetVibLfoToPitch(pVoice->pPresetRegion),
179 0, false, samplerate
180 );
181 update(0);
182 }
183
184 void VibLfoUnit::Increment() {
185 if (DelayStage()) return;
186
187 SignalUnit::Increment();
188
189 Level = render();
190 }
191
192
193 EndpointUnit::EndpointUnit(SF2SignalUnitRack* rack): EndpointSignalUnit(rack) {
194
195 }
196
197 void EndpointUnit::Trigger() {
198 prmModEgPitch->Coeff = pVoice->pRegion->GetModEnvToPitch(pVoice->pPresetRegion);
199 if (prmModEgPitch->Coeff == ::sf2::NONE) prmModEgPitch->Coeff = 0;
200
201 prmModEgCutoff->Coeff = pVoice->pRegion->GetModEnvToFilterFc(pVoice->pPresetRegion); // cents
202 if (prmModEgCutoff->Coeff == ::sf2::NONE) prmModEgCutoff->Coeff = 0;
203
204 prmModLfoVol->Coeff = pVoice->pRegion->GetModLfoToVolume(pVoice->pPresetRegion); // centibels
205 if (prmModLfoVol->Coeff == ::sf2::NONE) prmModLfoVol->Coeff = 0;
206
207 prmModLfoCutoff->Coeff = pVoice->pRegion->GetModLfoToFilterFc(pVoice->pPresetRegion);
208 if (prmModLfoCutoff->Coeff == ::sf2::NONE) prmModLfoCutoff->Coeff = 0;
209
210 prmModLfoPitch->Coeff = pVoice->pRegion->GetModLfoToPitch(pVoice->pPresetRegion);
211 if (prmModLfoPitch->Coeff == ::sf2::NONE) prmModLfoPitch->Coeff = 0;
212 }
213
214 bool EndpointUnit::Active() {
215 return prmVolEg->pUnit->Active(); // volEGUnit
216 }
217
218 float EndpointUnit::GetVolume() {
219 if (!prmVolEg->pUnit->Active()) return 0;
220 return prmVolEg->GetValue() *
221 ::sf2::ToRatio(prmModLfoVol->GetValue() /*logarithmically modified */);
222 }
223
224 float EndpointUnit::GetFilterCutoff() {
225 double modEg, modLfo;
226 modEg = prmModEgCutoff->pUnit->Active() ? prmModEgCutoff->GetValue() : 0;
227 modEg = RTMath::CentsToFreqRatioUnlimited(modEg);
228
229 modLfo = prmModLfoCutoff->pUnit->Active() ? prmModLfoCutoff->GetValue() : 0;
230 modLfo = RTMath::CentsToFreqRatioUnlimited(modLfo);
231
232 return modEg * modLfo;
233 }
234
235 float EndpointUnit::GetPitch() {
236 double modEg, modLfo, vibLfo;
237 modEg = prmModEgPitch->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmModEgPitch->GetValue()) : 1;
238 modLfo = prmModLfoPitch->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmModLfoPitch->GetValue()) : 1;
239 vibLfo = prmVibLfo->pUnit->Active() ? RTMath::CentsToFreqRatioUnlimited(prmVibLfo->GetValue()) : 1;
240
241 return modEg * modLfo * vibLfo;
242 }
243
244 float EndpointUnit::GetResonance() {
245 return 1;
246 }
247
248 SF2SignalUnitRack::SF2SignalUnitRack(Voice* voice)
249 : SignalUnitRack(MaxUnitCount), pVoice(voice), suVolEG(this), suModEG(this), suModLfo(this), suVibLfo(this), suEndpoint(this) {
250
251 suVolEG.pVoice = suModEG.pVoice = suModLfo.pVoice = suVibLfo.pVoice = suEndpoint.pVoice = voice;
252 Units.add(&suVolEG);
253 Units.add(&suModEG);
254 Units.add(&suModLfo);
255 Units.add(&suVibLfo);
256 Units.add(&suEndpoint);
257
258 // Volume envelope
259 suEndpoint.Params.add(SignalUnit::Parameter(&suVolEG)); // Volume
260 // Modulation envelope
261 suEndpoint.Params.add(SignalUnit::Parameter(&suModEG)); // Pitch
262 suEndpoint.Params.add(SignalUnit::Parameter(&suModEG)); // Filter cutoff
263 // Modulation LFO
264 suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Volume
265 suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Pitch
266 suEndpoint.Params.add(SignalUnit::Parameter(&suModLfo)); // Filter cutoff
267 // Vibrato LFO
268 suEndpoint.Params.add(SignalUnit::Parameter(&suVibLfo)); // Pitch
269
270 /* This should be done at the end because when a parameter is added to
271 ArrayList a new copy is made for all parameters */
272 suEndpoint.prmVolEg = &suEndpoint.Params[0];
273 suEndpoint.prmModEgPitch = &suEndpoint.Params[1];
274 suEndpoint.prmModEgCutoff = &suEndpoint.Params[2];
275 suEndpoint.prmModLfoVol = &suEndpoint.Params[3];
276 suEndpoint.prmModLfoPitch = &suEndpoint.Params[4];
277 suEndpoint.prmModLfoCutoff = &suEndpoint.Params[5];
278 suEndpoint.prmVibLfo = &suEndpoint.Params[6];
279 }
280
281 EndpointSignalUnit* SF2SignalUnitRack::GetEndpointUnit() {
282 return static_cast<EndpointSignalUnit*> (&suEndpoint);
283 }
284
285 void SF2SignalUnitRack::EnterFadeOutStage() {
286 suVolEG.enterFadeOutStage();
287 }
288
289 }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC