45 |
~Voice(); |
~Voice(); |
46 |
void Kill(); |
void Kill(); |
47 |
void Release(); |
void Release(); |
48 |
void RenderAudio(); |
void Render(uint Samples); |
49 |
int Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument); |
int Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument); |
50 |
inline bool IsActive() { return Active; } |
inline bool IsActive() { return Active; } |
51 |
inline void SetOutput(float* pOutput, uint OutputBufferSize) { this->pOutput = pOutput; this->OutputBufferSize = OutputBufferSize; } |
inline void SetOutputLeft(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputLeft = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; } |
52 |
|
inline void SetOutputRight(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputRight = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; } |
53 |
private: |
private: |
54 |
// Types |
// Types |
55 |
enum playback_state_t { |
enum playback_state_t { |
59 |
}; |
}; |
60 |
|
|
61 |
// Attributes |
// Attributes |
62 |
float Volume; ///< Volume level of the voice |
float Volume; ///< Volume level of the voice |
63 |
float* pOutput; ///< Audio output buffer |
float* pOutputLeft; ///< Audio output buffer (left channel) |
64 |
uint OutputBufferSize; ///< Fragment size of the audio output buffer |
float* pOutputRight; ///< Audio output buffer (right channel) |
65 |
double Pos; ///< Current playback position in sample |
uint MaxSamplesPerCycle; ///< Size of each audio output buffer |
66 |
double CurrentPitch; ///< Current pitch depth (number of sample points to move on with each render step) |
double Pos; ///< Current playback position in sample |
67 |
gig::Sample* pSample; ///< Pointer to the sample to be played back |
double CurrentPitch; ///< Current pitch depth (number of sample points to move on with each render step) |
68 |
gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice |
gig::Sample* pSample; ///< Pointer to the sample to be played back |
69 |
bool Active; ///< If this voice object is currently in usage |
gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice |
70 |
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 |
bool Active; ///< If this voice object is currently in usage |
71 |
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 |
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 |
72 |
Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream |
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 |
73 |
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. |
Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream |
74 |
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 |
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. |
75 |
int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed |
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 |
76 |
|
int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed |
77 |
EG_VCA EG1; |
EG_VCA EG1; |
78 |
|
|
79 |
// Static Attributes |
// Static Attributes |
80 |
static DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again |
static DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again |
81 |
|
|
82 |
// Methods |
// Methods |
83 |
void Interpolate(sample_t* pSrc); |
void Interpolate(uint Samples, sample_t* pSrc); |
84 |
void InterpolateAndLoop(sample_t* pSrc); |
void InterpolateAndLoop(uint Samples, sample_t* pSrc); |
85 |
inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume) { |
inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume) { |
86 |
int pos_int = double_to_int(this->Pos); // integer position |
int pos_int = double_to_int(this->Pos); // integer position |
87 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
float pos_fract = this->Pos - pos_int; // fractional part of position |
89 |
|
|
90 |
#if USE_LINEAR_INTERPOLATION |
#if USE_LINEAR_INTERPOLATION |
91 |
// left channel |
// left channel |
92 |
this->pOutput[i++] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
this->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
93 |
// right channel |
// right channel |
94 |
this->pOutput[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
this->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
95 |
#else // polynomial interpolation |
#else // polynomial interpolation |
96 |
// calculate left channel |
// calculate left channel |
97 |
float xm1 = pSrc[pos_int]; |
float xm1 = pSrc[pos_int]; |
101 |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
float a = (3 * (x0 - x1) - xm1 + x2) / 2; |
102 |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
103 |
float c = (x1 - xm1) / 2; |
float c = (x1 - xm1) / 2; |
104 |
this->pOutput[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
this->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
105 |
|
|
106 |
//calculate right channel |
//calculate right channel |
107 |
xm1 = pSrc[pos_int+1]; |
xm1 = pSrc[pos_int+1]; |
111 |
a = (3 * (x0 - x1) - xm1 + x2) / 2; |
a = (3 * (x0 - x1) - xm1 + x2) / 2; |
112 |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
113 |
c = (x1 - xm1) / 2; |
c = (x1 - xm1) / 2; |
114 |
this->pOutput[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
this->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
115 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
116 |
|
|
117 |
this->Pos += this->CurrentPitch; |
this->Pos += this->CurrentPitch; |
133 |
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); |
134 |
#endif // USE_LINEAR_INTERPOLATION |
#endif // USE_LINEAR_INTERPOLATION |
135 |
|
|
136 |
this->pOutput[i++] += sample_point; |
this->pOutputLeft[i] += sample_point; |
137 |
this->pOutput[i++] += sample_point; |
this->pOutputRight[i++] += sample_point; |
138 |
|
|
139 |
this->Pos += this->CurrentPitch; |
this->Pos += this->CurrentPitch; |
140 |
} |
} |