This sample will play a ".wav" file of the USB root folder. Only RIFF format, 48kHz, 16bit, 2ch.
Dependencies: USBHostDac USBHost_custom_Addiso
Fork of USBHostDac_example by
dec_wav.h
00001 /**************************************************************************//** 00002 * @file dec_wav.h 00003 * @brief wav 00004 ******************************************************************************/ 00005 #include "mbed.h" 00006 00007 /** A class to communicate a dec_wav 00008 * 00009 */ 00010 class dec_wav { 00011 public: 00012 00013 /** analyze header 00014 * 00015 * @param p_title title tag buffer 00016 * @param p_artist artist tag buffer 00017 * @param p_album album tag buffer 00018 * @param tag_size tag buffer size 00019 * @param fp file pointer 00020 * @return true = success, false = failure 00021 */ 00022 bool AnalyzeHeder(uint8_t * p_title, uint8_t * p_artist, uint8_t * p_album, uint16_t tag_size, FILE * fp) { 00023 bool result = false; 00024 size_t read_size; 00025 uint8_t wk_read_buff[36]; 00026 uint8_t *data; 00027 uint32_t chunk_size; 00028 uint32_t sub_chunk_size; 00029 uint32_t list_index_max; 00030 bool list_ok = false; 00031 uint32_t read_index = 0; 00032 uint32_t data_index = 0; 00033 uint16_t wk_len; 00034 00035 if (fp == NULL) { 00036 return false; 00037 } 00038 music_data_size = 0; 00039 music_data_index = 0; 00040 wav_fp = fp; 00041 if (p_title != NULL) { 00042 p_title[0] = '\0'; 00043 } 00044 if (p_artist != NULL) { 00045 p_artist[0] = '\0'; 00046 } 00047 if (p_album != NULL) { 00048 p_album[0] = '\0'; 00049 } 00050 00051 read_size = fread(&wk_read_buff[0], sizeof(char), 36, wav_fp); 00052 if (read_size < 36) { 00053 // do nothing 00054 } else if (memcmp(&wk_read_buff[0], "RIFF", 4) != 0) { 00055 // do nothing 00056 } else if (memcmp(&wk_read_buff[8], "WAVE", 4) != 0) { 00057 // do nothing 00058 } else if (memcmp(&wk_read_buff[12], "fmt ", 4) != 0) { 00059 // do nothing 00060 } else { 00061 read_index += 36; 00062 channel = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8); 00063 sampling_rate = ((uint32_t)wk_read_buff[24] << 0) 00064 + ((uint32_t)wk_read_buff[25] << 8) 00065 + ((uint32_t)wk_read_buff[26] << 16) 00066 + ((uint32_t)wk_read_buff[27] << 24); 00067 block_size = ((uint32_t)wk_read_buff[34] << 0) + ((uint32_t)wk_read_buff[35] << 8); 00068 while (1) { 00069 read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp); 00070 read_index += 8; 00071 if (read_size < 8) { 00072 break; 00073 } else { 00074 chunk_size = ((uint32_t)wk_read_buff[4] << 0) 00075 + ((uint32_t)wk_read_buff[5] << 8) 00076 + ((uint32_t)wk_read_buff[6] << 16) 00077 + ((uint32_t)wk_read_buff[7] << 24); 00078 if (memcmp(&wk_read_buff[0], "data", 4) == 0) { 00079 result = true; 00080 music_data_size = chunk_size; 00081 if (list_ok == true) { 00082 break; 00083 } else { 00084 data_index = read_index; 00085 fseek(wav_fp, chunk_size, SEEK_CUR); 00086 read_index += chunk_size; 00087 } 00088 } else if (memcmp(&wk_read_buff[0], "LIST", 4) == 0) { 00089 list_ok = true; 00090 list_index_max = read_index + chunk_size; 00091 read_size = fread(&wk_read_buff[0], sizeof(char), 4, wav_fp); 00092 read_index += 4; 00093 while (read_index < list_index_max) { 00094 read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp); 00095 read_index += 8; 00096 if (read_size < 8) { 00097 break; 00098 } else if (memcmp(&wk_read_buff[0], "INAM", 4) == 0) { 00099 data = p_title; 00100 } else if (memcmp(&wk_read_buff[0], "IART", 4) == 0) { 00101 data = p_artist; 00102 } else if (memcmp(&wk_read_buff[0], "IPRD", 4) == 0) { 00103 data = p_album; 00104 } else { 00105 data = NULL; 00106 } 00107 if ((data != NULL) && (tag_size != 0)) { 00108 sub_chunk_size = ((uint32_t)wk_read_buff[4] << 0) 00109 + ((uint32_t)wk_read_buff[5] << 8) 00110 + ((uint32_t)wk_read_buff[6] << 16) 00111 + ((uint32_t)wk_read_buff[7] << 24); 00112 if (sub_chunk_size > (tag_size - 1)) { 00113 wk_len = (tag_size - 1); 00114 } else { 00115 wk_len = sub_chunk_size; 00116 } 00117 read_size = fread(data, sizeof(char), wk_len, wav_fp); 00118 read_index += sub_chunk_size; 00119 fseek(wav_fp, read_index, SEEK_SET); 00120 data[wk_len] = '\0'; 00121 } 00122 } 00123 if (data_index != 0) { 00124 break; 00125 } else { 00126 fseek(wav_fp, list_index_max, SEEK_SET); 00127 } 00128 } else { 00129 fseek(wav_fp, chunk_size, SEEK_CUR); 00130 read_index += chunk_size; 00131 } 00132 } 00133 } 00134 00135 if (data_index != 0) { 00136 fseek(wav_fp, data_index, SEEK_SET); 00137 } 00138 } 00139 00140 return result; 00141 }; 00142 00143 /** get next data 00144 * 00145 * @param buf data buffer address 00146 * @param len data buffer length 00147 * @return get data size 00148 */ 00149 size_t GetNextData(void *buf, size_t len) { 00150 if ((music_data_index + len) > music_data_size) { 00151 len = music_data_size - music_data_index; 00152 } 00153 music_data_index += len; 00154 00155 return fread(buf, sizeof(char), len, wav_fp); 00156 }; 00157 00158 /** get channel 00159 * 00160 * @return channel 00161 */ 00162 uint16_t GetChannel() { 00163 return channel; 00164 }; 00165 00166 /** get block size 00167 * 00168 * @return block size 00169 */ 00170 uint16_t GetBlockSize() { 00171 return block_size; 00172 }; 00173 00174 /** get sampling rate 00175 * 00176 * @return sampling rate 00177 */ 00178 uint16_t GetSamplingRate() { 00179 return sampling_rate; 00180 }; 00181 00182 private: 00183 FILE * wav_fp; 00184 uint32_t music_data_size; 00185 uint32_t music_data_index; 00186 uint16_t channel; 00187 uint16_t block_size; 00188 uint32_t sampling_rate; 00189 };
Generated on Wed Jul 13 2022 02:40:52 by 1.7.2