Files
ESP8266Audio/src/AudioGeneratorMP3.h
T
Earle F. Philhower, III 2dac08ef4e 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.
2017-10-21 21:15:59 -07:00

65 lines
1.7 KiB
C++

/*
AudioGeneratorMP3
Wrap libmad MP3 library to play audio
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _AUDIOGENERATORMP3_H
#define _AUDIOGENERATORMP3_H
#include "AudioGenerator.h"
#include "libmad/config.h"
#include "libmad/mad.h"
class AudioGeneratorMP3 : AudioGenerator
{
public:
AudioGeneratorMP3();
virtual ~AudioGeneratorMP3() override;
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
virtual bool loop() override;
virtual bool stop() override;
virtual bool isRunning() override;
bool SetBufferSize(int sz);
protected:
int buffLen;
unsigned char *buff;
int lastReadPos;
unsigned int lastRate;
int lastChannels;
// Decoding bits
struct mad_stream stream;
struct mad_frame frame;
struct mad_synth synth;
int samplePtr;
int nsCount;
int nsCountMax;
bool inInnerDecode;
// The internal helpers
enum mad_flow ErrorToFlow();
enum mad_flow Input();
bool DecodeNextFrame();
bool GetOneSample(int16_t sample[2]);
};
#endif