This is a sample that drives a speaker with PWM.
Fork of GR-PEACH_Audio_WAV by
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.
Format | Wav file (RIFF format) ".wav" |
Channel | 1ch and 2ch |
Frequencies | 32kHz, 44.1kHz and 48kHz |
Quantization bit rate | 8bits 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:
- 2016-12-20
- Revision:
- 10:e8f52c4aa394
- Parent:
- 9:c7c0a97fdb7f
- Child:
- 11:221c23d820d9
File content as of revision 10:e8f52c4aa394:
#include "mbed.h" #include "rtos.h" #include "PwmOutSpeaker.h" #include "dec_wav.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" #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 #endif #define AUDIO_WRITE_BUFF_SIZE (4096) #define FILE_NAME_LEN (64) #define TEXT_SIZE (64 + 1) //null-terminated #define FLD_PATH "/storage/" #define MOUNT_NAME "storage" //4 bytes aligned! No cache memory #if defined(__ICCARM__) #pragma data_alignment=4 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_SIZE]; #else static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_SIZE]__attribute((aligned(4))); #endif //Tag buffer static uint8_t title_buf[TEXT_SIZE]; static uint8_t artist_buf[TEXT_SIZE]; static uint8_t album_buf[TEXT_SIZE]; static bool file_skip = false; static PwmOutSpeaker audio(P4_5, P4_7); static InterruptIn button(USER_BUTTON0); static void button_fall(void) { file_skip = true; } int main() { FILE * fp = NULL; DIR * d = NULL; char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN]; size_t audio_data_size; dec_wav wav_file; 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) while(1) { // try to connect a storage device while(!storage.connect()) { 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 (fp == NULL) { // file search if (d == NULL) { d = opendir(FLD_PATH); } struct dirent * p; while ((p = readdir(d)) != NULL) { size_t len = strlen(p->d_name); if ((len > 4) && (len < FILE_NAME_LEN) && (strncasecmp(&p->d_name[len - 4], ".wav", 4) == 0)) { strcpy(file_path, FLD_PATH); strcat(file_path, p->d_name); fp = fopen(file_path, "r"); if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf, TEXT_SIZE, fp) == false) { fclose(fp); fp = NULL; } else if ((wav_file.GetChannel() != 2) || (audio.format(wav_file.GetBlockSize()) == false) || (audio.frequency(wav_file.GetSamplingRate()) == false)) { printf("Error File :%s\n", p->d_name); printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate()); printf("\n"); fclose(fp); fp = NULL; } else { printf("File :%s\n", p->d_name); printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate()); printf("Title :%s\n", title_buf); printf("Artist :%s\n", artist_buf); printf("Album :%s\n", album_buf); printf("\n"); break; } } } if (p == NULL) { closedir(d); d = NULL; } } else { // file read audio_data_size = wav_file.GetNextData(audio_write_buff, AUDIO_WRITE_BUFF_SIZE); if (audio_data_size > 0) { audio.write(audio_write_buff, audio_data_size); } // file close if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (file_skip == true)) { file_skip = false; fclose(fp); fp = NULL; Thread::wait(500); } } } // close check if (fp != NULL) { fclose(fp); fp = NULL; } if (d != NULL) { closedir(d); d = NULL; } } }