MCPcopy Create free account
hub / github.com/EasyRPG/Player / FillBufferDifferentRate

Method FillBufferDifferentRate

src/audio_resampler.cpp:498–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

496}
497
498int AudioResampler::FillBufferDifferentRate(uint8_t* buffer, int length) {
499 const int input_samplesize = AudioDecoder::GetSamplesizeForFormat(input_format);
500 const int output_samplesize = AudioDecoder::GetSamplesizeForFormat(output_format);
501 //The buffer size has to be a multiple of a frame
502 const int buffer_size=sizeof(internal_buffer) - sizeof(internal_buffer)%(nr_of_channels*((input_samplesize>output_samplesize) ? input_samplesize : output_samplesize));
503
504 int total_output_frames = length / (output_samplesize*nr_of_channels);
505 int amount_of_samples_to_read = 0;
506 int amount_of_samples_read = 0;
507
508 uint8_t * advanced_input_buffer = internal_buffer;
509 int unused_frames = 0;
510 int empty_buffer_space = 0;
511 int error = 0;
512
513 #ifdef HAVE_LIBSPEEXDSP
514 spx_uint32_t numerator = 0;
515 spx_uint32_t denominator = 0;
516 #endif
517
518 while (total_output_frames > 0) {
519 //Calculate how much frames of the last cycle are unused - to reuse them
520 unused_frames = conversion_data.input_frames - conversion_data.input_frames_used;
521 empty_buffer_space = buffer_size / output_samplesize - unused_frames*nr_of_channels;
522
523 advanced_input_buffer = internal_buffer;
524
525 //If there is still unused data in the input_buffer order it to the front
526 for (int i = 0; i < unused_frames*nr_of_channels*output_samplesize; i++) {
527 *advanced_input_buffer = *(advanced_input_buffer + empty_buffer_space*output_samplesize);
528 advanced_input_buffer++;
529 }
530 //advanced_input_buffer is now offset to the first frame of new data!
531
532 //ensure that the input buffer is not able to overrun
533 amount_of_samples_to_read = (input_samplesize > output_samplesize) ? (empty_buffer_space*output_samplesize) / input_samplesize : empty_buffer_space;
534
535 //Read as many frames as needed to refill the buffer (filled after the conversion to float)
536 if (amount_of_samples_to_read != 0) {
537 switch (output_format) {
538 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;
539 #ifdef HAVE_LIBSPEEXDSP
540 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;
541 #endif
542 default: error_message = "internal error: output_format is not convertable"; return ERROR;
543 }
544 if (amount_of_samples_read < 0) {
545 error_message = wrapped_decoder->GetError();
546 return amount_of_samples_read; //error occured
547 }
548 }
549 //Now we have a prepared full buffer of converted values
550
551 //Prepare the source data
552 conversion_data.input_frames = amount_of_samples_read / nr_of_channels + unused_frames;
553 conversion_data.output_frames = total_output_frames;
554
555 #if defined(HAVE_LIBSPEEXDSP)

Callers

nothing calls this directly

Calls 5

DecodeAndConvertFloatFunction · 0.85
DecodeAndConvertInt16Function · 0.85
GetErrorMethod · 0.80
getMethod · 0.45
IsFinishedMethod · 0.45

Tested by

no test coverage detected