Daiki Kato / Mbed OS GR-PEACH_Audio_WAV_PwmOut Featured

Dependencies:   EasyPlayback

Fork of GR-PEACH_Audio_WAV by Renesas

This is a sample that drives a speaker with PWM. This sample will play a ".wav" file of the microSD or USB memory root folder. If the USER_BUTTON0 is pressed, the next song is played.

/media/uploads/dkato/pwm_speaker_img.png

FormatWav file (RIFF format) ".wav"
Channel1ch and 2ch
Frequencies32kHz, 44.1kHz and 48kHz
Quantization bit rate8bits and 16bits


You can adjust the volume by changing the following.

main.cpp

AudioPlayer.outputVolume(0.5);  // Volume control (min:0.0 max:1.0)


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

Committer:
dkato
Date:
Fri Mar 24 04:30:17 2017 +0000
Revision:
11:221c23d820d9
Parent:
10:e8f52c4aa394
Child:
13:7e3063fc0e10
Supports mbed os 5.4.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:a24aaf3a41b1 1 #include "mbed.h"
dkato 9:c7c0a97fdb7f 2 #include "rtos.h"
dkato 9:c7c0a97fdb7f 3 #include "PwmOutSpeaker.h"
dkato 4:01651a6c3f9a 4 #include "dec_wav.h"
dkato 11:221c23d820d9 5 #include "FATFileSystem.h"
dkato 11:221c23d820d9 6 #include "SDBlockDevice_GR_PEACH.h"
dkato 11:221c23d820d9 7 #include "USBHostMSD.h"
dkato 4:01651a6c3f9a 8
dkato 11:221c23d820d9 9 #if defined(TARGET_RZ_A1H)
dkato 11:221c23d820d9 10 #include "usb_host_setting.h"
dkato 0:a24aaf3a41b1 11 #else
dkato 11:221c23d820d9 12 #define USB_HOST_CH 0
dkato 11:221c23d820d9 13 #endif
dkato 11:221c23d820d9 14 #if (USB_HOST_CH == 1) //Audio Shield USB1
dkato 11:221c23d820d9 15 static DigitalOut usb1en(P3_8);
dkato 0:a24aaf3a41b1 16 #endif
dkato 0:a24aaf3a41b1 17
dkato 4:01651a6c3f9a 18 #define AUDIO_WRITE_BUFF_SIZE (4096)
dkato 0:a24aaf3a41b1 19 #define FILE_NAME_LEN (64)
dkato 4:01651a6c3f9a 20 #define TEXT_SIZE (64 + 1) //null-terminated
dkato 9:c7c0a97fdb7f 21 #define FLD_PATH "/storage/"
dkato 9:c7c0a97fdb7f 22 #define MOUNT_NAME "storage"
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 9:c7c0a97fdb7f 27 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_SIZE];
dkato 8:7121197d098e 28 #else
dkato 9:c7c0a97fdb7f 29 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_SIZE]__attribute((aligned(4)));
dkato 8:7121197d098e 30 #endif
dkato 4:01651a6c3f9a 31 //Tag buffer
dkato 4:01651a6c3f9a 32 static uint8_t title_buf[TEXT_SIZE];
dkato 4:01651a6c3f9a 33 static uint8_t artist_buf[TEXT_SIZE];
dkato 4:01651a6c3f9a 34 static uint8_t album_buf[TEXT_SIZE];
dkato 9:c7c0a97fdb7f 35 static bool file_skip = false;
dkato 0:a24aaf3a41b1 36
dkato 9:c7c0a97fdb7f 37 static PwmOutSpeaker audio(P4_5, P4_7);
dkato 9:c7c0a97fdb7f 38 static InterruptIn button(USER_BUTTON0);
dkato 9:c7c0a97fdb7f 39
dkato 9:c7c0a97fdb7f 40 static void button_fall(void) {
dkato 9:c7c0a97fdb7f 41 file_skip = true;
dkato 0:a24aaf3a41b1 42 }
dkato 0:a24aaf3a41b1 43
dkato 4:01651a6c3f9a 44 int main() {
dkato 0:a24aaf3a41b1 45 FILE * fp = NULL;
dkato 0:a24aaf3a41b1 46 DIR * d = NULL;
dkato 0:a24aaf3a41b1 47 char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN];
dkato 1:967144cffd53 48 size_t audio_data_size;
dkato 4:01651a6c3f9a 49 dec_wav wav_file;
dkato 11:221c23d820d9 50 int storage_type = 0;
dkato 0:a24aaf3a41b1 51
dkato 9:c7c0a97fdb7f 52 button.fall(&button_fall);
dkato 11:221c23d820d9 53
dkato 5:983467c1466b 54 #if (USB_HOST_CH == 1) //Audio Shield USB1
dkato 5:983467c1466b 55 //Audio Shield USB1 enable
dkato 5:983467c1466b 56 usb1en = 1; //Outputs high level
dkato 5:983467c1466b 57 Thread::wait(5);
dkato 5:983467c1466b 58 usb1en = 0; //Outputs low level
dkato 5:983467c1466b 59 #endif
dkato 11:221c23d820d9 60 FATFileSystem fs(MOUNT_NAME);
dkato 11:221c23d820d9 61 SDBlockDevice_GR_PEACH sd;
dkato 11:221c23d820d9 62 USBHostMSD usb;
dkato 11:221c23d820d9 63
dkato 11:221c23d820d9 64 audio.outputVolume(1.0f); // Volume control (min:0.0 max:1.0)
dkato 0:a24aaf3a41b1 65
dkato 0:a24aaf3a41b1 66 while(1) {
dkato 9:c7c0a97fdb7f 67 // try to connect a storage device
dkato 11:221c23d820d9 68 while (1) {
dkato 11:221c23d820d9 69 if (sd.connect()) {
dkato 11:221c23d820d9 70 storage_type = 0; // SD
dkato 11:221c23d820d9 71 fs.mount(&sd);
dkato 11:221c23d820d9 72 break;
dkato 11:221c23d820d9 73 }
dkato 11:221c23d820d9 74 if (usb.connect()) {
dkato 11:221c23d820d9 75 storage_type = 1; // USB
dkato 11:221c23d820d9 76 fs.mount(&usb);
dkato 11:221c23d820d9 77 break;
dkato 11:221c23d820d9 78 }
dkato 0:a24aaf3a41b1 79 Thread::wait(500);
dkato 0:a24aaf3a41b1 80 }
dkato 0:a24aaf3a41b1 81
dkato 0:a24aaf3a41b1 82 // in a loop, append a file
dkato 0:a24aaf3a41b1 83 // if the device is disconnected, we try to connect it again
dkato 0:a24aaf3a41b1 84 while(1) {
dkato 0:a24aaf3a41b1 85 // if device disconnected, try to connect again
dkato 11:221c23d820d9 86 if (storage_type == 0) {
dkato 11:221c23d820d9 87 if (!sd.connected()) break;
dkato 11:221c23d820d9 88 } else {
dkato 11:221c23d820d9 89 if (!usb.connected()) break;
dkato 0:a24aaf3a41b1 90 }
dkato 0:a24aaf3a41b1 91 if (fp == NULL) {
dkato 0:a24aaf3a41b1 92 // file search
dkato 0:a24aaf3a41b1 93 if (d == NULL) {
dkato 0:a24aaf3a41b1 94 d = opendir(FLD_PATH);
dkato 0:a24aaf3a41b1 95 }
dkato 0:a24aaf3a41b1 96 struct dirent * p;
dkato 0:a24aaf3a41b1 97 while ((p = readdir(d)) != NULL) {
dkato 0:a24aaf3a41b1 98 size_t len = strlen(p->d_name);
dkato 4:01651a6c3f9a 99 if ((len > 4) && (len < FILE_NAME_LEN)
dkato 9:c7c0a97fdb7f 100 && (strncasecmp(&p->d_name[len - 4], ".wav", 4) == 0)) {
dkato 0:a24aaf3a41b1 101 strcpy(file_path, FLD_PATH);
dkato 0:a24aaf3a41b1 102 strcat(file_path, p->d_name);
dkato 0:a24aaf3a41b1 103 fp = fopen(file_path, "r");
dkato 5:983467c1466b 104 if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
dkato 4:01651a6c3f9a 105 TEXT_SIZE, fp) == false) {
dkato 0:a24aaf3a41b1 106 fclose(fp);
dkato 0:a24aaf3a41b1 107 fp = NULL;
dkato 5:983467c1466b 108 } else if ((wav_file.GetChannel() != 2)
dkato 7:4b6799c255ea 109 || (audio.format(wav_file.GetBlockSize()) == false)
dkato 7:4b6799c255ea 110 || (audio.frequency(wav_file.GetSamplingRate()) == false)) {
dkato 7:4b6799c255ea 111 printf("Error 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("\n");
dkato 5:983467c1466b 115 fclose(fp);
dkato 5:983467c1466b 116 fp = NULL;
dkato 0:a24aaf3a41b1 117 } else {
dkato 7:4b6799c255ea 118 printf("File :%s\n", p->d_name);
dkato 7:4b6799c255ea 119 printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
dkato 7:4b6799c255ea 120 wav_file.GetBlockSize(), wav_file.GetSamplingRate());
dkato 7:4b6799c255ea 121 printf("Title :%s\n", title_buf);
dkato 7:4b6799c255ea 122 printf("Artist :%s\n", artist_buf);
dkato 7:4b6799c255ea 123 printf("Album :%s\n", album_buf);
dkato 0:a24aaf3a41b1 124 printf("\n");
dkato 0:a24aaf3a41b1 125 break;
dkato 0:a24aaf3a41b1 126 }
dkato 0:a24aaf3a41b1 127 }
dkato 0:a24aaf3a41b1 128 }
dkato 0:a24aaf3a41b1 129 if (p == NULL) {
dkato 0:a24aaf3a41b1 130 closedir(d);
dkato 0:a24aaf3a41b1 131 d = NULL;
dkato 0:a24aaf3a41b1 132 }
dkato 0:a24aaf3a41b1 133 } else {
dkato 0:a24aaf3a41b1 134 // file read
dkato 9:c7c0a97fdb7f 135 audio_data_size = wav_file.GetNextData(audio_write_buff, AUDIO_WRITE_BUFF_SIZE);
dkato 4:01651a6c3f9a 136 if (audio_data_size > 0) {
dkato 9:c7c0a97fdb7f 137 audio.write(audio_write_buff, audio_data_size);
dkato 0:a24aaf3a41b1 138 }
dkato 0:a24aaf3a41b1 139
dkato 0:a24aaf3a41b1 140 // file close
dkato 9:c7c0a97fdb7f 141 if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (file_skip == true)) {
dkato 9:c7c0a97fdb7f 142 file_skip = false;
dkato 0:a24aaf3a41b1 143 fclose(fp);
dkato 0:a24aaf3a41b1 144 fp = NULL;
dkato 0:a24aaf3a41b1 145 Thread::wait(500);
dkato 0:a24aaf3a41b1 146 }
dkato 0:a24aaf3a41b1 147 }
dkato 0:a24aaf3a41b1 148 }
dkato 0:a24aaf3a41b1 149
dkato 0:a24aaf3a41b1 150 // close check
dkato 0:a24aaf3a41b1 151 if (fp != NULL) {
dkato 0:a24aaf3a41b1 152 fclose(fp);
dkato 0:a24aaf3a41b1 153 fp = NULL;
dkato 0:a24aaf3a41b1 154 }
dkato 0:a24aaf3a41b1 155 if (d != NULL) {
dkato 0:a24aaf3a41b1 156 closedir(d);
dkato 0:a24aaf3a41b1 157 d = NULL;
dkato 0:a24aaf3a41b1 158 }
dkato 11:221c23d820d9 159 fs.unmount();
dkato 0:a24aaf3a41b1 160 }
dkato 0:a24aaf3a41b1 161 }