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 |
108 |
float* pOutputLeft; ///< Audio output channel buffer (left) |
float CrossfadeVolume; ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course) |
|
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 |
|
109 |
double Pos; ///< Current playback position in sample |
double Pos; ///< Current playback position in sample |
110 |
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 |
111 |
double PitchBend; ///< Current pitch value of the pitchbend wheel |
double PitchBend; ///< Current pitch value of the pitchbend wheel |
112 |
::gig::Sample* pSample; ///< Pointer to the sample to be played back |
::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 |
::gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice |
114 |
|
::gig::DimensionRegion* pDimRgn; ///< Pointer to the articulation information of current dimension region of this voice |
115 |
bool Active; ///< If this voice object is currently in usage |
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 |
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 |
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 |
157 |
#if USE_LINEAR_INTERPOLATION |
#if USE_LINEAR_INTERPOLATION |
158 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
159 |
// left channel |
// left channel |
160 |
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]))); |
161 |
// right channel |
// right channel |
162 |
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]))); |
163 |
#else // no filter |
#else // no filter |
164 |
// left channel |
// left channel |
165 |
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])); |
166 |
// right channel |
// right channel |
167 |
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])); |
168 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
169 |
#else // polynomial interpolation |
#else // polynomial interpolation |
170 |
// calculate left channel |
// calculate left channel |
172 |
float x0 = pSrc[pos_int+2]; |
float x0 = pSrc[pos_int+2]; |
173 |
float x1 = pSrc[pos_int+4]; |
float x1 = pSrc[pos_int+4]; |
174 |
float x2 = pSrc[pos_int+6]; |
float x2 = pSrc[pos_int+6]; |
175 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
176 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
177 |
float c = (x1 - xm1) / 2; |
float c = (x1 - xm1) * 0.5f; |
178 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
179 |
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)); |
180 |
#else // no filter |
#else // no filter |
181 |
pOutputLeft[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); |
182 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
183 |
|
|
184 |
//calculate right channel |
//calculate right channel |
186 |
x0 = pSrc[pos_int+3]; |
x0 = pSrc[pos_int+3]; |
187 |
x1 = pSrc[pos_int+5]; |
x1 = pSrc[pos_int+5]; |
188 |
x2 = pSrc[pos_int+7]; |
x2 = pSrc[pos_int+7]; |
189 |
a = (3 * (x0 - x1) - xm1 + x2) / 2; |
a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
190 |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
191 |
c = (x1 - xm1) / 2; |
c = (x1 - xm1) * 0.5f; |
192 |
#if ENABLE_FILTER |
#if ENABLE_FILTER |
193 |
pOutputRight[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)); |
194 |
#else // no filter |
#else // no filter |
195 |
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); |
196 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
197 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
198 |
|
|
210 |
float x0 = pSrc[pos_int+1]; |
float x0 = pSrc[pos_int+1]; |
211 |
float x1 = pSrc[pos_int+2]; |
float x1 = pSrc[pos_int+2]; |
212 |
float x2 = pSrc[pos_int+3]; |
float x2 = pSrc[pos_int+3]; |
213 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
float a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f; |
214 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
float b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f; |
215 |
float c = (x1 - xm1) / 2; |
float c = (x1 - xm1) * 0.5f; |
216 |
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); |
217 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
218 |
|
|
220 |
sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point); |
sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point); |
221 |
#endif // ENABLE_FILTER |
#endif // ENABLE_FILTER |
222 |
|
|
223 |
pOutputLeft[i] += sample_point; |
pEngine->pOutputLeft[i] += sample_point; |
224 |
pOutputRight[i++] += sample_point; |
pEngine->pOutputRight[i++] += sample_point; |
225 |
|
|
226 |
this->Pos += pitch; |
this->Pos += pitch; |
227 |
} |
} |
228 |
|
|
229 |
|
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 |
inline float Constrain(float ValueToCheck, float Min, float Max) { |
inline float Constrain(float ValueToCheck, float Min, float Max) { |
238 |
if (ValueToCheck > Max) ValueToCheck = Max; |
if (ValueToCheck > Max) ValueToCheck = Max; |
239 |
else if (ValueToCheck < Min) ValueToCheck = Min; |
else if (ValueToCheck < Min) ValueToCheck = Min; |