64 lines
2.3 KiB
C
64 lines
2.3 KiB
C
/*
|
|
* speaker_pcm
|
|
*
|
|
* Plays 8-bit PCM audio on pin 11 using pulse-width modulation (PWM).
|
|
* For Arduino with Atmega168 at 16 MHz.
|
|
*
|
|
* Uses two timers. The first changes the sample value 8000 times a second.
|
|
* The second holds pin 11 high for 0-255 ticks out of a 256-tick cycle,
|
|
* depending on sample value. The second timer repeats 62500 times per second
|
|
* (16000000 / 256), much faster than the playback rate (8000 Hz), so
|
|
* it almost sounds halfway decent, just really quiet on a PC speaker.
|
|
*
|
|
* Takes over Timer 1 (16-bit) for the 8000 Hz timer. This breaks PWM
|
|
* (analogWrite()) for Arduino pins 9 and 10. Takes Timer 2 (8-bit)
|
|
* for the pulse width modulation, breaking PWM for pins 11 & 3.
|
|
*
|
|
* References:
|
|
* http://www.uchobby.com/index.php/2007/11/11/arduino-sound-part-1/
|
|
* http://www.atmel.com/dyn/resources/prod_documents/doc2542.pdf
|
|
* http://www.evilmadscientist.com/article.php/avrdac
|
|
* http://gonium.net/md/2006/12/27/i-will-think-before-i-code/
|
|
* http://fly.cc.fer.hr/GDM/articles/sndmus/speaker2.html
|
|
* http://www.gamedev.net/reference/articles/article442.asp
|
|
*
|
|
* Michael Smith <michael@hurts.ca>
|
|
*/
|
|
|
|
/*
|
|
* The audio data needs to be unsigned, 8-bit, 8000 Hz, and small enough
|
|
* to fit in flash. 10000-13000 samples is about the limit.
|
|
*
|
|
* sounddata.h should look like this:
|
|
* const int sounddata_length=10000;
|
|
* const unsigned char sounddata_data[] PROGMEM = { ..... };
|
|
*
|
|
* You can use wav2c from GBA CSS:
|
|
* http://thieumsweb.free.fr/english/gbacss.html
|
|
* Then add "PROGMEM" in the right place. I hacked it up to dump the samples
|
|
* as unsigned rather than signed, but it shouldn't matter.
|
|
*
|
|
* http://musicthing.blogspot.com/2005/05/tiny-music-makers-pt-4-mac-startup.html
|
|
* mplayer -ao pcm macstartup.mp3
|
|
* sox audiodump.wav -v 1.32 -c 1 -r 8000 -u -1 macstartup-8000.wav
|
|
* sox macstartup-8000.wav macstartup-cut.wav trim 0 10000s
|
|
* wav2c macstartup-cut.wav sounddata.h sounddata
|
|
*
|
|
* (starfox) nb. under sox 12.18 (distributed in CentOS 5), i needed to run
|
|
* the following command to convert my wav file to the appropriate format:
|
|
* sox audiodump.wav -c 1 -r 8000 -u -b macstartup-8000.wav
|
|
*/
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
void startPlayback(unsigned char const *data, int length);
|
|
void stopPlayback();
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|