USBAudio speaker example

Dependencies:   mbed USBDevice

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Wed Nov 30 09:51:59 2011 +0000
Child:
1:0335b5c18618
Commit message:

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Wed Nov 30 09:51:59 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/USBDevice/#181abc5c9bf5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 30 09:51:59 2011 +0000
@@ -0,0 +1,81 @@
+// Hello World example for the USBMIDI library
+
+#include "mbed.h"
+#include "USBAudio.h"
+#include "CircBuffer.h"
+
+Serial pc(USBTX, USBRX);
+
+// frequency: 48 kHz
+#define FREQ 48000
+
+// 1 channel: mono
+#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
+
+// USBAudio
+USBAudio audio(FREQ, NB_CHA, 0x7180, 0x7500);
+
+// speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker
+AnalogOut speaker(p18);
+
+// 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];
+
+// show if an audio packet is available
+volatile bool available = false;
+
+// index of the value which will be send to the speaker
+int index_buf = 0;
+
+// previous value sent to the speaker
+uint16_t p_val = 0;
+
+// function executed each 1/FREQ s
+void tic_handler() {
+    float speaker_value;
+
+    if (available) {
+        //convert 2 bytes in float
+        speaker_value = (float)(*((int16_t *)&buf[index_buf]));
+        
+        // speaker_value between 0 and 65535
+        speaker_value += 32768.0;
+
+        // adjust according to current volume
+        speaker_value *= audio.getVolume();
+        
+        // as two bytes has been read, we move the index of two bytes
+        index_buf += 2;
+        
+        // if we have read all the buffer, no more data available
+        if (index_buf == LENGTH_AUDIO_PACKET) {
+            index_buf = 0;
+            available = false;
+        }
+    } else {
+        speaker_value = p_val;
+    }
+    
+    p_val = speaker_value;
+
+    // send value to the speaker
+    speaker.write_u16((uint16_t)speaker_value);
+}
+
+int main() {
+
+    // attach a function executed each 1/FREQ s
+    tic.attach_us(tic_handler, 1000000.0/(float)FREQ);
+
+    while (1) {
+        // read an audio packet
+        audio.read(buf);
+        available = true;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Nov 30 09:51:59 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7110ebee3484