| 421 | } |
| 422 | |
| 423 | int AudioResampler::FillBufferSameRate(uint8_t* buffer, int length) { |
| 424 | const int input_samplesize = AudioDecoder::GetSamplesizeForFormat(input_format); |
| 425 | const int output_samplesize = AudioDecoder::GetSamplesizeForFormat(output_format); |
| 426 | //The buffer size has to be a multiple of a frame |
| 427 | const int buffer_size=sizeof(internal_buffer) - sizeof(internal_buffer)%(nr_of_channels*input_samplesize); |
| 428 | |
| 429 | int total_output_frames = length / (output_samplesize*nr_of_channels); |
| 430 | int amount_of_data_to_read = 0; |
| 431 | int amount_of_data_read = total_output_frames*nr_of_channels; |
| 432 | |
| 433 | int decoded = 0; |
| 434 | |
| 435 | if (input_samplesize > output_samplesize) { |
| 436 | //It is necessary to use the internal_buffer to convert the samples. |
| 437 | while (total_output_frames > 0) { |
| 438 | amount_of_data_to_read = buffer_size / input_samplesize; |
| 439 | |
| 440 | //limit amount_of_data_to_read in the last loop |
| 441 | amount_of_data_to_read = (amount_of_data_to_read > total_output_frames) ? total_output_frames : amount_of_data_to_read; |
| 442 | |
| 443 | switch (output_format) { |
| 444 | case AudioDecoder::Format::F32: |
| 445 | amount_of_data_read = DecodeAndConvertFloat(wrapped_decoder.get(), internal_buffer, amount_of_data_to_read, input_samplesize, input_format); |
| 446 | break; |
| 447 | #ifdef HAVE_LIBSPEEXDSP |
| 448 | case AudioDecoder::Format::S16: |
| 449 | amount_of_data_read = DecodeAndConvertInt16(wrapped_decoder.get(), internal_buffer, amount_of_data_to_read, input_samplesize, input_format); |
| 450 | break; |
| 451 | #endif |
| 452 | default: error_message = "internal error: output_format is not convertable"; return ERROR; |
| 453 | } |
| 454 | if (amount_of_data_read < 0) { |
| 455 | error_message = wrapped_decoder->GetError(); |
| 456 | return amount_of_data_read; //error occured |
| 457 | } |
| 458 | |
| 459 | //Copy the converted samples |
| 460 | for (int i = 0; i < amount_of_data_read*output_samplesize; i++) { |
| 461 | buffer[i] = internal_buffer[i]; |
| 462 | } |
| 463 | //Prepare next loop |
| 464 | total_output_frames -= amount_of_data_read; |
| 465 | decoded += amount_of_data_read; |
| 466 | buffer += amount_of_data_read*output_samplesize; |
| 467 | |
| 468 | //If the end of the decoder is reached (it has finished) |
| 469 | if (amount_of_data_read < amount_of_data_to_read) { |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | } |
| 474 | } else { |
| 475 | //It is possible to work inplace as length is specified for the output samplesize. |
| 476 | switch (output_format) { |
| 477 | case AudioDecoder::Format::F32: |
| 478 | decoded = DecodeAndConvertFloat(wrapped_decoder.get(), buffer, amount_of_data_read, input_samplesize, input_format); |
| 479 | break; |
| 480 | #ifdef HAVE_LIBSPEEXDSP |
nothing calls this directly
no test coverage detected