all repos — mgba @ 94cc48faf11e9065025c9529e19b670bd5dd0c2a

mGBA Game Boy Advance Emulator

Account for floating point error in resampling
Jeffrey Pfau jeffrey@endrift.com
Thu, 16 Jan 2014 02:12:35 -0800
commit

94cc48faf11e9065025c9529e19b670bd5dd0c2a

parent

4e88cc86d97f670d17533fa0293772264bd8d8d4

1 files changed, 12 insertions(+), 8 deletions(-)

jump to
M src/platform/sdl/sdl-audio.csrc/platform/sdl/sdl-audio.c

@@ -46,9 +46,10 @@ int32_t left[BUFFER_SIZE];

int32_t right[BUFFER_SIZE]; // toRead is in GBA samples + // TODO: Do this with fixed-point math int toRead = samples / context->ratio; while (samples > 0) { - int currentRead = BUFFER_SIZE >> 2; + int currentRead = BUFFER_SIZE >> 1; if (currentRead > toRead) { currentRead = toRead; }

@@ -57,22 +58,25 @@ toRead -= read;

unsigned i; for (i = 0; i < read; ++i) { context->drift += context->ratio; - while (context->drift >= 0) { + while (context->drift >= 1.f) { output->left = left[i]; output->right = right[i]; ++output; --samples; -#ifndef NDEBUG + context->drift -= 1.f; if (samples < 0) { - abort(); + return; } -#endif - context->drift -= 1.f; } } if (read < BUFFER_SIZE >> 2) { - memset(output, 0, toRead); - return; + if (samples > 1) { + memset(output, 0, samples * sizeof(struct StereoSample)); + return; + } else { + // Account for lost sample due to floating point conversion + toRead = 1; + } } } }