Closed toybuilder closed 1 month ago
I filed this issue, as other people trying to get this working might get thrown off by this issue. I got lucky and the first MP3 file I tried worked. It was when I tried to use other MP3 files that I went down the rabbit hole. If someone tried with a "bad" MP3 file to begin with, they could have a much harder time.
Can you provide more information about the different MP3 files you have tried? Bitrate, etc.
On https://github.com/toybuilder/esp-arduino-i2s-audio/, I checked in good.mp3 and nogood.mp3 nogood.mp3 (192kbps) was recorded with Windows Sound Recorder. It doesn't work. That file was then re-encoded into good.mp3 (225kbps) by using an online conversion tool (https://restream.io/tools/mp3-converter).
Some other files that I tried and worked were 256kbps mp3's downloaded from pixabay.com. The Windows Sound Recorder files that fall failed were between 96kbps to 192kbps.
It occurred to me that I should just try removing the ID3 tag -- following the instruction found at https://www.frightideas.com/blog/post/removing-id3-tags, I made a copy of nogood.mp3 without ID3 tag (or other properties) and that worked. I checked that into the github repo that I mentioned earlier.
For MP3 playback we use this ESP-IDF component. Maybe you can post an issue there with your findings and they can do the necessary steps to update the code to play the bad mp3
I think the problem is in ESP_I2S.cpp itself.
If you look at the reference playback code, err=MP3Decode(...) does not result in an early exit of the decoding:
https://github.com/chmorgan/libhelix-mp3/blob/fb0c06b5cb964c50706c08b0c3bc6615f08d0ffe/testwrap/main.c#L156C3-L175C4 (specifically: https://github.com/chmorgan/libhelix-mp3/blob/fb0c06b5cb964c50706c08b0c3bc6615f08d0ffe/testwrap/main.c#L162)
but in ESP_I2S.cpp, it just exits prematurely:
(Disregard this comment. I now think it is wrong.)
I realized that I can re-build my program under Arduino with Core Debug Level: Verbose logging turned on, and I now see that the problem is err -6 (ERR_MP3_INVALID_FRAMEHEADER).
I will file the issue there. Thank you for pointing me to the libhelix-mp3 component.
It looks like I actually was on the right track originally... The issue is that MP3Decode will return when an ID3 tag contains bytes that look like a sync pattern for an MP3 frame, which does not result in a proper decoding of an MP3 frame header. MP3Decode then needs to be called again to resume searching for the next sync pattern.
I think maybe adding a note in the documentation for playMP3() is needed, informing the reader to remove ID3 tags from the source data AND/OR to properly handle skipping over the ID3 data.
FWIW, I was able to get it to work by making the following changes to ESP_I2S.cpp:
if (err) {
log_e("Decode ERROR: %d", err);
changes below
if (bytesAvailable) {
readPtr++;
bytesAvailable--;
}
//MP3FreeDecoder(decoder);
//return false;
changes above
} else {
MP3GetLastFrameInfo(decoder, &frameInfo);
configureTX(frameInfo.samprate, (i2s_data_bit_width_t)frameInfo.bitsPerSample, (i2s_slot_mode_t)frameInfo.nChans);
write((uint8_t *)outBuf, (size_t)((frameInfo.bitsPerSample / 8) * frameInfo.outputSamps));
}
} while (true);
@toybuilder Can I close this as asnwered?
I guess so. At least if others encounter the same issue, we've at least described what the problem and workaround are.
Board
ESP32 dev module
Device Description
ESP32-WROOM on custom board that has IIS decoder on board.
Hardware Configuration
N/A
Version
latest master (checkout manually)
IDE Name
Arduino
Operating System
Windows 11
Flash frequency
80MHz
PSRAM enabled
yes
Upload speed
921600
Description
When playing back MP3 files, some MP3 files will play, others will not. It might be related to the handling of ID3 tag as described in https://github.com/adafruit/Adafruit_MP3/issues/5 ?
Sample "bad" mp3 file at https://github.com/toybuilder/esp-arduino-i2s-audio/blob/main/nogood.mp3
Hardware is fine, as playback of other MP3 files work as expected.
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide