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"
        }
    }
}
Committer:
Osamu Nakamura
Date:
Wed Mar 22 14:37:49 2017 +0900
Revision:
9:a1045daef81d
Parent:
8:7121197d098e
Child:
10:1e59ae6a81df
Introduced mbed OS 5 instead of mbed OS 2(Classic)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:a24aaf3a41b1 1 #include "mbed.h"
dkato 0:a24aaf3a41b1 2 #include "TLV320_RBSP.h"
Osamu Nakamura 9:a1045daef81d 3 #include "FATFileSystem.h"
dkato 0:a24aaf3a41b1 4 #include "USBHostMSD.h"
dkato 4:01651a6c3f9a 5 #include "dec_wav.h"
dkato 4:01651a6c3f9a 6
dkato 0:a24aaf3a41b1 7 #if defined(TARGET_RZ_A1H)
dkato 0:a24aaf3a41b1 8 #include "usb_host_setting.h"
dkato 0:a24aaf3a41b1 9 #else
dkato 0:a24aaf3a41b1 10 #define USB_HOST_CH 0
dkato 0:a24aaf3a41b1 11 #endif
dkato 0:a24aaf3a41b1 12
dkato 0:a24aaf3a41b1 13 #if (USB_HOST_CH == 1) //Audio Shield USB1
dkato 0:a24aaf3a41b1 14 DigitalOut usb1en(P3_8);
dkato 0:a24aaf3a41b1 15 #endif
dkato 0:a24aaf3a41b1 16 DigitalIn button(USER_BUTTON0);
dkato 0:a24aaf3a41b1 17
dkato 4:01651a6c3f9a 18 #define AUDIO_WRITE_BUFF_SIZE (4096)
dkato 4:01651a6c3f9a 19 #define AUDIO_WRITE_BUFF_NUM (9)
dkato 0:a24aaf3a41b1 20 #define FILE_NAME_LEN (64)
dkato 4:01651a6c3f9a 21 #define TEXT_SIZE (64 + 1) //null-terminated
dkato 1:967144cffd53 22 #define FLD_PATH "/usb/"
dkato 0:a24aaf3a41b1 23
dkato 4:01651a6c3f9a 24 //4 bytes aligned! No cache memory
dkato 8:7121197d098e 25 #if defined(__ICCARM__)
dkato 8:7121197d098e 26 #pragma data_alignment=4
dkato 8:7121197d098e 27 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE]@ ".mirrorram";
dkato 8:7121197d098e 28 #else
dkato 4:01651a6c3f9a 29 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE]
dkato 4:01651a6c3f9a 30 __attribute((section("NC_BSS"),aligned(4)));
dkato 8:7121197d098e 31 #endif
dkato 4:01651a6c3f9a 32 //Tag buffer
dkato 4:01651a6c3f9a 33 static uint8_t title_buf[TEXT_SIZE];
dkato 4:01651a6c3f9a 34 static uint8_t artist_buf[TEXT_SIZE];
dkato 4:01651a6c3f9a 35 static uint8_t album_buf[TEXT_SIZE];
dkato 0:a24aaf3a41b1 36
dkato 7:4b6799c255ea 37 TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6,
dkato 4:01651a6c3f9a 38 0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0);
dkato 0:a24aaf3a41b1 39
dkato 4:01651a6c3f9a 40 static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data) {
dkato 0:a24aaf3a41b1 41 if (result < 0) {
dkato 4:01651a6c3f9a 42 printf("audio write callback error %d\n", result);
dkato 0:a24aaf3a41b1 43 }
dkato 0:a24aaf3a41b1 44 }
dkato 0:a24aaf3a41b1 45
dkato 4:01651a6c3f9a 46 int main() {
dkato 4:01651a6c3f9a 47 rbsp_data_conf_t audio_write_async_ctl = {&callback_audio_write_end, NULL};
dkato 0:a24aaf3a41b1 48 FILE * fp = NULL;
dkato 0:a24aaf3a41b1 49 DIR * d = NULL;
dkato 0:a24aaf3a41b1 50 char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN];
dkato 4:01651a6c3f9a 51 int buff_index = 0;
dkato 1:967144cffd53 52 size_t audio_data_size;
dkato 4:01651a6c3f9a 53 dec_wav wav_file;
Osamu Nakamura 9:a1045daef81d 54
Osamu Nakamura 9:a1045daef81d 55 FATFileSystem fs("usb");
Osamu Nakamura 9:a1045daef81d 56 USBHostMSD msd;
dkato 0:a24aaf3a41b1 57
dkato 5:983467c1466b 58 #if (USB_HOST_CH == 1) //Audio Shield USB1
dkato 5:983467c1466b 59 //Audio Shield USB1 enable
dkato 5:983467c1466b 60 usb1en = 1; //Outputs high level
dkato 5:983467c1466b 61 Thread::wait(5);
dkato 5:983467c1466b 62 usb1en = 0; //Outputs low level
dkato 5:983467c1466b 63 #endif
dkato 5:983467c1466b 64
dkato 0:a24aaf3a41b1 65 audio.power(0x02); // mic off
Osamu Nakamura 9:a1045daef81d 66 audio.inputVolume(0.7, 0.7);
dkato 0:a24aaf3a41b1 67
dkato 0:a24aaf3a41b1 68 while(1) {
dkato 0:a24aaf3a41b1 69 // try to connect a MSD device
dkato 0:a24aaf3a41b1 70 while(!msd.connect()) {
dkato 0:a24aaf3a41b1 71 Thread::wait(500);
dkato 0:a24aaf3a41b1 72 }
Osamu Nakamura 9:a1045daef81d 73
Osamu Nakamura 9:a1045daef81d 74 // Now that the MSD device is connected, file system is mounted.
Osamu Nakamura 9:a1045daef81d 75 fs.mount(&msd);
dkato 0:a24aaf3a41b1 76
dkato 0:a24aaf3a41b1 77 // in a loop, append a file
dkato 0:a24aaf3a41b1 78 // if the device is disconnected, we try to connect it again
dkato 0:a24aaf3a41b1 79 while(1) {
dkato 0:a24aaf3a41b1 80 // if device disconnected, try to connect again
dkato 0:a24aaf3a41b1 81 if (!msd.connected()) {
dkato 0:a24aaf3a41b1 82 break;
dkato 0:a24aaf3a41b1 83 }
dkato 0:a24aaf3a41b1 84 if (fp == NULL) {
dkato 0:a24aaf3a41b1 85 // file search
dkato 0:a24aaf3a41b1 86 if (d == NULL) {
dkato 0:a24aaf3a41b1 87 d = opendir(FLD_PATH);
dkato 0:a24aaf3a41b1 88 }
dkato 0:a24aaf3a41b1 89 struct dirent * p;
dkato 0:a24aaf3a41b1 90 while ((p = readdir(d)) != NULL) {
dkato 0:a24aaf3a41b1 91 size_t len = strlen(p->d_name);
dkato 4:01651a6c3f9a 92 if ((len > 4) && (len < FILE_NAME_LEN)
dkato 4:01651a6c3f9a 93 && (memcmp(&p->d_name[len - 4], ".wav", 4) == 0)) {
dkato 0:a24aaf3a41b1 94 strcpy(file_path, FLD_PATH);
dkato 0:a24aaf3a41b1 95 strcat(file_path, p->d_name);
dkato 0:a24aaf3a41b1 96 fp = fopen(file_path, "r");
dkato 5:983467c1466b 97 if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
dkato 4:01651a6c3f9a 98 TEXT_SIZE, fp) == false) {
dkato 0:a24aaf3a41b1 99 fclose(fp);
dkato 0:a24aaf3a41b1 100 fp = NULL;
dkato 5:983467c1466b 101 } else if ((wav_file.GetChannel() != 2)
dkato 7:4b6799c255ea 102 || (audio.format(wav_file.GetBlockSize()) == false)
dkato 7:4b6799c255ea 103 || (audio.frequency(wav_file.GetSamplingRate()) == false)) {
dkato 7:4b6799c255ea 104 printf("Error File :%s\n", p->d_name);
dkato 7:4b6799c255ea 105 printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
dkato 7:4b6799c255ea 106 wav_file.GetBlockSize(), wav_file.GetSamplingRate());
dkato 7:4b6799c255ea 107 printf("\n");
dkato 5:983467c1466b 108 fclose(fp);
dkato 5:983467c1466b 109 fp = NULL;
dkato 0:a24aaf3a41b1 110 } else {
dkato 7:4b6799c255ea 111 printf("File :%s\n", p->d_name);
dkato 7:4b6799c255ea 112 printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
dkato 7:4b6799c255ea 113 wav_file.GetBlockSize(), wav_file.GetSamplingRate());
dkato 7:4b6799c255ea 114 printf("Title :%s\n", title_buf);
dkato 7:4b6799c255ea 115 printf("Artist :%s\n", artist_buf);
dkato 7:4b6799c255ea 116 printf("Album :%s\n", album_buf);
dkato 0:a24aaf3a41b1 117 printf("\n");
dkato 0:a24aaf3a41b1 118 break;
dkato 0:a24aaf3a41b1 119 }
dkato 0:a24aaf3a41b1 120 }
dkato 0:a24aaf3a41b1 121 }
dkato 0:a24aaf3a41b1 122 if (p == NULL) {
dkato 0:a24aaf3a41b1 123 closedir(d);
dkato 0:a24aaf3a41b1 124 d = NULL;
dkato 0:a24aaf3a41b1 125 }
dkato 0:a24aaf3a41b1 126 } else {
dkato 0:a24aaf3a41b1 127 // file read
dkato 4:01651a6c3f9a 128 uint8_t * p_buf = audio_write_buff[buff_index];
dkato 0:a24aaf3a41b1 129
dkato 5:983467c1466b 130 audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE);
dkato 4:01651a6c3f9a 131 if (audio_data_size > 0) {
dkato 4:01651a6c3f9a 132 audio.write(p_buf, audio_data_size, &audio_write_async_ctl);
dkato 4:01651a6c3f9a 133 buff_index++;
dkato 4:01651a6c3f9a 134 if (buff_index >= AUDIO_WRITE_BUFF_NUM) {
dkato 4:01651a6c3f9a 135 buff_index = 0;
dkato 0:a24aaf3a41b1 136 }
dkato 0:a24aaf3a41b1 137 }
dkato 0:a24aaf3a41b1 138
dkato 0:a24aaf3a41b1 139 // file close
dkato 4:01651a6c3f9a 140 if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (button == 0)) {
dkato 0:a24aaf3a41b1 141 fclose(fp);
dkato 0:a24aaf3a41b1 142 fp = NULL;
dkato 0:a24aaf3a41b1 143 Thread::wait(500);
dkato 0:a24aaf3a41b1 144 }
dkato 0:a24aaf3a41b1 145 }
dkato 0:a24aaf3a41b1 146 }
dkato 0:a24aaf3a41b1 147
dkato 0:a24aaf3a41b1 148 // close check
dkato 0:a24aaf3a41b1 149 if (fp != NULL) {
dkato 0:a24aaf3a41b1 150 fclose(fp);
dkato 0:a24aaf3a41b1 151 fp = NULL;
dkato 0:a24aaf3a41b1 152 }
dkato 0:a24aaf3a41b1 153 if (d != NULL) {
dkato 0:a24aaf3a41b1 154 closedir(d);
dkato 0:a24aaf3a41b1 155 d = NULL;
dkato 0:a24aaf3a41b1 156 }
dkato 0:a24aaf3a41b1 157 }
dkato 0:a24aaf3a41b1 158 }