Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label wav. Show all posts
Showing posts with label wav. Show all posts

Saturday, 7 January 2012

MBED Example: How to play WAV sound file from SDCard using SPI and AnalogOut

.from: https://github.com/leroilion/mbed/blob/master/PlayMusic/Examples/PlayMusic/main.cpp

#include "mbed.h"
#include "SDFileSystem.h"
SDFileSystem sd(11, 12, 13, 14, "sd");
AnalogOut aout(18);
int main() {
int ch; //first read byte
int ch2; //second read byte
int dat; //combined 16 bit word for DtoA
FILE *fp = fopen("/sd/QI2.wav", "r"); //open File to read
if(fp == NULL) {error("Could not open file for write\n");}
while(!feof(fp)) { //Play the music until End Of File
dat = 0;
ch = fgetc(fp); //get first byte
ch2 =fgetc(fp); //get second byte
ch = ch << 8; //shift bits 8 places to left
dat = ch2 | ch; //combine bits to single 16 bit int
aout.write_u16((unsigned short)(dat)); //DtoA conversion
wait_us(88); //set for 11k sample rate
}
fclose(fp); //close the file
}

MBED Example: Read RIFF WAV Header

.info: https://github.com/leroilion/mbed/blob/master/PlayMusic/Examples/HeaderReader/main.cpp

#include "mbed.h"
#include "SDFileSystem.h"
#include "MobileLCD.h"
MobileLCD lcd(5, 6, 7, 8, 9);
SDFileSystem sd(11, 12, 13, 14, "sd");
int main() {
lcd.background(0x0000FF);
    lcd.cls();
lcd.printf("Header Reader");
lcd.locate(0,2);
int ch;
FILE *fp = fopen("/sd/QI2.wav", "r");
if(fp == NULL) {
error("Could not open file for write\n");
lcd.printf("Could not open file for read");
}
for(int i = 1; i < 5; i++) { //"RIFF"
ch = fgetc(fp);
lcd.printf("%c ", ch);
if(i == 4) {lcd.locate(0,3);
}
}
for(int i = 5; i < 9; i++) { //Chunk size
ch = fgetc(fp);
lcd.printf("%x ", ch);
if(i == 8) {lcd.locate(0,4);}
}
for(int i = 9; i < 13; i++) { //"WAVE"
ch = fgetc(fp);
lcd.printf("%c ", ch);
if(i == 12) {lcd.locate(0,5);}
}
for(int i = 13; i < 17; i++) { //ID = "fmt"
ch = fgetc(fp);
lcd.printf("%c ", ch);
if(i == 16) {lcd.locate(0,6);}
}
for(int i = 17; i < 21; i++) { //Chunk Size
ch = fgetc(fp);
lcd.printf("%x ", ch);
if(i == 20) {lcd.locate(0,7);}
}
for(int i = 21; i < 23; i++) { //Audio format
ch = fgetc(fp); //1 = Uncompressed
lcd.printf("%x ", ch);
if(i == 22) {lcd.locate(0,8);}
}
for(int i = 23; i < 25; i++) { //Channels
ch = fgetc(fp); //1 = mono, 2 = stereo
lcd.printf("%x ", ch);
if(i == 24) {lcd.locate(0,9);}
}
for(int i = 25; i < 29; i++) { //Sample Rate
ch = fgetc(fp);
lcd.printf("%x ", ch);
if(i == 28) {lcd.locate(0,10);}
}
for(int i = 29; i < 33; i++) { //Byte Rate
ch = fgetc(fp);
lcd.printf("%x ", ch);
if(i == 32) {lcd.locate(0,11);}
}
for(int i = 33; i < 35; i++) { //Align
ch = fgetc(fp);
lcd.printf("%x ", ch);
if(i == 34) {lcd.locate(0,12);}
}
for(int i = 35; i < 37; i++) { //Bits per Sample
ch = fgetc(fp);
lcd.printf("%d ", ch);
if(i == 36) {lcd.locate(0,13);}
}
for(int i = 37; i < 41; i++) { //"data"
ch = fgetc(fp);
lcd.printf("%c ", ch);
if(i == 40) {lcd.locate(0,14);}
}
for(int i = 41; i < 45; i++) { //data chunk size
ch = fgetc(fp);
lcd.printf("%x ", ch);
}
fclose(fp); //close the file
lcd.locate(0,15);
lcd.printf("file closed");
}