Because we are allocating objects totaling over 80% of the ESP8266 memory
space, we are very sensitive to memory fragmentation. While we can control
our own allocs, the IP and SDK can allocate any size, at any time, which
can cause us to not be able to find the large contiguous memory areas we need
for the codes.
Work around this by preallocating, before anything else, buffer memory and
never freeing it. Pass this memory into the codec and it will allocate from
this block and be guaranteed to always succeed.
This may fix the last of the hangs on the webradio/http streaming, which only
seemed to happen after a stop/start cycle.
When the input file(stream) doesn't open, you could end up calling
mp3::stop() which would try and mad_finish structures that were
not yet initialized. Now carry a flag when these are set, and only
_finish when it's set.
Valgrind found another uninitialized use. BitsPerSample was not being
called by the LIBMAD decoder, so it was possible to start out in a bad
state. Now always set it to 16, the decoder native output size.
Channels are initialized to 2, but this setting is always reset on
a per-frame basis anyway.
As seen in issue #21, the MP3 decoder would barf on a single lost sync
error, even though it was not actually a fatel error.
Rewrote the logic to include another Input() on DNF failure, and only die
when Input() actually fails. Seems to fix the problem in issue #21 and also
did not break the WebRadio.
DecodeNextFrame had a leftover while(1) in the inner loop without any
additional Input() calls, which could lead to an infinite loop on an
error condition. Remove that unneeded loop. Addresses some of the
stuff found in issue #21
All objects can have a metadata and/or status report callback once created.
They will call the source program with the parsed ID3 tag, ICY StreamTitle,
or text and numeric error ID of any interesting event. It is up to the end
application to figure out what to do with this. Normally, you'd just print
it to the LCD or Serial port. But it is possible to detect, say, a buffer
overflow and then switch to a lower bitrate stream for example.
Adds a virtual loop() method to the file input and audio output objects.
By default does nothing, but it can be used to prefill buffers/etc. in the
case of a buffered input, or to update a volume meter on an output, etc.
The AudioGenerators all now call it as part of their own loop()s, so there
is no user intervention required to make it work.
Thanks to @armSeb for the idea with his SPI RAM code.
MP3 maximum frame size of 1441 bytes, and now that there is a real
buffering class available it makes no sense to include additional space
in ths decoder. Reduce to the minimum to save memory in default case.
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.