Simple pass-through of <Ruby> tags in subtitles.
[libdcp.git] / test / sound_asset_writer_test.cc
1 /*
2     Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34
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>
40 #include <functional>
41
42
43 using std::shared_ptr;
44
45
46 static
47 void
48 no_padding_test(boost::filesystem::path path, std::function<void (shared_ptr<dcp::SoundAssetWriter>, boost::random::mt19937&, boost::random::uniform_int_distribution<>&)> write)
49 {
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);
52
53         boost::random::mt19937 rng(1);
54         boost::random::uniform_int_distribution<> dist(0, 32767);
55
56         write(writer, rng, dist);
57         writer->finalize();
58
59         dcp::SoundAsset check(path);
60         auto reader = check.start_read();
61         auto frame = reader->get_frame(0);
62
63         rng.seed(1);
64
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));
68                 }
69         }
70 }
71
72
73 BOOST_AUTO_TEST_CASE(sound_asset_writer_float_no_padding_test)
74 {
75         auto path = boost::filesystem::path("build/test/sound_asset_writer_float_no_padding_test.mxf");
76
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);
79                 float* pointers[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);
84                         }
85                         pointers[channel] = buffers[channel].data();
86                 }
87
88                 writer->write(pointers, 6, 2000);
89         };
90
91         no_padding_test(path, write);
92 }
93
94
95 BOOST_AUTO_TEST_CASE(sound_asset_writer_int_no_padding_test)
96 {
97         auto path = boost::filesystem::path("build/test/sound_asset_writer_int_no_padding_test.mxf");
98
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);
106                         }
107                         pointers[channel] = buffers[channel].data();
108                 }
109
110                 writer->write(pointers, 6, 2000);
111         };
112
113         no_padding_test(path, write);
114 }
115
116
117 static
118 void
119 padding_test(boost::filesystem::path path, std::function<void (shared_ptr<dcp::SoundAssetWriter>, boost::random::mt19937&, boost::random::uniform_int_distribution<>&)> write)
120 {
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);
123
124         boost::random::mt19937 rng(1);
125         boost::random::uniform_int_distribution<> dist(0, 32767);
126
127         write(writer, rng, dist);
128         writer->finalize();
129
130         dcp::SoundAsset check(path);
131         auto reader = check.start_read();
132         auto frame = reader->get_frame(0);
133
134         rng.seed(1);
135
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));
139                 }
140         }
141
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);
145                 }
146         }
147 }
148
149
150 BOOST_AUTO_TEST_CASE(sound_asset_writer_float_padding_test)
151 {
152         auto path = boost::filesystem::path("build/test/sound_asset_writer_float_padding_test.mxf");
153
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);
156                 float* pointers[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);
161                         }
162                         pointers[channel] = buffers[channel].data();
163                 }
164
165                 writer->write(pointers, 6, 2000);
166         };
167
168         padding_test(path, write);
169 }
170
171
172 BOOST_AUTO_TEST_CASE(sound_asset_writer_int_padding_test)
173 {
174         auto path = boost::filesystem::path("build/test/sound_asset_writer_int_padding_test.mxf");
175
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);
183                         }
184                         pointers[channel] = buffers[channel].data();
185                 }
186
187                 writer->write(pointers, 6, 2000);
188         };
189
190         padding_test(path, write);
191 }