Anh Tran / Mbed OS GR-Boards_WebCamera

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EasyDec_WavCnv2ch.cpp Source File

EasyDec_WavCnv2ch.cpp

00001 /* mbed EasyDec_WavCnv2ch Library
00002  * Copyright (C) 2017 dkato
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "EasyDec_WavCnv2ch.h"
00018 
00019 bool EasyDec_WavCnv2ch::AnalyzeHeder(char* p_title, char* p_artist, char* p_album, uint16_t tag_size, FILE* fp) {
00020     bool result = false;
00021     size_t read_size;
00022     uint8_t wk_read_buff[36];
00023     char *data;
00024     uint32_t chunk_size;
00025     uint32_t sub_chunk_size;
00026     uint32_t list_index_max;
00027     bool list_ok = false;
00028     uint32_t read_index = 0;
00029     uint32_t data_index = 0;
00030     uint16_t wk_len;
00031 
00032     if (fp == NULL) {
00033         return false;
00034     }
00035     music_data_size  = 0;
00036     music_data_index = 0;
00037     wav_fp = fp;
00038     if (p_title != NULL) {
00039         p_title[0] = '\0';
00040     }
00041     if (p_artist != NULL) {
00042         p_artist[0] = '\0';
00043     }
00044     if (p_album != NULL) {
00045         p_album[0] = '\0';
00046     }
00047 
00048     read_size = fread(&wk_read_buff[0], sizeof(char), 36, wav_fp);
00049     if (read_size < 36) {
00050         // do nothing
00051     } else if (memcmp(&wk_read_buff[0], "RIFF", 4) != 0) {
00052         // do nothing
00053     } else if (memcmp(&wk_read_buff[8], "WAVE", 4) != 0) {
00054         // do nothing
00055     } else if (memcmp(&wk_read_buff[12], "fmt ", 4) != 0) {
00056         // do nothing
00057     } else {
00058         read_index += 36;
00059         channel = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8);
00060         sampling_rate = ((uint32_t)wk_read_buff[24] << 0)
00061                       + ((uint32_t)wk_read_buff[25] << 8)
00062                       + ((uint32_t)wk_read_buff[26] << 16)
00063                       + ((uint32_t)wk_read_buff[27] << 24);
00064         block_size = ((uint32_t)wk_read_buff[34] << 0) + ((uint32_t)wk_read_buff[35] << 8);
00065         while (1) {
00066             read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
00067             read_index += 8;
00068             if (read_size < 8) {
00069                 break;
00070             } else {
00071                 chunk_size = ((uint32_t)wk_read_buff[4] << 0)
00072                            + ((uint32_t)wk_read_buff[5] << 8)
00073                            + ((uint32_t)wk_read_buff[6] << 16)
00074                            + ((uint32_t)wk_read_buff[7] << 24);
00075                 if (memcmp(&wk_read_buff[0], "data", 4) == 0) {
00076                     result = true;
00077                     music_data_size = chunk_size;
00078                     if (list_ok == true) {
00079                         break;
00080                     } else {
00081                         data_index = read_index;
00082                         fseek(wav_fp, chunk_size, SEEK_CUR);
00083                         read_index += chunk_size;
00084                     }
00085                 } else if (memcmp(&wk_read_buff[0], "LIST", 4) == 0) {
00086                     list_ok = true;
00087                     list_index_max = read_index + chunk_size;
00088                     read_size = fread(&wk_read_buff[0], sizeof(char), 4, wav_fp);
00089                     read_index += 4;
00090                     while (read_index < list_index_max) {
00091                         read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
00092                         read_index += 8;
00093                         if (read_size < 8) {
00094                             break;
00095                         } else if (memcmp(&wk_read_buff[0], "INAM", 4) == 0) {
00096                             data = p_title;
00097                         } else if (memcmp(&wk_read_buff[0], "IART", 4) == 0) {
00098                             data = p_artist;
00099                         } else if (memcmp(&wk_read_buff[0], "IPRD", 4) == 0) {
00100                             data = p_album;
00101                         } else {
00102                             data = NULL;
00103                         }
00104                         sub_chunk_size = ((uint32_t)wk_read_buff[4] << 0)
00105                                        + ((uint32_t)wk_read_buff[5] << 8)
00106                                        + ((uint32_t)wk_read_buff[6] << 16)
00107                                        + ((uint32_t)wk_read_buff[7] << 24);
00108                         if ((data != NULL) && (tag_size != 0)) {
00109                             if (sub_chunk_size > (uint32_t)(tag_size - 1)) {
00110                                 wk_len = (tag_size - 1);
00111                             } else {
00112                                 wk_len = sub_chunk_size;
00113                             }
00114                             read_size = fread(data, sizeof(char), wk_len, wav_fp);
00115                             data[wk_len] = '\0';
00116                         }
00117                         if ((sub_chunk_size & 0x00000001) != 0) {
00118                             sub_chunk_size += 1;
00119                         }
00120                         read_index += sub_chunk_size;
00121                         fseek(wav_fp, read_index, SEEK_SET);
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 size_t EasyDec_WavCnv2ch::GetNextData(void *buf, size_t len) {
00144     size_t ret_size;
00145     size_t read_max = len;
00146 
00147     if (block_size < 8) {
00148         return -1;
00149     }
00150 
00151     if ((channel == 1) && (len != 0)) {
00152         read_max /= 2;
00153     }
00154 
00155     if ((music_data_index + read_max) > music_data_size) {
00156         read_max = music_data_size - music_data_index;
00157     }
00158 
00159     if (read_max <= 0) {
00160         return 0;
00161     }
00162 
00163     music_data_index += read_max;
00164 
00165     if (buf == NULL) {
00166         fseek(wav_fp, read_max, SEEK_CUR);
00167         ret_size = read_max;
00168         if ((channel == 1) && (ret_size > 0)) {
00169             ret_size *= 2;
00170         }
00171     } else {
00172         ret_size = fread(buf, sizeof(char), read_max, wav_fp);
00173 
00174         if ((channel == 1) && (ret_size > 0)) {
00175             int block_byte;
00176             int idx_w = len - 1;
00177             int idx_r = ret_size - 1;
00178             int idx_r_last;
00179             int j;
00180 
00181             block_byte = (block_size + 7) / 8;
00182             while (idx_w >= 0) {
00183                 idx_r_last = idx_r;
00184                 for (j = 0; j < block_byte; j++) {
00185                     ((uint8_t*)buf)[idx_w--] = ((uint8_t*)buf)[idx_r--];
00186                 }
00187                 idx_r = idx_r_last;
00188                 for (j = 0; j < block_byte; j++) {
00189                     ((uint8_t*)buf)[idx_w--] = ((uint8_t*)buf)[idx_r--];
00190                 }
00191             }
00192             ret_size *= 2;
00193         }
00194     }
00195 
00196     return ret_size;
00197 }
00198 
00199 uint16_t EasyDec_WavCnv2ch::GetChannel() {
00200     if (channel == 1) {
00201         return 2;
00202     } else {
00203         return channel;
00204     }
00205 }
00206 
00207 uint16_t EasyDec_WavCnv2ch::GetBlockSize() {
00208     return block_size;
00209 }
00210 
00211 uint32_t EasyDec_WavCnv2ch::GetSamplingRate() {
00212     return sampling_rate;
00213 }
00214 
00215