34 |
Voice::~Voice() { |
Voice::~Voice() { |
35 |
} |
} |
36 |
|
|
37 |
void Voice::Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument) { |
/** |
38 |
Active = true; |
* Initializes and triggers the voice, a disk stream will be launched if |
39 |
this->MIDIKey = MIDIKey; |
* needed. |
40 |
pRegion = Instrument->GetRegion(MIDIKey); |
* |
41 |
PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed |
* @returns 0 on success, a value < 0 if something failed |
42 |
Pos = 0; |
*/ |
43 |
|
int Voice::Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument) { |
44 |
|
Active = true; |
45 |
|
this->MIDIKey = MIDIKey; |
46 |
|
pRegion = Instrument->GetRegion(MIDIKey); |
47 |
|
PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed |
48 |
|
Pos = 0; |
49 |
|
ReleaseVelocity = 127; // default release velocity value |
50 |
|
|
51 |
if (!pRegion) { |
if (!pRegion) { |
52 |
std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush; |
std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush; |
53 |
Active = false; |
Kill(); |
54 |
return; |
return -1; |
55 |
} |
} |
56 |
|
|
57 |
//TODO: current MIDI controller values are not taken into account yet |
//TODO: current MIDI controller values are not taken into account yet |
74 |
long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; |
long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; |
75 |
DiskVoice = cachedsamples < pSample->SamplesTotal; |
DiskVoice = cachedsamples < pSample->SamplesTotal; |
76 |
|
|
77 |
if (DiskVoice) { |
if (DiskVoice) { // voice to be streamed from disk |
78 |
MaxRAMPos = cachedsamples - (OutputBufferSize << MAX_PITCH) / pSample->Channels; |
MaxRAMPos = cachedsamples - (OutputBufferSize << MAX_PITCH) / pSample->Channels; |
79 |
pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos); |
|
80 |
dmsg(5,("Disk voice launched (cached samples: %d, total Samples: %d, MaxRAMPos: %d\n", cachedsamples, pSample->SamplesTotal, MaxRAMPos)); |
// check if there's a loop defined which completely fits into the cached (RAM) part of the sample |
81 |
|
if (pSample->Loops && pSample->LoopEnd <= MaxRAMPos) { |
82 |
|
RAMLoop = true; |
83 |
|
LoopCyclesLeft = pSample->LoopPlayCount; |
84 |
|
} |
85 |
|
else RAMLoop = false; |
86 |
|
|
87 |
|
if (pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos, !RAMLoop) < 0) { |
88 |
|
dmsg(1,("Disk stream order failed!\n")); |
89 |
|
Kill(); |
90 |
|
return -1; |
91 |
|
} |
92 |
|
dmsg(4,("Disk voice launched (cached samples: %d, total Samples: %d, MaxRAMPos: %d, RAMLooping: %s\n", cachedsamples, pSample->SamplesTotal, MaxRAMPos, (RAMLoop) ? "yes" : "no")); |
93 |
} |
} |
94 |
else { |
else { // RAM only voice |
95 |
MaxRAMPos = cachedsamples; |
MaxRAMPos = cachedsamples; |
96 |
dmsg(5,("RAM only voice launched\n")); |
if (pSample->Loops) { |
97 |
|
RAMLoop = true; |
98 |
|
LoopCyclesLeft = pSample->LoopPlayCount; |
99 |
|
} |
100 |
|
else RAMLoop = false; |
101 |
|
dmsg(4,("RAM only voice launched (Looping: %s)\n", (RAMLoop) ? "yes" : "no")); |
102 |
} |
} |
103 |
|
|
104 |
CurrentPitch = pow(2, (double) (MIDIKey - (int) pSample->MIDIUnityNote) / (double) 12); |
// Pitch according to keyboard position (if keyrange > 1 key) |
105 |
|
CurrentPitch = (pRegion->KeyRange.high != pRegion->KeyRange.low) ? |
106 |
|
pow(2, (double) (MIDIKey - (int) pSample->MIDIUnityNote) / (double) 12) : 1.0; |
107 |
|
Volume = pDimRgn->GetVelocityAttenuation(Velocity); |
108 |
|
|
109 |
// ************************************************ |
// ************************************************ |
110 |
// TODO: ARTICULATION DATA HANDLING IS MISSING HERE |
// TODO: ARTICULATION DATA HANDLING IS MISSING HERE |
111 |
// ************************************************ |
// ************************************************ |
112 |
|
|
113 |
|
return 0; // success |
114 |
} |
} |
115 |
|
|
116 |
|
/** |
117 |
|
* Renders the audio data for this voice for the current audio fragment. |
118 |
|
* The sample input data can either come from RAM (cached sample or sample |
119 |
|
* part) or directly from disk. The output signal will be rendered by |
120 |
|
* resampling / interpolation. If this voice is a disk streaming voice and |
121 |
|
* the voice completely played back the cached RAM part of the sample, it |
122 |
|
* will automatically switch to disk playback for the next RenderAudio() |
123 |
|
* call. |
124 |
|
*/ |
125 |
void Voice::RenderAudio() { |
void Voice::RenderAudio() { |
126 |
|
|
127 |
switch (this->PlaybackState) { |
switch (this->PlaybackState) { |
128 |
|
|
129 |
case playback_state_ram: { |
case playback_state_ram: { |
130 |
Interpolate((sample_t*) pSample->GetCache().pStart); |
if (RAMLoop) InterpolateAndLoop((sample_t*) pSample->GetCache().pStart); |
131 |
|
else Interpolate((sample_t*) pSample->GetCache().pStart); |
132 |
if (DiskVoice) { |
if (DiskVoice) { |
133 |
// check if we reached the allowed limit of the sample RAM cache |
// check if we reached the allowed limit of the sample RAM cache |
134 |
if (Pos > MaxRAMPos) { |
if (Pos > MaxRAMPos) { |
148 |
DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID); |
DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID); |
149 |
if (!DiskStreamRef.pStream) { |
if (!DiskStreamRef.pStream) { |
150 |
std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush; |
std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush; |
151 |
pDiskThread->OrderDeletionOfStream(&DiskStreamRef); |
Kill(); |
|
this->Active = false; |
|
152 |
return; |
return; |
153 |
} |
} |
154 |
DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos)); |
DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos)); |
169 |
break; |
break; |
170 |
|
|
171 |
case playback_state_end: |
case playback_state_end: |
172 |
this->Active = false; // free voice |
Kill(); // free voice |
173 |
break; |
break; |
174 |
} |
} |
175 |
} |
} |
176 |
|
|
177 |
|
/** |
178 |
|
* Interpolates the input audio data (no loop). |
179 |
|
* |
180 |
|
* @param pSrc - pointer to input sample data |
181 |
|
*/ |
182 |
void Voice::Interpolate(sample_t* pSrc) { |
void Voice::Interpolate(sample_t* pSrc) { |
183 |
float effective_volume = 1; // TODO: use the art. data instead |
float effective_volume = this->Volume; |
184 |
int i = 0; |
int i = 0; |
185 |
|
|
186 |
// ************************************************ |
// ************************************************ |
190 |
// FIXME: assuming either mono or stereo |
// FIXME: assuming either mono or stereo |
191 |
if (this->pSample->Channels == 2) { // Stereo Sample |
if (this->pSample->Channels == 2) { // Stereo Sample |
192 |
while (i < this->OutputBufferSize) { |
while (i < this->OutputBufferSize) { |
193 |
#ifdef USE_LINEAR_INTERPOLATION |
InterpolateOneStep_Stereo(pSrc, i, effective_volume); |
|
int pos_int = double_to_int(this->Pos); // integer position |
|
|
float pos_fract = this->Pos - pos_int; // fractional part of position |
|
|
pos_int <<= 1; |
|
|
// left channel |
|
|
this->pOutput[i++] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])); |
|
|
// right channel |
|
|
this->pOutput[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])); |
|
|
#else // polynomial interpolation |
|
|
//FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile |
|
|
xm1 = pSrc[pos_int]; |
|
|
x0 = pSrc[pos_int+1]; |
|
|
x1 = pSrc[pos_int+2]; |
|
|
x2 = pSrc[pos_int+3]; |
|
|
a = (3 * (x0-x1) - xm1 + x2) / 2; |
|
|
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
|
|
c = (x1 - xm1) / 2; |
|
|
this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
|
|
#endif // USE_LINEAR_INTERPOLATION |
|
|
|
|
|
this->Pos += this->CurrentPitch; |
|
194 |
} |
} |
195 |
} |
} |
196 |
else { // Mono Sample |
else { // Mono Sample |
197 |
while (i < this->OutputBufferSize) { |
while (i < this->OutputBufferSize) { |
198 |
#ifdef USE_LINEAR_INTERPOLATION |
InterpolateOneStep_Mono(pSrc, i, effective_volume); |
199 |
int pos_int = double_to_int(this->Pos); // integer position |
} |
200 |
float pos_fract = this->Pos - pos_int; // fractional part of position |
} |
201 |
float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int])); |
} |
202 |
this->pOutput[i] += sample_point; |
|
203 |
this->pOutput[i+1] += sample_point; |
/** |
204 |
i += 2; |
* Interpolates the input audio data, this method honors looping. |
205 |
#else // polynomial interpolation |
* |
206 |
//FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile |
* @param pSrc - pointer to input sample data |
207 |
xm1 = pSrc[pos_int]; |
*/ |
208 |
x0 = pSrc[pos_int+1]; |
void Voice::InterpolateAndLoop(sample_t* pSrc) { |
209 |
x1 = pSrc[pos_int+2]; |
float effective_volume = this->Volume; |
210 |
x2 = pSrc[pos_int+3]; |
int i = 0; |
211 |
a = (3 * (x0-x1) - xm1 + x2) / 2; |
|
212 |
b = 2 * x1 + xm1 - (5 * x0 + x2) / 2; |
// ************************************************ |
213 |
c = (x1 - xm1) / 2; |
// TODO: ARTICULATION DATA HANDLING IS MISSING HERE |
214 |
this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0); |
// ************************************************ |
|
#endif |
|
215 |
|
|
216 |
this->Pos += this->CurrentPitch; |
// FIXME: assuming either mono or stereo |
217 |
|
if (pSample->Channels == 2) { // Stereo Sample |
218 |
|
if (pSample->LoopPlayCount) { |
219 |
|
// render loop (loop count limited) |
220 |
|
while (i < OutputBufferSize && LoopCyclesLeft) { |
221 |
|
InterpolateOneStep_Stereo(pSrc, i, effective_volume); |
222 |
|
if (Pos > pSample->LoopEnd) { |
223 |
|
Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; |
224 |
|
LoopCyclesLeft--; |
225 |
|
} |
226 |
|
} |
227 |
|
// render on without loop |
228 |
|
while (i < OutputBufferSize) { |
229 |
|
InterpolateOneStep_Stereo(pSrc, i, effective_volume); |
230 |
|
} |
231 |
|
} |
232 |
|
else { // render loop (endless loop) |
233 |
|
while (i < OutputBufferSize) { |
234 |
|
InterpolateOneStep_Stereo(pSrc, i, effective_volume); |
235 |
|
if (Pos > pSample->LoopEnd) { |
236 |
|
Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize); |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
} |
241 |
|
else { // Mono Sample |
242 |
|
if (pSample->LoopPlayCount) { |
243 |
|
// render loop (loop count limited) |
244 |
|
while (i < OutputBufferSize && LoopCyclesLeft) { |
245 |
|
InterpolateOneStep_Mono(pSrc, i, effective_volume); |
246 |
|
if (Pos > pSample->LoopEnd) { |
247 |
|
Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; |
248 |
|
LoopCyclesLeft--; |
249 |
|
} |
250 |
|
} |
251 |
|
// render on without loop |
252 |
|
while (i < OutputBufferSize) { |
253 |
|
InterpolateOneStep_Mono(pSrc, i, effective_volume); |
254 |
|
} |
255 |
|
} |
256 |
|
else { // render loop (endless loop) |
257 |
|
while (i < OutputBufferSize) { |
258 |
|
InterpolateOneStep_Mono(pSrc, i, effective_volume); |
259 |
|
if (Pos > pSample->LoopEnd) { |
260 |
|
Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; |
261 |
|
} |
262 |
|
} |
263 |
} |
} |
264 |
} |
} |
265 |
} |
} |
266 |
|
|
267 |
|
/** |
268 |
|
* Immediately kill the voice. |
269 |
|
*/ |
270 |
void Voice::Kill() { |
void Voice::Kill() { |
271 |
if (DiskVoice && DiskStreamRef.State != Stream::state_unused) { |
if (DiskVoice && DiskStreamRef.State != Stream::state_unused) { |
272 |
pDiskThread->OrderDeletionOfStream(&DiskStreamRef); |
pDiskThread->OrderDeletionOfStream(&DiskStreamRef); |
273 |
} |
} |
274 |
|
DiskStreamRef.pStream = NULL; |
275 |
|
DiskStreamRef.hStream = 0; |
276 |
|
DiskStreamRef.State = Stream::state_unused; |
277 |
|
DiskStreamRef.OrderID = 0; |
278 |
Active = false; |
Active = false; |
279 |
} |
} |