96kHz-16bit USB Audio Output interface with UDA1345 and 24.576MHz Xtal.

Dependencies:   I2S USBDevice mbed

Fork of USBAudioPlayback by Samuel Mokrani

Revision:
3:44343b9dd8b6
Parent:
0:eb53799c0b97
Child:
4:a3aee5fc768a
--- a/main.cpp	Fri Mar 01 13:22:17 2013 +0000
+++ b/main.cpp	Tue Feb 04 15:22:19 2014 +0000
@@ -2,14 +2,15 @@
 
 #include "mbed.h"
 #include "USBAudio.h"
+#include "I2S.h"
 
 // frequency: 48 kHz
-#define FREQ_SPK 48000
-#define FREQ_MIC 48000
+#define FREQ_SPK 96000
+#define FREQ_MIC 8000
 
 // 2channels: stereo
 #define NB_CHA_SPK 2
-#define NB_CHA_MIC 2
+#define NB_CHA_MIC 1
 
 // length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2
 #define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK
@@ -18,23 +19,66 @@
 // USBAudio object
 USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378);
 
+DigitalOut myled(LED1);
+
+I2S i2s(I2S_TRANSMIT, p5, p6, p7);
+I2S i2srx(I2S_RECEIVE, p8, p29, p15);
+
+#define CHUNKS 64
+
+int buf_out[LENGTH_AUDIO_PACKET_MIC/sizeof(int)];
+int16_t buf_in[CHUNKS][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)];
+int16_t * stream_in = NULL;
+int16_t * stream_in_tail = NULL;
+int read_pos = 0;
+int write_pos = 0;
+
+void genwave()
+{
+    int buf[8];
+    if (stream_in == NULL) {
+        buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = buf[7] = 0;
+        i2s.write(buf, 6);
+        return;
+    }
+
+    myled = 1;
+    buf[0] = stream_in[0];
+    buf[1] = stream_in[1];
+    buf[2] = stream_in[2];
+    buf[3] = stream_in[3];
+    buf[4] = stream_in[4];
+    buf[5] = stream_in[5];
+    buf[6] = stream_in[6];
+    buf[7] = stream_in[7];
+    i2s.write(buf, 8);
+    stream_in += 8;
+    if (stream_in >= stream_in_tail) {
+        write_pos++;
+        write_pos %= CHUNKS;
+        stream_in = buf_in[write_pos];
+        stream_in_tail = &buf_in[write_pos][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)];
+    }
+    myled = 0;
+}
+
 int main() {
-    // buffer of int
-    int buf_in[LENGTH_AUDIO_PACKET_SPK/sizeof(int)];
-    int buf_out[LENGTH_AUDIO_PACKET_MIC/sizeof(int)];
-    int * stream_out = buf_in;
-    int * stream_in = buf_out;
-    int * tmp = NULL;
+    i2srx.masterslave(I2S_SLAVE);
+    i2srx.frequency(96000);
+    i2srx.start();
+    i2s.attach(genwave);
+    i2s.masterslave(I2S_MASTER);
+    i2s.wordsize(16);
+    i2s.frequency(96000);
+    i2s.set_interrupt_fifo_level(0);
+    i2s.start();
 
     while (1) {
-        // read and write one audio packet each frame
-        audio.readWrite((uint8_t *)stream_in, (uint8_t *)stream_out);
-        
-        // swap the buffers
-        tmp = stream_in;
-        stream_in = stream_out;
-        stream_out = tmp;
+        audio.readWrite((uint8_t *)buf_in[read_pos++], (uint8_t *)buf_out);
+        read_pos %= CHUNKS;
+        if (stream_in == NULL && read_pos > CHUNKS / 2) {
+            stream_in = buf_in[write_pos];
+            stream_in_tail = &buf_in[write_pos][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)];
+        }
     }
 }
-
-