| 1 |
/* |
| 2 |
* Copyright (C) 2020-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "wav_container.hpp" |
| 27 |
#include <cmath> |
| 28 |
#include <memory> |
| 29 |
#include "file_io_error.hpp" |
| 30 |
|
| 31 |
namespace io |
| 32 |
{ |
| 33 |
namespace |
| 34 |
{ |
| 35 |
inline void assertValue(bool f, size_t pos) |
| 36 |
{ |
| 37 |
if (!f) throw io::FileCorruptionError(io::FileType::WAV, pos); |
| 38 |
} |
| 39 |
|
| 40 |
enum WavOffset : size_t |
| 41 |
{ |
| 42 |
RIFF_OFFS = 0, |
| 43 |
FILE_SIZE_OFFS = 4, |
| 44 |
WAVE_OFFS = 8, |
| 45 |
FMT_OFS = 12, |
| 46 |
FMT_SIZE_OFFS = 16, |
| 47 |
FORMAT_OFFS = 20, |
| 48 |
NCH_OFFS = 22, |
| 49 |
RATE_OFFS = 24, |
| 50 |
BYTE_RATE_OFFS = 28, |
| 51 |
BLOCK_SIZE_OFFS = 32, |
| 52 |
BIT_SIZE_OFFS = 34, |
| 53 |
DATA_OFFS = 36, |
| 54 |
DATA_SIZE_OFFS = 40, |
| 55 |
PREPARED_SIZE = 44 |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
WavContainer::WavContainer(uint32_t rate, uint16_t nCh, uint16_t bitSize) |
| 60 |
: nCh_(nCh), |
| 61 |
bitSize_(bitSize), |
| 62 |
rate_(rate) |
| 63 |
{ |
| 64 |
// RIFF header |
| 65 |
buf_.appendString("RIFF"); |
| 66 |
uint16_t byteSize = bitSize_ / 8; |
| 67 |
buf_.appendUint32(36); |
| 68 |
buf_.appendString("WAVE"); |
| 69 |
|
| 70 |
// fmt chunk |
| 71 |
buf_.appendString("fmt "); |
| 72 |
uint32_t chunkOfs = 16; |
| 73 |
buf_.appendUint32(chunkOfs); |
| 74 |
uint16_t fmtId = 1; // Raw linear PCM |
| 75 |
buf_.appendUint16(fmtId); |
| 76 |
buf_.appendUint16(nCh_); |
| 77 |
buf_.appendUint32(rate_); |
| 78 |
uint16_t blockSize = byteSize * nCh_; |
| 79 |
uint32_t byteRate = blockSize * rate_; |
| 80 |
buf_.appendUint32(byteRate); |
| 81 |
buf_.appendUint16(blockSize); |
| 82 |
buf_.appendUint16(bitSize_); |
| 83 |
|
| 84 |
// Data chunk |
| 85 |
buf_.appendString("data"); |
| 86 |
buf_.appendUint32(0); |
| 87 |
} |
| 88 |
|
| 89 |
WavContainer::WavContainer(const BinaryContainer& bc) |
| 90 |
{ |
| 91 |
buf_.resize(PREPARED_SIZE); |
| 92 |
size_t p = 0; |
| 93 |
assertValue(bc.readString(p, 4) == "RIFF", p); |
| 94 |
buf_.writeString(RIFF_OFFS, "RIFF"); |
| 95 |
p += 4; |
| 96 |
uint32_t fileSize = bc.readUint32(p) + 8; |
| 97 |
assertValue(fileSize == bc.size(), p); |
| 98 |
p += 4; |
| 99 |
assertValue(bc.readString(p, 4) == "WAVE", p); |
| 100 |
buf_.writeString(WAVE_OFFS, "WAVE"); |
| 101 |
p += 4; |
| 102 |
|
| 103 |
while (p < fileSize) { |
| 104 |
std::string id = bc.readString(p, 4); |
| 105 |
p += 4; |
| 106 |
|
| 107 |
if (id == "fmt ") { |
| 108 |
buf_.writeString(FMT_OFS, "fmt "); |
| 109 |
uint32_t fmtSize = bc.readUint32(p); |
| 110 |
buf_.writeUint32(FMT_SIZE_OFFS, 16); |
| 111 |
size_t fmtp = p + 4; |
| 112 |
p = fmtp + fmtSize; |
| 113 |
assertValue(bc.readUint16(fmtp) == 1, fmtp); // Only support linear PCM |
| 114 |
buf_.writeUint16(FORMAT_OFFS, 1); |
| 115 |
fmtp += 2; |
| 116 |
nCh_ = bc.readUint16(fmtp); |
| 117 |
buf_.writeUint16(NCH_OFFS, nCh_); |
| 118 |
fmtp += 2; |
| 119 |
rate_ = bc.readUint32(fmtp); |
| 120 |
buf_.writeUint32(RATE_OFFS, rate_); |
| 121 |
fmtp += 4; |
| 122 |
byteRate_ = bc.readUint32(fmtp); |
| 123 |
buf_.writeUint32(BYTE_RATE_OFFS, byteRate_); |
| 124 |
fmtp += 4; |
| 125 |
blockSize_ = bc.readUint16(fmtp); |
| 126 |
assertValue(byteRate_ == blockSize_ * rate_, fmtp); |
| 127 |
buf_.writeUint16(BLOCK_SIZE_OFFS, blockSize_); |
| 128 |
fmtp += 2; |
| 129 |
bitSize_ = bc.readUint16(fmtp); |
| 130 |
assertValue(bitSize_ == 16, fmtp); // Only support 16-bit |
| 131 |
assertValue(blockSize_ == nCh_ * bitSize_ / 8, fmtp); |
| 132 |
buf_.writeUint16(BIT_SIZE_OFFS, bitSize_); |
| 133 |
/* fmtp += 2; */ |
| 134 |
} |
| 135 |
else if (id == "data") { |
| 136 |
buf_.writeString(DATA_OFFS, "data"); |
| 137 |
uint32_t dataSize = bc.readUint32(p); |
| 138 |
assertValue(p + dataSize <= bc.size(), p); |
| 139 |
buf_.writeUint32(DATA_SIZE_OFFS, dataSize); |
| 140 |
p += 4; |
| 141 |
buf_.appendBinaryContainer(bc.getSubcontainer(p, dataSize)); |
| 142 |
p += dataSize; |
| 143 |
} |
| 144 |
else { |
| 145 |
p += (bc.readUint32(p) + 4); // Jump to next chunk |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
buf_.writeUint32(FILE_SIZE_OFFS, buf_.size() - 8); |
| 150 |
} |
| 151 |
|
| 152 |
void WavContainer::setChannelCount(uint16_t n) |
| 153 |
{ |
| 154 |
nCh_ = n; |
| 155 |
buf_.writeUint16(NCH_OFFS, nCh_); |
| 156 |
updateBlockSize(); |
| 157 |
} |
| 158 |
|
| 159 |
void WavContainer::setBitSize(uint16_t size) |
| 160 |
{ |
| 161 |
bitSize_ = size; |
| 162 |
buf_.writeUint16(BIT_SIZE_OFFS, bitSize_); |
| 163 |
updateBlockSize(); |
| 164 |
} |
| 165 |
|
| 166 |
void WavContainer::setSampleRate(uint32_t rate) |
| 167 |
{ |
| 168 |
rate_ = rate; |
| 169 |
buf_.writeUint16(rate, rate); |
| 170 |
updateBlockSize(); |
| 171 |
updateByteRate(); |
| 172 |
} |
| 173 |
|
| 174 |
WavContainer::size_type WavContainer::getSampleCount() const |
| 175 |
{ |
| 176 |
return (buf_.size() - PREPARED_SIZE) * bitSize_ / 8 / nCh_; |
| 177 |
} |
| 178 |
|
| 179 |
void WavContainer::appendSample(const int16_t* sample, size_type nSamples) |
| 180 |
{ |
| 181 |
size_t dataSize = nCh_ * nSamples * sizeof(int16_t); |
| 182 |
buf_.appendArray(reinterpret_cast<const uint8_t*>(sample), dataSize); |
| 183 |
updateSizeDataAfterAppendSample(); |
| 184 |
} |
| 185 |
|
| 186 |
void WavContainer::appendSample(const std::vector<int16_t>& sample) |
| 187 |
{ |
| 188 |
size_t dataSize = sample.size() * sizeof(int16_t); |
| 189 |
buf_.appendArray(reinterpret_cast<const uint8_t*>(sample.data()), dataSize); |
| 190 |
updateSizeDataAfterAppendSample(); |
| 191 |
} |
| 192 |
|
| 193 |
void WavContainer::appendSample(const BinaryContainer& sample) |
| 194 |
{ |
| 195 |
buf_.appendBinaryContainer(sample); |
| 196 |
updateSizeDataAfterAppendSample(); |
| 197 |
} |
| 198 |
|
| 199 |
BinaryContainer WavContainer::getSample() const noexcept |
| 200 |
{ |
| 201 |
return buf_.getSubcontainer(PREPARED_SIZE, buf_.size() - PREPARED_SIZE); |
| 202 |
} |
| 203 |
|
| 204 |
// WavContainer* WavContainer::resample(const WavContainer* src, uint32_t rate) |
| 205 |
// { |
| 206 |
// std::unique_ptr<WavContainer> tgt |
| 207 |
// = std::make_unique<WavContainer>(0, rate, src->getChannelCount(), src->getBitSize()); |
| 208 |
// assert(src->getBitSize() == 16); // Only support int16_t |
| 209 |
|
| 210 |
// size_t nCh = src->getChannelCount(); |
| 211 |
// size_t tsize = src->getSampleCount() * tgt->getSampleRate() / src->getSampleRate(); |
| 212 |
// BinaryContainer tbc(tsize * 2 * nCh); |
| 213 |
// BinaryContainer sbc = src->getSample(); |
| 214 |
// double r = static_cast<double>(src->getSampleRate()) / tgt->getSampleRate(); |
| 215 |
|
| 216 |
// for (size_t n = 0; n < tsize; ++n) { |
| 217 |
// double curnf = n * r; |
| 218 |
// int curni = static_cast<int>(curnf); |
| 219 |
// double sub = curnf - curni; |
| 220 |
// for (size_t ch = 0; ch < nCh; ++ch) { |
| 221 |
// double a = sbc.readInt16((curni * nCh + ch) * 2); |
| 222 |
// if (sub == 0.) { |
| 223 |
// double b = sbc.readInt16(((curni + 1) * nCh + ch) * 2); |
| 224 |
// tbc.appendInt16(static_cast<int16_t>(std::round(a + (b - a) * sub))); |
| 225 |
// } |
| 226 |
// else { |
| 227 |
// tbc.appendInt16(static_cast<int16_t>(std::round(a))); |
| 228 |
// } |
| 229 |
// } |
| 230 |
// } |
| 231 |
|
| 232 |
// tgt->storeSample(tbc); |
| 233 |
// return tgt.release(); |
| 234 |
// } |
| 235 |
|
| 236 |
// WavContainer* WavContainer::mono(const WavContainer* src) |
| 237 |
// { |
| 238 |
// std::unique_ptr<WavContainer> tgt |
| 239 |
// = std::make_unique<WavContainer>(0, src->getSampleRate(), 1, src->getBitSize()); |
| 240 |
// assert(src->getBitSize() == 16); // Only support int16_t |
| 241 |
|
| 242 |
// BinaryContainer tbc; |
| 243 |
// BinaryContainer sbc = src->getSample(); |
| 244 |
// uint16_t nCh = src->getChannelCount(); |
| 245 |
// size_t size = src->getSampleCount(); |
| 246 |
// for (size_t i = 0; i < size; ++i) { |
| 247 |
// int32_t v = 0; |
| 248 |
// for (size_t ch = 0; ch < nCh; ++ch) { |
| 249 |
// v += sbc.readInt16((i * nCh + ch) * 2); |
| 250 |
// } |
| 251 |
// tbc.writeInt16(i, static_cast<int16_t>(clamp(v, -32768, 32767))); |
| 252 |
// } |
| 253 |
// tgt->storeSample(tbc); |
| 254 |
|
| 255 |
// return tgt.release(); |
| 256 |
// } |
| 257 |
|
| 258 |
void WavContainer::updateBlockSize() |
| 259 |
{ |
| 260 |
blockSize_ = nCh_ * bitSize_ / 8; |
| 261 |
buf_.writeUint16(BLOCK_SIZE_OFFS, blockSize_); |
| 262 |
} |
| 263 |
|
| 264 |
void WavContainer::updateByteRate() |
| 265 |
{ |
| 266 |
byteRate_ = blockSize_ * rate_; |
| 267 |
buf_.writeUint32(BYTE_RATE_OFFS, byteRate_); |
| 268 |
} |
| 269 |
|
| 270 |
void WavContainer::updateSizeDataAfterAppendSample() |
| 271 |
{ |
| 272 |
buf_.writeUint32(FILE_SIZE_OFFS, buf_.size() - 8); |
| 273 |
buf_.writeUint32(DATA_SIZE_OFFS, buf_.size() - PREPARED_SIZE); |
| 274 |
} |
| 275 |
} |