To call Azure Marketplace Translation (and Speech) service

Dependencies:   EthernetInterface-FRDM HTTPClient-SSL R_BSP SDFileSystem TLV320_RBSP USBHost mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dec_wav.h Source File

dec_wav.h

Go to the documentation of this file.
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                 //printf("dev_wav1\r\n");
00070                 read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
00071                 read_index += 8;
00072                 if (read_size < 8) {
00073                     break;
00074                 } else {
00075                     chunk_size = ((uint32_t)wk_read_buff[4] << 0)
00076                                + ((uint32_t)wk_read_buff[5] << 8)
00077                                + ((uint32_t)wk_read_buff[6] << 16)
00078                                + ((uint32_t)wk_read_buff[7] << 24);
00079                     if (memcmp(&wk_read_buff[0], "data", 4) == 0) {
00080                         result = true;
00081                         music_data_size = chunk_size;
00082                         if (list_ok == true) {
00083                             break;
00084                         } else {
00085                             data_index = read_index;
00086                             fseek(wav_fp, chunk_size, SEEK_CUR);
00087                             read_index += chunk_size;
00088                         }
00089                         //printf("dev_wav2\r\n");
00090                     } else if (memcmp(&wk_read_buff[2], "data", 4) == 0) {
00091                         read_index += 2;
00092                         chunk_size = ((uint32_t)wk_read_buff[6] << 0)
00093                                 + ((uint32_t)wk_read_buff[7] << 8);
00094                         result = true;
00095                         music_data_size = chunk_size;
00096                         if (list_ok == true) {
00097                             break;
00098                         } else {
00099                             data_index = read_index;
00100                             fseek(wav_fp, chunk_size, SEEK_CUR);
00101                             read_index += chunk_size;
00102                         }                    
00103                     } else if (memcmp(&wk_read_buff[0], "LIST", 4) == 0) {
00104                         list_ok = true;
00105                         list_index_max = read_index + chunk_size;
00106                         read_size = fread(&wk_read_buff[0], sizeof(char), 4, wav_fp);
00107                         read_index += 4;
00108                         while (read_index < list_index_max) {
00109                             read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
00110                             read_index += 8;
00111                             if (read_size < 8) {
00112                                 break;
00113                             } else if (memcmp(&wk_read_buff[0], "INAM", 4) == 0) {
00114                                 data = p_title;
00115                             } else if (memcmp(&wk_read_buff[0], "IART", 4) == 0) {
00116                                 data = p_artist;
00117                             } else if (memcmp(&wk_read_buff[0], "IPRD", 4) == 0) {
00118                                 data = p_album;
00119                             } else {
00120                                 data = NULL;
00121                             }
00122                             if ((data != NULL) && (tag_size != 0)) {
00123                                 sub_chunk_size = ((uint32_t)wk_read_buff[4] << 0)
00124                                                + ((uint32_t)wk_read_buff[5] << 8)
00125                                                + ((uint32_t)wk_read_buff[6] << 16)
00126                                                + ((uint32_t)wk_read_buff[7] << 24);
00127                                 if (sub_chunk_size > (tag_size - 1)) {
00128                                     wk_len = (tag_size - 1);
00129                                 } else {
00130                                     wk_len = sub_chunk_size;
00131                                 }
00132                                 read_size = fread(data, sizeof(char), wk_len, wav_fp);
00133                                 read_index += sub_chunk_size;
00134                                 fseek(wav_fp, read_index, SEEK_SET);
00135                                 data[wk_len] = '\0';
00136                             }
00137                         }
00138                         if (data_index != 0) {
00139                             break;
00140                         } else {
00141                             fseek(wav_fp, list_index_max, SEEK_SET);
00142                         }
00143                         //printf("dev_wav3\r\n");
00144                     } else {
00145                         fseek(wav_fp, chunk_size, SEEK_CUR);
00146                         read_index += chunk_size;
00147                         //printf("dev_wav4\r\n");
00148                     }
00149                 }
00150             }
00151 
00152             if (data_index != 0) {
00153                 fseek(wav_fp, data_index, SEEK_SET);
00154                 //printf("dev_wav5\r\n");
00155             }
00156         }
00157 
00158         return result;
00159     };
00160 
00161     /** get next data
00162      *
00163      * @param buf data buffer address
00164      * @param len data buffer length
00165      * @return get data size
00166      */
00167     size_t GetNextData(void *buf, size_t len) {
00168         if (block_size == 24) {
00169             // Add padding
00170             int write_index = 0;
00171             int wavfile_index;
00172             int read_len;
00173             int pading_index = 0;
00174             uint8_t * p_buf = (uint8_t *)buf;
00175             size_t ret;
00176 
00177             if ((music_data_index + len) > music_data_size) {
00178                 len = music_data_size - music_data_index;
00179             }
00180             while (write_index < len) {
00181                 read_len = (len - write_index) * 3 / 4;
00182                 if (read_len > sizeof(wk_wavfile_buff)) {
00183                     read_len = sizeof(wk_wavfile_buff);
00184                 }
00185                 music_data_index += read_len;
00186                 ret = fread(wk_wavfile_buff, sizeof(char), read_len, wav_fp);
00187                 if (ret < read_len) {
00188                     break;
00189                 }
00190                 wavfile_index = 0;
00191                 while ((write_index < len) && (wavfile_index < read_len)) {
00192                     if (pading_index == 0) {
00193                         p_buf[write_index] = 0;
00194                     } else {
00195                         p_buf[write_index] = wk_wavfile_buff[wavfile_index];
00196                         wavfile_index++;
00197                     }
00198                     if (pading_index < 3) {
00199                         pading_index++;
00200                     } else {
00201                         pading_index = 0;
00202                     }
00203                     write_index++;
00204                 }
00205             }
00206 
00207             return write_index;
00208         } else {
00209             if ((music_data_index + len) > music_data_size) {
00210                 len = music_data_size - music_data_index;
00211             }
00212             music_data_index += len;
00213 
00214             return fread(buf, sizeof(char), len, wav_fp);
00215         }
00216     };
00217 
00218     /** get channel
00219      *
00220      * @return channel
00221      */
00222     uint16_t GetChannel() {
00223         return channel;
00224     };
00225 
00226     /** get block size
00227      *
00228      * @return block size
00229      */
00230     uint16_t GetBlockSize() {
00231         return block_size;
00232     };
00233 
00234     /** get sampling rate
00235      *
00236      * @return sampling rate
00237      */
00238     uint32_t GetSamplingRate() {
00239         return sampling_rate;
00240     };
00241 
00242 private:
00243     #define FILE_READ_BUFF_SIZE    (3072)
00244 
00245     FILE * wav_fp;
00246     uint32_t music_data_size;
00247     uint32_t music_data_index;
00248     uint16_t channel;
00249     uint16_t block_size;
00250     uint32_t sampling_rate;
00251     uint8_t wk_wavfile_buff[FILE_READ_BUFF_SIZE];
00252 };