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

main.cpp

Committer:
dkato
Date:
2017-07-06
Revision:
13:7e3063fc0e10
Parent:
11:221c23d820d9
Child:
14:f3eda9558df6

File content as of revision 13:7e3063fc0e10:

#include "mbed.h"
#include "EasyPlaybackPWM.h"
#include "EasyDec_WavCnv2ch.h"
#include "FATFileSystem.h"
#include "SDBlockDevice_GR_PEACH.h"
#include "USBHostMSD.h"

#define FILE_NAME_LEN          (64)
#define MOUNT_NAME             "storage"
#define TAG_BUFF_SIZE          (64 + 1) //null-terminated

static InterruptIn skip_btn(USER_BUTTON0);
static EasyPlaybackPWM AudioPlayer(P4_5, P4_7);

static void skip_btn_fall(void) {
    AudioPlayer.skip();
}

int main() {
    DIR  * d;
    struct dirent * p;
    char file_path[sizeof("/"MOUNT_NAME"/") + FILE_NAME_LEN];
    FATFileSystem fs("storage");
    SDBlockDevice_GR_PEACH sd;
    USBHostMSD usb;

    // decoder setting
    AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".wav");
    AudioPlayer.add_decoder<EasyDec_WavCnv2ch>(".WAV");

    // volume control
    AudioPlayer.outputVolume(0.10f);  // Volume control (min:0.0 max:1.0)

    // button setting
    skip_btn.fall(&skip_btn_fall);

    // wait for the storage device to be connected
    printf("Finding a storage...\r\n");
    while (1) {
        if (sd.connect()) {
            fs.mount(&sd);
            break;
        }
        if (usb.connect()) {
            fs.mount(&usb);
            break;
        }
        Thread::wait(500);
    }
    printf("done\r\n");

    while(1) {
        // file search
        d = opendir("/"MOUNT_NAME"/");
        while ((p = readdir(d)) != NULL) {
            size_t len = strlen(p->d_name);
            if (len < FILE_NAME_LEN) {
                // make file path
                sprintf(file_path, "/%s/%s", MOUNT_NAME, p->d_name);
                printf("%s\r\n", file_path);

                // playback
                AudioPlayer.play(file_path);
            }
        }
        closedir(d);
    }
}