USBAudio example using a microphone

Dependencies:   USBDevice mbed

Revision:
3:e6a29c83ac52
Parent:
0:539ec61e1fbb
Child:
4:bef3b485f22e
--- a/main.cpp	Fri Dec 16 17:12:56 2011 +0000
+++ b/main.cpp	Mon Dec 19 15:46:17 2011 +0000
@@ -1,20 +1,50 @@
 #include "mbed.h"
 #include "USBAudio.h"
+#include "SDFileSystem.h"
 
 extern "C" void HardFault_Handler() {
     error("Hard Fault!\n");
 }
 
 USBAudio audio(8000, 1, 0x74ac, 0x8788);
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
+AnalogIn mic(p20);
+DigitalOut p(p21);
 
-//USBHID hid;
-uint16_t buf[8];
+/* Wav file header data, for setting up the transfer protocol */
+short channels;
+long sampleRate;
+short wordWidth;
+
+int16_t buf[8];
 
 int main() {
-    for (int i = 0; i < 8; i++) {
-        buf[i] = (i % 2) ? -255 : 255;
+    FILE * fp;
+
+    fp = fopen("/sd/3.wav", "r");
+    if (fp == NULL) {
+        error("Could not open file for write\n");
     }
+
+    printf("test\r\n");
+    /* Parse wav file header */
+    fseek(fp, 22, SEEK_SET);
+    fread(&channels, 2, 1, fp);
+    fseek(fp, 24, SEEK_SET);
+    fread(&sampleRate, 4, 1, fp);
+    fseek(fp, 34, SEEK_SET);
+    fread(&wordWidth, 2, 1, fp);
+
     while (1) {
+        p = 1;
+        for (int i = 0; i < 8; i++) {
+            if (!feof(fp)) {
+                fread(buf + i, 2, 1, fp);
+            } else {
+                fseek(fp, 36, SEEK_SET);
+            }
+        }
+        p = 0;
         audio.write((uint8_t *)buf);
     }
 }