This is a sample that drives a speaker with PWM.

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:
Thu Jul 06 04:50:46 2017 +0000
Revision:
13:7e3063fc0e10
Parent:
11:221c23d820d9
Child:
14:f3eda9558df6
Simplified sample

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 11:221c23d820d9 4 #include "FATFileSystem.h"
dkato 11:221c23d820d9 5 #include "SDBlockDevice_GR_PEACH.h"
dkato 11:221c23d820d9 6 #include "USBHostMSD.h"
dkato 4:01651a6c3f9a 7
dkato 0:a24aaf3a41b1 8 #define FILE_NAME_LEN (64)
dkato 9:c7c0a97fdb7f 9 #define MOUNT_NAME "storage"
dkato 13:7e3063fc0e10 10 #define TAG_BUFF_SIZE (64 + 1) //null-terminated
dkato 0:a24aaf3a41b1 11
dkato 13:7e3063fc0e10 12 static InterruptIn skip_btn(USER_BUTTON0);
dkato 13:7e3063fc0e10 13 static EasyPlaybackPWM AudioPlayer(P4_5, P4_7);
dkato 0:a24aaf3a41b1 14
dkato 13:7e3063fc0e10 15 static void skip_btn_fall(void) {
dkato 13:7e3063fc0e10 16 AudioPlayer.skip();
dkato 0:a24aaf3a41b1 17 }
dkato 0:a24aaf3a41b1 18
dkato 4:01651a6c3f9a 19 int main() {
dkato 13:7e3063fc0e10 20 DIR * d;
dkato 13:7e3063fc0e10 21 struct dirent * p;
dkato 13:7e3063fc0e10 22 char file_path[sizeof("/"MOUNT_NAME"/") + FILE_NAME_LEN];
dkato 13:7e3063fc0e10 23 FATFileSystem fs("storage");
dkato 11:221c23d820d9 24 SDBlockDevice_GR_PEACH sd;
dkato 11:221c23d820d9 25 USBHostMSD usb;
dkato 11:221c23d820d9 26
dkato 13:7e3063fc0e10 27 // decoder setting
dkato 13:7e3063fc0e10 28 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".wav");
dkato 13:7e3063fc0e10 29 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".WAV");
dkato 13:7e3063fc0e10 30
dkato 13:7e3063fc0e10 31 // volume control
dkato 13:7e3063fc0e10 32 AudioPlayer.outputVolume(0.10f); // Volume control (min:0.0 max:1.0)
dkato 13:7e3063fc0e10 33
dkato 13:7e3063fc0e10 34 // button setting
dkato 13:7e3063fc0e10 35 skip_btn.fall(&skip_btn_fall);
dkato 13:7e3063fc0e10 36
dkato 13:7e3063fc0e10 37 // wait for the storage device to be connected
dkato 13:7e3063fc0e10 38 printf("Finding a storage...\r\n");
dkato 13:7e3063fc0e10 39 while (1) {
dkato 13:7e3063fc0e10 40 if (sd.connect()) {
dkato 13:7e3063fc0e10 41 fs.mount(&sd);
dkato 13:7e3063fc0e10 42 break;
dkato 13:7e3063fc0e10 43 }
dkato 13:7e3063fc0e10 44 if (usb.connect()) {
dkato 13:7e3063fc0e10 45 fs.mount(&usb);
dkato 13:7e3063fc0e10 46 break;
dkato 13:7e3063fc0e10 47 }
dkato 13:7e3063fc0e10 48 Thread::wait(500);
dkato 13:7e3063fc0e10 49 }
dkato 13:7e3063fc0e10 50 printf("done\r\n");
dkato 0:a24aaf3a41b1 51
dkato 0:a24aaf3a41b1 52 while(1) {
dkato 13:7e3063fc0e10 53 // file search
dkato 13:7e3063fc0e10 54 d = opendir("/"MOUNT_NAME"/");
dkato 13:7e3063fc0e10 55 while ((p = readdir(d)) != NULL) {
dkato 13:7e3063fc0e10 56 size_t len = strlen(p->d_name);
dkato 13:7e3063fc0e10 57 if (len < FILE_NAME_LEN) {
dkato 13:7e3063fc0e10 58 // make file path
dkato 13:7e3063fc0e10 59 sprintf(file_path, "/%s/%s", MOUNT_NAME, p->d_name);
dkato 13:7e3063fc0e10 60 printf("%s\r\n", file_path);
dkato 0:a24aaf3a41b1 61
dkato 13:7e3063fc0e10 62 // playback
dkato 13:7e3063fc0e10 63 AudioPlayer.play(file_path);
dkato 0:a24aaf3a41b1 64 }
dkato 0:a24aaf3a41b1 65 }
dkato 13:7e3063fc0e10 66 closedir(d);
dkato 0:a24aaf3a41b1 67 }
dkato 0:a24aaf3a41b1 68 }
dkato 13:7e3063fc0e10 69