/[svn]/linuxsampler/tags/v0_1_0/src/voice.h
ViewVC logotype

Annotation of /linuxsampler/tags/v0_1_0/src/voice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (hide annotations) (download) (as text)
Sun Apr 11 17:25:40 2004 UTC (20 years, 1 month ago) by (unknown author)
File MIME type: text/x-c++hdr
File size: 14347 byte(s)
This commit was manufactured by cvs2svn to create tag 'v0_1_0'.
1 schoenebeck 9 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003 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 __VOICE_H__
24     #define __VOICE_H__
25    
26     #include "global.h"
27 schoenebeck 40 #include "rtmath.h"
28 schoenebeck 9 #include "diskthread.h"
29     #include "ringbuffer.h"
30     #include "stream.h"
31     #include "gig.h"
32 schoenebeck 30 #include "eg_vca.h"
33 schoenebeck 40 #include "eg_d.h"
34 schoenebeck 32 #include "rtelmemorypool.h"
35     #include "audiothread.h"
36 schoenebeck 38 #include "filter.h"
37 schoenebeck 39 #include "lfo.h"
38 schoenebeck 9
39 schoenebeck 18 #define USE_LINEAR_INTERPOLATION 1 ///< set to 0 if you prefer cubic interpolation (slower, better quality)
40 schoenebeck 38 #define ENABLE_FILTER 0 ///< if set to 0 then filter (VCF) code is ignored on compile time
41     #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)
42     #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
43 schoenebeck 40 #define FILTER_CUTOFF_MAX 10000.0f ///< maximum cutoff frequency (10kHz)
44     #define FILTER_CUTOFF_MIN 100.0f ///< minimum cutoff frequency (100Hz)
45 schoenebeck 9
46 schoenebeck 38 // Uncomment following line to override external cutoff controller
47     //#define OVERRIDE_FILTER_CUTOFF_CTRL 1 ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel')
48    
49     // Uncomment following line to override external resonance controller
50     //#define OVERRIDE_FILTER_RES_CTRL 91 ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth')
51    
52     // Uncomment following line to override filter type
53     //#define OVERRIDE_FILTER_TYPE gig::vcf_type_lowpass ///< either gig::vcf_type_lowpass, gig::vcf_type_bandpass or gig::vcf_type_highpass
54    
55    
56     /// Reflects a MIDI controller
57     struct midi_ctrl {
58     uint8_t controller; ///< MIDI control change controller number
59 schoenebeck 39 uint8_t value; ///< Current MIDI controller value
60     float fvalue; ///< Transformed / effective value (e.g. volume level or filter cutoff frequency)
61 schoenebeck 38 };
62    
63 schoenebeck 9 class Voice {
64     public:
65 schoenebeck 12 // Attributes
66 schoenebeck 32 int MIDIKey; ///< MIDI key number of the key that triggered the voice
67     uint ReleaseVelocity; ///< Reflects the release velocity value if a note-off command arrived for the voice.
68 schoenebeck 12
69 schoenebeck 32 // Static Attributes
70     static DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
71     static AudioThread* pEngine; ///< Pointer to the engine, to be able to access the event lists.
72    
73 schoenebeck 9 // Methods
74 schoenebeck 32 Voice();
75 schoenebeck 9 ~Voice();
76     void Kill();
77 schoenebeck 31 void Render(uint Samples);
78 schoenebeck 35 void Reset();
79 schoenebeck 40 int Trigger(ModulationSystem::Event* pNoteOnEvent, int PitchBend, gig::Instrument* pInstrument);
80 schoenebeck 31 inline bool IsActive() { return Active; }
81     inline void SetOutputLeft(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputLeft = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; }
82     inline void SetOutputRight(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputRight = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; }
83 schoenebeck 9 private:
84     // Types
85     enum playback_state_t {
86     playback_state_ram,
87     playback_state_disk,
88     playback_state_end
89     };
90    
91     // Attributes
92 schoenebeck 31 float Volume; ///< Volume level of the voice
93     float* pOutputLeft; ///< Audio output buffer (left channel)
94     float* pOutputRight; ///< Audio output buffer (right channel)
95     uint MaxSamplesPerCycle; ///< Size of each audio output buffer
96     double Pos; ///< Current playback position in sample
97 schoenebeck 40 double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
98     double PitchBend; ///< Current pitch value of the pitchbend wheel
99 schoenebeck 31 gig::Sample* pSample; ///< Pointer to the sample to be played back
100     gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
101     bool Active; ///< If this voice object is currently in usage
102     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
103     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
104     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
105     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.
106     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
107     int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
108 schoenebeck 32 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
109 schoenebeck 40 EG_VCA* pEG1; ///< Envelope Generator 1 (Amplification)
110     EG_VCA* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
111     EG_D* pEG3; ///< Envelope Generator 3 (Pitch)
112 schoenebeck 38 GigFilter FilterLeft;
113     GigFilter FilterRight;
114     midi_ctrl VCFCutoffCtrl;
115     midi_ctrl VCFResonanceCtrl;
116 schoenebeck 40 int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
117     static const float FILTER_CUTOFF_COEFF;
118     LFO<VCAManipulator>* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
119     LFO<VCFCManipulator>* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
120     LFO<VCOManipulator>* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
121     ModulationSystem::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).
122 schoenebeck 9
123 schoenebeck 40 // Static Methods
124     static float CalculateFilterCutoffCoeff();
125    
126 schoenebeck 9 // Methods
127 schoenebeck 32 void ProcessEvents(uint Samples);
128     void Interpolate(uint Samples, sample_t* pSrc, uint Skip);
129     void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);
130 schoenebeck 38 inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) {
131 schoenebeck 40 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
132     float pos_fract = this->Pos - pos_int; // fractional part of position
133 schoenebeck 26 pos_int <<= 1;
134 schoenebeck 39
135 schoenebeck 38 #if ENABLE_FILTER
136 schoenebeck 40 UpdateFilter_Stereo(cutoff + FILTER_CUTOFF_MIN, resonance);
137 schoenebeck 38 #endif // ENABLE_FILTER
138 schoenebeck 26
139     #if USE_LINEAR_INTERPOLATION
140 schoenebeck 38 #if ENABLE_FILTER
141     // left channel
142     this->pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])));
143     // right channel
144     this->pOutputRight[i++] += this->FilterRight.Apply(effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])));
145 schoenebeck 39 #else // no filter
146 schoenebeck 38 // left channel
147     this->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
148     // right channel
149     this->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
150     #endif // ENABLE_FILTER
151 schoenebeck 26 #else // polynomial interpolation
152     // calculate left channel
153     float xm1 = pSrc[pos_int];
154     float x0 = pSrc[pos_int+2];
155     float x1 = pSrc[pos_int+4];
156     float x2 = pSrc[pos_int+6];
157     float a = (3 * (x0 - x1) - xm1 + x2) / 2;
158     float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
159     float c = (x1 - xm1) / 2;
160 schoenebeck 38 #if ENABLE_FILTER
161     this->pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
162     #else // no filter
163     this->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
164 schoenebeck 39 #endif // ENABLE_FILTER
165 schoenebeck 26
166     //calculate right channel
167     xm1 = pSrc[pos_int+1];
168     x0 = pSrc[pos_int+3];
169     x1 = pSrc[pos_int+5];
170     x2 = pSrc[pos_int+7];
171     a = (3 * (x0 - x1) - xm1 + x2) / 2;
172     b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
173     c = (x1 - xm1) / 2;
174 schoenebeck 38 #if ENABLE_FILTER
175     this->pOutputRight[i++] += this->FilterRight.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));
176     #else // no filter
177     this->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
178     #endif // ENABLE_FILTER
179 schoenebeck 26 #endif // USE_LINEAR_INTERPOLATION
180    
181 schoenebeck 32 this->Pos += pitch;
182 schoenebeck 26 }
183 schoenebeck 38 inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) {
184 schoenebeck 40 int pos_int = RTMath::DoubleToInt(this->Pos); // integer position
185     float pos_fract = this->Pos - pos_int; // fractional part of position
186 schoenebeck 39
187 schoenebeck 38 #if ENABLE_FILTER
188 schoenebeck 40 UpdateFilter_Mono(cutoff + FILTER_CUTOFF_MIN, resonance);
189 schoenebeck 38 #endif // ENABLE_FILTER
190 schoenebeck 26
191     #if USE_LINEAR_INTERPOLATION
192     float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
193     #else // polynomial interpolation
194     float xm1 = pSrc[pos_int];
195     float x0 = pSrc[pos_int+1];
196     float x1 = pSrc[pos_int+2];
197     float x2 = pSrc[pos_int+3];
198     float a = (3 * (x0 - x1) - xm1 + x2) / 2;
199     float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
200     float c = (x1 - xm1) / 2;
201     float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
202 schoenebeck 39 #endif // USE_LINEAR_INTERPOLATION
203 schoenebeck 26
204 schoenebeck 39 #if ENABLE_FILTER
205 schoenebeck 38 sample_point = this->FilterLeft.Apply(sample_point);
206     #endif // ENABLE_FILTER
207    
208 schoenebeck 31 this->pOutputLeft[i] += sample_point;
209     this->pOutputRight[i++] += sample_point;
210 schoenebeck 26
211 schoenebeck 32 this->Pos += pitch;
212 schoenebeck 26 }
213 schoenebeck 39 inline void UpdateFilter_Stereo(float cutoff, float& resonance) {
214 schoenebeck 40 if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) {
215 schoenebeck 38 FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate());
216     FilterRight.SetParameters(cutoff, resonance, ModulationSystem::SampleRate());
217     }
218     }
219 schoenebeck 39 inline void UpdateFilter_Mono(float cutoff, float& resonance) {
220 schoenebeck 40 if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) {
221 schoenebeck 38 FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate());
222     }
223     }
224     inline float Constrain(float ValueToCheck, float Min, float Max) {
225     if (ValueToCheck > Max) ValueToCheck = Max;
226     else if (ValueToCheck < Min) ValueToCheck = Min;
227 schoenebeck 39 return ValueToCheck;
228 schoenebeck 38 }
229 schoenebeck 9 };
230    
231     #endif // __VOICE_H__

  ViewVC Help
Powered by ViewVC