/* vim:set ts=2 sw=2 sts=2 et: */ /** * \author Marcus Holland-Moritz (github@mhxnet.de) * \copyright Copyright (c) Marcus Holland-Moritz * * This file is part of dwarfs. * * dwarfs is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * dwarfs is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with dwarfs. If not, see . */ #include #include #include #include using namespace dwarfs; TEST(pcm_sample_transformer, uint8_8bit) { std::vector packed{0, 1, 42, 254, 255}; std::vector unpacked; std::vector repacked; unpacked.resize(packed.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Unsigned, pcm_sample_padding::Msb, 1, 8); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-128, -127, -86, 126, 127}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, uint16_12bit_be_msb) { std::vector tmp{ folly::Endian::big(0), folly::Endian::big(1), folly::Endian::big(2047), folly::Endian::big(2048), folly::Endian::big(2049), folly::Endian::big(4094), folly::Endian::big(4095), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(2 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Unsigned, pcm_sample_padding::Msb, 2, 12); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-2048, -2047, -1, 0, 1, 2046, 2047}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, uint16_12bit_be_lsb) { std::vector tmp{ folly::Endian::big(0 * 16), folly::Endian::big(1 * 16), folly::Endian::big(2047 * 16), folly::Endian::big(2048 * 16), folly::Endian::big(2049 * 16), folly::Endian::big(4094 * 16), folly::Endian::big(4095 * 16), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(2 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Unsigned, pcm_sample_padding::Lsb, 2, 12); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-2048, -2047, -1, 0, 1, 2046, 2047}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, int16_16bit_be) { std::vector tmp{ folly::Endian::big(-32768), folly::Endian::big(-32767), folly::Endian::big(-1), folly::Endian::big(0), folly::Endian::big(1), folly::Endian::big(32766), folly::Endian::big(32767), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(2 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Signed, pcm_sample_padding::Msb, 2, 16); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-32768, -32767, -1, 0, 1, 32766, 32767}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, int16_14bit_le_lsb) { std::vector tmp{ folly::Endian::little(-8192 * 4), folly::Endian::little(-8191 * 4), folly::Endian::little(-1 * 4), folly::Endian::little(0 * 4), folly::Endian::little(1 * 4), folly::Endian::little(8190 * 4), folly::Endian::little(8191 * 4), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(2 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Little, pcm_sample_signedness::Signed, pcm_sample_padding::Lsb, 2, 14); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-8192, -8191, -1, 0, 1, 8190, 8191}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, int32_24bit_be_lsb) { std::vector tmp{ folly::Endian::big(-8388608 * 256), folly::Endian::big(-8388607 * 256), folly::Endian::big(-1 * 256), folly::Endian::big(0 * 256), folly::Endian::big(1 * 256), folly::Endian::big(8388606 * 256), folly::Endian::big(8388607 * 256), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(4 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Signed, pcm_sample_padding::Lsb, 4, 24); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-8388608, -8388607, -1, 0, 1, 8388606, 8388607}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, int32_24bit_le_msb) { std::vector tmp{ folly::Endian::little(-8388608), folly::Endian::little(-8388607), folly::Endian::little(-1), folly::Endian::little(0), folly::Endian::little(1), folly::Endian::little(8388606), folly::Endian::little(8388607), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(4 * tmp.size()); ::memcpy(packed.data(), tmp.data(), packed.size()); unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Little, pcm_sample_signedness::Signed, pcm_sample_padding::Msb, 4, 24); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-8388608, -8388607, -1, 0, 1, 8388606, 8388607}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); } TEST(pcm_sample_transformer, int24_20bit_be_lsb) { std::vector const tmp{ folly::Endian::big(-524288 * 16), folly::Endian::big(-524287 * 16), folly::Endian::big(-1 * 16), folly::Endian::big(0 * 16), folly::Endian::big(1 * 16), folly::Endian::big(524286 * 16), folly::Endian::big(524287 * 16), }; std::vector packed; std::vector unpacked; std::vector repacked; packed.resize(3 * tmp.size()); for (size_t i = 0; i < tmp.size(); ++i) { ::memcpy(packed.data() + 3 * i, reinterpret_cast(&tmp[i]) + 1, 3); } unpacked.resize(tmp.size()); repacked.resize(packed.size()); pcm_sample_transformer xfm(pcm_sample_endianness::Big, pcm_sample_signedness::Signed, pcm_sample_padding::Lsb, 3, 20); xfm.unpack(unpacked, packed); xfm.pack(repacked, unpacked); std::vector ref{-524288, -524287, -1, 0, 1, 524286, 524287}; EXPECT_EQ(ref, unpacked); EXPECT_EQ(packed, repacked); }