1 /*
2 * This file is part of EasyRPG Player.
3 *
4 * EasyRPG Player is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * EasyRPG Player is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "system.h"
19
20 #ifdef USE_AUDIO_RESAMPLER
21
22 #include <cassert>
23 #include <cstring>
24 #include "audio_resampler.h"
25 #include "output.h"
26
27 #define ERROR -1
28 #define STANDARD_PITCH 100
29
30 /**
31 * Utility function used to convert a buffer of a arbitrary AudioDecoder::Format to a float buffer
32 *
33 * @param[in] wrapped_decoder The decoder from which audio samples are read
34 * @param[inout] buffer The buffer which will receive the converted samples,
35 * has to be at least amount_of_samples_to_read*sizeof(float) bytes big.
36 * @param[in] amount_of_samples_to_read The number of samples to read.
37 * @param[in] input_samplesize The size of one sample of the decoder in it's original format - given in bytes
38 * @param[in] input_format The original format of the samples
39 *
40 * @return The number of converted samples - if this number is smaller than amount_of_samples_to_read the wrapped decoder has reaches it's end.
41 * If the returned value has a negative value an error occured.
42 */
DecodeAndConvertFloat(AudioDecoderBase * wrapped_decoder,uint8_t * buffer,int amount_of_samples_to_read,const int input_samplesize,const AudioDecoder::Format input_format)43 inline static int DecodeAndConvertFloat(AudioDecoderBase* wrapped_decoder,
44 uint8_t * buffer,
45 int amount_of_samples_to_read,
46 const int input_samplesize,
47 const AudioDecoder::Format input_format){
48 float* bufferAsFloat = (float*)buffer;
49
50 //Workaround for decoders which don't detect their own end
51 if (wrapped_decoder->IsFinished())
52 return 0;
53
54 int amount_of_samples_read = wrapped_decoder->Decode(buffer, amount_of_samples_to_read*input_samplesize);
55 if (amount_of_samples_read <= 0) {
56 return amount_of_samples_read; //error occured - or nothing read
57 } else {
58 amount_of_samples_read /= input_samplesize;
59 }
60
61 //Convert the read data (amount_of_data_read is at least one at this moment)
62 switch (input_format) {
63 case AudioDecoder::Format::S8:
64 //Convert inplace (the last frames are unused if smaller)
65 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
66 bufferAsFloat[i] = ((int8_t*)bufferAsFloat)[i] / 128.0;
67 }
68 break;
69 case AudioDecoder::Format::U8:
70 //Convert inplace (the last frames are unused if smaller)
71 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
72 bufferAsFloat[i] = ((uint8_t*)bufferAsFloat)[i] / 128.0 - 1.0;
73 }
74 break;
75 case AudioDecoder::Format::S16:
76 //Convert inplace (the last frames are unused if smaller)
77 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
78 bufferAsFloat[i] = ((int16_t*)bufferAsFloat)[i] / 32768.0;
79 }
80 break;
81 case AudioDecoder::Format::U16:
82 //Convert inplace (the last frames are unused if smaller)
83 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
84 bufferAsFloat[i] = ((uint16_t*)bufferAsFloat)[i] / 32768.0 - 1.0;
85 }
86 break;
87 case AudioDecoder::Format::S32:
88 //Convert inplace (same size)
89 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
90 bufferAsFloat[i] = ((int32_t*)bufferAsFloat)[i] / 2147483648.0;
91 }
92 break;
93 case AudioDecoder::Format::U32:
94 //Convert inplace (same size)
95 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
96 bufferAsFloat[i] = ((uint32_t*)bufferAsFloat)[i] / 2147483648.0 - 1.0;
97 }
98 break;
99 case AudioDecoder::Format::F32:
100 //Nothing to convert
101 break;
102 }
103 return amount_of_samples_read;
104 }
105
106 #if defined(HAVE_LIBSPEEXDSP)
107 /**
108 * Utility function used to convert a buffer of a arbitrary AudioDecoder::Format to a int16 buffer
109 *
110 * @param[in] wrapped_decoder The decoder from which audio samples are read
111 * @param[inout] buffer The buffer which will receive the converted samples,
112 * has to be at least amount_of_samples_to_read*max(sizeof(int16_t),input_samplesize) bytes big.
113 * @param[in] amount_of_samples_to_read The number of samples to read.
114 * @param[in] input_samplesize The size of one sample of the decoder in it's original format - given in bytes
115 * @param[in] input_format The original format of the samples
116 *
117 * @return The number of converted samples - if this number is smaller than amount_of_samples_to_read the wrapped decoder has reaches it's end.
118 * If the returned value has a negative value an error occured.
119 */
DecodeAndConvertInt16(AudioDecoderBase * wrapped_decoder,uint8_t * buffer,int amount_of_samples_to_read,const int input_samplesize,const AudioDecoder::Format input_format)120 inline static int DecodeAndConvertInt16(AudioDecoderBase* wrapped_decoder,
121 uint8_t * buffer,
122 int amount_of_samples_to_read,
123 const int input_samplesize,
124 const AudioDecoder::Format input_format){
125 int16_t* bufferAsInt16 = (int16_t*)buffer;
126
127 //Workaround for decoders which don't detect their own end
128 if (wrapped_decoder->IsFinished())
129 return 0;
130
131 int amount_of_samples_read = wrapped_decoder->Decode(buffer, amount_of_samples_to_read*input_samplesize);
132 if (amount_of_samples_read <= 0) {
133 return amount_of_samples_read; //error occured - or nothing read
134 } else {
135 //Convert the number of bytes to the number of samples
136 amount_of_samples_read /= input_samplesize;
137 }
138 //Convert the read data (amount_of_data_read is at least one at this moment)
139 switch (input_format) {
140 case AudioDecoder::Format::S8:
141 //Convert inplace (the last frames are unused if smaller)
142 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
143 bufferAsInt16[i] = ((int8_t*)bufferAsInt16)[i] << 8;
144 }
145 break;
146 case AudioDecoder::Format::U8:
147 //Convert inplace (the last frames are unused if smaller)
148 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
149 bufferAsInt16[i] = (((int16_t)(((uint8_t*)bufferAsInt16)[i])) - 128) << 8;
150 }
151 break;
152 case AudioDecoder::Format::S16:
153 //Nothing to convert
154 break;
155 case AudioDecoder::Format::U16:
156 //Convert unsigned to signed
157 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
158 bufferAsInt16[i] = (int16_t)(((int32_t)(((uint16_t*)bufferAsInt16)[i])) - 32768);
159 }
160 break;
161 case AudioDecoder::Format::S32:
162 //Convert inplace (from front to back to prevent overwriting the buffer)
163 for (int i = 0; i < amount_of_samples_read; i++) {
164 bufferAsInt16[i] = (int16_t)((((int32_t*)bufferAsInt16)[i]) >> 16);
165 }
166 break;
167 case AudioDecoder::Format::U32:
168 //Convert inplace (from front to back to prevent overwriting the buffer)
169 for (int i = 0; i < amount_of_samples_read; i++) {
170 bufferAsInt16[i] = (int16_t)(((int32_t)((((uint32_t*)bufferAsInt16)[i]) >> 16)) - 32768);
171 }
172 break;
173 case AudioDecoder::Format::F32:
174 //Convert inplace (from front to back to prevent overwriting the buffer)
175 for (int i = 0; i < amount_of_samples_read; i++) {
176 float number = ((((float*)bufferAsInt16)[i])*32768.0);
177 bufferAsInt16[i] = (number <= 32767.0) ? ((number >= -32768.0) ? number : -32768) : 32767;
178 }
179 break;
180 }
181 return amount_of_samples_read;
182 }
183 #endif
184
AudioResampler(std::unique_ptr<AudioDecoderBase> wrapped,AudioResampler::Quality quality)185 AudioResampler::AudioResampler(std::unique_ptr<AudioDecoderBase> wrapped, AudioResampler::Quality quality)
186 : wrapped_decoder(std::move(wrapped))
187 {
188 //There is no need for a standalone resampler decoder
189 assert(wrapped_decoder != 0);
190
191 music_type = wrapped_decoder->GetType();
192 lasterror = 0;
193
194 #if defined(HAVE_LIBSPEEXDSP)
195 switch (quality) {
196 case Quality::Low:
197 sampling_quality = 0;
198 break;
199 case Quality::Medium:
200 sampling_quality = 3;
201 break;
202 case Quality::High:
203 sampling_quality = 5;
204 break;
205 }
206 #elif defined(HAVE_LIBSAMPLERATE)
207 switch (quality) {
208 case Quality::Low:
209 sampling_quality = SRC_SINC_FASTEST;
210 break;
211 case Quality::Medium:
212 sampling_quality = SRC_SINC_MEDIUM_QUALITY;
213 break;
214 case Quality::High:
215 sampling_quality = SRC_SINC_BEST_QUALITY;
216 break;
217 }
218 #endif
219
220 finished = false;
221 }
222
~AudioResampler()223 AudioResampler::~AudioResampler() {
224 if (conversion_state) {
225 #if defined(HAVE_LIBSPEEXDSP)
226 speex_resampler_destroy(conversion_state);
227 #elif defined(HAVE_LIBSAMPLERATE)
228 src_delete(conversion_state);
229 #endif
230 }
231 }
232
WasInited() const233 bool AudioResampler::WasInited() const {
234 return wrapped_decoder->WasInited();
235 }
236
Open(Filesystem_Stream::InputStream stream)237 bool AudioResampler::Open(Filesystem_Stream::InputStream stream) {
238 if (wrapped_decoder->Open(std::move(stream))) {
239 wrapped_decoder->GetFormat(input_rate, input_format, nr_of_channels);
240
241 //determine if the input format is supported by the resampler
242 switch (input_format) {
243 case Format::F32: output_format = input_format; break;
244 #ifdef HAVE_LIBSPEEXDSP
245 case Format::S16: output_format = input_format; break;
246 #endif
247 default: output_format = Format::F32; break;
248 }
249
250 //Set input format to output_format if possible
251 wrapped_decoder->SetFormat(input_rate, output_format, nr_of_channels);
252 //Reread format to get new values
253 wrapped_decoder->GetFormat(input_rate, input_format, nr_of_channels);
254 output_rate = input_rate;
255
256 #if defined(HAVE_LIBSPEEXDSP)
257 conversion_state = speex_resampler_init(nr_of_channels, input_rate, output_rate, sampling_quality, &lasterror);
258 conversion_data.ratio_num = input_rate;
259 conversion_data.ratio_denom = output_rate;
260 speex_resampler_skip_zeros(conversion_state);
261 #elif defined(HAVE_LIBSAMPLERATE)
262 conversion_state = src_new(sampling_quality, nr_of_channels, &lasterror);
263 #endif
264
265 //Init the conversion data structure
266 conversion_data.input_frames = 0;
267 conversion_data.input_frames_used = 0;
268 finished = false;
269
270 if (conversion_state)
271 return true;
272 }
273
274 conversion_state = nullptr;
275 return false;
276 }
277
Pause()278 void AudioResampler::Pause() {
279 wrapped_decoder->Pause();
280 }
281
Resume()282 void AudioResampler::Resume() {
283 wrapped_decoder->Resume();
284 }
285
GetVolume() const286 int AudioResampler::GetVolume() const {
287 return wrapped_decoder->GetVolume();
288 }
289
SetVolume(int volume)290 void AudioResampler::SetVolume(int volume) {
291 wrapped_decoder->SetVolume(volume);
292 }
293
SetFade(int end,std::chrono::milliseconds duration)294 void AudioResampler::SetFade(int end, std::chrono::milliseconds duration) {
295 wrapped_decoder->SetFade(end, duration);
296 }
297
Seek(std::streamoff offset,std::ios_base::seekdir origin)298 bool AudioResampler::Seek(std::streamoff offset, std::ios_base::seekdir origin) {
299 if (wrapped_decoder->Seek(offset, origin)) {
300 //reset conversion data
301 conversion_data.input_frames = 0;
302 conversion_data.input_frames_used = 0;
303 finished = wrapped_decoder->IsFinished();
304 #if defined(HAVE_LIBSPEEXDSP)
305 speex_resampler_reset_mem(conversion_state);
306 #elif defined(HAVE_LIBSAMPLERATE)
307 src_reset(conversion_state);
308 #endif
309 return true;
310 }
311 return false;
312
313 }
314
Tell() const315 std::streampos AudioResampler::Tell() const {
316 return wrapped_decoder->Tell();
317 }
318
GetTicks() const319 int AudioResampler::GetTicks() const {
320 return wrapped_decoder->GetTicks();
321 }
322
IsFinished() const323 bool AudioResampler::IsFinished() const {
324 return finished;
325 }
326
Update(std::chrono::microseconds delta)327 void AudioResampler::Update(std::chrono::microseconds delta) {
328 wrapped_decoder->Update(delta);
329 }
330
GetFormat(int & frequency,AudioDecoder::Format & format,int & channels) const331 void AudioResampler::GetFormat(int& frequency, AudioDecoder::Format& format, int& channels) const {
332 frequency = output_rate;
333 format = output_format;
334 channels = mono_to_stereo_resample ? 2 : nr_of_channels;
335 }
336
SetFormat(int freq,AudioDecoder::Format fmt,int channels)337 bool AudioResampler::SetFormat(int freq, AudioDecoder::Format fmt, int channels) {
338 //Check whether requested format is supported by the resampler
339 switch (fmt) {
340 case Format::F32:
341 output_format = fmt;
342 break;
343 #ifdef HAVE_LIBSPEEXDSP
344 case Format::S16:
345 output_format = fmt;
346 break;
347 #endif
348 default:
349 break;
350 }
351 wrapped_decoder->SetFormat(input_rate, output_format, channels);
352 wrapped_decoder->GetFormat(input_rate, input_format, nr_of_channels);
353 output_rate = freq;
354
355 mono_to_stereo_resample = false;
356 if (channels == 2 && nr_of_channels == 1) {
357 mono_to_stereo_resample = true;
358 }
359
360 return ((nr_of_channels == channels || mono_to_stereo_resample) && (output_format == fmt));
361 }
362
GetPitch() const363 int AudioResampler::GetPitch() const {
364 return pitch;
365 }
366
SetPitch(int pitch_)367 bool AudioResampler::SetPitch(int pitch_) {
368 pitch_handled_by_decoder = wrapped_decoder->SetPitch(pitch_);
369 pitch = pitch_;
370 return true;
371 }
372
FillBuffer(uint8_t * buffer,int length)373 int AudioResampler::FillBuffer(uint8_t* buffer, int length) {
374 int amount_filled = 0;
375
376 int bytes_to_read = length;
377 if (mono_to_stereo_resample) {
378 bytes_to_read /= 2;
379 }
380
381 if ((input_rate == output_rate) && ((pitch == STANDARD_PITCH) || pitch_handled_by_decoder)) {
382 // Do only format conversion
383 amount_filled = FillBufferSameRate(buffer, bytes_to_read);
384 } else {
385 if (!conversion_state) {
386 error_message = "internal error: state pointer is a nullptr";
387 amount_filled = ERROR;
388 } else {
389 //Do samplerate conversion
390 amount_filled = FillBufferDifferentRate(buffer, bytes_to_read);
391 }
392 }
393
394 if (!mono_to_stereo_resample || amount_filled <= 0) {
395 return amount_filled;
396 }
397
398 // Resample mono to stereo
399 int sample_size = AudioDecoder::GetSamplesizeForFormat(output_format);
400
401 // Duplicate data from the back, allows writing to the buffer directly
402 for (int i = amount_filled - sample_size; i > 0; i -= sample_size) {
403 // left channel
404 memcpy(&buffer[i * 2], &buffer[i], sample_size);
405 // right channel
406 memcpy(&buffer[i * 2 + sample_size], &buffer[i], sample_size);
407 }
408
409 return amount_filled * 2;
410 }
411
FillBufferSameRate(uint8_t * buffer,int length)412 int AudioResampler::FillBufferSameRate(uint8_t* buffer, int length) {
413 const int input_samplesize = AudioDecoder::GetSamplesizeForFormat(input_format);
414 const int output_samplesize = AudioDecoder::GetSamplesizeForFormat(output_format);
415 //The buffer size has to be a multiple of a frame
416 const int buffer_size=sizeof(internal_buffer) - sizeof(internal_buffer)%(nr_of_channels*input_samplesize);
417
418 int total_output_frames = length / (output_samplesize*nr_of_channels);
419 int amount_of_data_to_read = 0;
420 int amount_of_data_read = total_output_frames*nr_of_channels;
421
422 int decoded = 0;
423
424 if (input_samplesize > output_samplesize) {
425 //It is necessary to use the internal_buffer to convert the samples.
426 while (total_output_frames > 0) {
427 amount_of_data_to_read = buffer_size / input_samplesize;
428
429 //limit amount_of_data_to_read in the last loop
430 amount_of_data_to_read = (amount_of_data_to_read > total_output_frames) ? total_output_frames : amount_of_data_to_read;
431
432 switch (output_format) {
433 case AudioDecoder::Format::F32:
434 amount_of_data_read = DecodeAndConvertFloat(wrapped_decoder.get(), internal_buffer, amount_of_data_to_read, input_samplesize, input_format);
435 break;
436 #ifdef HAVE_LIBSPEEXDSP
437 case AudioDecoder::Format::S16:
438 amount_of_data_read = DecodeAndConvertInt16(wrapped_decoder.get(), internal_buffer, amount_of_data_to_read, input_samplesize, input_format);
439 break;
440 #endif
441 default: error_message = "internal error: output_format is not convertable"; return ERROR;
442 }
443 if (amount_of_data_read < 0) {
444 error_message = wrapped_decoder->GetError();
445 return amount_of_data_read; //error occured
446 }
447
448 //Copy the converted samples
449 for (int i = 0; i < amount_of_data_read*output_samplesize; i++) {
450 buffer[i] = internal_buffer[i];
451 }
452 //Prepare next loop
453 total_output_frames -= amount_of_data_read;
454 decoded += amount_of_data_read;
455 buffer += amount_of_data_read*output_samplesize;
456
457 //If the end of the decoder is reached (it has finished)
458 if (amount_of_data_read < amount_of_data_to_read) {
459 break;
460 }
461
462 }
463 } else {
464 //It is possible to work inplace as length is specified for the output samplesize.
465 switch (output_format) {
466 case AudioDecoder::Format::F32:
467 decoded = DecodeAndConvertFloat(wrapped_decoder.get(), buffer, amount_of_data_read, input_samplesize, input_format);
468 break;
469 #ifdef HAVE_LIBSPEEXDSP
470 case AudioDecoder::Format::S16:
471 decoded = DecodeAndConvertInt16(wrapped_decoder.get(), buffer, amount_of_data_read, input_samplesize, input_format);
472 break;
473 #endif
474 default: error_message = "internal error: output_format is not convertable"; return ERROR;
475 }
476 }
477
478 finished = wrapped_decoder->IsFinished();
479 if (decoded < 0) {
480 error_message = wrapped_decoder->GetError();
481 return decoded;
482 } else {
483 return decoded*output_samplesize;
484 }
485 }
486
FillBufferDifferentRate(uint8_t * buffer,int length)487 int AudioResampler::FillBufferDifferentRate(uint8_t* buffer, int length) {
488 const int input_samplesize = AudioDecoder::GetSamplesizeForFormat(input_format);
489 const int output_samplesize = AudioDecoder::GetSamplesizeForFormat(output_format);
490 //The buffer size has to be a multiple of a frame
491 const int buffer_size=sizeof(internal_buffer) - sizeof(internal_buffer)%(nr_of_channels*((input_samplesize>output_samplesize) ? input_samplesize : output_samplesize));
492
493 int total_output_frames = length / (output_samplesize*nr_of_channels);
494 int amount_of_samples_to_read = 0;
495 int amount_of_samples_read = 0;
496
497 uint8_t * advanced_input_buffer = internal_buffer;
498 int unused_frames = 0;
499 int empty_buffer_space = 0;
500 int error = 0;
501
502 #ifdef HAVE_LIBSPEEXDSP
503 spx_uint32_t numerator = 0;
504 spx_uint32_t denominator = 0;
505 #endif
506
507 while (total_output_frames > 0) {
508 //Calculate how much frames of the last cycle are unused - to reuse them
509 unused_frames = conversion_data.input_frames - conversion_data.input_frames_used;
510 empty_buffer_space = buffer_size / output_samplesize - unused_frames*nr_of_channels;
511
512 advanced_input_buffer = internal_buffer;
513
514 //If there is still unused data in the input_buffer order it to the front
515 for (int i = 0; i < unused_frames*nr_of_channels*output_samplesize; i++) {
516 *advanced_input_buffer = *(advanced_input_buffer + empty_buffer_space*output_samplesize);
517 advanced_input_buffer++;
518 }
519 //advanced_input_buffer is now offset to the first frame of new data!
520
521 //ensure that the input buffer is not able to overrun
522 amount_of_samples_to_read = (input_samplesize > output_samplesize) ? (empty_buffer_space*output_samplesize) / input_samplesize : empty_buffer_space;
523
524 //Read as many frames as needed to refill the buffer (filled after the conversion to float)
525 if (amount_of_samples_to_read != 0) {
526 switch (output_format) {
527 case AudioDecoder::Format::F32: amount_of_samples_read = DecodeAndConvertFloat(wrapped_decoder.get(), advanced_input_buffer, amount_of_samples_to_read, input_samplesize, input_format); break;
528 #ifdef HAVE_LIBSPEEXDSP
529 case AudioDecoder::Format::S16: amount_of_samples_read = DecodeAndConvertInt16(wrapped_decoder.get(), advanced_input_buffer, amount_of_samples_to_read, input_samplesize, input_format); break;
530 #endif
531 default: error_message = "internal error: output_format is not convertable"; return ERROR;
532 }
533 if (amount_of_samples_read < 0) {
534 error_message = wrapped_decoder->GetError();
535 return amount_of_samples_read; //error occured
536 }
537 }
538 //Now we have a prepared full buffer of converted values
539
540 //Prepare the source data
541 conversion_data.input_frames = amount_of_samples_read / nr_of_channels + unused_frames;
542 conversion_data.output_frames = total_output_frames;
543
544 #if defined(HAVE_LIBSPEEXDSP)
545 conversion_data.input_frames_used = conversion_data.input_frames;
546 conversion_data.output_frames_gen = conversion_data.output_frames;
547
548 //libspeexdsp defines a sample rate conversion with a fraction (input/output)
549 numerator = input_rate*pitch;
550 denominator = output_rate * STANDARD_PITCH;
551 if (pitch_handled_by_decoder) {
552 numerator = input_rate;
553 denominator = output_rate;
554 }
555 if (conversion_data.ratio_num != numerator || conversion_data.ratio_denom != denominator) {
556 speex_resampler_set_rate_frac(conversion_state, numerator, denominator, input_rate, output_rate);
557 conversion_data.ratio_num = numerator;
558 conversion_data.ratio_denom = denominator;
559 }
560
561 //A pitfall from libspeexdsp if the output buffer is defined to big - everything stutters -achieved good values with the same size as the input buffer for a maximum
562 conversion_data.output_frames_gen=(conversion_data.input_frames<conversion_data.output_frames_gen) ? conversion_data.input_frames :conversion_data.output_frames_gen;
563
564 switch (output_format) {
565 case Format::F32:
566 error = speex_resampler_process_interleaved_float(conversion_state, (float*)internal_buffer, &conversion_data.input_frames_used, (float*)buffer, &conversion_data.output_frames_gen);
567 break;
568 case Format::S16:
569 error = speex_resampler_process_interleaved_int(conversion_state, (spx_int16_t*)internal_buffer, &conversion_data.input_frames_used, (spx_int16_t*)buffer, &conversion_data.output_frames_gen);
570 break;
571 default: error_message = "internal error: output_format is not convertable"; return ERROR;
572 }
573
574 if (error != 0) {
575 error_message = speex_resampler_strerror(error);
576 return ERROR;
577 }
578 #elif defined(HAVE_LIBSAMPLERATE)
579 conversion_data.data_in = (float*)internal_buffer;
580 conversion_data.data_out = (float*)buffer;
581 if (pitch_handled_by_decoder) {
582 conversion_data.src_ratio = (output_rate*1.0) / input_rate;
583 }
584 else {
585 conversion_data.src_ratio = (output_rate*STANDARD_PITCH *1.0) / (input_rate*pitch*1.0);
586 }
587 conversion_data.end_of_input = (wrapped_decoder->IsFinished()) ? 1 : 0;
588
589 //Now let libsamplerate filter the data
590 error = src_process(conversion_state, &conversion_data);
591
592 if (error != 0) {
593 error_message = src_strerror(error);
594 return ERROR;
595 }
596 #endif
597
598 total_output_frames -= conversion_data.output_frames_gen;
599 buffer += conversion_data.output_frames_gen*nr_of_channels*output_samplesize;
600
601 if ((conversion_data.input_frames == 0 && conversion_data.output_frames_gen <= conversion_data.output_frames) || conversion_data.output_frames_gen == 0) {
602 finished = true;
603 //There is nothing left to convert - return how much samples (in bytes) are converted!
604 return length - total_output_frames*(output_samplesize*nr_of_channels);
605 }
606 }
607 return length;
608 }
609
610 #endif
611