2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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 DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 /** @defgroup selfcontained Self-contained tests of single classes / method sets */
24 /** @file test/audio_analysis_test.cc
25 * @brief Test AudioAnalysis class.
26 * @ingroup selfcontained
30 #include "lib/analyse_audio_job.h"
31 #include "lib/audio_analysis.h"
32 #include "lib/audio_content.h"
33 #include "lib/content_factory.h"
34 #include "lib/dcp_content.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/ffmpeg_content.h"
39 #include "lib/job_manager.h"
40 #include "lib/playlist.h"
41 #include "lib/ratio.h"
43 #include <boost/test/unit_test.hpp>
46 using std::make_shared;
48 using namespace dcpomatic;
51 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
53 int const channels = 3;
54 int const points = 4096;
56 auto random_float = []() {
57 return (float (rand ()) / RAND_MAX) * 2 - 1;
61 for (int i = 0; i < channels; ++i) {
62 for (int j = 0; j < points; ++j) {
64 p[AudioPoint::PEAK] = random_float ();
65 p[AudioPoint::RMS] = random_float ();
70 vector<AudioAnalysis::PeakTime> peak;
71 for (int i = 0; i < channels; ++i) {
72 peak.push_back (AudioAnalysis::PeakTime(random_float(), DCPTime(rand())));
74 a.set_sample_peak (peak);
76 a.set_samples_per_point (100);
77 a.set_sample_rate (48000);
78 a.write ("build/test/audio_analysis_serialisation_test");
80 AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
81 for (int i = 0; i < channels; ++i) {
82 BOOST_CHECK_EQUAL (b.points(i), points);
83 for (int j = 0; j < points; ++j) {
84 AudioPoint p = a.get_point (i, j);
85 AudioPoint q = b.get_point (i, j);
86 BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], q[AudioPoint::PEAK], 1);
87 BOOST_CHECK_CLOSE (p[AudioPoint::RMS], q[AudioPoint::RMS], 1);
91 BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3U);
92 for (int i = 0; i < channels; ++i) {
93 BOOST_CHECK_CLOSE (b.sample_peak()[i].peak, peak[i].peak, 1);
94 BOOST_CHECK_EQUAL (b.sample_peak()[i].time.get(), peak[i].time.get());
97 BOOST_CHECK_EQUAL (a.samples_per_point(), 100);
98 BOOST_CHECK_EQUAL (a.sample_rate(), 48000);
102 BOOST_AUTO_TEST_CASE (audio_analysis_test)
104 auto film = new_test_film ("audio_analysis_test");
105 film->set_dcp_content_type (DCPContentType::from_isdcf_name("FTR"));
106 film->set_container (Ratio::from_id("185"));
107 film->set_name ("audio_analysis_test");
108 boost::filesystem::path p = TestPaths::private_data() / "betty_L.wav";
110 auto c = make_shared<FFmpegContent>(p);
111 film->examine_and_add_content (c);
112 BOOST_REQUIRE (!wait_for_jobs());
114 auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
115 JobManager::instance()->add (job);
116 BOOST_REQUIRE (!wait_for_jobs());
120 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
121 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
123 auto film = new_test_film ("audio_analysis_negative_delay_test");
124 film->set_name ("audio_analysis_negative_delay_test");
125 auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
126 film->examine_and_add_content (c);
127 BOOST_REQUIRE (!wait_for_jobs());
129 c->audio->set_delay (-250);
131 auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
132 JobManager::instance()->add (job);
133 BOOST_REQUIRE (!wait_for_jobs());
137 /** Check audio analysis that is incorrect in 2e98263 */
138 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
140 auto film = new_test_film ("audio_analysis_test2");
141 film->set_name ("audio_analysis_test2");
142 auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "3d_thx_broadway_2010_lossless.m2ts");
143 film->examine_and_add_content (c);
144 BOOST_REQUIRE (!wait_for_jobs());
146 auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
147 JobManager::instance()->add (job);
148 BOOST_REQUIRE (!wait_for_jobs());
152 /* Test a case which was reported to throw an exception; analysing
153 * a 12-channel DCP's audio.
155 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
157 auto film = new_test_film ("analyse_audio_test");
158 film->set_container (Ratio::from_id ("185"));
159 film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
160 film->set_name ("frobozz");
162 auto content = make_shared<FFmpegContent>("test/data/white.wav");
163 film->examine_and_add_content (content);
164 BOOST_REQUIRE (!wait_for_jobs());
166 film->set_audio_channels (12);
167 boost::signals2::connection connection;
169 JobManager::instance()->analyse_audio(film, film->playlist(), false, connection, [&done](Job::Result) { done = true; });
170 BOOST_REQUIRE (!wait_for_jobs());
175 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
176 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
178 auto film = new_test_film ("analyse_audio_test");
179 film->set_container (Ratio::from_id ("185"));
180 film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
181 film->set_name ("frobozz");
182 auto content = content_factory(TestPaths::private_data() / "20 The Wedding Convoy Song.m4a")[0];
183 film->examine_and_add_content (content);
184 BOOST_REQUIRE (!wait_for_jobs());
186 auto playlist = make_shared<Playlist>();
187 playlist->add (film, content);
188 boost::signals2::connection c;
189 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
190 BOOST_CHECK (!wait_for_jobs ());
194 BOOST_AUTO_TEST_CASE (analyse_audio_leqm_test)
196 auto film = new_test_film2 ("analyse_audio_leqm_test");
197 film->set_audio_channels (2);
198 auto content = content_factory(TestPaths::private_data() / "betty_stereo_48k.wav")[0];
199 film->examine_and_add_content (content);
200 BOOST_REQUIRE (!wait_for_jobs());
202 auto playlist = make_shared<Playlist>();
203 playlist->add (film, content);
204 boost::signals2::connection c;
205 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
206 BOOST_CHECK (!wait_for_jobs());
208 AudioAnalysis analysis(film->audio_analysis_path(playlist));
210 /* The CLI tool of leqm_nrt gives this value for betty_stereo_48k.wav */
211 BOOST_CHECK_CLOSE (analysis.leqm().get_value_or(0), 88.276, 0.001);
215 BOOST_AUTO_TEST_CASE(analyse_audio_leqm_same_with_empty_channels)
217 auto dcp = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
218 auto film = new_test_film2("analyse_audio_leqm_test2", { dcp });
219 film->set_audio_channels(8);
221 auto analyse = [film, dcp](int channels) {
222 film->set_audio_channels(channels);
223 auto playlist = make_shared<Playlist>();
224 playlist->add(film, dcp);
225 boost::signals2::connection c;
226 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
227 BOOST_CHECK(!wait_for_jobs());
228 AudioAnalysis analysis(film->audio_analysis_path(playlist));
229 return analysis.leqm().get_value_or(0);
232 BOOST_CHECK_CLOSE(analyse( 6), 84.51411, 0.001);
233 BOOST_CHECK_CLOSE(analyse( 8), 84.51411, 0.001);
234 BOOST_CHECK_CLOSE(analyse(16), 84.51411, 0.001);
238 /** Bug #2364; a file with a lot of silent video at the end (about 50s worth)
239 * crashed the audio analysis with an OOM on Windows.
241 BOOST_AUTO_TEST_CASE(analyse_audio_with_long_silent_end)
243 auto content = content_factory(TestPaths::private_data() / "2364.mkv")[0];
244 auto film = new_test_film2("analyse_audio_with_long_silent_end", { content });
246 auto playlist = make_shared<Playlist>();
247 playlist->add(film, content);
248 boost::signals2::connection c;
249 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
250 BOOST_CHECK(!wait_for_jobs());
254 BOOST_AUTO_TEST_CASE(analyse_audio_with_strange_channel_count)
256 auto content = content_factory(TestPaths::private_data() / "mali.mkv")[0];
257 auto film = new_test_film2("analyse_audio_with_strange_channel_count", { content });
259 auto playlist = make_shared<Playlist>();
260 playlist->add(film, content);
261 boost::signals2::connection c;
262 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
263 BOOST_CHECK(!wait_for_jobs());
267 BOOST_AUTO_TEST_CASE(analyse_audio_with_more_channels_than_film)
269 auto picture = content_factory("test/data/flat_red.png");
270 auto film_16ch = new_test_film2("analyse_audio_with_more_channels_than_film_16ch", picture);
271 film_16ch->set_audio_channels(16);
272 make_and_verify_dcp(film_16ch);
274 auto pcm_16ch = find_file(film_16ch->dir(film_16ch->dcp_name()), "pcm_");
275 auto sound = content_factory(pcm_16ch)[0];
277 auto film_6ch = new_test_film2("analyse_audio_with_more_channels_than_film_6ch", { sound });
279 auto playlist = make_shared<Playlist>();
280 playlist->add(film_6ch, sound);
281 boost::signals2::connection c;
282 JobManager::instance()->analyse_audio(film_6ch, playlist, false, c, [](Job::Result) {});
283 BOOST_CHECK(!wait_for_jobs());