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:
Tue Jul 24 08:46:48 2018 +0000
Revision:
16:63d0cf9e117b
Parent:
15:570acc3d2b89
Changed to be usable with EasyPlayback

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:a24aaf3a41b1 1 #include "mbed.h"
dkato 15:570acc3d2b89 2 #include "SdUsbConnect.h"
dkato 16:63d0cf9e117b 3 #include "EasyPlayback.h"
dkato 16:63d0cf9e117b 4 #include "EasyDec_WavCnv2ch.h"
dkato 4:01651a6c3f9a 5
dkato 16:63d0cf9e117b 6 #define FNAME_LEN (64)
dkato 9:c7c0a97fdb7f 7 #define MOUNT_NAME "storage"
dkato 0:a24aaf3a41b1 8
dkato 13:7e3063fc0e10 9 static InterruptIn skip_btn(USER_BUTTON0);
dkato 16:63d0cf9e117b 10 static EasyPlayback AudioPlayer(EasyPlayback::AUDIO_TPYE_PWM, P4_5, P4_7);
dkato 0:a24aaf3a41b1 11
dkato 13:7e3063fc0e10 12 static void skip_btn_fall(void) {
dkato 13:7e3063fc0e10 13 AudioPlayer.skip();
dkato 0:a24aaf3a41b1 14 }
dkato 0:a24aaf3a41b1 15
dkato 4:01651a6c3f9a 16 int main() {
dkato 13:7e3063fc0e10 17 DIR * d;
dkato 13:7e3063fc0e10 18 struct dirent * p;
dkato 16:63d0cf9e117b 19 char file_path[sizeof("/"MOUNT_NAME"/") + FNAME_LEN];
dkato 16:63d0cf9e117b 20 SdUsbConnect storage(MOUNT_NAME);
dkato 13:7e3063fc0e10 21
dkato 16:63d0cf9e117b 22 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".wav"); // decoder setting
dkato 16:63d0cf9e117b 23 AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".WAV"); // decoder setting
dkato 16:63d0cf9e117b 24 AudioPlayer.outputVolume(0.5); // Volume control (min:0.0 max:1.0)
dkato 16:63d0cf9e117b 25 skip_btn.fall(&skip_btn_fall); // button setting
dkato 13:7e3063fc0e10 26
dkato 16:63d0cf9e117b 27 while (1) {
dkato 16:63d0cf9e117b 28 storage.wait_connect();
dkato 16:63d0cf9e117b 29 d = opendir("/"MOUNT_NAME"/"); // file search
dkato 13:7e3063fc0e10 30 while ((p = readdir(d)) != NULL) {
dkato 13:7e3063fc0e10 31 size_t len = strlen(p->d_name);
dkato 16:63d0cf9e117b 32 if (len < FNAME_LEN) {
dkato 16:63d0cf9e117b 33 sprintf(file_path, "/%s/%s", MOUNT_NAME, p->d_name); // make file path
dkato 13:7e3063fc0e10 34 printf("%s\r\n", file_path);
dkato 16:63d0cf9e117b 35 AudioPlayer.play(file_path); // playback
dkato 0:a24aaf3a41b1 36 }
dkato 0:a24aaf3a41b1 37 }
dkato 13:7e3063fc0e10 38 closedir(d);
dkato 0:a24aaf3a41b1 39 }
dkato 0:a24aaf3a41b1 40 }