Simplest way to do sound generation from an mp3 file? - Page 1
Forum section: electronics / general

author: peter-h
author_rank: Super Contributor
timestamp: June 02, 2025, 02:44:56 pm
content: Am I right that one just needs a task (perhaps an RTOS task running every 1ms) which runs an mp3 decoder (which reads blocks from a flash file system; FatFS in my case) and stuffs the packets into a FIFO in RAM, and then you have a 48kHz (not 48.1) timer ISR which picks these up and stuffs them into a DAC?The DAC would be only 12 bit; no hifi needed. It is for audio annunciations.What would be the easiest to use freeware mp3 decoder, in C?Thank you in advance.

author: mariush
author_rank: Super Contributor
timestamp: June 02, 2025, 03:42:35 pm
content: libMAD is the first thing that comes to mind :https://www.underbit.com/products/mad/though it's 24 bit pcm output, fixed integer decoder ...If it's for audio annunciations, maybe consider Opus mono 11025 Hz - 16000 Hz, CELT mode only (opusenc --speech --downmix-mono --bitrate 16 input_file output_file for 16 kbps speech only opus

author: peter-h
author_rank: Super Contributor
timestamp: June 02, 2025, 04:09:36 pm
content: I would prefer mp3 and it is a great point that a high bitrate isn't needed in this case, so 32kbps probably. The packet rate also doesn't need to be 48k; 8k is possible. I'd prefer mp3 because it is more widely used and probably any mp3 editor can generate the required file.I certainly can't use an RTOS task to stuff the DAC because 1kHz is the max rate.

author: tszaboo
author_rank: Super Contributor
timestamp: June 02, 2025, 04:14:24 pm
content: The easiest for me was circuit python on the RP2040. Copy the MP3 to the file system, few lines of code to play it. Use I2S to output.I don't think it's really flexible though. For starters, it doesn't use the second core for decoding, so your main core was blocked often. And it's not very good for debugging, or understanding what was going on, it's a black box.And the code is not C.

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 02, 2025, 04:19:35 pm
content: Does your MCU not have an internal 12-bit DAC? Many newer STM32 do. But maybe you use a STM32F4 (IIRC) and I don't remember if they do.If so, you use a timer to trigger the DAC, certainly not in an ISR to feed the DAC manually, that would be extremely wasteful. Use a DMA channel for this. Then you need double buffering.If using an external DAC, you should be able to use a similar approach using DMA -> SPI and trigger data transfers with a timer, depending on your MCU, you may not need any ISR either and use only "hardware triggers".But if quality is of no real importance and your MCU doesn't have a DAC, I would suggest using PWM instead. Then it can be done similarly using DMA and no need for any ISR.In terms of encoding, I would recommend the Opus codec as well, it's significantly more lightweight and more than enough for your purpose. You could even go for some simple delta modulation, which is even more lightweight:https://en.wikipedia.org/wiki/Delta_modulation

author: DavidAlfa
author_rank: Super Contributor
timestamp: June 02, 2025, 04:35:52 pm
content: Which MCU? STM32 have free decoder lib (Spirit).It only needs few buffers and a data read callback (Basically, "Feed me!").https://github.com/deividAlfa/STM32F411-Black-pill-USB-WAV-MP3-playerRemember it only works with FPU! The CPU usage is very low, I could decode it at only 8MHz while DMA-feeding a i2s DAC and attending other tasks.

author: peter-h
author_rank: Super Contributor
timestamp: June 02, 2025, 07:26:14 pm
content: SW - yes 32F4 and it has the 12 bit DAC as I mentioned earlier. Indeed I have already made a waveform generator (for 400Hz synchro emulation) which uses a timer to trigger the DAC which then uses DMA to load itself from RAM, so no CPU loading at all, but the CPU does have to manage the table in RAM otherwise, in this case, the entire "piece of audio" will need to be RAM-resident.

author: aldi
author_rank: Contributor
timestamp: June 02, 2025, 07:32:56 pm
content: Used this one a while ago:https://github.com/lieff/minimp3

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 02, 2025, 08:02:11 pm
content: Since you use STM32Cube, you can have a look at their audio library, it includes a MP3 decoder:https://www.st.com/en/embedded-software/x-cube-audio.html

author: peter-h
author_rank: Super Contributor
timestamp: June 02, 2025, 08:05:46 pm
content: I find ST's code to be really convoluted and (for me) hard to understand.

author: zezo2030
author_rank: Contributor
timestamp: June 03, 2025, 11:07:58 pm
content: Quote from: peter-h on June 02, 2025, 02:44:56 pmHi petterthe best solution to use jieli mcu, it can use mp3,wav etc, also its very cheap, around 1$...like AC6082A2 mcu.https://www.zh-jieli.com/product/they use Codeblocks software with gcc.this is the github pagehttps://github.com/Jieli-Tech.programming toolhttps://www.aliexpress.com/item/1005004688181549.html

author: DavidAlfa
author_rank: Super Contributor
timestamp: June 04, 2025, 05:07:22 am
content: Peter, Spirit lib is dead easy to use, just check the code on github...

author: cv007
author_rank: Frequent Contributor
timestamp: June 04, 2025, 07:18:16 am
content: Unless/until you have a storage space limitation, the simple wav file is about as easy as it gets. Pick the lowest bitrate that sounds good, move the wav data from storage to dac via dma with no decoding needed. The code will be simple and the cpu will not break a sweat.

author: peter-h
author_rank: Super Contributor
timestamp: June 04, 2025, 07:34:06 am
content: Yes; I am looking at mp3 to save storage space. Otherwise one could just have the DAC values in the FLASH. WAW is about 10x bigger than even high quality mp3.

author: bson
author_rank: Unknown
timestamp: June 08, 2025, 10:00:43 pm
content: www.mp3-tech.orghas a bunch of decoder sources, although the license on some is questionable. MP3PlayLib looks pretty good, and layer 1,2 audio sources if not used could be removed - for just MP3. I'd dig deeper or ask what the license is if something is workable.

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 08, 2025, 10:13:30 pm
content: minimp3, as mentioned by aldi, seems to be a "popular" option that works well enough.

author: Smokey
author_rank: Super Contributor
timestamp: June 08, 2025, 10:33:25 pm
content: As always .. what are you trying to make? How many do you plan on making? What is your price point target?If you are just making one off thing for your self for hobby use then don't worry about optimizing stuff. Get a dedicated mp3 decoder chip or a micro that has a library and have at it.If you are making a product then then the above questions becomes really important to making a decision.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: June 09, 2025, 09:12:29 am
content: The easiest way is to buy an EUR 5 MP3 player. Maybe write some uC firmware to push it's buttons.MP3 needs quite a lot of brainpower to do the decoding. (In the old days, you'd need an 80486 to do it. the 80386 was not fast enough). Open source libraries that work on a microcontroller are a bit buggy, commercial stuff has their licensing. The most practical is probably still a dedicated decoder chip such as the VS1053.But why MP3? uSD cards have become so big that you can store many hours of audio in simple Wav format that you can stream directly to a DAC. But you will need a constant stream of I2S and probably need DMA to achieve this.And this is worth repeating:Quote from: Smokey on June 08, 2025, 10:33:25 pm

author: DavidAlfa
author_rank: Super Contributor
timestamp: June 09, 2025, 01:43:36 pm
content: With FPU, I've tested that 8MHz is enough...The 486 lacked it, software floats can be 2000 times slower or more..

author: peter-h
author_rank: Super Contributor
timestamp: June 09, 2025, 09:42:47 pm
content: I have a "commercial" product and am looking at adding some sound playback to it.That minimp3 module looks like it might do it.