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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 236 - (hide annotations) (download) (as text)
Thu Sep 9 18:44:18 2004 UTC (19 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 15613 byte(s)
* added support for crossfades
* support sample playback start offset

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 53 * *
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/RTELMemoryPool.h"
35 schoenebeck 203 #include "../../drivers/audio/AudioOutputDevice.h"
36 schoenebeck 53 #include "../../lib/fileloader/libgig/gig.h"
37 schoenebeck 80 #include "../common/BiquadFilter.h"
38 schoenebeck 53 #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 schoenebeck 80 #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 schoenebeck 53 #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     // Attributes
84     int MIDIKey; ///< MIDI key number of the key that triggered the voice
85     DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
86    
87     // Methods
88     Voice();
89     ~Voice();
90     void Kill();
91     void Render(uint Samples);
92     void Reset();
93     void SetOutput(AudioOutputDevice* pAudioOutputDevice);
94     void SetEngine(Engine* pEngine);
95 schoenebeck 233 int Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer = 0);
96 schoenebeck 53 inline bool IsActive() { return Active; }
97     private:
98     // Types
99     enum playback_state_t {
100     playback_state_ram,
101     playback_state_disk,
102     playback_state_end
103     };
104    
105     // Attributes
106     gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists.
107     float Volume; ///< Volume level of the voice
108 schoenebeck 236 float CrossfadeVolume; ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course)
109 schoenebeck 53 double Pos; ///< Current playback position in sample
110     double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
111     double PitchBend; ///< Current pitch value of the pitchbend wheel
112     ::gig::Sample* pSample; ///< Pointer to the sample to be played back
113     ::gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
114 schoenebeck 236 ::gig::DimensionRegion* pDimRgn; ///< Pointer to the articulation information of current dimension region of this voice
115 schoenebeck 53 bool Active; ///< If this voice object is currently in usage
116     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
117     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
118     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
119     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.
120     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
121     int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
122     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
123     EGADSR* pEG1; ///< Envelope Generator 1 (Amplification)
124     EGADSR* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
125     EGDecay* pEG3; ///< Envelope Generator 3 (Pitch)
126     Filter FilterLeft;
127     Filter FilterRight;
128     midi_ctrl VCFCutoffCtrl;
129     midi_ctrl VCFResonanceCtrl;
130     int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
131     static const float FILTER_CUTOFF_COEFF;
132 schoenebeck 80 static const int FILTER_UPDATE_MASK;
133 schoenebeck 53 VCAManipulator* pVCAManipulator;
134     VCFCManipulator* pVCFCManipulator;
135     VCOManipulator* pVCOManipulator;
136     LFO<gig::VCAManipulator>* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
137     LFO<gig::VCFCManipulator>* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
138     LFO<gig::VCOManipulator>* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
139     Event* pTriggerEvent; ///< 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).
140    
141     // Static Methods
142     static float CalculateFilterCutoffCoeff();
143 schoenebeck 80 static int CalculateFilterUpdateMask();
144 schoenebeck 53
145     // Methods
146     void ProcessEvents(uint Samples);
147 schoenebeck 80 #if ENABLE_FILTER
148     void CalculateBiquadParameters(uint Samples);
149     #endif // ENABLE_FILTER
150 schoenebeck 53 void Interpolate(uint Samples, sample_t* pSrc, uint Skip);
151     void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);
152 schoenebeck 80 inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) {
153 schoenebeck 53 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
154     float pos_fract = this->Pos - pos_int; // fractional part of position
155     pos_int <<= 1;
156    
157     #if USE_LINEAR_INTERPOLATION
158     #if ENABLE_FILTER
159     // left channel
160 schoenebeck 225 pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])));
161 schoenebeck 53 // right channel
162 schoenebeck 225 pEngine->pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])));
163 schoenebeck 53 #else // no filter
164     // left channel
165 schoenebeck 225 pEngine->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
166 schoenebeck 53 // right channel
167 schoenebeck 225 pEngine->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
168 schoenebeck 53 #endif // ENABLE_FILTER
169     #else // polynomial interpolation
170     // calculate left channel
171     float xm1 = pSrc[pos_int];
172     float x0 = pSrc[pos_int+2];
173     float x1 = pSrc[pos_int+4];
174     float x2 = pSrc[pos_int+6];
175 letz 99 float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
176     float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
177     float c = (x1 - xm1) * 0.5f;
178 schoenebeck 53 #if ENABLE_FILTER
179 schoenebeck 225 pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
180 schoenebeck 53 #else // no filter
181 schoenebeck 225 pEngine->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
182 schoenebeck 53 #endif // ENABLE_FILTER
183    
184     //calculate right channel
185     xm1 = pSrc[pos_int+1];
186     x0 = pSrc[pos_int+3];
187     x1 = pSrc[pos_int+5];
188     x2 = pSrc[pos_int+7];
189 letz 99 a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
190     b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
191     c = (x1 - xm1) * 0.5f;
192 schoenebeck 53 #if ENABLE_FILTER
193 schoenebeck 225 pEngine->pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
194 schoenebeck 53 #else // no filter
195 schoenebeck 225 pEngine->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
196 schoenebeck 53 #endif // ENABLE_FILTER
197     #endif // USE_LINEAR_INTERPOLATION
198    
199     this->Pos += pitch;
200     }
201 schoenebeck 97
202 schoenebeck 80 inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) {
203 schoenebeck 53 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
204     float pos_fract = this->Pos - pos_int; // fractional part of position
205    
206     #if USE_LINEAR_INTERPOLATION
207     float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
208     #else // polynomial interpolation
209     float xm1 = pSrc[pos_int];
210     float x0 = pSrc[pos_int+1];
211     float x1 = pSrc[pos_int+2];
212     float x2 = pSrc[pos_int+3];
213 letz 99 float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
214     float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
215     float c = (x1 - xm1) * 0.5f;
216 schoenebeck 53 float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
217     #endif // USE_LINEAR_INTERPOLATION
218    
219     #if ENABLE_FILTER
220 schoenebeck 80 sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point);
221 schoenebeck 53 #endif // ENABLE_FILTER
222    
223 schoenebeck 225 pEngine->pOutputLeft[i] += sample_point;
224     pEngine->pOutputRight[i++] += sample_point;
225 schoenebeck 53
226     this->Pos += pitch;
227     }
228 schoenebeck 97
229 schoenebeck 236 inline float CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
230     return (CrossfadeControllerValue <= pDimRgn->Crossfade.in_start) ? 0.0f
231     : (CrossfadeControllerValue < pDimRgn->Crossfade.in_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.in_start) / float(pDimRgn->Crossfade.in_end - pDimRgn->Crossfade.in_start)
232     : (CrossfadeControllerValue <= pDimRgn->Crossfade.out_start) ? 1.0f
233     : (CrossfadeControllerValue < pDimRgn->Crossfade.out_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.out_start) / float(pDimRgn->Crossfade.out_end - pDimRgn->Crossfade.out_start)
234     : 0.0f;
235     }
236    
237 schoenebeck 53 inline float Constrain(float ValueToCheck, float Min, float Max) {
238     if (ValueToCheck > Max) ValueToCheck = Max;
239     else if (ValueToCheck < Min) ValueToCheck = Min;
240     return ValueToCheck;
241     }
242     };
243    
244     }} // namespace LinuxSampler::gig
245    
246     #endif // __LS_GIG_VOICE_H__

  ViewVC Help
Powered by ViewVC