From 2dac08ef4edb7437bdb4db6acf76642814e8e053 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 21 Oct 2017 21:15:59 -0700 Subject: [PATCH] Fix mono MP3 decoding causing choppy output Fixes issue #5 about choppy audio on low bitrate, mono MP3s. The number of 32-sample elements to generate per MP3 data frame was set to 36 as that was what I saw on all my source files. However, on mono files only half the number of samples are to be generated and you'd end up with the synthesizer bands running out of energy and have periods of 0-samples between proper data. Now simply check each decoded frame for the proper NSMAX value and only generate that many. Tested on force.mp3 in the issue referenced. --- src/AudioGeneratorMP3.cpp | 4 +++- src/AudioGeneratorMP3.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 0636940..bca1795 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -28,6 +28,7 @@ AudioGeneratorMP3::AudioGeneratorMP3() output = NULL; buffLen = 2048; buff = NULL; + nsCountMax = 1152/32; } AudioGeneratorMP3::~AudioGeneratorMP3() @@ -106,6 +107,7 @@ bool AudioGeneratorMP3::DecodeNextFrame() ErrorToFlow(); // Always returns CONTINUE continue; } + nsCountMax = MAD_NSBSAMPLES(&frame.header); return true; } inInnerDecode = false; @@ -116,7 +118,7 @@ bool AudioGeneratorMP3::DecodeNextFrame() bool AudioGeneratorMP3::GetOneSample(int16_t sample[2]) { - if ( (samplePtr >= synth.pcm.length) && (nsCount >= 1152/32) ) { + if ( (samplePtr >= synth.pcm.length) && (nsCount >= nsCountMax) ) { if (!DecodeNextFrame()) return false; samplePtr = 9999; nsCount = 0; diff --git a/src/AudioGeneratorMP3.h b/src/AudioGeneratorMP3.h index be32f46..f7ff464 100644 --- a/src/AudioGeneratorMP3.h +++ b/src/AudioGeneratorMP3.h @@ -49,6 +49,7 @@ class AudioGeneratorMP3 : AudioGenerator struct mad_synth synth; int samplePtr; int nsCount; + int nsCountMax; bool inInnerDecode; // The internal helpers