void MainWindow::exportWAVImpl(Sound::AudioStream *sound, Common::WriteStream &wav) { assert(sound); const uint16 channels = sound->getChannels(); const uint32 rate = sound->getRate(); std::deque<SoundBuffer> buffers; uint64 length = getSoundLength(sound); if (length != Sound::RewindableAudioStream::kInvalidLength) buffers.resize((length / (SoundBuffer::kBufferSize / channels)) + 1); uint32 samples = 0; std::deque<SoundBuffer>::iterator buffer = buffers.begin(); while (!sound->endOfStream()) { if (buffer == buffers.end()) { buffers.push_back(SoundBuffer()); buffer = --buffers.end(); } buffer->samples = sound->readBuffer(buffer->buffer, SoundBuffer::kBufferSize); if (buffer->samples > 0) samples += buffer->samples; ++buffer; } samples /= channels; const uint32 dataSize = samples * channels * 2; const uint32 byteRate = rate * channels * 2; const uint16 blockAlign = channels * 2; wav.writeUint32BE(MKTAG('R', 'I', 'F', 'F')); wav.writeUint32LE(36 + dataSize); wav.writeUint32BE(MKTAG('W', 'A', 'V', 'E')); wav.writeUint32BE(MKTAG('f', 'm', 't', ' ')); wav.writeUint32LE(16); wav.writeUint16LE(1); wav.writeUint16LE(channels); wav.writeUint32LE(rate); wav.writeUint32LE(byteRate); wav.writeUint16LE(blockAlign); wav.writeUint16LE(16); wav.writeUint32BE(MKTAG('d', 'a', 't', 'a')); wav.writeUint32LE(dataSize); for (std::deque<SoundBuffer>::const_iterator b = buffers.begin(); b != buffers.end(); ++b) for (int i = 0; i < b->samples; i++) wav.writeUint16LE(b->buffer[i]); }
static void saveOrLoadCommonArray(Common::WriteStream &stream, A &array) { uint count = array.size(); assert(count < 0xFFFF); stream.writeUint16LE(count); for (uint i = 0; i < count; ++i) { saveOrLoad(stream, array[i]); } }
static void saveOrLoad(Common::WriteStream &stream, uint16 &i) { stream.writeUint16LE(i); }
void TwoDAFile::writeBinary(Common::WriteStream &out) const { const size_t columnCount = _headers.size(); const size_t rowCount = _rows.size(); const size_t cellCount = columnCount * rowCount; out.writeString("2DA V2.b\n"); // Write the column headers for (std::vector<Common::UString>::const_iterator h = _headers.begin(); h != _headers.end(); ++h) { out.writeString(*h); out.writeByte('\t'); } out.writeByte('\0'); // Write the row indices out.writeUint32LE((uint32) rowCount); for (size_t i = 0; i < rowCount; i++) { out.writeString(Common::composeString(i)); out.writeByte('\t'); } /* Deduplicate cell data strings. Binary 2DA files don't store the * data for each cell directly: instead, each cell contains an offset * into a data array. This way, cells with the same data only need to * to store this data once. * * The original binary 2DA files in KotOR/KotOR2 make extensive use * of that, and we should do this as well. * * Basically, this involves going through each cell, and looking up * if we already saved this particular piece of data. If not, save * it, otherwise only remember the offset. There's no need to be * particularly smart about it, so we're just doing it the naive * O(n^2) way. */ std::vector<Common::UString> data; std::vector<size_t> offsets; data.reserve(cellCount); offsets.reserve(cellCount); size_t dataSize = 0; std::vector<size_t> cells; cells.reserve(cellCount); for (size_t i = 0; i < rowCount; i++) { assert(_rows[i]); for (size_t j = 0; j < columnCount; j++) { const Common::UString cell = _rows[i]->getString(j); // Do we already know about this cell data string? size_t foundCell = SIZE_MAX; for (size_t k = 0; k < data.size(); k++) { if (data[k] == cell) { foundCell = k; break; } } // If not, add it to the cell data array if (foundCell == SIZE_MAX) { foundCell = data.size(); data.push_back(cell); offsets.push_back(dataSize); dataSize += data.back().size() + 1; if (dataSize > 65535) throw Common::Exception("TwoDAFile::writeBinary(): Cell data size overflow"); } // Remember the offset to the cell data array cells.push_back(offsets[foundCell]); } } // Write cell data offsets for (std::vector<size_t>::const_iterator c = cells.begin(); c != cells.end(); ++c) out.writeUint16LE((uint16) *c); // Size of the all cell data strings out.writeUint16LE((uint16) dataSize); // Write cell data strings for (std::vector<Common::UString>::const_iterator d = data.begin(); d != data.end(); ++d) { out.writeString(*d); out.writeByte('\0'); } }