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

Revision:
11:221c23d820d9
Parent:
10:e8f52c4aa394
Child:
13:7e3063fc0e10
--- a/main.cpp	Tue Dec 20 07:28:04 2016 +0000
+++ b/main.cpp	Fri Mar 24 04:30:17 2017 +0000
@@ -2,25 +2,17 @@
 #include "rtos.h"
 #include "PwmOutSpeaker.h"
 #include "dec_wav.h"
+#include "FATFileSystem.h"
+#include "SDBlockDevice_GR_PEACH.h"
+#include "USBHostMSD.h"
 
-/**** User Selection *********/
-/** Storage setting **/
-#define STORAGE_TYPE                        (0)     /* Select 0(SD) or 1(USB) */
-/*****************************/
-
-/** Storage config **/
-#if (STORAGE_TYPE == 0)
-  #include "SDFileSystem_GR_PEACH.h"
+#if defined(TARGET_RZ_A1H)
+#include "usb_host_setting.h"
 #else
-  #include "USBHostMSD.h"
-  #if defined(TARGET_RZ_A1H)
-    #include "usb_host_setting.h"
-  #else
-    #define USB_HOST_CH     0
-  #endif
-  #if (USB_HOST_CH == 1) //Audio Shield USB1
-    static DigitalOut usb1en(P3_8);
-  #endif
+#define USB_HOST_CH     0
+#endif
+#if (USB_HOST_CH == 1) //Audio Shield USB1
+static DigitalOut usb1en(P3_8);
 #endif
 
 #define AUDIO_WRITE_BUFF_SIZE  (4096)
@@ -55,35 +47,46 @@
     char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN];
     size_t audio_data_size;
     dec_wav wav_file;
+    int storage_type = 0;
 
     button.fall(&button_fall);
-#if (STORAGE_TYPE == 0)
-    SDFileSystem_GR_PEACH storage(MOUNT_NAME);
-#else
+
 #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
-    USBHostMSD storage(MOUNT_NAME);
-#endif
-    audio.outputVolume(0.10f);  // Volume control (min:0.0 max:1.0)
+    FATFileSystem fs(MOUNT_NAME);
+    SDBlockDevice_GR_PEACH sd;
+    USBHostMSD usb;
+
+    audio.outputVolume(1.0f);  // Volume control (min:0.0 max:1.0)
 
     while(1) {
         // try to connect a storage device
-        while(!storage.connect()) {
+        while (1) {
+            if (sd.connect()) {
+                storage_type = 0; // SD
+                fs.mount(&sd);
+                break;
+            }
+            if (usb.connect()) {
+                storage_type = 1; // USB
+                fs.mount(&usb);
+                break;
+            }
             Thread::wait(500);
         }
-        storage.unmount();
-        storage.mount();
 
         // in a loop, append a file
         // if the device is disconnected, we try to connect it again
         while(1) {
             // if device disconnected, try to connect again
-            if (!storage.connected()) {
-                break;
+            if (storage_type == 0) {
+                if (!sd.connected())  break;
+            } else {
+                if (!usb.connected()) break;
             }
             if (fp == NULL) {
                 // file search
@@ -153,5 +156,6 @@
             closedir(d);
             d = NULL;
         }
+        fs.unmount();
     }
 }