bool WavDecoder::Init() { DataStream* ds = m_ds; ::memset(&m_format,0,sizeof(m_format)); //LOG_DEBUG("init"); /// read shunks while (!ds->Eof()) { Byte chunk_name[4]; if (ds->Read(chunk_name,4)!=4) break; UInt32 chunk_size = 0; if (ds->Read(reinterpret_cast<Byte*>(&chunk_size),4)!=4) break; //// "fmt " if (::strncmp(reinterpret_cast<const char*>(chunk_name),"fmt ",4)==0) { //LOG_DEBUG("found chunk 'fmt '"); if (chunk_size<16) return false; if (ds->Read(reinterpret_cast<Byte*>(&m_format),sizeof(m_format))!=sizeof(m_format)) return false; //endian_fix(&m_format); /// uncompressed PCM if (m_format.compression_code!=1) return false; if (m_format.num_channels!=1 && m_format.num_channels!=2) return false; if (m_format.bits_per_sample!=8 && m_format.bits_per_sample!=16) return false; if (m_format.num_channels == 1) { if (m_format.bits_per_sample==8) m_type = SAMPLE_TYPE_MONO_8; else if (m_format.bits_per_sample==16) m_type = SAMPLE_TYPE_MONO_16; } else if (m_format.num_channels == 2) { if (m_format.bits_per_sample==8) m_type = SAMPLE_TYPE_STEREO_8; else if (m_format.bits_per_sample==16) m_type = SAMPLE_TYPE_STEREO_16; } m_freq = m_format.sample_rate; assert(chunk_size>=UInt32(sizeof(m_format))); m_ds->Seek(chunk_size-UInt32(sizeof(m_format)),F_SEEK_CURRENT); } /// "data" else if (::strncmp(reinterpret_cast<const char*>(chunk_name),"data",4)==0) { //LOG_DEBUG("found chunk 'data'"); m_samples+=chunk_size/m_format.block_align; assert(chunk_size<=std::numeric_limits<Int32>::max()); m_ds->Seek(chunk_size,F_SEEK_CURRENT); } /// another else { LOG_DEBUG("skip shunk '"<<chunk_name[0]<<chunk_name[1]<<chunk_name[2]<<chunk_name[3]<<"' "<< chunk_size << " bytes"); assert(chunk_size<=std::numeric_limits<Int32>::max()); m_ds->Seek(chunk_size,F_SEEK_CURRENT); } } m_ds->Seek(4+4+4,F_SEEK_BEGIN); m_unreaded = 0; return m_samples!=0 && m_type!=SAMPLE_TYPE_UNKNOWN && m_freq!=0; }
void LoadBuffer() { size = strm->Read(buffer,size); readed = 0; if (size==0 && strm->Eof()) eof = true; }