Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile

Revision:
2:c939d0501184
Parent:
1:c47a2f0816bb
--- a/Recorder.cpp	Mon Dec 08 10:06:35 2014 +0000
+++ b/Recorder.cpp	Mon Dec 08 23:30:49 2014 +0000
@@ -1,12 +1,14 @@
 #include "Recorder.h"
 
-#define RAM_TOTAL 1024
+#define RAM_TOTAL 2048
 
+AnalogOut dacout(p18);
 AnalogIn adcin(p20);
+DigitalOut led_play_ok(LED1);
 DigitalOut led_rec_ok(LED2);
 
 Ticker ticker;
-float buffer[RAM_TOTAL];
+extern float *buffer;
 int rp = 0;
 int wp = 0;
 int dropout = 0;
@@ -22,6 +24,21 @@
         } \
     } while(0)
 
+
+void tickplay(void)
+{
+    /*
+     * Check the play underflow
+     */
+    if (rp != wp) {
+        dacout = buffer[rp];
+        rp = (rp + 1) & (RAM_TOTAL - 1);
+    } else {
+        dropout++;
+    }
+    led_play_ok = !led_play_ok;
+}
+
 void tickrec(void)
 {
     int np = (wp + 1) & (RAM_TOTAL - 1);
@@ -34,6 +51,51 @@
     led_rec_ok = !led_rec_ok;
 }
 
+int play(const char *filename)
+{
+    WavFileResult result;
+    wavfile_info_t info;
+    wavfile_data_t data;
+
+    WAVFILE *wf = wavfile_open(filename, WavFileModeRead, &result);
+    WAVFILE_ERROR_PRINT(result);
+    WAVFILE_ERROR_PRINT(wavfile_read_info(wf, &info));
+
+    //printf("[PLAY:%s]\r\n", filename);
+    //printf("\tWAVFILE_INFO_AUDIO_FORMAT(&info)    = %d\r\n", WAVFILE_INFO_AUDIO_FORMAT(&info));
+    //printf("\tWAVFILE_INFO_NUM_CHANNELS(&info)    = %d\r\n", WAVFILE_INFO_NUM_CHANNELS(&info));
+    //printf("\tWAVFILE_INFO_SAMPLE_RATE(&info)     = %d\r\n", WAVFILE_INFO_SAMPLE_RATE(&info));
+    //printf("\tWAVFILE_INFO_BYTE_RATE(&info)       = %d\r\n", WAVFILE_INFO_BYTE_RATE(&info));
+    //printf("\tWAVFILE_INFO_BLOCK_ALIGN(&info)     = %d\r\n", WAVFILE_INFO_BLOCK_ALIGN(&info));
+    //printf("\tWAVFILE_INFO_BITS_PER_SAMPLE(&info) = %d\r\n", WAVFILE_INFO_BITS_PER_SAMPLE(&info));
+
+    const int interval_us =  1000000 / WAVFILE_INFO_SAMPLE_RATE(&info);
+
+    rp = 0;
+    wp = 0;
+    dropout = 0;
+    ticker.attach_us(tickplay, interval_us);
+    while (1) {
+        int np = (wp + 1) & (RAM_TOTAL - 1);
+        while (np == rp) {
+            wait_us(1);
+        }
+        WAVFILE_ERROR_PRINT(wavfile_read_data(wf, &data));
+        if (WAVFILE_DATA_IS_END_OF_DATA(&data)) {
+            break;
+        }
+        buffer[wp] = WAVFILE_DATA_CHANNEL_DATA(&data, 0);
+        wp = np;
+    }
+    ticker.detach();
+    dacout = 0.5;
+    led_play_ok = 0;
+    //printf("\t-- Play done. (dropout=%d) --\r\n", dropout);
+
+    WAVFILE_ERROR_PRINT(wavfile_close(wf));
+    return 0;
+}
+
 int rec(const char *filename, const int nsec)
 {
     WavFileResult result;