/[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 56 - (hide annotations) (download) (as text)
Tue Apr 27 09:21:58 2004 UTC (19 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 15421 byte(s)
updated copyright header for 2004

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     #include "../../audiodriver/AudioOutputDevice.h"
36     #include "../../lib/fileloader/libgig/gig.h"
37     #include "Engine.h"
38     #include "Stream.h"
39     #include "DiskThread.h"
40    
41     #include "EGDecay.h"
42     #include "Filter.h"
43     #include "../common/LFO.h"
44    
45     #define USE_LINEAR_INTERPOLATION 1 ///< set to 0 if you prefer cubic interpolation (slower, better quality)
46     #define ENABLE_FILTER 0 ///< if set to 0 then filter (VCF) code is ignored on compile time
47     #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)
48     #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
49     #define FILTER_CUTOFF_MAX 10000.0f ///< maximum cutoff frequency (10kHz)
50     #define FILTER_CUTOFF_MIN 100.0f ///< minimum cutoff frequency (100Hz)
51    
52     // Uncomment following line to override external cutoff controller
53     //#define OVERRIDE_FILTER_CUTOFF_CTRL 1 ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel')
54    
55     // Uncomment following line to override external resonance controller
56     //#define OVERRIDE_FILTER_RES_CTRL 91 ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth')
57    
58     // Uncomment following line to override filter type
59     //#define OVERRIDE_FILTER_TYPE ::gig::vcf_type_lowpass ///< either ::gig::vcf_type_lowpass, ::gig::vcf_type_bandpass or ::gig::vcf_type_highpass
60    
61     namespace LinuxSampler { namespace gig {
62    
63     class Engine;
64     class EGADSR;
65     class VCAManipulator;
66     class VCFCManipulator;
67     class VCOManipulator;
68    
69     /// Reflects a MIDI controller
70     struct midi_ctrl {
71     uint8_t controller; ///< MIDI control change controller number
72     uint8_t value; ///< Current MIDI controller value
73     float fvalue; ///< Transformed / effective value (e.g. volume level or filter cutoff frequency)
74     };
75    
76     /** Gig Voice
77     *
78     * Renders a voice for the Gigasampler format.
79     */
80     class Voice {
81     public:
82     // Attributes
83     int MIDIKey; ///< MIDI key number of the key that triggered the voice
84     DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
85    
86     // Methods
87     Voice();
88     ~Voice();
89     void Kill();
90     void Render(uint Samples);
91     void Reset();
92     void SetOutput(AudioOutputDevice* pAudioOutputDevice);
93     void SetEngine(Engine* pEngine);
94     int Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument);
95     inline bool IsActive() { return Active; }
96     private:
97     // Types
98     enum playback_state_t {
99     playback_state_ram,
100     playback_state_disk,
101     playback_state_end
102     };
103    
104     // Attributes
105     gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists.
106     float Volume; ///< Volume level of the voice
107     float* pOutputLeft; ///< Audio output channel buffer (left)
108     float* pOutputRight; ///< Audio output channel buffer (right)
109     uint SampleRate; ///< Sample rate of the engines output audio signal (in Hz)
110     uint MaxSamplesPerCycle; ///< Size of each audio output buffer
111     double Pos; ///< Current playback position in sample
112     double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
113     double PitchBend; ///< Current pitch value of the pitchbend wheel
114     ::gig::Sample* pSample; ///< Pointer to the sample to be played back
115     ::gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
116     bool Active; ///< If this voice object is currently in usage
117     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
118     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
119     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
120     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.
121     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
122     int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
123     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
124     EGADSR* pEG1; ///< Envelope Generator 1 (Amplification)
125     EGADSR* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
126     EGDecay* pEG3; ///< Envelope Generator 3 (Pitch)
127     Filter FilterLeft;
128     Filter FilterRight;
129     midi_ctrl VCFCutoffCtrl;
130     midi_ctrl VCFResonanceCtrl;
131     int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
132     static const float FILTER_CUTOFF_COEFF;
133     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    
144     // Methods
145     void ProcessEvents(uint Samples);
146     void Interpolate(uint Samples, sample_t* pSrc, uint Skip);
147     void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);
148     inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) {
149     int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
150     float pos_fract = this->Pos - pos_int; // fractional part of position
151     pos_int <<= 1;
152    
153     #if ENABLE_FILTER
154     UpdateFilter_Stereo(cutoff + FILTER_CUTOFF_MIN, resonance);
155     #endif // ENABLE_FILTER
156    
157     #if USE_LINEAR_INTERPOLATION
158     #if ENABLE_FILTER
159     // left channel
160     pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])));
161     // right channel
162     pOutputRight[i++] += this->FilterRight.Apply(effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])));
163     #else // no filter
164     // left channel
165     pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
166     // right channel
167     pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
168     #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     float a = (3 * (x0 - x1) - xm1 + x2) / 2;
176     float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
177     float c = (x1 - xm1) / 2;
178     #if ENABLE_FILTER
179     pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
180     #else // no filter
181     pOutputRight[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
182     #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     a = (3 * (x0 - x1) - xm1 + x2) / 2;
190     b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
191     c = (x1 - xm1) / 2;
192     #if ENABLE_FILTER
193     pOutputLeft[i++] += this->FilterRight.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
194     #else // no filter
195     pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
196     #endif // ENABLE_FILTER
197     #endif // USE_LINEAR_INTERPOLATION
198    
199     this->Pos += pitch;
200     }
201     inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) {
202     int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
203     float pos_fract = this->Pos - pos_int; // fractional part of position
204    
205     #if ENABLE_FILTER
206     UpdateFilter_Mono(cutoff + FILTER_CUTOFF_MIN, resonance);
207     #endif // ENABLE_FILTER
208    
209     #if USE_LINEAR_INTERPOLATION
210     float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
211     #else // polynomial interpolation
212     float xm1 = pSrc[pos_int];
213     float x0 = pSrc[pos_int+1];
214     float x1 = pSrc[pos_int+2];
215     float x2 = pSrc[pos_int+3];
216     float a = (3 * (x0 - x1) - xm1 + x2) / 2;
217     float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
218     float c = (x1 - xm1) / 2;
219     float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
220     #endif // USE_LINEAR_INTERPOLATION
221    
222     #if ENABLE_FILTER
223     sample_point = this->FilterLeft.Apply(sample_point);
224     #endif // ENABLE_FILTER
225    
226     pOutputLeft[i] += sample_point;
227     pOutputRight[i++] += sample_point;
228    
229     this->Pos += pitch;
230     }
231     inline void UpdateFilter_Stereo(float cutoff, float& resonance) {
232     if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) {
233     FilterLeft.SetParameters(cutoff, resonance, SampleRate);
234     FilterRight.SetParameters(cutoff, resonance, SampleRate);
235     }
236     }
237     inline void UpdateFilter_Mono(float cutoff, float& resonance) {
238     if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) {
239     FilterLeft.SetParameters(cutoff, resonance, SampleRate);
240     }
241     }
242     inline float Constrain(float ValueToCheck, float Min, float Max) {
243     if (ValueToCheck > Max) ValueToCheck = Max;
244     else if (ValueToCheck < Min) ValueToCheck = Min;
245     return ValueToCheck;
246     }
247     };
248    
249     }} // namespace LinuxSampler::gig
250    
251     #endif // __LS_GIG_VOICE_H__

  ViewVC Help
Powered by ViewVC