michael kosinski / Mbed OS RGA-MJPEG_VideoDemo

Dependencies:   GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP SDBlockDevice_GR_PEACH TLV320_RBSP USBHost_custom

Fork of RGA_HelloWorld by Renesas

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                 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 (block_size == 24) {
00151             // Add padding
00152             int write_index = 0;
00153             int wavfile_index;
00154             int read_len;
00155             int pading_index = 0;
00156             uint8_t * p_buf = (uint8_t *)buf;
00157             size_t ret;
00158 
00159             if ((music_data_index + len) > music_data_size) {
00160                 len = music_data_size - music_data_index;
00161             }
00162             while (write_index < len) {
00163                 read_len = (len - write_index) * 3 / 4;
00164                 if (read_len > sizeof(wk_wavfile_buff)) {
00165                     read_len = sizeof(wk_wavfile_buff);
00166                 }
00167                 music_data_index += read_len;
00168                 ret = fread(wk_wavfile_buff, sizeof(char), read_len, wav_fp);
00169                 if (ret < read_len) {
00170                     break;
00171                 }
00172                 wavfile_index = 0;
00173                 while ((write_index < len) && (wavfile_index < read_len)) {
00174                     if (pading_index == 0) {
00175                         p_buf[write_index] = 0;
00176                     } else {
00177                         p_buf[write_index] = wk_wavfile_buff[wavfile_index];
00178                         wavfile_index++;
00179                     }
00180                     if (pading_index < 3) {
00181                         pading_index++;
00182                     } else {
00183                         pading_index = 0;
00184                     }
00185                     write_index++;
00186                 }
00187             }
00188 
00189             return write_index;
00190         } else {
00191             if ((music_data_index + len) > music_data_size) {
00192                 len = music_data_size - music_data_index;
00193             }
00194             music_data_index += len;
00195 
00196             return fread(buf, sizeof(char), len, wav_fp);
00197         }
00198     };
00199 
00200     /** get channel
00201      *
00202      * @return channel
00203      */
00204     uint16_t GetChannel() {
00205         return channel;
00206     };
00207 
00208     /** get block size
00209      *
00210      * @return block size
00211      */
00212     uint16_t GetBlockSize() {
00213         return block_size;
00214     };
00215 
00216     /** get sampling rate
00217      *
00218      * @return sampling rate
00219      */
00220     uint32_t GetSamplingRate() {
00221         return sampling_rate;
00222     };
00223 
00224 private:
00225     #define FILE_READ_BUFF_SIZE    (3072)
00226 
00227     FILE * wav_fp;
00228     uint32_t music_data_size;
00229     uint32_t music_data_index;
00230     uint16_t channel;
00231     uint16_t block_size;
00232     uint32_t sampling_rate;
00233     uint8_t wk_wavfile_buff[FILE_READ_BUFF_SIZE];
00234 };