2 Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 #include "sound_asset.h"
36 #include "sound_asset_writer.h"
37 #include <boost/filesystem.hpp>
38 #include <boost/random.hpp>
39 #include <boost/test/unit_test.hpp>
43 using std::shared_ptr;
48 no_padding_test(boost::filesystem::path path, std::function<void (shared_ptr<dcp::SoundAssetWriter>, boost::random::mt19937&, boost::random::uniform_int_distribution<>&)> write)
50 dcp::SoundAsset asset({24, 1}, 48000, 6, dcp::LanguageTag{"en-GB"}, dcp::Standard::SMPTE);
51 auto writer = asset.start_write(path, {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
53 boost::random::mt19937 rng(1);
54 boost::random::uniform_int_distribution<> dist(0, 32767);
56 write(writer, rng, dist);
59 dcp::SoundAsset check(path);
60 auto reader = check.start_read();
61 auto frame = reader->get_frame(0);
65 for (auto channel = 0; channel < 6; ++channel) {
66 for (auto sample = 0; sample < 2000; ++sample) {
67 BOOST_REQUIRE_EQUAL(frame->get(channel, sample), dist(rng));
73 BOOST_AUTO_TEST_CASE(sound_asset_writer_float_no_padding_test)
75 auto path = boost::filesystem::path("build/test/sound_asset_writer_float_no_padding_test.mxf");
77 auto write = [](shared_ptr<dcp::SoundAssetWriter> writer, boost::random::mt19937& rng, boost::random::uniform_int_distribution<>& dist) {
78 std::vector<std::vector<float>> buffers(6);
80 for (auto channel = 0; channel < 6; ++channel) {
81 buffers[channel].resize(2000);
82 for (int sample = 0; sample < 2000; ++sample) {
83 buffers[channel][sample] = static_cast<float>(dist(rng)) / (1 << 23);
85 pointers[channel] = buffers[channel].data();
88 writer->write(pointers, 6, 2000);
91 no_padding_test(path, write);
95 BOOST_AUTO_TEST_CASE(sound_asset_writer_int_no_padding_test)
97 auto path = boost::filesystem::path("build/test/sound_asset_writer_int_no_padding_test.mxf");
99 auto write = [](shared_ptr<dcp::SoundAssetWriter> writer, boost::random::mt19937& rng, boost::random::uniform_int_distribution<>& dist) {
100 std::vector<std::vector<int32_t>> buffers(6);
101 int32_t* pointers[6];
102 for (auto channel = 0; channel < 6; ++channel) {
103 buffers[channel].resize(2000);
104 for (int sample = 0; sample < 2000; ++sample) {
105 buffers[channel][sample] = dist(rng);
107 pointers[channel] = buffers[channel].data();
110 writer->write(pointers, 6, 2000);
113 no_padding_test(path, write);
119 padding_test(boost::filesystem::path path, std::function<void (shared_ptr<dcp::SoundAssetWriter>, boost::random::mt19937&, boost::random::uniform_int_distribution<>&)> write)
121 dcp::SoundAsset asset({24, 1}, 48000, 14, dcp::LanguageTag{"en-GB"}, dcp::Standard::SMPTE);
122 auto writer = asset.start_write(path, {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
124 boost::random::mt19937 rng(1);
125 boost::random::uniform_int_distribution<> dist(0, 32767);
127 write(writer, rng, dist);
130 dcp::SoundAsset check(path);
131 auto reader = check.start_read();
132 auto frame = reader->get_frame(0);
136 for (auto channel = 0; channel < 6; ++channel) {
137 for (auto sample = 0; sample < 2000; ++sample) {
138 BOOST_REQUIRE_EQUAL(frame->get(channel, sample), dist(rng));
142 for (auto channel = 7; channel < 14; ++channel) {
143 for (auto sample = 0; sample < 2000; ++sample) {
144 BOOST_REQUIRE_EQUAL(frame->get(channel, sample), 0);
150 BOOST_AUTO_TEST_CASE(sound_asset_writer_float_padding_test)
152 auto path = boost::filesystem::path("build/test/sound_asset_writer_float_padding_test.mxf");
154 auto write = [](shared_ptr<dcp::SoundAssetWriter> writer, boost::random::mt19937& rng, boost::random::uniform_int_distribution<>& dist) {
155 std::vector<std::vector<float>> buffers(6);
157 for (auto channel = 0; channel < 6; ++channel) {
158 buffers[channel].resize(2000);
159 for (int sample = 0; sample < 2000; ++sample) {
160 buffers[channel][sample] = static_cast<float>(dist(rng)) / (1 << 23);
162 pointers[channel] = buffers[channel].data();
165 writer->write(pointers, 6, 2000);
168 padding_test(path, write);
172 BOOST_AUTO_TEST_CASE(sound_asset_writer_int_padding_test)
174 auto path = boost::filesystem::path("build/test/sound_asset_writer_int_padding_test.mxf");
176 auto write = [](shared_ptr<dcp::SoundAssetWriter> writer, boost::random::mt19937& rng, boost::random::uniform_int_distribution<>& dist) {
177 std::vector<std::vector<int32_t>> buffers(6);
178 int32_t* pointers[6];
179 for (auto channel = 0; channel < 6; ++channel) {
180 buffers[channel].resize(2000);
181 for (int sample = 0; sample < 2000; ++sample) {
182 buffers[channel][sample] = dist(rng);
184 pointers[channel] = buffers[channel].data();
187 writer->write(pointers, 6, 2000);
190 padding_test(path, write);