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 Dec 20 05:41:42 2016 +0000
Revision:
9:c7c0a97fdb7f
Parent:
8:7121197d098e
Child:
10:e8f52c4aa394
Changed to sample to drive the speaker with PWM.

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 0:a24aaf3a41b1 71
dkato 0:a24aaf3a41b1 72 while(1) {
dkato 9:c7c0a97fdb7f 73 // try to connect a storage device
dkato 9:c7c0a97fdb7f 74 while(!storage.connect()) {
dkato 0:a24aaf3a41b1 75 Thread::wait(500);
dkato 0:a24aaf3a41b1 76 }
dkato 9:c7c0a97fdb7f 77 storage.unmount();
dkato 9:c7c0a97fdb7f 78 storage.mount();
dkato 0:a24aaf3a41b1 79
dkato 0:a24aaf3a41b1 80 // in a loop, append a file
dkato 0:a24aaf3a41b1 81 // if the device is disconnected, we try to connect it again
dkato 0:a24aaf3a41b1 82 while(1) {
dkato 0:a24aaf3a41b1 83 // if device disconnected, try to connect again
dkato 9:c7c0a97fdb7f 84 if (!storage.connected()) {
dkato 0:a24aaf3a41b1 85 break;
dkato 0:a24aaf3a41b1 86 }
dkato 0:a24aaf3a41b1 87 if (fp == NULL) {
dkato 0:a24aaf3a41b1 88 // file search
dkato 0:a24aaf3a41b1 89 if (d == NULL) {
dkato 0:a24aaf3a41b1 90 d = opendir(FLD_PATH);
dkato 0:a24aaf3a41b1 91 }
dkato 0:a24aaf3a41b1 92 struct dirent * p;
dkato 0:a24aaf3a41b1 93 while ((p = readdir(d)) != NULL) {
dkato 0:a24aaf3a41b1 94 size_t len = strlen(p->d_name);
dkato 4:01651a6c3f9a 95 if ((len > 4) && (len < FILE_NAME_LEN)
dkato 9:c7c0a97fdb7f 96 && (strncasecmp(&p->d_name[len - 4], ".wav", 4) == 0)) {
dkato 0:a24aaf3a41b1 97 strcpy(file_path, FLD_PATH);
dkato 0:a24aaf3a41b1 98 strcat(file_path, p->d_name);
dkato 0:a24aaf3a41b1 99 fp = fopen(file_path, "r");
dkato 5:983467c1466b 100 if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
dkato 4:01651a6c3f9a 101 TEXT_SIZE, fp) == false) {
dkato 0:a24aaf3a41b1 102 fclose(fp);
dkato 0:a24aaf3a41b1 103 fp = NULL;
dkato 5:983467c1466b 104 } else if ((wav_file.GetChannel() != 2)
dkato 7:4b6799c255ea 105 || (audio.format(wav_file.GetBlockSize()) == false)
dkato 7:4b6799c255ea 106 || (audio.frequency(wav_file.GetSamplingRate()) == false)) {
dkato 7:4b6799c255ea 107 printf("Error File :%s\n", p->d_name);
dkato 7:4b6799c255ea 108 printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
dkato 7:4b6799c255ea 109 wav_file.GetBlockSize(), wav_file.GetSamplingRate());
dkato 7:4b6799c255ea 110 printf("\n");
dkato 5:983467c1466b 111 fclose(fp);
dkato 5:983467c1466b 112 fp = NULL;
dkato 0:a24aaf3a41b1 113 } else {
dkato 7:4b6799c255ea 114 printf("File :%s\n", p->d_name);
dkato 7:4b6799c255ea 115 printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(),
dkato 7:4b6799c255ea 116 wav_file.GetBlockSize(), wav_file.GetSamplingRate());
dkato 7:4b6799c255ea 117 printf("Title :%s\n", title_buf);
dkato 7:4b6799c255ea 118 printf("Artist :%s\n", artist_buf);
dkato 7:4b6799c255ea 119 printf("Album :%s\n", album_buf);
dkato 0:a24aaf3a41b1 120 printf("\n");
dkato 0:a24aaf3a41b1 121 break;
dkato 0:a24aaf3a41b1 122 }
dkato 0:a24aaf3a41b1 123 }
dkato 0:a24aaf3a41b1 124 }
dkato 0:a24aaf3a41b1 125 if (p == NULL) {
dkato 0:a24aaf3a41b1 126 closedir(d);
dkato 0:a24aaf3a41b1 127 d = NULL;
dkato 0:a24aaf3a41b1 128 }
dkato 0:a24aaf3a41b1 129 } else {
dkato 0:a24aaf3a41b1 130 // file read
dkato 9:c7c0a97fdb7f 131 audio_data_size = wav_file.GetNextData(audio_write_buff, AUDIO_WRITE_BUFF_SIZE);
dkato 4:01651a6c3f9a 132 if (audio_data_size > 0) {
dkato 9:c7c0a97fdb7f 133 audio.write(audio_write_buff, audio_data_size);
dkato 0:a24aaf3a41b1 134 }
dkato 0:a24aaf3a41b1 135
dkato 0:a24aaf3a41b1 136 // file close
dkato 9:c7c0a97fdb7f 137 if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (file_skip == true)) {
dkato 9:c7c0a97fdb7f 138 file_skip = false;
dkato 0:a24aaf3a41b1 139 fclose(fp);
dkato 0:a24aaf3a41b1 140 fp = NULL;
dkato 0:a24aaf3a41b1 141 Thread::wait(500);
dkato 0:a24aaf3a41b1 142 }
dkato 0:a24aaf3a41b1 143 }
dkato 0:a24aaf3a41b1 144 }
dkato 0:a24aaf3a41b1 145
dkato 0:a24aaf3a41b1 146 // close check
dkato 0:a24aaf3a41b1 147 if (fp != NULL) {
dkato 0:a24aaf3a41b1 148 fclose(fp);
dkato 0:a24aaf3a41b1 149 fp = NULL;
dkato 0:a24aaf3a41b1 150 }
dkato 0:a24aaf3a41b1 151 if (d != NULL) {
dkato 0:a24aaf3a41b1 152 closedir(d);
dkato 0:a24aaf3a41b1 153 d = NULL;
dkato 0:a24aaf3a41b1 154 }
dkato 0:a24aaf3a41b1 155 }
dkato 0:a24aaf3a41b1 156 }