Go Down

Topic: Playing .wav sound through Arduino Uno (Read 7380 times) previous topic - next topic

dmc0162

Jun 21, 2012, 04:48 am Last Edit: Jun 21, 2012, 05:02 am by dmc0162 Reason: 1
For one of the functions of my project, I was wondering if it is possible for my Arduino Uno to play a very simple and very short .wav through a small speaker.  I found this website which provides a program to encode the .wav at a very low bit rate to conserve memory:

http://www.romanblack.com/picsound.htm


I have put my file into this program, and I have the option to export a few different ways, and I was wondering which will be the proper file for use in my Arduino code:

These are my options:
Export binary file .BTC format
Export PIC assembler file .ASM
Export PIC MkroC file .C

I would assume since we program the arduino basically in C language that I would use the last one.  


When I convert the file into a .C file, I get code that looks something like this:


//=====================================================
// AUTO CREATED FILE made by Windows BTc Sound Encoder
// v2.0    Copyright 2002-2008 - Roman Black
//
// Hippyware.  
// www.RomanBlack.com
//=====================================================

// File Details:
// Size 142336 bits     (17792 bytes)
// Sound encoded at 44100 bits/sec
// using BTc16 1bit Algorithm to be decoded on
// the following circuit:
//
//
//             R = 1596 ohms  
//  
// Digital -----------R-----*----- Analogue  
//                          |      out  
//                          |    
//                          |    
//                          C = 0.22 uF    
//                          |    
//                          |    
//                          |    
//                         Gnd    
//
//
//=====================================================
// Bitstream data is MikroC .C table format,
// in 'functions' of 2048 bytes with a .C 'return' as
// the last of every 256 byte block. At the end of every
// 2048 bytes the C compiler inserts its own return.
//
// Bits are played from left to right, from ms_bit to
// ls_bit.
//=====================================================
//--------------------------------------------------
void sound_data1()    org (1 * 256)
{
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;
  asm retlw 0xAA ;

....... and so on.


My question is, what programming function would I actually use in my code to send this sound signal to one of the pins. Thanks

TMRh20

The info at http://arduino.cc/playground/Code/PCMAudio is a good starting point for trying to play sound clips, and would probably work with that type of file given the correct modifications.

The above reference uses timers built into the ATMega chips to create a PWM signal. To learn how to do this you pretty much have to read the datasheet for the chips, since it writes directly to the registers of the chip instead of using the basic digitalWrite() type functions.

This is pretty much the only way I have found to make the Arduino play back sound files with reasonable quality.

Nick Gammon

Quote
// Size 142336 bits     (17792 bytes)


The Arduino Uno has 2048 bytes of memory, so there is no way that will fit. Playing tunes with PWM is within its capabilities.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up