Pages: [1]   Go Down
Author Topic: Direct WAV playback from SD: The Beginnings of a Library...  (Read 1269 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 6
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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! :

https://sites.google.com/site/tmrh20sprojects/home/tmr_pcm/TMRpcm.h?attredirects=0&d=1
https://sites.google.com/site/tmrh20sprojects/home/tmr_pcm/TMRpcm.cpp?attredirects=0&d=1

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
« Last Edit: June 20, 2012, 10:50:43 pm by TMRh20 » Logged

Offline Offline
Jr. Member
**
Karma: 6
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Update:

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...
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 688
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am interested in your library.  Keep it up!

Curious, would this be for any SD WAV board or a specific one?
« Last Edit: June 25, 2012, 12:12:07 pm by codlink » Logged

//LiNK

Offline Offline
Jr. Member
**
Karma: 6
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!

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.
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 688
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bookmarked thread.
Logged

//LiNK

Offline Offline
Jr. Member
**
Karma: 6
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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:

https://sites.google.com/site/tmrh20sprojects/home/tmr_pcm/interruptsv2/TMRpcm.zip?attredirects=0&d=1
Logged

Offline Offline
Jr. Member
**
Karma: 6
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Features:

    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

Logged

Pages: [1]   Go Up
Jump to: