This sample will play a ".wav" file of the USB root folder. Only RIFF format, 48kHz, 16bit, 2ch.

Dependencies:   USBHostDac USBHost_custom_Addiso

Fork of USBHostDac_example by GR-PEACH_producer_meeting

Isochronous transfer sample for GR-PEACH. This sample will play a ".wav" file of the USB root folder. Only RIFF format, 48kHz, 16bit, 2ch.

/media/uploads/dkato/usbdacsample.png
The default setting of serial communication (baud rate etc.) in mbed is shown the following link.
Please refer to the link and change the settings of your PC terminal software.
The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication

Revision:
1:82fe51d45902
Parent:
0:39320363f99f
Child:
2:4611f39d677b
--- a/main.cpp	Wed Apr 01 10:51:50 2015 +0000
+++ b/main.cpp	Fri Jun 26 07:40:06 2015 +0000
@@ -1,100 +1,48 @@
 #include "mbed.h"
 #include "USBHostDac.h"
 #include "USBHostMSD.h"
+#include "dec_wav.h"
 
-DigitalOut led(LED1);
+#if defined(TARGET_RZ_A1H)
+#include "usb_host_setting.h"
+#else
+#define USB_HOST_CH     0
+#endif
+
+#if (USB_HOST_CH == 1) //Audio Shield USB1
+DigitalOut usb1en(P3_8);
+#endif
 DigitalIn  button(USER_BUTTON0);
 
-#define READ_BUFF_SIZE         (4096)
-#define READ_BUFF_NUM          (3)
-#define MAIL_QUEUE_SIZE        (READ_BUFF_NUM - 1)
+#define AUDIO_WRITE_BUFF_SIZE  (4096)
+#define AUDIO_WRITE_BUFF_NUM   (9)
 #define FILE_NAME_LEN          (64)
-
-typedef struct {
-    uint8_t * p_data;
-    uint32_t  data_size;
-    bool      flush;
-} mail_t;
-Mail<mail_t, MAIL_QUEUE_SIZE> mail_box;
-
-static uint8_t read_buff[READ_BUFF_NUM][READ_BUFF_SIZE];
-static uint32_t read_buff_no = 0;
-
-void usbdac_task(void const*) {
-    USBHostDac usbdac;
-
-    while(1) {
-
-        // try to connect a usbdac device
-        while(!usbdac.connect()) {
-            Thread::wait(500);
-        }
-
-        // in a loop, print all characters received
-        // if the device is disconnected, we try to connect it again
-        while (1) {
-            // if device disconnected, try to connect it again
-            if (!usbdac.connected()) {
-                break;
-            }
-
-            osEvent evt = mail_box.get();
-            if (evt.status == osEventMail) {
-                mail_t *mail = (mail_t *)evt.value.p;
-                usbdac.send(mail->p_data, mail->data_size, mail->flush);
-                mail_box.free(mail);
-            }
-        }
-    }
-}
+#define TEXT_SIZE              (64 + 1) //null-terminated
+#define FLD_PATH               "/usb/"
 
-static bool analyze_wav_heder(FILE * fp) {
-    bool result = false;
-    size_t read_size;
-    uint8_t wk_read_buff[36];
-    uint32_t fmt_size;
-    uint16_t ch;
-    uint32_t sampling_rate;
-    uint16_t block_size;
+static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE];
+//Tag buffer
+static uint8_t title_buf[TEXT_SIZE];
+static uint8_t artist_buf[TEXT_SIZE];
+static uint8_t album_buf[TEXT_SIZE];
 
-    read_size = fread(&wk_read_buff[0], sizeof(char), sizeof(wk_read_buff), fp);
-    if (read_size < sizeof(wk_read_buff)) {
-        // do nothing
-    } else if (memcmp(&wk_read_buff[0], "RIFF", 4) != 0) {
-        // do nothing
-    } else if (memcmp(&wk_read_buff[8], "WAVE", 4) != 0) {
-        // do nothing
-    } else if (memcmp(&wk_read_buff[12], "fmt ", 4) != 0) {
-        // do nothing
-    } else {
-        result = true;
-        fmt_size = ((uint32_t)wk_read_buff[16] << 0)
-                 + ((uint32_t)wk_read_buff[17] << 8)
-                 + ((uint32_t)wk_read_buff[18] << 16)
-                 + ((uint32_t)wk_read_buff[19] << 24);
-        ch = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8);
-        sampling_rate = ((uint32_t)wk_read_buff[24] << 0)
-                      + ((uint32_t)wk_read_buff[25] << 8)
-                      + ((uint32_t)wk_read_buff[26] << 16)
-                      + ((uint32_t)wk_read_buff[27] << 24);
-        block_size = ((uint32_t)wk_read_buff[34] << 0) + ((uint32_t)wk_read_buff[35] << 8);
-
-        if ((ch == 2) && (block_size == 16) && (sampling_rate == 48000)) {
-            fseek(fp, 28 + fmt_size, SEEK_SET);
-        } else {
-            result = false;
-        }
-    }
-
-    return result;
-}
-
-void msd_task(void const *) {
+int main() {
     FILE * fp = NULL;
     DIR  * d = NULL;
-    char file_path[sizeof("/usb/") + FILE_NAME_LEN];
+    char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN];
+    int buff_index = 0;
+    size_t audio_data_size;
+    dec_wav wav_file;
+
+#if (USB_HOST_CH == 1) //Audio Shield USB1
+    //Audio Shield USB1 enable
+    usb1en = 1;        //Outputs high level
+    Thread::wait(5);
+    usb1en = 0;        //Outputs low level
+#endif
 
     USBHostMSD msd("usb");
+    USBHostDac usbdac;
 
     while(1) {
         // try to connect a MSD device
@@ -109,24 +57,34 @@
             if (!msd.connected()) {
                 break;
             }
-
             if (fp == NULL) {
                 // file search
                 if (d == NULL) {
-                    d = opendir("/usb/");
+                    d = opendir(FLD_PATH);
                 }
                 struct dirent * p;
                 while ((p = readdir(d)) != NULL) {
                     size_t len = strlen(p->d_name);
-                    if ((len > 4) && (len <= FILE_NAME_LEN) && (memcmp(&p->d_name[len - 4], ".wav", 4) == 0)) {
-                        strcpy(file_path, "/usb/");
+                    if ((len > 4) && (len < FILE_NAME_LEN)
+                        && (memcmp(&p->d_name[len - 4], ".wav", 4) == 0)) {
+                        strcpy(file_path, FLD_PATH);
                         strcat(file_path, p->d_name);
                         fp = fopen(file_path, "r");
-                        if (analyze_wav_heder(fp) == false) {
+                        if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
+                                                   TEXT_SIZE, fp) == false) {
+                            fclose(fp);
+                            fp = NULL;
+                        } else if ((wav_file.GetChannel() != 2)
+                                || (wav_file.GetBlockSize() != 16)
+                                || (wav_file.GetSamplingRate() != 48000)) {
                             fclose(fp);
                             fp = NULL;
                         } else {
-                            printf("%s\n", p->d_name);
+                            printf("File  :%s\n", p->d_name);
+                            printf("Title :%s\n", title_buf);
+                            printf("Artist:%s\n", artist_buf);
+                            printf("Album :%s\n", album_buf);
+                            printf("\n");
                             break;
                         }
                     }
@@ -137,27 +95,28 @@
                 }
             } else {
                 // file read
-                size_t read_size = fread(&read_buff[read_buff_no][0], sizeof(char), READ_BUFF_SIZE, fp);
-                if (read_size > 0) {
-                    mail_t *mail = mail_box.alloc(osWaitForever);
-                    if (mail != NULL) {
-                        mail->p_data = &read_buff[read_buff_no][0];
-                        mail->data_size = read_size;
-                        if (read_size < READ_BUFF_SIZE) {
-                            mail->flush = true;    // last data
-                        } else {
-                            mail->flush = false;
-                        }
-                        mail_box.put(mail);
-                        read_buff_no++;
-                        if (read_buff_no >= READ_BUFF_NUM) {
-                            read_buff_no = 0;
-                        }
+                uint8_t * p_buf = audio_write_buff[buff_index];
+
+                audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE);
+                if (audio_data_size > 0) {
+                    bool flush = false;
+
+                    // try to connect a usbdac device
+                    while(!usbdac.connect()) {
+                        Thread::wait(500);
+                    }
+                    if (audio_data_size < AUDIO_WRITE_BUFF_SIZE) {
+                        flush = true;
+                    }
+                    usbdac.send(p_buf, audio_data_size, flush);
+                    buff_index++;
+                    if (buff_index >= AUDIO_WRITE_BUFF_NUM) {
+                        buff_index = 0;
                     }
                 }
 
                 // file close
-                if ((read_size < READ_BUFF_SIZE) || (button == 0)) {
+                if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (button == 0)) {
                     fclose(fp);
                     fp = NULL;
                     Thread::wait(500);
@@ -176,12 +135,3 @@
         }
     }
 }
-
-int main() {
-    Thread usbdaclTask(usbdac_task, NULL, osPriorityNormal, 1024 * 4);
-    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
-    while(1) {
-        led=!led;
-        Thread::wait(500);
-    }
-}