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:
Tue Dec 20 07:28:04 2016 +0000
Revision:
10:e8f52c4aa394
Parent:
9:c7c0a97fdb7f
Child:
11:221c23d820d9
Change volume setting.

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