/[svn]/linuxsampler/trunk/src/engines/gig/Voice.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/Voice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (show annotations) (download) (as text)
Fri Oct 8 21:04:51 2004 UTC (19 years, 5 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 17577 byte(s)
forgot to modify include for Pool.h

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
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 #ifndef __LS_GIG_VOICE_H__
24 #define __LS_GIG_VOICE_H__
25
26 #include "../../common/global.h"
27
28 #if DEBUG_HEADERS
29 # warning Voice.h included
30 #endif // DEBUG_HEADERS
31
32 #include "../../common/RTMath.h"
33 #include "../../common/RingBuffer.h"
34 #include "../../common/Pool.h"
35 #include "../../drivers/audio/AudioOutputDevice.h"
36 #include "../../lib/fileloader/libgig/gig.h"
37 #include "../common/BiquadFilter.h"
38 #include "Engine.h"
39 #include "Stream.h"
40 #include "DiskThread.h"
41
42 #include "EGDecay.h"
43 #include "Filter.h"
44 #include "../common/LFO.h"
45
46 #define USE_LINEAR_INTERPOLATION 0 ///< set to 0 if you prefer cubic interpolation (slower, better quality)
47 #define ENABLE_FILTER 1 ///< if set to 0 then filter (VCF) code is ignored on compile time
48 #define FILTER_UPDATE_PERIOD 64 ///< amount of sample points after which filter parameters (cutoff, resonance) are going to be updated (higher value means less CPU load, but also worse parameter resolution, this value will be aligned to a power of two)
49 #define FORCE_FILTER_USAGE 0 ///< if set to 1 then filter is always used, if set to 0 filter is used only in case the instrument file defined one
50 #define FILTER_CUTOFF_MAX 10000.0f ///< maximum cutoff frequency (10kHz)
51 #define FILTER_CUTOFF_MIN 100.0f ///< minimum cutoff frequency (100Hz)
52
53 // Uncomment following line to override external cutoff controller
54 //#define OVERRIDE_FILTER_CUTOFF_CTRL 1 ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel')
55
56 // Uncomment following line to override external resonance controller
57 //#define OVERRIDE_FILTER_RES_CTRL 91 ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth')
58
59 // Uncomment following line to override filter type
60 //#define OVERRIDE_FILTER_TYPE ::gig::vcf_type_lowpass ///< either ::gig::vcf_type_lowpass, ::gig::vcf_type_bandpass or ::gig::vcf_type_highpass
61
62 namespace LinuxSampler { namespace gig {
63
64 class Engine;
65 class EGADSR;
66 class VCAManipulator;
67 class VCFCManipulator;
68 class VCOManipulator;
69
70 /// Reflects a MIDI controller
71 struct midi_ctrl {
72 uint8_t controller; ///< MIDI control change controller number
73 uint8_t value; ///< Current MIDI controller value
74 float fvalue; ///< Transformed / effective value (e.g. volume level or filter cutoff frequency)
75 };
76
77 /** Gig Voice
78 *
79 * Renders a voice for the Gigasampler format.
80 */
81 class Voice {
82 public:
83 // Types
84 enum type_t {
85 type_normal,
86 type_release_trigger_required, ///< If the key of this voice will be released, it causes a release triggered voice to be spawned
87 type_release_trigger ///< Release triggered voice which cannot be killed by releasing its key
88 };
89
90 // Attributes
91 type_t Type; ///< Voice Type
92 int MIDIKey; ///< MIDI key number of the key that triggered the voice
93 uint KeyGroup;
94 DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
95
96 // Methods
97 Voice();
98 ~Voice();
99 void Kill(Pool<Event>::Iterator& itKillEvent);
100 void KillImmediately();
101 void Render(uint Samples);
102 void Reset();
103 void SetOutput(AudioOutputDevice* pAudioOutputDevice);
104 void SetEngine(Engine* pEngine);
105 int Trigger(Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer = 0, bool ReleaseTriggerVoice = false);
106 inline bool IsActive() { return Active; }
107 private:
108 // Types
109 enum playback_state_t {
110 playback_state_ram,
111 playback_state_disk,
112 playback_state_end
113 };
114
115 // Attributes
116 gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists.
117 float Volume; ///< Volume level of the voice
118 float PanLeft;
119 float PanRight;
120 float CrossfadeVolume; ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course)
121 double Pos; ///< Current playback position in sample
122 double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
123 double PitchBend; ///< Current pitch value of the pitchbend wheel
124 ::gig::Sample* pSample; ///< Pointer to the sample to be played back
125 ::gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
126 ::gig::DimensionRegion* pDimRgn; ///< Pointer to the articulation information of current dimension region of this voice
127 bool Active; ///< If this voice object is currently in usage
128 playback_state_t PlaybackState; ///< When a sample will be triggered, it will be first played from RAM cache and after a couple of sample points it will switch to disk streaming and at the end of a disk stream we have to add null samples, so the interpolator can do it's work correctly
129 bool DiskVoice; ///< If the sample is very short it completely fits into the RAM cache and doesn't need to be streamed from disk, in that case this flag is set to false
130 Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
131 unsigned long MaxRAMPos; ///< The upper allowed limit (not actually the end) in the RAM sample cache, after that point it's not safe to chase the interpolator another time over over the current cache position, instead we switch to disk then.
132 bool RAMLoop; ///< If this voice has a loop defined which completely fits into the cached RAM part of the sample, in this case we handle the looping within the voice class, else if the loop is located in the disk stream part, we let the disk stream handle the looping
133 int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
134 uint Delay; ///< Number of sample points the rendering process of this voice should be delayed (jitter correction), will be set to 0 after the first audio fragment cycle
135 EGADSR* pEG1; ///< Envelope Generator 1 (Amplification)
136 EGADSR* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
137 EGDecay* pEG3; ///< Envelope Generator 3 (Pitch)
138 Filter FilterLeft;
139 Filter FilterRight;
140 midi_ctrl VCFCutoffCtrl;
141 midi_ctrl VCFResonanceCtrl;
142 int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
143 static const float FILTER_CUTOFF_COEFF;
144 static const int FILTER_UPDATE_MASK;
145 VCAManipulator* pVCAManipulator;
146 VCFCManipulator* pVCFCManipulator;
147 VCOManipulator* pVCOManipulator;
148 LFO<gig::VCAManipulator>* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
149 LFO<gig::VCFCManipulator>* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
150 LFO<gig::VCOManipulator>* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
151 Pool<Event>::Iterator itTriggerEvent; ///< First event on the key's list the voice should process (only needed for the first audio fragment in which voice was triggered, after that it will be set to NULL).
152 Pool<Event>::Iterator itKillEvent; ///< Event which caused this voice to be killed
153
154 // Static Methods
155 static float CalculateFilterCutoffCoeff();
156 static int CalculateFilterUpdateMask();
157
158 // Methods
159 void ProcessEvents(uint Samples);
160 #if ENABLE_FILTER
161 void CalculateBiquadParameters(uint Samples);
162 #endif // ENABLE_FILTER
163 void InterpolateNoLoop(uint Samples, sample_t* pSrc, uint Skip);
164 void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);
165
166 inline void InterpolateMono(sample_t* pSrc, int& i) {
167 InterpolateOneStep_Mono(pSrc, i,
168 pEngine->pSynthesisParameters[Event::destination_vca][i] * PanLeft,
169 pEngine->pSynthesisParameters[Event::destination_vca][i] * PanRight,
170 pEngine->pSynthesisParameters[Event::destination_vco][i],
171 pEngine->pBasicFilterParameters[i],
172 pEngine->pMainFilterParameters[i]);
173 }
174
175 inline void InterpolateStereo(sample_t* pSrc, int& i) {
176 InterpolateOneStep_Stereo(pSrc, i,
177 pEngine->pSynthesisParameters[Event::destination_vca][i] * PanLeft,
178 pEngine->pSynthesisParameters[Event::destination_vca][i] * PanRight,
179 pEngine->pSynthesisParameters[Event::destination_vco][i],
180 pEngine->pBasicFilterParameters[i],
181 pEngine->pMainFilterParameters[i]);
182 }
183
184 inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float volume_left, float volume_right, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) {
185 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
186 float pos_fract = this->Pos - pos_int; // fractional part of position
187 pos_int <<= 1;
188
189 #if USE_LINEAR_INTERPOLATION
190 #if ENABLE_FILTER
191 // left channel
192 pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, volume_left * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])));
193 // right channel
194 pEngine->pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, volume_right * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])));
195 #else // no filter
196 // left channel
197 pEngine->pOutputLeft[i] += volume_left * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
198 // right channel
199 pEngine->pOutputRight[i++] += volume_right * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
200 #endif // ENABLE_FILTER
201 #else // polynomial interpolation
202 // calculate left channel
203 float xm1 = pSrc[pos_int];
204 float x0 = pSrc[pos_int+2];
205 float x1 = pSrc[pos_int+4];
206 float x2 = pSrc[pos_int+6];
207 float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
208 float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
209 float c = (x1 - xm1) * 0.5f;
210 #if ENABLE_FILTER
211 pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, volume_left * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
212 #else // no filter
213 pEngine->pOutputLeft[i] += volume_left * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
214 #endif // ENABLE_FILTER
215
216 //calculate right channel
217 xm1 = pSrc[pos_int+1];
218 x0 = pSrc[pos_int+3];
219 x1 = pSrc[pos_int+5];
220 x2 = pSrc[pos_int+7];
221 a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
222 b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
223 c = (x1 - xm1) * 0.5f;
224 #if ENABLE_FILTER
225 pEngine->pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, volume_right * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
226 #else // no filter
227 pEngine->pOutputRight[i++] += volume_right * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
228 #endif // ENABLE_FILTER
229 #endif // USE_LINEAR_INTERPOLATION
230
231 this->Pos += pitch;
232 }
233
234 inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float volume_left, float volume_right, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) {
235 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
236 float pos_fract = this->Pos - pos_int; // fractional part of position
237
238 #if USE_LINEAR_INTERPOLATION
239 float sample_point = pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]);
240 #else // polynomial interpolation
241 float xm1 = pSrc[pos_int];
242 float x0 = pSrc[pos_int+1];
243 float x1 = pSrc[pos_int+2];
244 float x2 = pSrc[pos_int+3];
245 float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
246 float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
247 float c = (x1 - xm1) * 0.5f;
248 float sample_point = (((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0;
249 #endif // USE_LINEAR_INTERPOLATION
250
251 #if ENABLE_FILTER
252 sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point);
253 #endif // ENABLE_FILTER
254
255 pEngine->pOutputLeft[i] += sample_point * volume_left;
256 pEngine->pOutputRight[i++] += sample_point * volume_right;
257
258 this->Pos += pitch;
259 }
260
261 inline float CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
262 return (CrossfadeControllerValue <= pDimRgn->Crossfade.in_start) ? 0.0f
263 : (CrossfadeControllerValue < pDimRgn->Crossfade.in_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.in_start) / float(pDimRgn->Crossfade.in_end - pDimRgn->Crossfade.in_start)
264 : (CrossfadeControllerValue <= pDimRgn->Crossfade.out_start) ? 1.0f
265 : (CrossfadeControllerValue < pDimRgn->Crossfade.out_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.out_start) / float(pDimRgn->Crossfade.out_end - pDimRgn->Crossfade.out_start)
266 : 0.0f;
267 }
268
269 inline float Constrain(float ValueToCheck, float Min, float Max) {
270 if (ValueToCheck > Max) ValueToCheck = Max;
271 else if (ValueToCheck < Min) ValueToCheck = Min;
272 return ValueToCheck;
273 }
274 };
275
276 }} // namespace LinuxSampler::gig
277
278 #endif // __LS_GIG_VOICE_H__

  ViewVC Help
Powered by ViewVC