/[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 2217 - (hide annotations) (download)
Tue Jul 26 15:51:30 2011 UTC (12 years, 9 months ago) by iliev
File size: 11157 byte(s)
* removed helper classes SignalUnitBase,
  EndpointSignalUnitBase, SignalUnitRackBase
* clean up sf2::Voice
* bumped version to 1.0.0.cvs12

1 iliev 2205 /***************************************************************************
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 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     pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE
58 iliev 2205 );
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 iliev 2217 /*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 iliev 2205 }*/
88     // TODO: ^^^
89    
90     increment(1);
91 iliev 2217 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
92 iliev 2205 }
93     }
94    
95     void ModEGUnit::Trigger() {
96 iliev 2217 double d = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
97     uiDelayTrigger = pVoice->pRegion->GetEG2PreAttackDelay(pVoice->pPresetRegion) * d;
98 iliev 2205
99     trigger (
100     0, // should be in permille
101 iliev 2217 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 iliev 2205 );
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 iliev 2217 if (!toStageEndLeft()) update(EG::event_stage_end, pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
134 iliev 2205 }
135     }
136    
137    
138 iliev 2207 void ModLfoUnit::Trigger() {
139     //reset
140     Level = 0;
141    
142     // set the delay trigger
143 iliev 2217 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
144     uiDelayTrigger = pVoice->pRegion->GetDelayModLfo(pVoice->pPresetRegion) * samplerate;
145 iliev 2207 ////////////
146    
147     trigger (
148 iliev 2217 pVoice->pRegion->GetFreqModLfo(pVoice->pPresetRegion),
149 iliev 2207 start_level_min,
150     1, 0, false, samplerate
151     );
152     update(0);
153     }
154    
155     void ModLfoUnit::Increment() {
156     if (DelayStage()) return;
157    
158 iliev 2217 SignalUnit::Increment();
159 iliev 2207
160     Level = render();
161     }
162    
163    
164 iliev 2205 void VibLfoUnit::Trigger() {
165 iliev 2207 //reset
166     Level = 0;
167    
168 iliev 2205 // set the delay trigger
169 iliev 2217 double samplerate = pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE;
170     uiDelayTrigger = pVoice->pRegion->GetDelayVibLfo(pVoice->pPresetRegion) * samplerate;
171 iliev 2205 ////////////
172    
173     trigger (
174 iliev 2217 pVoice->pRegion->GetFreqVibLfo(pVoice->pPresetRegion),
175 iliev 2205 start_level_min,
176 iliev 2217 pVoice->pRegion->GetVibLfoToPitch(pVoice->pPresetRegion),
177 iliev 2205 0, false, samplerate
178     );
179     update(0);
180     }
181 iliev 2207
182 iliev 2205 void VibLfoUnit::Increment() {
183     if (DelayStage()) return;
184    
185 iliev 2217 SignalUnit::Increment();
186 iliev 2205
187     Level = render();
188     }
189    
190 iliev 2207
191 iliev 2217 EndpointUnit::EndpointUnit(SF2SignalUnitRack* rack): EndpointSignalUnit(rack), pVoice(rack->pVoice) {
192 iliev 2205
193     }
194 iliev 2207
195     void EndpointUnit::Trigger() {
196 iliev 2217 prmModEgPitch->Coeff = pVoice->pRegion->GetModEnvToPitch(pVoice->pPresetRegion);
197 iliev 2207 if (prmModEgPitch->Coeff == ::sf2::NONE) prmModEgPitch->Coeff = 0;
198    
199 iliev 2217 prmModEgCutoff->Coeff = pVoice->pRegion->GetModEnvToFilterFc(pVoice->pPresetRegion); // cents
200 iliev 2207 if (prmModEgCutoff->Coeff == ::sf2::NONE) prmModEgCutoff->Coeff = 0;
201    
202 iliev 2217 prmModLfoVol->Coeff = pVoice->pRegion->GetModLfoToVolume(pVoice->pPresetRegion); // centibels
203 iliev 2207 if (prmModLfoVol->Coeff == ::sf2::NONE) prmModLfoVol->Coeff = 0;
204    
205 iliev 2217 prmModLfoCutoff->Coeff = pVoice->pRegion->GetModLfoToFilterFc(pVoice->pPresetRegion);
206 iliev 2207 if (prmModLfoCutoff->Coeff == ::sf2::NONE) prmModLfoCutoff->Coeff = 0;
207    
208 iliev 2217 prmModLfoPitch->Coeff = pVoice->pRegion->GetModLfoToPitch(pVoice->pPresetRegion);
209 iliev 2207 if (prmModLfoPitch->Coeff == ::sf2::NONE) prmModLfoPitch->Coeff = 0;
210     }
211 iliev 2205
212     bool EndpointUnit::Active() {
213     if (Params.size() < 1) return false;
214 iliev 2207 return prmVolEg->pUnit->Active(); // volEGUnit
215 iliev 2205 }
216    
217     float EndpointUnit::GetVolume() {
218 iliev 2207 if (!prmVolEg->pUnit->Active()) return 0;
219     return prmVolEg->GetValue() *
220     ::sf2::ToRatio(prmModLfoVol->GetValue() /*logarithmically modified */);
221 iliev 2205 }
222    
223     float EndpointUnit::GetFilterCutoff() {
224 iliev 2207 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 iliev 2205 }
233    
234     float EndpointUnit::GetPitch() {
235 iliev 2207 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 iliev 2205
240 iliev 2207 return modEg * modLfo * vibLfo;
241 iliev 2205 }
242    
243     float EndpointUnit::GetResonance() {
244     return 1;
245     }
246    
247 iliev 2217 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 iliev 2205 Units.add(&suVolEG);
252     Units.add(&suModEG);
253 iliev 2207 Units.add(&suModLfo);
254 iliev 2205 Units.add(&suVibLfo);
255     Units.add(&suEndpoint);
256    
257     // Volume envelope
258 iliev 2207 suEndpoint.Params.add(SignalUnit::Parameter(&suVolEG)); // Volume
259 iliev 2205 // Modulation envelope
260 iliev 2207 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 iliev 2205 // Vibrato LFO
267 iliev 2207 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 iliev 2205 }
279    
280     EndpointSignalUnit* SF2SignalUnitRack::GetEndpointUnit() {
281     return static_cast<EndpointSignalUnit*> (&suEndpoint);
282     }
283    
284     }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC