/[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 3316 - (show annotations) (download)
Thu Jul 20 12:05:53 2017 UTC (6 years, 8 months ago) by schoenebeck
File size: 11642 byte(s)
* NKSP: Implemented built-in script function "change_sustain()".
* Bumped version (2.0.0.svn72).

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

  ViewVC Help
Powered by ViewVC