#include "object.h" #include #include "othertypes.h" #include "util.h" namespace si { Object::Object() { type_ = MxOb::Null; id_ = 0; last_chunk_split_ = false; } bool Object::ReplaceWithFile(const char *f) { std::ifstream is(f); if (!is.is_open() || !is.good()) { return false; } return ReplaceWithFile(is); } bool Object::ExtractToFile(const char *f) const { std::ofstream os(f); if (!os.is_open() || !os.good()) { return false; } return ExtractToFile(os); } bool Object::ReplaceWithFile(std::istream &is) { data_.clear(); switch (this->filetype()) { case MxOb::WAV: { if (ReadU32(is) != RIFF::RIFF_) { return false; } // Skip total size ReadU32(is); if (ReadU32(is) != RIFF::WAVE) { return false; } bytearray fmt; bytearray data; while (is.good()) { uint32_t id = ReadU32(is); uint32_t sz = ReadU32(is); if (id == RIFF::fmt_) { fmt.resize(sz); is.read(fmt.data(), fmt.size()); } else if (id == RIFF::data) { data.resize(sz); is.read(data.data(), data.size()); } else { is.seekg(sz, std::ios::cur); } } if (fmt.empty() || data.empty()) { return false; } data_.push_back(fmt); WAVFmt *fmt_info = fmt.cast(); size_t second_in_bytes = fmt_info->Channels * fmt_info->SampleRate * (fmt_info->BitsPerSample/8); size_t max; for (size_t i=0; i(); bytearray frame_sizes(smk.Frames * sizeof(uint32_t)); is.read(frame_sizes.data(), frame_sizes.size()); hdr.append(frame_sizes); // Read frame types bytearray frame_types(smk.Frames); is.read(frame_types.data(), frame_types.size()); hdr.append(frame_types); // Read Huffman trees bytearray huffman(smk.TreesSize); is.read(huffman.data(), huffman.size()); hdr.append(huffman); // Place header into data vector data_.resize(smk.Frames + 1); data_[0] = hdr; uint32_t *real_sizes = frame_sizes.cast(); for (uint32_t i=0; i 0) { bytearray &d = data_[i+1]; d.resize(sz); is.read(d.data(), d.size()); } } return true; } default: LogWarning() << "Don't yet know how to chunk type " << RIFF::PrintU32AsString(this->filetype()) << std::endl; break; } return false; } bool Object::ExtractToFile(std::ostream &os) const { switch (this->filetype()) { case MxOb::WAV: { // Write RIFF header RIFF::Chk riff = RIFF::BeginChunk(os, RIFF::RIFF_); WriteU32(os, RIFF::WAVE); { RIFF::Chk fmt = RIFF::BeginChunk(os, RIFF::fmt_); WriteBytes(os, data_.at(0)); RIFF::EndChunk(os, fmt); } { RIFF::Chk data = RIFF::BeginChunk(os, RIFF::data); // Merge all chunks after the first one for (size_t i=1; iid() == id) { return this; } for (Children::const_iterator it=GetChildren().begin(); it!=GetChildren().end(); it++) { if (Object *o = static_cast(*it)->FindSubObjectWithID(id)) { return o; } } return NULL; } }