I'm trying to play wav or mp3 files from an SD card connected to ESP32. I'm using DAC LM385 and a speaker connected to it. How can I make this work? I couldn't find any library that would allow me to do it simply and for many audio files. The only solution that I saw was the one that uses wav audio in hex form and XT Dac Audio library.
The DFRobot MP3 player is popular and seems to work well, with any Arduino.
It takes quite a bit of sophisticated code to play MP3 files, and while that has been implemented on ESP32, it would still be quite a challenge for someone without a lot of experience.
It is not difficult to read a .wav file and code has been posted on the web for that. You need to process the header correctly, then output data at the correct sample rate to a suitable DAC/audio amplifier.
The LM385 IC is not a DAC, and most DACs can't interface directly to a speaker. Please post a link to what you actually have.
For MP3 you'd need a library for the decoder (decompressor). I don't know if one exists or if the EXP is powerful enough to decode in real time.
WAV files are pretty straight-forward, but there are variations in sample rate, bit depth, and number of channels that make it a little complicated unless you only have one WAV format and you know it in advance.
Basically, it's just a header with all of that information followed by a series of samples, each one representing the height/amplitude of the WAV, one sample at a time.
Send those values to the DAC at the right sample rate and you get audio-out!
Most wave files are signed (negative & positive values like the real waveform) but most DACs can't put-out negative voltages so the data has to be offset/biased to half the range of the DAC. (The DC bias can be filtered-out with a series capacitor.)
8-bit WAV files are unsigned (positive values only).
The data has to be scaled to match the DAC. If you have an 8-bit DAC you have to send it 8-bit data.
Another complication is that the Arduino doesn't have an operating system, so no standard file system so reading/writing files is "limited".
Thanks guys for all replies! I'm using lm385 it's a simple amplifier, not a Dac (apologies for the mistake). I just want to play simple WAV files (prepared in Audacity to match 8bits) from the SD card. Sounds simple but I can't find any tutorial that would show how to do it.
The library docs make it clear that TMPpcm does not support ESP32.
I suggest instead to use a supported processor for this simple task.
A 3$ eBay Arduino Pro Mini will work.
Or, search for "esp32 play .wav file", which turns this up as hit #2:
Ok, so what are the WAV files? Music? Before you commit to this LM3eighty-probably-six amplifier, if you don't have experience with such a chip I don't mind telling you they are very underwhelming.
What Arduino do you have and what are you hoping to achieve?
Edit, the ESP32, got it.
So I tried a few different ways of audio with Arduino. The LM386 was greeting card quality. The Adafruit ones such as the Max9744 were...ok... electronic toy quality.
Then I spotted these:
They (the Kinter Co, I guess?) have a few class D and T options and these are excellent. You can hear them. I found them way better than any other low price, easy install option I had tried with Arduino before. I like an Uno and the Adafruit Wave Shield, I'm not sure if they still make them. I would ideally choose an option that includes a 3.5mm stereo (even if it's running dual mono) minijack, then get a 3.5mm stereo minijack to RCA cable, plug into an amp such as the Kinter and you have good enough for shop music quality.
One drawback, you have to supply your own +12V power adapter, but they aren't hard to come by in the retired devices bin or a local charity shop.
Want to upgrade even more? Similar to the Kinter, is the Fosi Class D stereo amp, with Bluetooth. I have one of these and it's solid. Better sound quality than the Kinter and plenty loud enough to groove to. Best of all, when you want options, flick the switch and connect Spotify from your phone for an easy stereo setup anywhere at home.
No I do not sell these for Amazon or anything, but as a stereo guy, the Fosi is the most very solid choice for the price.
In reply #10, all the gibberish in the links you posted, including and immediately following the "?" character, are trackers identifying your personal browsing, search and shopping history.
It is not a bad idea to remove that potentially embarrassing data, before posting on a public forum. The link will still work.
But if a wav file is too large, larger than the max buffer size of ESP32, how shall I do? Shall I split the wav file into several small pieces, and feed every small piece of data to buffer every time after last buffer has been played over?