Bugfix: Resampler did not allocate enough space sometimes (rounding error)

This commit is contained in:
Johannes Schriewer 2024-03-01 01:31:03 +01:00
parent 463ff5869e
commit 240346b7da
4 changed files with 10 additions and 8 deletions

View file

@ -4,6 +4,7 @@
#include "audio.h"
#include "audio_internal.h"
#include "audio_filter_resample.h"
#include "deps/resampler/speex_resampler.h"
typedef struct _FilterResamplerContext {
@ -25,8 +26,7 @@ AudioPipelineStatus filter_resample_push(AudioPipelineElement *self, AudioBuffer
return PipelineError;
}
uint32_t buf_sz = (self->sample_rate / context->source_sample_rate) * buffer->buf_size;
uint32_t buf_sz = (((float)self->sample_rate / (float)context->source_sample_rate) * (float)buffer->buf_size) + 1;
// Realloc if buffer size mismatch
if ((context->outputBuffer != NULL) && (context->outputBuffer->buf_size < buf_sz)) {
@ -63,7 +63,7 @@ AudioPipelineStatus filter_resample_push(AudioPipelineElement *self, AudioBuffer
if (in_sz != (buffer->buf_size / self->channels / 2)) {
fprintf(stderr, "WARN: Processed %d samples of %d!\n", in_sz, (buffer->buf_size / self->channels / 2));
}
if (out_sz != (context->outputBuffer->buf_size / self->channels / 2)) {
if ((context->outputBuffer->buf_size / self->channels / 2) - out_sz > 2) {
fprintf(stderr, "WARN: Output only %d samples of %d!\n", out_sz, (context->outputBuffer->buf_size / self->channels / 2));
}
@ -98,7 +98,7 @@ AudioPipelineStatus filter_resample_link(AudioPipelineElement *self, AudioPipeli
if (source->sample_rate > 0) {
int err = 0;
context->st = speex_resampler_init(source->channels, source->sample_rate, self->sample_rate, 4, &err);
context->st = speex_resampler_init(source->channels, source->sample_rate, self->sample_rate, RESAMPLE_QUALITY, &err);
if (context->st == NULL) {
fprintf(stderr, "ERROR: Error initializing resampler: %d\n", err);
return PipelineError;

View file

@ -1,8 +1,8 @@
/* Make use of ARM4 assembly optimizations */
/* #undef ARM4_ASM */
#undef ARM4_ASM
/* Make use of ARM5E assembly optimizations */
/* #undef ARM5E_ASM */
#undef ARM5E_ASM
/* Enable NEON support */
#define USE_NEON /**/
@ -11,7 +11,7 @@
#define EXPORT __attribute__((visibility("default")))
/* Debug fixed-point implementation */
/* #undef FIXED_DEBUG */
#undef FIXED_DEBUG
/* Use C99 variable-size arrays */
#define VAR_ARRAYS /**/

View file

@ -559,7 +559,7 @@ static int update_filter(SpeexResamplerState *st)
for (i=st->nb_channels;i--;)
{
spx_uint32_t j;
spx_uint32_t olen = old_length;
spx_uint32_t olen; /* = old_length; */
/*if (st->magic_samples[i])*/
{
/* Try and remove the magic samples as if nothing had happened */

View file

@ -13,5 +13,7 @@
*/
AudioPipelineElement *audio_filter_resample(uint32_t output_sample_rate);
#define RESAMPLE_QUALITY 1
#endif /* AUDIOPIPELINE_AUDIO_FILTER_RESAMPLE_H__INCLUDED */