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 Jul 24 05:45:07 2018 +0000
Revision:
15:570acc3d2b89
Parent:
14:f3eda9558df6
Child:
16:63d0cf9e117b
upports mbed-os 5.9.1 and later

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:a24aaf3a41b1 1 #include "mbed.h"
dkato 13:7e3063fc0e10 2 #include "EasyPlaybackPWM.h"
dkato 13:7e3063fc0e10 3 #include "EasyDec_WavCnv2ch.h"
dkato 15:570acc3d2b89 4 #include "SdUsbConnect.h"
dkato 4:01651a6c3f9a 5
dkato 0:a24aaf3a41b1 6 #define FILE_NAME_LEN (64)
dkato 9:c7c0a97fdb7f 7 #define MOUNT_NAME "storage"
dkato 13:7e3063fc0e10 8 #define TAG_BUFF_SIZE (64 + 1) //null-terminated
dkato 0:a24aaf3a41b1 9
dkato 13:7e3063fc0e10 10 static InterruptIn skip_btn(USER_BUTTON0);
dkato 13:7e3063fc0e10 11 static EasyPlaybackPWM AudioPlayer(P4_5, P4_7);
dkato 0:a24aaf3a41b1 12
dkato 13:7e3063fc0e10 13 static void skip_btn_fall(void) {
dkato 13:7e3063fc0e10 14 AudioPlayer.skip();
dkato 0:a24aaf3a41b1 15 }
dkato 0:a24aaf3a41b1 16
dkato 4:01651a6c3f9a 17 int main() {
dkato 13:7e3063fc0e10 18 DIR * d;
dkato 13:7e3063fc0e10 19 struct dirent * p;
dkato 13:7e3063fc0e10 20 char file_path[sizeof("/"MOUNT_NAME"/") + FILE_NAME_LEN];
dkato 11:221c23d820d9 21
dkato 13:7e3063fc0e10 22 // decoder setting
dkato 13:7e3063fc0e10 23 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".wav");
dkato 13:7e3063fc0e10 24 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".WAV");
dkato 13:7e3063fc0e10 25
dkato 13:7e3063fc0e10 26 // volume control
dkato 14:f3eda9558df6 27 AudioPlayer.outputVolume(0.5); // Volume control (min:0.0 max:1.0)
dkato 13:7e3063fc0e10 28
dkato 13:7e3063fc0e10 29 // button setting
dkato 13:7e3063fc0e10 30 skip_btn.fall(&skip_btn_fall);
dkato 13:7e3063fc0e10 31
dkato 13:7e3063fc0e10 32 // wait for the storage device to be connected
dkato 13:7e3063fc0e10 33 printf("Finding a storage...\r\n");
dkato 15:570acc3d2b89 34 SdUsbConnect storage(MOUNT_NAME);
dkato 15:570acc3d2b89 35 storage.wait_connect();
dkato 13:7e3063fc0e10 36 printf("done\r\n");
dkato 0:a24aaf3a41b1 37
dkato 0:a24aaf3a41b1 38 while(1) {
dkato 13:7e3063fc0e10 39 // file search
dkato 13:7e3063fc0e10 40 d = opendir("/"MOUNT_NAME"/");
dkato 13:7e3063fc0e10 41 while ((p = readdir(d)) != NULL) {
dkato 13:7e3063fc0e10 42 size_t len = strlen(p->d_name);
dkato 13:7e3063fc0e10 43 if (len < FILE_NAME_LEN) {
dkato 13:7e3063fc0e10 44 // make file path
dkato 13:7e3063fc0e10 45 sprintf(file_path, "/%s/%s", MOUNT_NAME, p->d_name);
dkato 13:7e3063fc0e10 46 printf("%s\r\n", file_path);
dkato 0:a24aaf3a41b1 47
dkato 13:7e3063fc0e10 48 // playback
dkato 13:7e3063fc0e10 49 AudioPlayer.play(file_path);
dkato 0:a24aaf3a41b1 50 }
dkato 0:a24aaf3a41b1 51 }
dkato 13:7e3063fc0e10 52 closedir(d);
dkato 0:a24aaf3a41b1 53 }
dkato 0:a24aaf3a41b1 54 }
dkato 13:7e3063fc0e10 55