Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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@7:4b6799c255ea, 2015-08-18 (annotated)
- Committer:
- dkato
- Date:
- Tue Aug 18 04:19:19 2015 +0000
- Revision:
- 7:4b6799c255ea
- Parent:
- 5:983467c1466b
- Child:
- 8:7121197d098e
High-Resolution Audio
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:a24aaf3a41b1 | 1 | #include "mbed.h" |
dkato | 0:a24aaf3a41b1 | 2 | #include "TLV320_RBSP.h" |
dkato | 0:a24aaf3a41b1 | 3 | #include "USBHostMSD.h" |
dkato | 4:01651a6c3f9a | 4 | #include "dec_wav.h" |
dkato | 4:01651a6c3f9a | 5 | |
dkato | 0:a24aaf3a41b1 | 6 | #if defined(TARGET_RZ_A1H) |
dkato | 0:a24aaf3a41b1 | 7 | #include "usb_host_setting.h" |
dkato | 0:a24aaf3a41b1 | 8 | #else |
dkato | 0:a24aaf3a41b1 | 9 | #define USB_HOST_CH 0 |
dkato | 0:a24aaf3a41b1 | 10 | #endif |
dkato | 0:a24aaf3a41b1 | 11 | |
dkato | 0:a24aaf3a41b1 | 12 | #if (USB_HOST_CH == 1) //Audio Shield USB1 |
dkato | 0:a24aaf3a41b1 | 13 | DigitalOut usb1en(P3_8); |
dkato | 0:a24aaf3a41b1 | 14 | #endif |
dkato | 0:a24aaf3a41b1 | 15 | DigitalIn button(USER_BUTTON0); |
dkato | 0:a24aaf3a41b1 | 16 | |
dkato | 4:01651a6c3f9a | 17 | #define AUDIO_WRITE_BUFF_SIZE (4096) |
dkato | 4:01651a6c3f9a | 18 | #define AUDIO_WRITE_BUFF_NUM (9) |
dkato | 0:a24aaf3a41b1 | 19 | #define FILE_NAME_LEN (64) |
dkato | 4:01651a6c3f9a | 20 | #define TEXT_SIZE (64 + 1) //null-terminated |
dkato | 1:967144cffd53 | 21 | #define FLD_PATH "/usb/" |
dkato | 0:a24aaf3a41b1 | 22 | |
dkato | 4:01651a6c3f9a | 23 | //4 bytes aligned! No cache memory |
dkato | 4:01651a6c3f9a | 24 | static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE] |
dkato | 4:01651a6c3f9a | 25 | __attribute((section("NC_BSS"),aligned(4))); |
dkato | 4:01651a6c3f9a | 26 | //Tag buffer |
dkato | 4:01651a6c3f9a | 27 | static uint8_t title_buf[TEXT_SIZE]; |
dkato | 4:01651a6c3f9a | 28 | static uint8_t artist_buf[TEXT_SIZE]; |
dkato | 4:01651a6c3f9a | 29 | static uint8_t album_buf[TEXT_SIZE]; |
dkato | 0:a24aaf3a41b1 | 30 | |
dkato | 7:4b6799c255ea | 31 | TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6, |
dkato | 4:01651a6c3f9a | 32 | 0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0); |
dkato | 0:a24aaf3a41b1 | 33 | |
dkato | 4:01651a6c3f9a | 34 | static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data) { |
dkato | 0:a24aaf3a41b1 | 35 | if (result < 0) { |
dkato | 4:01651a6c3f9a | 36 | printf("audio write callback error %d\n", result); |
dkato | 0:a24aaf3a41b1 | 37 | } |
dkato | 0:a24aaf3a41b1 | 38 | } |
dkato | 0:a24aaf3a41b1 | 39 | |
dkato | 4:01651a6c3f9a | 40 | int main() { |
dkato | 4:01651a6c3f9a | 41 | rbsp_data_conf_t audio_write_async_ctl = {&callback_audio_write_end, NULL}; |
dkato | 0:a24aaf3a41b1 | 42 | FILE * fp = NULL; |
dkato | 0:a24aaf3a41b1 | 43 | DIR * d = NULL; |
dkato | 0:a24aaf3a41b1 | 44 | char file_path[sizeof(FLD_PATH) + FILE_NAME_LEN]; |
dkato | 4:01651a6c3f9a | 45 | int buff_index = 0; |
dkato | 1:967144cffd53 | 46 | size_t audio_data_size; |
dkato | 4:01651a6c3f9a | 47 | dec_wav wav_file; |
dkato | 0:a24aaf3a41b1 | 48 | |
dkato | 5:983467c1466b | 49 | #if (USB_HOST_CH == 1) //Audio Shield USB1 |
dkato | 5:983467c1466b | 50 | //Audio Shield USB1 enable |
dkato | 5:983467c1466b | 51 | usb1en = 1; //Outputs high level |
dkato | 5:983467c1466b | 52 | Thread::wait(5); |
dkato | 5:983467c1466b | 53 | usb1en = 0; //Outputs low level |
dkato | 5:983467c1466b | 54 | #endif |
dkato | 5:983467c1466b | 55 | |
dkato | 0:a24aaf3a41b1 | 56 | audio.power(0x02); // mic off |
dkato | 0:a24aaf3a41b1 | 57 | audio.inputVolume(0.7, 0.7); |
dkato | 0:a24aaf3a41b1 | 58 | |
dkato | 0:a24aaf3a41b1 | 59 | USBHostMSD msd("usb"); |
dkato | 0:a24aaf3a41b1 | 60 | |
dkato | 0:a24aaf3a41b1 | 61 | while(1) { |
dkato | 0:a24aaf3a41b1 | 62 | // try to connect a MSD device |
dkato | 0:a24aaf3a41b1 | 63 | while(!msd.connect()) { |
dkato | 0:a24aaf3a41b1 | 64 | Thread::wait(500); |
dkato | 0:a24aaf3a41b1 | 65 | } |
dkato | 0:a24aaf3a41b1 | 66 | |
dkato | 0:a24aaf3a41b1 | 67 | // in a loop, append a file |
dkato | 0:a24aaf3a41b1 | 68 | // if the device is disconnected, we try to connect it again |
dkato | 0:a24aaf3a41b1 | 69 | while(1) { |
dkato | 0:a24aaf3a41b1 | 70 | // if device disconnected, try to connect again |
dkato | 0:a24aaf3a41b1 | 71 | if (!msd.connected()) { |
dkato | 0:a24aaf3a41b1 | 72 | break; |
dkato | 0:a24aaf3a41b1 | 73 | } |
dkato | 0:a24aaf3a41b1 | 74 | if (fp == NULL) { |
dkato | 0:a24aaf3a41b1 | 75 | // file search |
dkato | 0:a24aaf3a41b1 | 76 | if (d == NULL) { |
dkato | 0:a24aaf3a41b1 | 77 | d = opendir(FLD_PATH); |
dkato | 0:a24aaf3a41b1 | 78 | } |
dkato | 0:a24aaf3a41b1 | 79 | struct dirent * p; |
dkato | 0:a24aaf3a41b1 | 80 | while ((p = readdir(d)) != NULL) { |
dkato | 0:a24aaf3a41b1 | 81 | size_t len = strlen(p->d_name); |
dkato | 4:01651a6c3f9a | 82 | if ((len > 4) && (len < FILE_NAME_LEN) |
dkato | 4:01651a6c3f9a | 83 | && (memcmp(&p->d_name[len - 4], ".wav", 4) == 0)) { |
dkato | 0:a24aaf3a41b1 | 84 | strcpy(file_path, FLD_PATH); |
dkato | 0:a24aaf3a41b1 | 85 | strcat(file_path, p->d_name); |
dkato | 0:a24aaf3a41b1 | 86 | fp = fopen(file_path, "r"); |
dkato | 5:983467c1466b | 87 | if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf, |
dkato | 4:01651a6c3f9a | 88 | TEXT_SIZE, fp) == false) { |
dkato | 0:a24aaf3a41b1 | 89 | fclose(fp); |
dkato | 0:a24aaf3a41b1 | 90 | fp = NULL; |
dkato | 5:983467c1466b | 91 | } else if ((wav_file.GetChannel() != 2) |
dkato | 7:4b6799c255ea | 92 | || (audio.format(wav_file.GetBlockSize()) == false) |
dkato | 7:4b6799c255ea | 93 | || (audio.frequency(wav_file.GetSamplingRate()) == false)) { |
dkato | 7:4b6799c255ea | 94 | printf("Error File :%s\n", p->d_name); |
dkato | 7:4b6799c255ea | 95 | printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(), |
dkato | 7:4b6799c255ea | 96 | wav_file.GetBlockSize(), wav_file.GetSamplingRate()); |
dkato | 7:4b6799c255ea | 97 | printf("\n"); |
dkato | 5:983467c1466b | 98 | fclose(fp); |
dkato | 5:983467c1466b | 99 | fp = NULL; |
dkato | 0:a24aaf3a41b1 | 100 | } else { |
dkato | 7:4b6799c255ea | 101 | printf("File :%s\n", p->d_name); |
dkato | 7:4b6799c255ea | 102 | printf("Audio Info :%dch, %dbit, %dHz\n", wav_file.GetChannel(), |
dkato | 7:4b6799c255ea | 103 | wav_file.GetBlockSize(), wav_file.GetSamplingRate()); |
dkato | 7:4b6799c255ea | 104 | printf("Title :%s\n", title_buf); |
dkato | 7:4b6799c255ea | 105 | printf("Artist :%s\n", artist_buf); |
dkato | 7:4b6799c255ea | 106 | printf("Album :%s\n", album_buf); |
dkato | 0:a24aaf3a41b1 | 107 | printf("\n"); |
dkato | 0:a24aaf3a41b1 | 108 | break; |
dkato | 0:a24aaf3a41b1 | 109 | } |
dkato | 0:a24aaf3a41b1 | 110 | } |
dkato | 0:a24aaf3a41b1 | 111 | } |
dkato | 0:a24aaf3a41b1 | 112 | if (p == NULL) { |
dkato | 0:a24aaf3a41b1 | 113 | closedir(d); |
dkato | 0:a24aaf3a41b1 | 114 | d = NULL; |
dkato | 0:a24aaf3a41b1 | 115 | } |
dkato | 0:a24aaf3a41b1 | 116 | } else { |
dkato | 0:a24aaf3a41b1 | 117 | // file read |
dkato | 4:01651a6c3f9a | 118 | uint8_t * p_buf = audio_write_buff[buff_index]; |
dkato | 0:a24aaf3a41b1 | 119 | |
dkato | 5:983467c1466b | 120 | audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE); |
dkato | 4:01651a6c3f9a | 121 | if (audio_data_size > 0) { |
dkato | 4:01651a6c3f9a | 122 | audio.write(p_buf, audio_data_size, &audio_write_async_ctl); |
dkato | 4:01651a6c3f9a | 123 | buff_index++; |
dkato | 4:01651a6c3f9a | 124 | if (buff_index >= AUDIO_WRITE_BUFF_NUM) { |
dkato | 4:01651a6c3f9a | 125 | buff_index = 0; |
dkato | 0:a24aaf3a41b1 | 126 | } |
dkato | 0:a24aaf3a41b1 | 127 | } |
dkato | 0:a24aaf3a41b1 | 128 | |
dkato | 0:a24aaf3a41b1 | 129 | // file close |
dkato | 4:01651a6c3f9a | 130 | if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE) || (button == 0)) { |
dkato | 0:a24aaf3a41b1 | 131 | fclose(fp); |
dkato | 0:a24aaf3a41b1 | 132 | fp = NULL; |
dkato | 0:a24aaf3a41b1 | 133 | Thread::wait(500); |
dkato | 0:a24aaf3a41b1 | 134 | } |
dkato | 0:a24aaf3a41b1 | 135 | } |
dkato | 0:a24aaf3a41b1 | 136 | } |
dkato | 0:a24aaf3a41b1 | 137 | |
dkato | 0:a24aaf3a41b1 | 138 | // close check |
dkato | 0:a24aaf3a41b1 | 139 | if (fp != NULL) { |
dkato | 0:a24aaf3a41b1 | 140 | fclose(fp); |
dkato | 0:a24aaf3a41b1 | 141 | fp = NULL; |
dkato | 0:a24aaf3a41b1 | 142 | } |
dkato | 0:a24aaf3a41b1 | 143 | if (d != NULL) { |
dkato | 0:a24aaf3a41b1 | 144 | closedir(d); |
dkato | 0:a24aaf3a41b1 | 145 | d = NULL; |
dkato | 0:a24aaf3a41b1 | 146 | } |
dkato | 0:a24aaf3a41b1 | 147 | } |
dkato | 0:a24aaf3a41b1 | 148 | } |