32 |
#include "../../common/RTMath.h" |
#include "../../common/RTMath.h" |
33 |
#include "../../common/RingBuffer.h" |
#include "../../common/RingBuffer.h" |
34 |
#include "../../common/RTELMemoryPool.h" |
#include "../../common/RTELMemoryPool.h" |
35 |
#include "../../audiodriver/AudioOutputDevice.h" |
#include "../../drivers/audio/AudioOutputDevice.h" |
36 |
#include "../../lib/fileloader/libgig/gig.h" |
#include "../../lib/fileloader/libgig/gig.h" |
37 |
#include "../common/BiquadFilter.h" |
#include "../common/BiquadFilter.h" |
38 |
#include "Engine.h" |
#include "Engine.h" |
92 |
void Reset(); |
void Reset(); |
93 |
void SetOutput(AudioOutputDevice* pAudioOutputDevice); |
void SetOutput(AudioOutputDevice* pAudioOutputDevice); |
94 |
void SetEngine(Engine* pEngine); |
void SetEngine(Engine* pEngine); |
95 |
int Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument); |
int Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer = 0); |
96 |
inline bool IsActive() { return Active; } |
inline bool IsActive() { return Active; } |
97 |
private: |
private: |
98 |
// Types |
// Types |
105 |
// Attributes |
// Attributes |
106 |
gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists. |
gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists. |
107 |
float Volume; ///< Volume level of the voice |
float Volume; ///< Volume level of the voice |
|
float* pOutputLeft; ///< Audio output channel buffer (left) |
|
|
float* pOutputRight; ///< Audio output channel buffer (right) |
|
|
uint SampleRate; ///< Sample rate of the engines output audio signal (in Hz) |
|
|
uint MaxSamplesPerCycle; ///< Size of each audio output buffer |
|
108 |
double Pos; ///< Current playback position in sample |
double Pos; ///< Current playback position in sample |
109 |
double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice |
double PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice |
110 |
double PitchBend; ///< Current pitch value of the pitchbend wheel |
double PitchBend; ///< Current pitch value of the pitchbend wheel |
152 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
float pos_fract = this->Pos - pos_int; // fractional part of position |
153 |
pos_int <<= 1; |
pos_int <<= 1; |
154 |
|
|
|
#if 0 //ENABLE_FILTER |
|
|
UpdateFilter_Stereo(cutoff + FILTER_CUTOFF_MIN, resonance); |
|
|
#endif // ENABLE_FILTER |
|
|
|
|
155 |
#if USE_LINEAR_INTERPOLATION |
#if USE_LINEAR_INTERPOLATION |
156 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
157 |
// left channel |
// left channel |
158 |
pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]))); |
pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]))); |
159 |
// right channel |
// right channel |
160 |
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]))); |
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]))); |
161 |
#else // no filter |
#else // no filter |
162 |
// left channel |
// left channel |
163 |
pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
pEngine->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
164 |
// right channel |
// right channel |
165 |
pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
pEngine->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
166 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
167 |
#else // polynomial interpolation |
#else // polynomial interpolation |
168 |
// calculate left channel |
// calculate left channel |
170 |
float x0 = pSrc[pos_int+2]; |
float x0 = pSrc[pos_int+2]; |
171 |
float x1 = pSrc[pos_int+4]; |
float x1 = pSrc[pos_int+4]; |
172 |
float x2 = pSrc[pos_int+6]; |
float x2 = pSrc[pos_int+6]; |
173 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
174 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
175 |
float c = (x1 - xm1) / 2; |
float c = (x1 - xm1) * 0.5f; |
176 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
177 |
pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
pEngine->pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
178 |
#else // no filter |
#else // no filter |
179 |
pOutputRight[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
pEngine->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
180 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
181 |
|
|
182 |
//calculate right channel |
//calculate right channel |
184 |
x0 = pSrc[pos_int+3]; |
x0 = pSrc[pos_int+3]; |
185 |
x1 = pSrc[pos_int+5]; |
x1 = pSrc[pos_int+5]; |
186 |
x2 = pSrc[pos_int+7]; |
x2 = pSrc[pos_int+7]; |
187 |
a = (3 * (x0 - x1) - xm1 + x2) / 2; |
a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
188 |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
189 |
c = (x1 - xm1) / 2; |
c = (x1 - xm1) * 0.5f; |
190 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
191 |
pOutputLeft[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
pEngine->pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0)); |
192 |
#else // no filter |
#else // no filter |
193 |
pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
pEngine->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
194 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
195 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
196 |
|
|
197 |
this->Pos += pitch; |
this->Pos += pitch; |
198 |
} |
} |
199 |
|
|
200 |
inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) { |
inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) { |
201 |
int pos_int = RTMath::DoubleToInt(this->Pos); // integer position |
int pos_int = RTMath::DoubleToInt(this->Pos); // integer position |
202 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
float pos_fract = this->Pos - pos_int; // fractional part of position |
203 |
|
|
|
#if 0 //ENABLE_FILTER |
|
|
UpdateFilter_Mono(cutoff + FILTER_CUTOFF_MIN, resonance); |
|
|
#endif // ENABLE_FILTER |
|
|
|
|
204 |
#if USE_LINEAR_INTERPOLATION |
#if USE_LINEAR_INTERPOLATION |
205 |
float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int])); |
float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int])); |
206 |
#else // polynomial interpolation |
#else // polynomial interpolation |
208 |
float x0 = pSrc[pos_int+1]; |
float x0 = pSrc[pos_int+1]; |
209 |
float x1 = pSrc[pos_int+2]; |
float x1 = pSrc[pos_int+2]; |
210 |
float x2 = pSrc[pos_int+3]; |
float x2 = pSrc[pos_int+3]; |
211 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
212 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
213 |
float c = (x1 - xm1) / 2; |
float c = (x1 - xm1) * 0.5f; |
214 |
float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
215 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
216 |
|
|
218 |
sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point); |
sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point); |
219 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
220 |
|
|
221 |
pOutputLeft[i] += sample_point; |
pEngine->pOutputLeft[i] += sample_point; |
222 |
pOutputRight[i++] += sample_point; |
pEngine->pOutputRight[i++] += sample_point; |
223 |
|
|
224 |
this->Pos += pitch; |
this->Pos += pitch; |
225 |
} |
} |
226 |
#if 0 |
|
|
inline void UpdateFilter_Stereo(float cutoff, float& resonance) { |
|
|
if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) { |
|
|
FilterLeft.SetParameters(cutoff, resonance, SampleRate); |
|
|
FilterRight.SetParameters(cutoff, resonance, SampleRate); |
|
|
} |
|
|
} |
|
|
inline void UpdateFilter_Mono(float cutoff, float& resonance) { |
|
|
if (!(++FilterUpdateCounter % FILTER_UPDATE_PERIOD) && (cutoff != FilterLeft.Cutoff() || resonance != FilterLeft.Resonance())) { |
|
|
FilterLeft.SetParameters(cutoff, resonance, SampleRate); |
|
|
} |
|
|
} |
|
|
#endif |
|
227 |
inline float Constrain(float ValueToCheck, float Min, float Max) { |
inline float Constrain(float ValueToCheck, float Min, float Max) { |
228 |
if (ValueToCheck > Max) ValueToCheck = Max; |
if (ValueToCheck > Max) ValueToCheck = Max; |
229 |
else if (ValueToCheck < Min) ValueToCheck = Min; |
else if (ValueToCheck < Min) ValueToCheck = Min; |