USBAudio speaker example

Dependencies:   mbed USBDevice

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Wed Nov 30 10:25:25 2011 +0000
Parent:
0:5176b3dfccd6
Child:
2:c3edc567ae33
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Nov 30 09:51:59 2011 +0000
+++ b/main.cpp	Wed Nov 30 10:25:25 2011 +0000
@@ -13,7 +13,7 @@
 #define NB_CHA 1
 
 // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
-#define LENGTH_AUDIO_PACKET 48 * 2 * 1
+#define AUDIO_LENGTH_AUDIO_PACKET 48 * 2 * 1
 
 // USBAudio
 USBAudio audio(FREQ, NB_CHA, 0x7180, 0x7500);
@@ -24,8 +24,8 @@
 // ticker to send data to the speaker at the good frequency
 Ticker tic;
 
-// buffer where will be store one audio packet
-uint8_t buf[LENGTH_AUDIO_PACKET];
+// buffer where will be store one audio packet (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8)
+int16_t buf[AUDIO_LENGTH_AUDIO_PACKET/2];
 
 // show if an audio packet is available
 volatile bool available = false;
@@ -42,7 +42,7 @@
 
     if (available) {
         //convert 2 bytes in float
-        speaker_value = (float)(*((int16_t *)&buf[index_buf]));
+        speaker_value = (float)(buf[index_buf]);
         
         // speaker_value between 0 and 65535
         speaker_value += 32768.0;
@@ -51,10 +51,10 @@
         speaker_value *= audio.getVolume();
         
         // as two bytes has been read, we move the index of two bytes
-        index_buf += 2;
+        index_buf++;
         
         // if we have read all the buffer, no more data available
-        if (index_buf == LENGTH_AUDIO_PACKET) {
+        if (index_buf == AUDIO_LENGTH_AUDIO_PACKET/2) {
             index_buf = 0;
             available = false;
         }
@@ -75,7 +75,7 @@
 
     while (1) {
         // read an audio packet
-        audio.read(buf);
+        audio.read((uint8_t *)buf);
         available = true;
     }
 }