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"
        }
    }
}

Files at this revision

API Documentation at this revision

Comitter:
1050186
Date:
Fri Dec 28 02:46:43 2018 +0000
Parent:
9:a1045daef81d
Commit message:
Support Mbed OS 5.10, replace Renesas libraries to mbed-gr-libs and optimize main.cpp and related files for supporting GR-LYCHEE too.

Changed in this revision

R_BSP.lib Show diff for this revision Revisions of this file
TLV320_RBSP.lib Show diff for this revision Revisions of this file
USBHost_custom.lib Show diff for this revision Revisions of this file
dec_wav.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-gr-libs.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r a1045daef81d -r 1e59ae6a81df R_BSP.lib
--- a/R_BSP.lib	Wed Mar 22 14:37:49 2017 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/Renesas/code/R_BSP/#c5c630882b90
diff -r a1045daef81d -r 1e59ae6a81df TLV320_RBSP.lib
--- a/TLV320_RBSP.lib	Wed Mar 22 14:37:49 2017 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/Renesas/code/TLV320_RBSP/#15487e571d9f
diff -r a1045daef81d -r 1e59ae6a81df USBHost_custom.lib
--- a/USBHost_custom.lib	Wed Mar 22 14:37:49 2017 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/Renesas/code/USBHost_custom/#cdc0d2ab4678
diff -r a1045daef81d -r 1e59ae6a81df dec_wav.h
--- a/dec_wav.h	Wed Mar 22 14:37:49 2017 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,234 +0,0 @@
-/**************************************************************************//**
-* @file          dec_wav.h
-* @brief         wav
-******************************************************************************/
-#include "mbed.h"
-
-/** A class to communicate a dec_wav
- *
- */
-class dec_wav {
-public:
-
-    /** analyze header
-     *
-     * @param p_title title tag buffer
-     * @param p_artist artist tag buffer
-     * @param p_album album tag buffer
-     * @param tag_size tag buffer size
-     * @param fp file pointer
-     * @return true = success, false = failure
-     */
-    bool AnalyzeHeder(uint8_t * p_title, uint8_t * p_artist, uint8_t * p_album, uint16_t tag_size, FILE * fp) {
-        bool result = false;
-        size_t read_size;
-        uint8_t wk_read_buff[36];
-        uint8_t *data;
-        uint32_t chunk_size;
-        uint32_t sub_chunk_size;
-        uint32_t list_index_max;
-        bool list_ok = false;
-        uint32_t read_index = 0;
-        uint32_t data_index = 0;
-        uint16_t wk_len;
-
-        if (fp == NULL) {
-            return false;
-        }
-        music_data_size  = 0;
-        music_data_index = 0;
-        wav_fp = fp;
-        if (p_title != NULL) {
-            p_title[0] = '\0';
-        }
-        if (p_artist != NULL) {
-            p_artist[0] = '\0';
-        }
-        if (p_album != NULL) {
-            p_album[0] = '\0';
-        }
-
-        read_size = fread(&wk_read_buff[0], sizeof(char), 36, wav_fp);
-        if (read_size < 36) {
-            // 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 {
-            read_index += 36;
-            channel = ((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);
-            while (1) {
-                read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
-                read_index += 8;
-                if (read_size < 8) {
-                    break;
-                } else {
-                    chunk_size = ((uint32_t)wk_read_buff[4] << 0)
-                               + ((uint32_t)wk_read_buff[5] << 8)
-                               + ((uint32_t)wk_read_buff[6] << 16)
-                               + ((uint32_t)wk_read_buff[7] << 24);
-                    if (memcmp(&wk_read_buff[0], "data", 4) == 0) {
-                        result = true;
-                        music_data_size = chunk_size;
-                        if (list_ok == true) {
-                            break;
-                        } else {
-                            data_index = read_index;
-                            fseek(wav_fp, chunk_size, SEEK_CUR);
-                            read_index += chunk_size;
-                        }
-                    } else if (memcmp(&wk_read_buff[0], "LIST", 4) == 0) {
-                        list_ok = true;
-                        list_index_max = read_index + chunk_size;
-                        read_size = fread(&wk_read_buff[0], sizeof(char), 4, wav_fp);
-                        read_index += 4;
-                        while (read_index < list_index_max) {
-                            read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
-                            read_index += 8;
-                            if (read_size < 8) {
-                                break;
-                            } else if (memcmp(&wk_read_buff[0], "INAM", 4) == 0) {
-                                data = p_title;
-                            } else if (memcmp(&wk_read_buff[0], "IART", 4) == 0) {
-                                data = p_artist;
-                            } else if (memcmp(&wk_read_buff[0], "IPRD", 4) == 0) {
-                                data = p_album;
-                            } else {
-                                data = NULL;
-                            }
-                            if ((data != NULL) && (tag_size != 0)) {
-                                sub_chunk_size = ((uint32_t)wk_read_buff[4] << 0)
-                                               + ((uint32_t)wk_read_buff[5] << 8)
-                                               + ((uint32_t)wk_read_buff[6] << 16)
-                                               + ((uint32_t)wk_read_buff[7] << 24);
-                                if (sub_chunk_size > (tag_size - 1)) {
-                                    wk_len = (tag_size - 1);
-                                } else {
-                                    wk_len = sub_chunk_size;
-                                }
-                                read_size = fread(data, sizeof(char), wk_len, wav_fp);
-                                read_index += sub_chunk_size;
-                                fseek(wav_fp, read_index, SEEK_SET);
-                                data[wk_len] = '\0';
-                            }
-                        }
-                        if (data_index != 0) {
-                            break;
-                        } else {
-                            fseek(wav_fp, list_index_max, SEEK_SET);
-                        }
-                    } else {
-                        fseek(wav_fp, chunk_size, SEEK_CUR);
-                        read_index += chunk_size;
-                    }
-                }
-            }
-
-            if (data_index != 0) {
-                fseek(wav_fp, data_index, SEEK_SET);
-            }
-        }
-
-        return result;
-    };
-
-    /** get next data
-     *
-     * @param buf data buffer address
-     * @param len data buffer length
-     * @return get data size
-     */
-    size_t GetNextData(void *buf, size_t len) {
-        if (block_size == 24) {
-            // Add padding
-            int write_index = 0;
-            int wavfile_index;
-            int read_len;
-            int pading_index = 0;
-            uint8_t * p_buf = (uint8_t *)buf;
-            size_t ret;
-
-            if ((music_data_index + len) > music_data_size) {
-                len = music_data_size - music_data_index;
-            }
-            while (write_index < len) {
-                read_len = (len - write_index) * 3 / 4;
-                if (read_len > sizeof(wk_wavfile_buff)) {
-                    read_len = sizeof(wk_wavfile_buff);
-                }
-                music_data_index += read_len;
-                ret = fread(wk_wavfile_buff, sizeof(char), read_len, wav_fp);
-                if (ret < read_len) {
-                    break;
-                }
-                wavfile_index = 0;
-                while ((write_index < len) && (wavfile_index < read_len)) {
-                    if (pading_index == 0) {
-                        p_buf[write_index] = 0;
-                    } else {
-                        p_buf[write_index] = wk_wavfile_buff[wavfile_index];
-                        wavfile_index++;
-                    }
-                    if (pading_index < 3) {
-                        pading_index++;
-                    } else {
-                        pading_index = 0;
-                    }
-                    write_index++;
-                }
-            }
-
-            return write_index;
-        } else {
-            if ((music_data_index + len) > music_data_size) {
-                len = music_data_size - music_data_index;
-            }
-            music_data_index += len;
-
-            return fread(buf, sizeof(char), len, wav_fp);
-        }
-    };
-
-    /** get channel
-     *
-     * @return channel
-     */
-    uint16_t GetChannel() {
-        return channel;
-    };
-
-    /** get block size
-     *
-     * @return block size
-     */
-    uint16_t GetBlockSize() {
-        return block_size;
-    };
-
-    /** get sampling rate
-     *
-     * @return sampling rate
-     */
-    uint32_t GetSamplingRate() {
-        return sampling_rate;
-    };
-
-private:
-    #define FILE_READ_BUFF_SIZE    (3072)
-
-    FILE * wav_fp;
-    uint32_t music_data_size;
-    uint32_t music_data_index;
-    uint16_t channel;
-    uint16_t block_size;
-    uint32_t sampling_rate;
-    uint8_t wk_wavfile_buff[FILE_READ_BUFF_SIZE];
-};
diff -r a1045daef81d -r 1e59ae6a81df main.cpp
--- 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);
     }
 }
diff -r a1045daef81d -r 1e59ae6a81df mbed-gr-libs.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-gr-libs.lib	Fri Dec 28 02:46:43 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/d-kato/mbed-gr-libs/#e8e5994d4ac5f76c13407bec22821499e6bf0ec1
diff -r a1045daef81d -r 1e59ae6a81df mbed-os.lib
--- a/mbed-os.lib	Wed Mar 22 14:37:49 2017 +0900
+++ b/mbed-os.lib	Fri Dec 28 02:46:43 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#cc58a7fb0c979440ab13a7fb25c4acd12009ae1c
+https://github.com/ARMmbed/mbed-os/#2fd0c5cfbd83fce62da6308f9d64c0ab64e1f0d6
diff -r a1045daef81d -r 1e59ae6a81df mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Fri Dec 28 02:46:43 2018 +0000
@@ -0,0 +1,13 @@
+{
+    "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"
+        }
+    }
+}
+