Let's Make Robots!

Wave file player program for adding quality sound to a robot.

Plays wave files from a AVR based MCU with very little external hardware!

Last year I wanted to add sound to a bot so I wrote a program that plays wave files.  I later rewrote it so that it would be completely interrupt driven so that I could have it play audio files while doing other tasks.  While this was writhen to run on the BahBots controller it can probably modified to also work on an Arduino Mega.

Features:

  • Plays 8bit wave formated audio files at up to 22ksps (stereo) or up to 44ksps (mono)
  • Works on any AVR Mega part with 4k RAM or more.
  • Used double buffering to overcome FAT32 file IO latency.
  • Uses PWM on Timer2 for Digital to Analog Conversion (pins OC2A & OC2B)
  • Uses Timer1 for sample updates
  • Needs very few (or no) external parts except for the amplifier and speaker
  • Used attached MicroSD card for wave files (FAT32 formated)
  • Allows for many hours of audio files.
  • Written in Bascom-AVR compiled Basic

Hardware used for this example:

  • Bahbots controller (see: http://letsmakerobots.com/node/23310 )
  • 16x2 LCD Display for showing filename of the current wave file.
  • Small home made audio amp based on an LM386 with a cheap speaker.
  • 7.2volt Li Ion battery pack

When using the code there are two main commands, PlayWaveFile and GetWaveFileInfo.  All that is needed is to play the file is the command:

PlayWaveFile “my_sound.wav”

The code will then start playing the wave file but also continue to the next line of code while its playing the file.

PlayWaveFile will also call GetWaveFileInfo when it starts playing a new file but you can use GetWaveFileInfo prior to playing the file so that you can get info about the file (I.E. Stereo or mono, Bits per second and more) before playing it. 

There is a significant amount of CPU utilization when playing a stereo wave file but if you use mono and a lower bit rate (16k is good) then there is a good amount of CPU left for other tasks.  Keep in mind not to bind the cpu in an interrupt or it could affect the audio quality!

Since it uses Timer2 for the PWM DAC it is not possible to play 16bit audio files.  It is very easy to convert them to 8bits using Microsoft’s Sound Recorder program.  Just load the original wave file, then select Save As and press the Format Change button at the bottom (make sure to use PCM as the format).  You can select bit rates and stereo or mono.  If using an MP3 as the original file, you will also need to first convert them to the wav file format and then use the Sound Recorder to go from 16bits to 8 bits.  You can use something like Audacity for this.

The program code can me found here:  http://bahbots.com/forum/viewtopic.php?f=2&t=2

Here are is a pic of the Controller showing the MicroSD and also the LM386 amplifier.

  

Let me know what people think and if modifications could be made to make it more useful.

-Glen

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I want to add sounds to my robot too. I was wonering if I made the same setup would I be able to acomplish the same effect except with an arduino uno?

Thank you for this great post,

-Mark

Mark, it might be possible but the Arduino only has 1 or 2k of ram.  This may not be enough for the needed fat file access to the SD card and also the needed double buffering required for clitch free playback.  I wrote this with Bascom Basic and used a mega1284p which has 16k of ram.  Again it may be possible, but would depend on ram usage by the fat file system drivers.  Probably not on the Mega168 though.

You can see the code here: http://bahbots.com/forum/viewtopic.php?f=2&t=2

-Glen

what would be really cool is if a separate MCU could divide up each song by a graphic EQ, then transmit the modified sound files to separate xBee modules so you can have wireless surround sound, albeit slow

so if I understand correctly, your reading a wave file, which as I recall, yields sequential values ranging from -128 to +128 (or similar), and your translating that into pwm pulses to the amp/speaker.   and of course with the xbee, you could endlessly stream to it.   that sounds like a great thing you have there.

 

Paul, the player gets the wave file from the MicroSD card and not the xBee.  The xBee is there for over-the-air bootloading and other projects.  It has no real function for the Wave file player.  Also, xBees can only transmit at a maximum of 115200 baud or about 11.5k bytes per second which is much slower then needed for good quality sound. 

I added some more pics that I hope make this a little clearer.  Let me know if there are any other questions.

-Glen

I realize your not using the xbee for reception, but you could receive new music that way. It wouldn't have to be at playable speeds. You could que up new songs over time and store them to the sd card, etc.