mackron / dr_libs

Audio decoding libraries for C/C++, each in a single source file.
Other
1.24k stars 205 forks source link

how to use on simple hardware, STM32F411 ? #185

Closed alfsch1 closed 3 years ago

alfsch1 commented 3 years ago

Hello, first : great work (i think). target: a simple audio-player, act. can play wav ; direct output to DAC (AD1852 , maybe CS4398 next...); problem: how to use your lib's , straight and simple (too complex is out of my level...)? hardware: STM32F411, ARM4f core w. float, 100MHz, 100K RAM, 500K flash; i read from micro-SD-card, SDIO 4bit mode, and put data direct to the "play-buffer" , circular 2x 8kB; output L/R, I2S, int16 goes by DMA to the DAC; control is with IR remote, from my old Sony CD player, and i made similar control: play, pause, next and so on. Sound is quite good, better than everything i had before, so far , so good. working fine now, with wav format. So i think about MP3 and Flac playing. Can you give me little help, how to use libs in most simple way? i open file, fill (for example) 8kB buffer - and when callback from DMA coming, i have to fill 8kB (2K L/R int16) in under 40ms, with decoded audio. (with wav no problem, needs about 5ms to read 8K from SD and fill DMA-buffer). so i put in my main:

define DR_MP3_IMPLEMENTATION

define DR_FLAC_IMPLEMENTATION

include "dr_flac.h"

include "dr_mp3.h"

and when MP3 file found, open and read... but how much buffers , for in and out of decoder, and how to declare? (malloc etc. is not here, i think; its not a PC...). drmp3_read_pcm_frames_s16() is probably, what i need, but what is right buffer size ? a simple example would be fine, i think, it would help others also, to use your libs ; i searched , but didnt find useful example.

best regards Alfred

mackron commented 3 years ago

You just read however many PCM frames you need using drmp3_read_pcm_frames_s16(). That will give you exactly the number of PCM frames you requested, or less if the end of the file has been reached (never more). It should work exactly the same as dr_wav. There isn't really an optimal buffer size - just use whatever size works best for your situation. It sounds like you need to read 2048 PCM frames at a time? Just specify 2048 as the desired frame count in drmp3_read_pcm_frames_s16(). You might want to check the return value and then fill any leftover data with silence (all zeroes) - this will happen when you reach the end of the file. If you cannot do this in 40 milliseconds you might need to consider using another library.

For MP3, note that dr_mp3 actually wraps around another library called minimp3 which means there's some overhead. If you have extreme restrictions, you could consider using minimp3 directly to avoid that overhead, but the downside is that it's slightly harder to use.

Regarding your comment about no malloc(), something must be available or else nothing will compile: https://github.com/mackron/dr_libs/blob/343aa923439e59e7a9f7726f70edc77a4500bdec/dr_mp3.h#L2412 Heap allocations are used internally by both dr_mp3 and dr_flac.

alfsch1 commented 3 years ago

thanks for your fast response ! filling up zeroes at the end - is my last problem. :) (with wav play, i do it anyway, thats clear.) about malloc , i will look at the libs and all this, around the HAL system, maybe there is some kind of memory allocation. when i have more time, maybe Friday; after work in evening is not enough (for me) to really go on. really ugly is, first to get all perfect running and then to see: fast enough or not... i will write about or ask...

alfsch1 commented 3 years ago

Hi, now FLAC is also playing, only MP3 has a problem, sound seems missing some "blocks", making ill sound. maybe a pointer/counter off by one, or so. and : malloc is there, i underestimated the HAL libs... CPU load about 70...80%, without any optimizing by me. (maybe, i try..) so far, so good. i use the minimp3, as you told me; and https://github.com/astoeckel/libfoxenflac for flac. thanks for your tips.

mackron commented 3 years ago

No problem at all. Thanks for pointing out libfoxenflac - I wasn't aware of that one. I'll point people to that if dr_flac is not appropriate for them for whatever reason.