Go Down

Topic: Direct WAV playback from SD: The Beginnings of a Library... (Read 1 time) previous topic - next topic

Jun 21, 2012, 05:23 am Last Edit: Jun 21, 2012, 05:50 am by TMRh20 Reason: 1
I have started writing a simple library for playback of wav files directly from an SD card, since I have been unable to find once that does this so far. I am testing it using an Arduino Mega, but it should work on other boards as well.

I seem to be able to play files up to 16khz or so (sample rate), but am not quite sure about the limits or capabilities of the timers, or read speed of an SD card. Unless somebody here just happens to know, I assume it will take a bit of testing and datasheet reading to figure out exactly....

I am also trying to figure out a method to run this completely using interrupts, so that other functions could maybe run while the sound is playing, but am not exactly sure how to both control the buffering and playback together or if I can do it all using interrupts.

This is based on the example at http://arduino.cc/playground/Code/PCMAudio

Here is the source code, any advice would be great! :


Functional Library with example sketch: https://sites.google.com/site/tmrh20sprojects/home/tmr_pcm/TMRpcm.zip?attredirects=0&d=1

Blog post: http://tmrh20.blogspot.ca/2012_06_01_archive.html


I figured out a way to run the audio using a single timer, which should allow compatibility with boards other than the Mega. Audio pin is now 11(Mega) or 9(Uno/Nano, etc) but is only tested on a Mega so far.

Buffering is now an individual byte loaded via an interrupt, removing the need for a memory buffer, and allowing other functions to run while the sound plays.

Also added 'volume control' which raises or lowers the average duty cycle.

see new links/info @ http://tmrh20.blogspot.ca/2012_06_01_archive.html

The main remaining problem is the max sample rate, which appears to be about 20 to 21khz currently. I am not sure if this is limited by the processing speed, the SD read speed, or both. Functionality is ok currently, so will leave that inquiry to the forum, or until time allows further testing...


Jun 25, 2012, 07:10 pm Last Edit: Jun 25, 2012, 07:12 pm by codlink Reason: 1
I am interested in your library.  Keep it up!

Curious, would this be for any SD WAV board or a specific one?


This should work with any SD card that is compatible with the standard SD library that comes with the Arduino IDE.

RIght now I am using a generic SD card module that I bought off EBay. The audio output is handled by an old 8-ohm speaker from a broken radio, so it is not super loud, but amplification could be applied for more volume if required.

It should also work with most Arduino boards, but I have only had the chance to test it with a Mega 2560.


Awesome.  Asked because I don't know which SD board I am getting... 

Bookmarked thread.

Update: The prev version had some issues, but I managed get it working fully asynchronously now using a single timer and nested interrupts to both buffer data and control the pwm signal. (Music plays apart from the main code, allowing the main loop to run while music is playing, and allowing starting/stopping of the music). A full explanation is on my blog which is mentioned in a previous post.

There is still some tweaking to do, but the basic commands are there for starting, stopping and volume. I also still have to test on a board other than a mega but think it should work on most.

It is about as simple as I think I can make it, so the next thing would be to build in a bit more functionality, but I'm thinking it might be worth looking into the difficulty involved in decoding mp3 files first.
Here is a link to the current libary package for anybody who would like to try it out and/or provide some feedback:


Been a while, but work on this library has slowed to a crawl since it works fairly well at this point. Thought it would be worth an update:


    PCM/WAV playback direct from SD card to attached speaker/amplifier

    Asynchronous Playback: Completely interrupt driven. Allows main loop to execute instructions while playback occurs.

    Playback uses a single timer (Timer 1)

    Supported formats: WAV files, 8-bit, 8 to 20khz Sample Rate (autodetection), mono

    Supported devices: Arduino Uno, Nano, Mega, etc.

    Files easily converted using iTunes or other software:

    iTunes Conversion:
    Click > Edit > Preferences > Import Settings
    Change the dropdown to WAV Encoder and Setting: Custom > 16.000kHz, 8-bit, Mono
    Right click any file in iTunes, and select "Create WAV Version"
    Copy file to SD card using computer

    Contains optional proof-of-concept add-on library for RF (Wireless) audio streaming using NRF24L01+ radio modules. See readme files for details.

The current version can be found here: https://github.com/TMRh20/TMRpcm

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131