This sample will play a ".wav" file of the USB root folder. Only RIFF format.

Fork of GR-PEACH_Audio_WAV by Daiki Kato

Audio sample for GR-PEACH or GR-LYCHEE. This sample will play a ".wav" file in the root of USB memory or SD card. If the USER_BUTTON0 is pressed, the next song is played.

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

Please refer to following link about Audio/Camera Shield.
https://developer.mbed.org/teams/Renesas/wiki/Audio_Camera-shield

For GR-PEACH:

FormatWav file (RIFF format) ".wav"
Channel2ch
Frequencies32kHz, 44.1kHz, 48kHz, 88.2kHz and 96kHz
Quantization bit rate16bits, 24bits and 32bits


For GR-LYCHEE:

FormatWav file (RIFF format) ".wav"
Channel2ch
Frequencies32kHz, 44.1kHz and 48kHz
Quantization bit rate16bits



  • Use USB0 of PEACH:
    /media/uploads/dkato/audio_wav_usb0.png

    If you use the USB0 as USB Host, please close GR-PEACH's JP3.
    /media/uploads/RyoheiHagimoto/usb.jpg

    Please select USB0 connector by the following configuration.

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "0"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "0"
        }
    }
}



  • Use USB1 of GR-PEACH:
    /media/uploads/dkato/audio_wav_usb1.png

    If you use the USB1 as USB Host, please close Audio/Camera Shield's JP1. /media/uploads/dkato/audiocamerashield_jp1.jpg

    Please select Audio/Camera Shield and USB1 connector by the following configuration.

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "1"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "1"
        }
    }
}
Revision:
10:1e59ae6a81df
Parent:
9:a1045daef81d
--- a/main.cpp	Wed Mar 22 14:37:49 2017 +0900
+++ b/main.cpp	Fri Dec 28 02:46:43 2018 +0000
@@ -1,158 +1,49 @@
 #include "mbed.h"
-#include "TLV320_RBSP.h"
-#include "FATFileSystem.h"
-#include "USBHostMSD.h"
-#include "dec_wav.h"
+#include "SdUsbConnect.h"
+#include "EasyPlayback.h"
+#include "EasyDec_WavCnv2ch.h"
 
-#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 AUDIO_WRITE_BUFF_SIZE  (4096)
-#define AUDIO_WRITE_BUFF_NUM   (9)
 #define FILE_NAME_LEN          (64)
-#define TEXT_SIZE              (64 + 1) //null-terminated
-#define FLD_PATH               "/usb/"
+#define MOUNT_NAME             "storage"
 
-//4 bytes aligned! No cache memory
-#if defined(__ICCARM__)
-#pragma data_alignment=4
-static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE]@ ".mirrorram";
-#else
-static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE]
-                __attribute((section("NC_BSS"),aligned(4)));
-#endif
-//Tag buffer
-static uint8_t title_buf[TEXT_SIZE];
-static uint8_t artist_buf[TEXT_SIZE];
-static uint8_t album_buf[TEXT_SIZE];
+static InterruptIn skip_btn(USER_BUTTON0);
+static EasyPlayback AudioPlayer;
 
-TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6,
-                  0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0);
-
-static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data) {
-    if (result < 0) {
-        printf("audio write callback error %d\n", result);
-    }
+static void skip_btn_fall(void) {
+    AudioPlayer.skip();
 }
 
 int main() {
-    rbsp_data_conf_t audio_write_async_ctl = {&callback_audio_write_end, NULL};
-    FILE * fp = NULL;
-    DIR  * d = NULL;
-    char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN];
-    int buff_index = 0;
-    size_t audio_data_size;
-    dec_wav wav_file;
-    
-    FATFileSystem fs("usb");
-    USBHostMSD msd;
+    DIR  * d;
+    struct dirent * p;
+    char file_path[sizeof("/"MOUNT_NAME"/") + FILE_NAME_LEN];
+    SdUsbConnect storage(MOUNT_NAME);
 
-#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
+    // decoder setting
+    AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".wav");
+    AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".WAV");
 
-    audio.power(0x02); // mic off
-    audio.inputVolume(0.7, 0.7);    
-
-    while(1) {
-        // try to connect a MSD device
-        while(!msd.connect()) {
-            Thread::wait(500);
-        }
-        
-        // Now that the MSD device is connected, file system is mounted.
-        fs.mount(&msd);
+    // volume control
+    AudioPlayer.outputVolume(0.5);  // Volume control (min:0.0 max:1.0)
 
-        // in a loop, append a file
-        // if the device is disconnected, we try to connect it again
-        while(1) {
-            // if device disconnected, try to connect again
-            if (!msd.connected()) {
-                break;
-            }
-            if (fp == NULL) {
-                // file search
-                if (d == NULL) {
-                    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, FLD_PATH);
-                        strcat(file_path, p->d_name);
-                        fp = fopen(file_path, "r");
-                        if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
-                                                   TEXT_SIZE, fp) == false) {
-                            fclose(fp);
-                            fp = NULL;
-                        } else if ((wav_file.GetChannel() != 2)
-                                || (audio.format(wav_file.GetBlockSize()) == false)
-                                || (audio.frequency(wav_file.GetSamplingRate()) == false)) {
-                            printf("Error File  :%s\n", p->d_name);
-                            printf("Audio Info  :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
-                                    wav_file.GetBlockSize(), wav_file.GetSamplingRate());
-                            printf("\n");
-                            fclose(fp);
-                            fp = NULL;
-                        } else {
-                            printf("File        :%s\n", p->d_name);
-                            printf("Audio Info  :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
-                                    wav_file.GetBlockSize(), wav_file.GetSamplingRate());
-                            printf("Title       :%s\n", title_buf);
-                            printf("Artist      :%s\n", artist_buf);
-                            printf("Album       :%s\n", album_buf);
-                            printf("\n");
-                            break;
-                        }
-                    }
-                }
-                if (p == NULL) {
-                    closedir(d);
-                    d = NULL;
-                }
-            } else {
-                // file read
-                uint8_t * p_buf = audio_write_buff[buff_index];
+    // button setting
+    skip_btn.fall(&skip_btn_fall);
 
-                audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE);
-                if (audio_data_size > 0) {
-                    audio.write(p_buf, audio_data_size, &audio_write_async_ctl);
-                    buff_index++;
-                    if (buff_index >= AUDIO_WRITE_BUFF_NUM) {
-                        buff_index = 0;
-                    }
-                }
+    while (1) {
+        storage.wait_connect();
+        // file search
+        d = opendir("/"MOUNT_NAME"/");
+        while ((p = readdir(d)) != NULL) {
+            size_t len = strlen(p->d_name);
+            if (len < FILE_NAME_LEN) {
+                // make file path
+                sprintf(file_path, "/%s/%s", MOUNT_NAME, p->d_name);
+                printf("%s\r\n", file_path);
 
-                // file close
-                if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (button == 0)) {
-                    fclose(fp);
-                    fp = NULL;
-                    Thread::wait(500);
-                }
+                // playback
+                AudioPlayer.play(file_path);
             }
         }
-
-        // close check
-        if (fp != NULL) {
-            fclose(fp);
-            fp = NULL;
-        }
-        if (d != NULL) {
-            closedir(d);
-            d = NULL;
-        }
+        closedir(d);
     }
 }