Difference between revisions of "SD card audio streaming"
From Gamebuino Wiki
(Created page with "The following code will stream an audio file off the SD card and play it out the Gambuino speaker. The comments at the top of the source file explain how to convert any regula...") |
|||
Line 21: | Line 21: | ||
#define PLAYBACK_FREQ 20000 | #define PLAYBACK_FREQ 20000 | ||
− | |||
#define SPEAKER 3 | #define SPEAKER 3 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#define BUFFER_SIZE 128 | #define BUFFER_SIZE 128 | ||
volatile unsigned char buffers[2][BUFFER_SIZE]; | volatile unsigned char buffers[2][BUFFER_SIZE]; | ||
Line 140: | Line 27: | ||
volatile unsigned char * currentPtr; | volatile unsigned char * currentPtr; | ||
volatile unsigned char * endPtr; | volatile unsigned char * endPtr; | ||
− | |||
File dataFile; | File dataFile; | ||
const int chipSelect = 10; | const int chipSelect = 10; | ||
void setup() { | void setup() { | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
pinMode(10, OUTPUT); | pinMode(10, OUTPUT); | ||
if (!SD.begin(chipSelect)) { | if (!SD.begin(chipSelect)) { | ||
− | |||
return; | return; | ||
} | } | ||
dataFile = SD.open("SAMPLE.RAW"); | dataFile = SD.open("SAMPLE.RAW"); | ||
− | |||
if (dataFile) { | if (dataFile) { | ||
loadBuffer(0); | loadBuffer(0); | ||
Line 170: | Line 46: | ||
Timer1.attachInterrupt(callback); | Timer1.attachInterrupt(callback); | ||
} | } | ||
− | |||
− | |||
− | |||
} | } | ||
void callback() | void callback() | ||
{ | { | ||
− | analogWrite(SPEAKER, (*currentPtr++) +128); | + | analogWrite(SPEAKER, (*currentPtr++) + 128); |
if (currentPtr >= endPtr) | if (currentPtr >= endPtr) | ||
{ | { | ||
Line 228: | Line 101: | ||
TCCR2B = TCCR2B & 0b11111000 | mode; | TCCR2B = TCCR2B & 0b11111000 | mode; | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
</pre> | </pre> |
Revision as of 2014-04-06T12:57:41
The following code will stream an audio file off the SD card and play it out the Gambuino speaker. The comments at the top of the source file explain how to convert any regular audio file (WAV, MP3 etc) into a stream of raw bytes ready for play.
A video of this demo in action can be found at http://youtu.be/4ZI_CQeKIio
// Gamebuino sound streaming sample - by Mark Feldman (aka "Myndale") // // Requires TimerOne library from http://playground.arduino.cc/Code/Timer1 // Uses 5110 display code from http://playground.arduino.cc/Code/PCD8544#.U0EYgvmSyDs // // This demo will look for a file called SAMPLE.RAW on the SD card containing raw // 8-bit signed samples intended to be played back at a frequency of 20kHz. // These files can be easily generated using the ffmpeg utility (which can be // downloaded at http://www.ffmpeg.org/) using the following command line parameters: // // ffmpeg -i your_sound_file.mp3 -f s8 -acodec pcm_s8 -ar 20000 -ac 1 -af "volume=5dB" SAMPLE.RAW #include <TimerOne.h> #include <SD.h> #define PLAYBACK_FREQ 20000 #define SPEAKER 3 #define BUFFER_SIZE 128 volatile unsigned char buffers[2][BUFFER_SIZE]; volatile int currentBuffer = 0; volatile unsigned char * currentPtr; volatile unsigned char * endPtr; File dataFile; const int chipSelect = 10; void setup() { pinMode(10, OUTPUT); if (!SD.begin(chipSelect)) { return; } dataFile = SD.open("SAMPLE.RAW"); if (dataFile) { loadBuffer(0); currentBuffer = 0; currentPtr = buffers[currentBuffer]; endPtr = currentPtr + BUFFER_SIZE; pinMode(SPEAKER, OUTPUT); setPwmFrequency(SPEAKER, 1); Timer1.initialize(1000000/PLAYBACK_FREQ); Timer1.attachInterrupt(callback); } } void callback() { analogWrite(SPEAKER, (*currentPtr++) + 128); if (currentPtr >= endPtr) { currentBuffer = 1-currentBuffer; currentPtr = buffers[currentBuffer]; endPtr = currentPtr + BUFFER_SIZE; } } void loop() { loadBuffer(1); while (currentBuffer != 1); loadBuffer(0); while (currentBuffer != 0); } void loadBuffer(int bufferNum) { for (int i=0; i<BUFFER_SIZE; i++) buffers[bufferNum][i] = dataFile.read(); } void setPwmFrequency(int pin, int divisor) { byte mode; if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 64: mode = 0x03; break; case 256: mode = 0x04; break; case 1024: mode = 0x05; break; default: return; } if(pin == 5 || pin == 6) { TCCR0B = TCCR0B & 0b11111000 | mode; } else { TCCR1B = TCCR1B & 0b11111000 | mode; } } else if(pin == 3 || pin == 11) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 32: mode = 0x03; break; case 64: mode = 0x04; break; case 128: mode = 0x05; break; case 256: mode = 0x06; break; case 1024: mode = 0x7; break; default: return; } TCCR2B = TCCR2B & 0b11111000 | mode; } }