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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3034 - (hide annotations) (download)
Mon Oct 31 00:05:00 2016 UTC (7 years, 5 months ago) by schoenebeck
File size: 11537 byte(s)
* Fixed a bunch of minor issues (mostly compiler warnings).
* Bumped version (2.0.0.svn31).

1 iliev 2205 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 persson 2311 * Copyright (C) 2011 - 2012 Grigor Iliev *
6 iliev 2205 * *
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 iliev 2217 SFSignalUnit::SFSignalUnit(SF2SignalUnitRack* rack): SignalUnit(rack), pVoice(NULL) {
29    
30     }
31    
32 iliev 2205 void EGUnit::EnterReleaseStage() {
33 iliev 2217 update(EG::event_release, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
34 iliev 2205 }
35    
36     void EGUnit::CancelRelease() {
37 iliev 2217 update(EG::event_cancel_release, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
38 iliev 2205 }
39    
40    
41     void VolEGUnit::Trigger() {
42     // set the delay trigger
43 iliev 2217 double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
44     uiDelayTrigger = pVoice->pRegion->GetEG1PreAttackDelay(pVoice->pPresetRegion) * d;
45 iliev 2205 ////////////
46 iliev 2207
47     // GetEG1Sustain gets the decrease in level in centibels
48 iliev 2217 uint sustain = ::sf2::ToRatio(-1 * pVoice->pRegion->GetEG1Sustain(pVoice->pPresetRegion)) * 1000; // in permille
49 iliev 2207
50 iliev 2205 trigger (
51     0, // should be in permille
52 iliev 2217 pVoice->pRegion->GetEG1Attack(pVoice->pPresetRegion),
53     pVoice->pRegion->GetEG1Hold(pVoice->pPresetRegion),
54     pVoice->pRegion->GetEG1Decay(pVoice->pPresetRegion),
55 iliev 2207 sustain,
56 iliev 2217 pVoice->pRegion->GetEG1Release(pVoice->pPresetRegion),
57 persson 2311 pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE,
58     false
59 iliev 2205 );
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 iliev 2217 /*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 iliev 2205 }*/
89     // TODO: ^^^
90    
91     increment(1);
92 iliev 2217 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
93 iliev 2205 }
94     }
95    
96     void ModEGUnit::Trigger() {
97 iliev 2217 double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
98     uiDelayTrigger = pVoice->pRegion->GetEG2PreAttackDelay(pVoice->pPresetRegion) * d;
99 iliev 2205
100     trigger (
101     0, // should be in permille
102 iliev 2217 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 persson 2311 pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE,
108     true
109 iliev 2205 );
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 iliev 2217 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
136 iliev 2218 }
137 iliev 2205 }
138    
139    
140 iliev 2207 void ModLfoUnit::Trigger() {
141     //reset
142     Level = 0;
143    
144     // set the delay trigger
145 iliev 2217 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
146     uiDelayTrigger = pVoice->pRegion->GetDelayModLfo(pVoice->pPresetRegion) * samplerate;
147 iliev 2207 ////////////
148    
149     trigger (
150 iliev 2217 pVoice->pRegion->GetFreqModLfo(pVoice->pPresetRegion),
151 iliev 2207 start_level_min,
152     1, 0, false, samplerate
153     );
154     update(0);
155     }
156    
157     void ModLfoUnit::Increment() {
158     if (DelayStage()) return;
159    
160 iliev 2217 SignalUnit::Increment();
161 iliev 2207
162     Level = render();
163     }
164    
165    
166 iliev 2205 void VibLfoUnit::Trigger() {
167 iliev 2207 //reset
168     Level = 0;
169    
170 iliev 2205 // set the delay trigger
171 iliev 2217 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
172     uiDelayTrigger = pVoice->pRegion->GetDelayVibLfo(pVoice->pPresetRegion) * samplerate;
173 iliev 2205 ////////////
174    
175     trigger (
176 iliev 2217 pVoice->pRegion->GetFreqVibLfo(pVoice->pPresetRegion),
177 iliev 2205 start_level_min,
178 iliev 2217 pVoice->pRegion->GetVibLfoToPitch(pVoice->pPresetRegion),
179 iliev 2205 0, false, samplerate
180     );
181     update(0);
182     }
183 iliev 2207
184 iliev 2205 void VibLfoUnit::Increment() {
185     if (DelayStage()) return;
186    
187 iliev 2217 SignalUnit::Increment();
188 iliev 2205
189     Level = render();
190     }
191    
192 iliev 2207
193 iliev 2218 EndpointUnit::EndpointUnit(SF2SignalUnitRack* rack): EndpointSignalUnit(rack) {
194 iliev 2205
195     }
196 iliev 2207
197     void EndpointUnit::Trigger() {
198 iliev 2217 prmModEgPitch->Coeff = pVoice->pRegion->GetModEnvToPitch(pVoice->pPresetRegion);
199 iliev 2207 if (prmModEgPitch->Coeff == ::sf2::NONE) prmModEgPitch->Coeff = 0;
200    
201 iliev 2217 prmModEgCutoff->Coeff = pVoice->pRegion->GetModEnvToFilterFc(pVoice->pPresetRegion); // cents
202 iliev 2207 if (prmModEgCutoff->Coeff == ::sf2::NONE) prmModEgCutoff->Coeff = 0;
203    
204 iliev 2217 prmModLfoVol->Coeff = pVoice->pRegion->GetModLfoToVolume(pVoice->pPresetRegion); // centibels
205 iliev 2207 if (prmModLfoVol->Coeff == ::sf2::NONE) prmModLfoVol->Coeff = 0;
206    
207 iliev 2217 prmModLfoCutoff->Coeff = pVoice->pRegion->GetModLfoToFilterFc(pVoice->pPresetRegion);
208 iliev 2207 if (prmModLfoCutoff->Coeff == ::sf2::NONE) prmModLfoCutoff->Coeff = 0;
209    
210 iliev 2217 prmModLfoPitch->Coeff = pVoice->pRegion->GetModLfoToPitch(pVoice->pPresetRegion);
211 iliev 2207 if (prmModLfoPitch->Coeff == ::sf2::NONE) prmModLfoPitch->Coeff = 0;
212     }
213 iliev 2205
214     bool EndpointUnit::Active() {
215 iliev 2207 return prmVolEg->pUnit->Active(); // volEGUnit
216 iliev 2205 }
217    
218     float EndpointUnit::GetVolume() {
219 iliev 2207 if (!prmVolEg->pUnit->Active()) return 0;
220     return prmVolEg->GetValue() *
221     ::sf2::ToRatio(prmModLfoVol->GetValue() /*logarithmically modified */);
222 iliev 2205 }
223    
224     float EndpointUnit::GetFilterCutoff() {
225 iliev 2207 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 iliev 2205 }
234    
235     float EndpointUnit::GetPitch() {
236 iliev 2207 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 iliev 2205
241 iliev 2207 return modEg * modLfo * vibLfo;
242 iliev 2205 }
243    
244     float EndpointUnit::GetResonance() {
245     return 1;
246     }
247    
248 iliev 2217 SF2SignalUnitRack::SF2SignalUnitRack(Voice* voice)
249 schoenebeck 3034 : SignalUnitRack(MaxUnitCount), suVolEG(this), suModEG(this), suModLfo(this), suVibLfo(this), suEndpoint(this), pVoice(voice) {
250 iliev 2217
251     suVolEG.pVoice = suModEG.pVoice = suModLfo.pVoice = suVibLfo.pVoice = suEndpoint.pVoice = voice;
252 iliev 2205 Units.add(&suVolEG);
253     Units.add(&suModEG);
254 iliev 2207 Units.add(&suModLfo);
255 iliev 2205 Units.add(&suVibLfo);
256     Units.add(&suEndpoint);
257    
258     // Volume envelope
259 iliev 2207 suEndpoint.Params.add(SignalUnit::Parameter(&suVolEG)); // Volume
260 iliev 2205 // Modulation envelope
261 iliev 2207 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 iliev 2205 // Vibrato LFO
268 iliev 2207 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 iliev 2205 }
280    
281     EndpointSignalUnit* SF2SignalUnitRack::GetEndpointUnit() {
282     return static_cast<EndpointSignalUnit*> (&suEndpoint);
283     }
284    
285 iliev 2218 void SF2SignalUnitRack::EnterFadeOutStage() {
286     suVolEG.enterFadeOutStage();
287     }
288 persson 2327
289     void SF2SignalUnitRack::EnterFadeOutStage(int maxFadeOutSteps) {
290     suVolEG.enterFadeOutStage(maxFadeOutSteps);
291     }
292    
293     void SF2SignalUnitRack::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) {
294     suVolEG.CalculateFadeOutCoeff(FadeOutTime, SampleRate);
295     }
296 iliev 2205 }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC