1 |
/*************************************************************************** |
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 |
#include "diskthread.h" |
28 |
#include "ringbuffer.h" |
29 |
#include "stream.h" |
30 |
#include "gig.h" |
31 |
#include "eg_vca.h" |
32 |
#include "rtelmemorypool.h" |
33 |
#include "audiothread.h" |
34 |
#include "filter.h" |
35 |
#include "lfo.h" |
36 |
|
37 |
#define MAX_PITCH 4 //FIXME: at the moment in octaves, should be changed into semitones |
38 |
#define USE_LINEAR_INTERPOLATION 1 ///< set to 0 if you prefer cubic interpolation (slower, better quality) |
39 |
#define ENABLE_FILTER 0 ///< if set to 0 then filter (VCF) code is ignored on compile time |
40 |
#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) |
41 |
#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 |
42 |
|
43 |
// Uncomment following line to override external cutoff controller |
44 |
//#define OVERRIDE_FILTER_CUTOFF_CTRL 1 ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel') |
45 |
|
46 |
// Uncomment following line to override external resonance controller |
47 |
//#define OVERRIDE_FILTER_RES_CTRL 91 ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth') |
48 |
|
49 |
// Uncomment following line to override filter type |
50 |
//#define OVERRIDE_FILTER_TYPE gig::vcf_type_lowpass ///< either gig::vcf_type_lowpass, gig::vcf_type_bandpass or gig::vcf_type_highpass |
51 |
|
52 |
|
53 |
/// Reflects a MIDI controller |
54 |
struct midi_ctrl { |
55 |
uint8_t controller; ///< MIDI control change controller number |
56 |
uint8_t value; ///< Current MIDI controller value |
57 |
float fvalue; ///< Transformed / effective value (e.g. volume level or filter cutoff frequency) |
58 |
}; |
59 |
|
60 |
class Voice { |
61 |
public: |
62 |
// Attributes |
63 |
int MIDIKey; ///< MIDI key number of the key that triggered the voice |
64 |
uint ReleaseVelocity; ///< Reflects the release velocity value if a note-off command arrived for the voice. |
65 |
|
66 |
// Static Attributes |
67 |
static DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again |
68 |
static AudioThread* pEngine; ///< Pointer to the engine, to be able to access the event lists. |
69 |
|
70 |
// Methods |
71 |
Voice(); |
72 |
~Voice(); |
73 |
void Kill(); |
74 |
void Render(uint Samples); |
75 |
void Reset(); |
76 |
int Trigger(ModulationSystem::Event* pNoteOnEvent, int Pitch, gig::Instrument* pInstrument); |
77 |
inline bool IsActive() { return Active; } |
78 |
inline void SetOutputLeft(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputLeft = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; } |
79 |
inline void SetOutputRight(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputRight = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; } |
80 |
private: |
81 |
// Types |
82 |
enum playback_state_t { |
83 |
playback_state_ram, |
84 |
playback_state_disk, |
85 |
playback_state_end |
86 |
}; |
87 |
|
88 |
// Attributes |
89 |
float Volume; ///< Volume level of the voice |
90 |
float* pOutputLeft; ///< Audio output buffer (left channel) |
91 |
float* pOutputRight; ///< Audio output buffer (right channel) |
92 |
uint MaxSamplesPerCycle; ///< Size of each audio output buffer |
93 |
double Pos; ///< Current playback position in sample |
94 |
double Pitch; ///< Current pitch depth (number of sample points to move on with each render step) |
95 |
gig::Sample* pSample; ///< Pointer to the sample to be played back |
96 |
gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice |
97 |
bool Active; ///< If this voice object is currently in usage |
98 |
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 |
99 |
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 |
100 |
Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream |
101 |
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. |
102 |
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 |
103 |
int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed |
104 |
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 |
105 |
EG_VCA EG1; |
106 |
GigFilter FilterLeft; |
107 |
GigFilter FilterRight; |
108 |
midi_ctrl VCFCutoffCtrl; |
109 |
midi_ctrl VCFResonanceCtrl; |
110 |
LFO* pLFO1; |
111 |
LFO* pLFO2; |
112 |
LFO* pLFO3; |
113 |
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). |
114 |
|
115 |
// Methods |
116 |
void ProcessEvents(uint Samples); |
117 |
void Interpolate(uint Samples, sample_t* pSrc, uint Skip); |
118 |
void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip); |
119 |
inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) { |
120 |
int pos_int = double_to_int(this->Pos); // integer position |
121 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
122 |
pos_int <<= 1; |
123 |
|
124 |
#if ENABLE_FILTER |
125 |
UpdateFilter_Stereo(cutoff + 20.0f, resonance); // 20Hz min. |
126 |
#endif // ENABLE_FILTER |
127 |
|
128 |
#if USE_LINEAR_INTERPOLATION |
129 |
#if ENABLE_FILTER |
130 |
// left channel |
131 |
this->pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]))); |
132 |
// right channel |
133 |
this->pOutputRight[i++] += this->FilterRight.Apply(effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]))); |
134 |
#else // no filter |
135 |
// left channel |
136 |
this->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
137 |
// right channel |
138 |
this->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
139 |
#endif // ENABLE_FILTER |
140 |
#else // polynomial interpolation |
141 |
// calculate left channel |
142 |
float xm1 = pSrc[pos_int]; |
143 |
float x0 = pSrc[pos_int+2]; |
144 |
float x1 = pSrc[pos_int+4]; |
145 |
float x2 = pSrc[pos_int+6]; |
146 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
147 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
148 |
float c = (x1 - xm1) / 2; |
149 |
#if ENABLE_FILTER |
150 |
this->pOutputLeft[i] += this->FilterLeft.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
151 |
#else // no filter |
152 |
this->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
153 |
#endif // ENABLE_FILTER |
154 |
|
155 |
//calculate right channel |
156 |
xm1 = pSrc[pos_int+1]; |
157 |
x0 = pSrc[pos_int+3]; |
158 |
x1 = pSrc[pos_int+5]; |
159 |
x2 = pSrc[pos_int+7]; |
160 |
a = (3 * (x0 - x1) - xm1 + x2) / 2; |
161 |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
162 |
c = (x1 - xm1) / 2; |
163 |
#if ENABLE_FILTER |
164 |
this->pOutputRight[i++] += this->FilterRight.Apply(effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
165 |
#else // no filter |
166 |
this->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
167 |
#endif // ENABLE_FILTER |
168 |
#endif // USE_LINEAR_INTERPOLATION |
169 |
|
170 |
this->Pos += pitch; |
171 |
} |
172 |
inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, float& cutoff, float& resonance) { |
173 |
int pos_int = double_to_int(this->Pos); // integer position |
174 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
175 |
|
176 |
#if ENABLE_FILTER |
177 |
UpdateFilter_Mono(cutoff + 20.0f, resonance); // 20Hz min. |
178 |
#endif // ENABLE_FILTER |
179 |
|
180 |
#if USE_LINEAR_INTERPOLATION |
181 |
float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int])); |
182 |
#else // polynomial interpolation |
183 |
float xm1 = pSrc[pos_int]; |
184 |
float x0 = pSrc[pos_int+1]; |
185 |
float x1 = pSrc[pos_int+2]; |
186 |
float x2 = pSrc[pos_int+3]; |
187 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
188 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
189 |
float c = (x1 - xm1) / 2; |
190 |
float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
191 |
#endif // USE_LINEAR_INTERPOLATION |
192 |
|
193 |
#if ENABLE_FILTER |
194 |
sample_point = this->FilterLeft.Apply(sample_point); |
195 |
#endif // ENABLE_FILTER |
196 |
|
197 |
this->pOutputLeft[i] += sample_point; |
198 |
this->pOutputRight[i++] += sample_point; |
199 |
|
200 |
this->Pos += pitch; |
201 |
} |
202 |
inline void UpdateFilter_Stereo(float cutoff, float& resonance) { |
203 |
static int updatecounter = 0; // we update the filter all FILTER_UPDATE_PERIOD samples |
204 |
if (!(++updatecounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) { |
205 |
FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
206 |
FilterRight.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
207 |
} |
208 |
} |
209 |
inline void UpdateFilter_Mono(float cutoff, float& resonance) { |
210 |
static int updatecounter = 0; // we update the filter all FILTER_UPDATE_PERIOD samples |
211 |
if (!(++updatecounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) { |
212 |
FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
213 |
} |
214 |
} |
215 |
inline void ForceUpdateFilter_Stereo(float cutoff, float& resonance) { |
216 |
if (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance()) { |
217 |
FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
218 |
FilterRight.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
219 |
} |
220 |
} |
221 |
inline void ForceUpdateFilter_Mono(float cutoff, float& resonance) { |
222 |
if (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance()) { |
223 |
FilterLeft.SetParameters(cutoff, resonance, ModulationSystem::SampleRate()); |
224 |
} |
225 |
} |
226 |
inline float Constrain(float ValueToCheck, float Min, float Max) { |
227 |
if (ValueToCheck > Max) ValueToCheck = Max; |
228 |
else if (ValueToCheck < Min) ValueToCheck = Min; |
229 |
return ValueToCheck; |
230 |
} |
231 |
inline int double_to_int(double f) { |
232 |
#if ARCH_X86 |
233 |
int i; |
234 |
__asm__ ("fistl %0" : "=m"(i) : "st"(f - 0.5) ); |
235 |
return i; |
236 |
#else |
237 |
return (int) f; |
238 |
#endif // ARCH_X86 |
239 |
} |
240 |
}; |
241 |
|
242 |
#endif // __VOICE_H__ |