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

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Wed Jun 24 10:00:46 2015 +0000
Parent:
4:01651a6c3f9a
Child:
6:ad0b3ce4284b
Commit message:
bug fixed

Changed in this revision

dec_wav.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/dec_wav.h	Wed Jun 24 09:05:12 2015 +0000
+++ b/dec_wav.h	Wed Jun 24 10:00:46 2015 +0000
@@ -19,7 +19,7 @@
      * @param fp file pointer
      * @return true = success, false = failure
      */
-    bool analyze_heder(uint8_t * p_title, uint8_t * p_artist, uint8_t * p_album, uint16_t tag_size, FILE * fp) {
+    bool AnalyzeHeder(uint8_t * p_title, uint8_t * p_artist, uint8_t * p_album, uint16_t tag_size, FILE * fp) {
         bool result = false;
         size_t read_size;
         uint8_t wk_read_buff[36];
@@ -31,9 +31,6 @@
         uint32_t read_index = 0;
         uint32_t data_index = 0;
         uint16_t wk_len;
-        uint16_t ch;
-        uint32_t sampling_rate;
-        uint16_t block_size;
 
         if (fp == NULL) {
             return false;
@@ -62,13 +59,13 @@
             // do nothing
         } else {
             read_index += 36;
-            ch = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8);
+            channel = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8);
             sampling_rate = ((uint32_t)wk_read_buff[24] << 0)
                           + ((uint32_t)wk_read_buff[25] << 8)
                           + ((uint32_t)wk_read_buff[26] << 16)
                           + ((uint32_t)wk_read_buff[27] << 24);
             block_size = ((uint32_t)wk_read_buff[34] << 0) + ((uint32_t)wk_read_buff[35] << 8);
-            if ((ch != 2) || (block_size != 16) || (sampling_rate != 44100)) {
+            if ((channel != 2) || (block_size != 16) || (sampling_rate != 44100)) {
                 return false;
             }
             while (1) {
@@ -152,7 +149,7 @@
      * @param len data buffer length
      * @return get data size
      */
-    size_t get_next_data(void *buf, size_t len) {
+    size_t GetNextData(void *buf, size_t len) {
         if ((music_data_index + len) > music_data_size) {
             len = music_data_size - music_data_index;
         }
@@ -161,8 +158,35 @@
         return fread(buf, sizeof(char), len, wav_fp);
     };
 
+    /** get channel
+     *
+     * @return channel
+     */
+    uint16_t GetChannel() {
+        return channel;
+    };
+
+    /** get block size
+     *
+     * @return block size
+     */
+    uint16_t GetBlockSize() {
+        return block_size;
+    };
+
+    /** get sampling rate
+     *
+     * @return sampling rate
+     */
+    uint16_t GetSamplingRate() {
+        return sampling_rate;
+    };
+
 private:
     FILE * wav_fp;
     uint32_t music_data_size;
     uint32_t music_data_index;
+    uint16_t channel;
+    uint16_t block_size;
+    uint32_t sampling_rate;
 };
--- a/main.cpp	Wed Jun 24 09:05:12 2015 +0000
+++ b/main.cpp	Wed Jun 24 10:00:46 2015 +0000
@@ -46,6 +46,13 @@
     size_t audio_data_size;
     dec_wav wav_file;
 
+#if (USB_HOST_CH == 1) //Audio Shield USB1
+    //Audio Shield USB1 enable
+    usb1en = 1;        //Outputs high level
+    Thread::wait(5);
+    usb1en = 0;        //Outputs low level
+#endif
+
     audio.power(0x02); // mic off
     audio.inputVolume(0.7, 0.7);
 
@@ -77,10 +84,15 @@
                         strcpy(file_path, FLD_PATH);
                         strcat(file_path, p->d_name);
                         fp = fopen(file_path, "r");
-                        if (wav_file.analyze_heder(title_buf, artist_buf, album_buf,
+                        if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf,
                                                    TEXT_SIZE, fp) == false) {
                             fclose(fp);
                             fp = NULL;
+                        } else if ((wav_file.GetChannel() != 2)
+                                || (wav_file.GetBlockSize() != 16)
+                                || (wav_file.GetSamplingRate() != 44100)) {
+                            fclose(fp);
+                            fp = NULL;
                         } else {
                             printf("File  :%s\n", p->d_name);
                             printf("Title :%s\n", title_buf);
@@ -99,7 +111,7 @@
                 // file read
                 uint8_t * p_buf = audio_write_buff[buff_index];
 
-                audio_data_size = wav_file.get_next_data(p_buf, AUDIO_WRITE_BUFF_SIZE);
+                audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE);
                 if (audio_data_size > 0) {
                     audio.write(p_buf, audio_data_size, &audio_write_async_ctl);
                     buff_index++;