Cleanup: use a lambda.
[dcpomatic.git] / test / audio_analysis_test.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 /** @defgroup selfcontained Self-contained tests of single classes / method sets */
23
24 /** @file  test/audio_analysis_test.cc
25  *  @brief Test AudioAnalysis class.
26  *  @ingroup selfcontained
27  */
28
29
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_type.h"
35 #include "lib/ffmpeg_content.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/film.h"
38 #include "lib/job_manager.h"
39 #include "lib/playlist.h"
40 #include "lib/ratio.h"
41 #include "test.h"
42 #include <boost/test/unit_test.hpp>
43
44
45 using std::make_shared;
46 using std::vector;
47 using namespace dcpomatic;
48
49
50 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
51 {
52         int const channels = 3;
53         int const points = 4096;
54
55         auto random_float = []() {
56                 return (float (rand ()) / RAND_MAX) * 2 - 1;
57         };
58
59         AudioAnalysis a (3);
60         for (int i = 0; i < channels; ++i) {
61                 for (int j = 0; j < points; ++j) {
62                         AudioPoint p;
63                         p[AudioPoint::PEAK] = random_float ();
64                         p[AudioPoint::RMS] = random_float ();
65                         a.add_point (i, p);
66                 }
67         }
68
69         vector<AudioAnalysis::PeakTime> peak;
70         for (int i = 0; i < channels; ++i) {
71                 peak.push_back (AudioAnalysis::PeakTime(random_float(), DCPTime(rand())));
72         }
73         a.set_sample_peak (peak);
74
75         a.set_samples_per_point (100);
76         a.set_sample_rate (48000);
77         a.write ("build/test/audio_analysis_serialisation_test");
78
79         AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
80         for (int i = 0; i < channels; ++i) {
81                 BOOST_CHECK_EQUAL (b.points(i), points);
82                 for (int j = 0; j < points; ++j) {
83                         AudioPoint p = a.get_point (i, j);
84                         AudioPoint q = b.get_point (i, j);
85                         BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], q[AudioPoint::PEAK], 1);
86                         BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  q[AudioPoint::RMS], 1);
87                 }
88         }
89
90         BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3U);
91         for (int i = 0; i < channels; ++i) {
92                 BOOST_CHECK_CLOSE (b.sample_peak()[i].peak, peak[i].peak, 1);
93                 BOOST_CHECK_EQUAL (b.sample_peak()[i].time.get(), peak[i].time.get());
94         }
95
96         BOOST_CHECK_EQUAL (a.samples_per_point(), 100);
97         BOOST_CHECK_EQUAL (a.sample_rate(), 48000);
98 }
99
100
101 BOOST_AUTO_TEST_CASE (audio_analysis_test)
102 {
103         auto film = new_test_film ("audio_analysis_test");
104         film->set_dcp_content_type (DCPContentType::from_isdcf_name("FTR"));
105         film->set_container (Ratio::from_id("185"));
106         film->set_name ("audio_analysis_test");
107         boost::filesystem::path p = TestPaths::private_data() / "betty_L.wav";
108
109         auto c = make_shared<FFmpegContent>(p);
110         film->examine_and_add_content (c);
111         BOOST_REQUIRE (!wait_for_jobs());
112
113         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
114         JobManager::instance()->add (job);
115         BOOST_REQUIRE (!wait_for_jobs());
116 }
117
118
119 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
120 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
121 {
122         auto film = new_test_film ("audio_analysis_negative_delay_test");
123         film->set_name ("audio_analysis_negative_delay_test");
124         auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
125         film->examine_and_add_content (c);
126         BOOST_REQUIRE (!wait_for_jobs());
127
128         c->audio->set_delay (-250);
129
130         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
131         JobManager::instance()->add (job);
132         BOOST_REQUIRE (!wait_for_jobs());
133 }
134
135
136 /** Check audio analysis that is incorrect in 2e98263 */
137 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
138 {
139         auto film = new_test_film ("audio_analysis_test2");
140         film->set_name ("audio_analysis_test2");
141         auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "3d_thx_broadway_2010_lossless.m2ts");
142         film->examine_and_add_content (c);
143         BOOST_REQUIRE (!wait_for_jobs());
144
145         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
146         JobManager::instance()->add (job);
147         BOOST_REQUIRE (!wait_for_jobs());
148 }
149
150
151 /* Test a case which was reported to throw an exception; analysing
152  * a 12-channel DCP's audio.
153  */
154 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
155 {
156         auto film = new_test_film ("analyse_audio_test");
157         film->set_container (Ratio::from_id ("185"));
158         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
159         film->set_name ("frobozz");
160
161         auto content = make_shared<FFmpegContent>("test/data/white.wav");
162         film->examine_and_add_content (content);
163         BOOST_REQUIRE (!wait_for_jobs());
164
165         film->set_audio_channels (12);
166         boost::signals2::connection connection;
167         bool done = false;
168         JobManager::instance()->analyse_audio(film, film->playlist(), false, connection, [&done](Job::Result) { done = true; });
169         BOOST_REQUIRE (!wait_for_jobs());
170         BOOST_CHECK (done);
171 }
172
173
174 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
175 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
176 {
177         auto film = new_test_film ("analyse_audio_test");
178         film->set_container (Ratio::from_id ("185"));
179         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
180         film->set_name ("frobozz");
181         auto content = content_factory(TestPaths::private_data() / "20 The Wedding Convoy Song.m4a")[0];
182         film->examine_and_add_content (content);
183         BOOST_REQUIRE (!wait_for_jobs());
184
185         auto playlist = make_shared<Playlist>();
186         playlist->add (film, content);
187         boost::signals2::connection c;
188         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
189         BOOST_CHECK (!wait_for_jobs ());
190 }
191
192
193 BOOST_AUTO_TEST_CASE (analyse_audio_leqm_test)
194 {
195         auto film = new_test_film2 ("analyse_audio_leqm_test");
196         film->set_audio_channels (2);
197         auto content = content_factory(TestPaths::private_data() / "betty_stereo_48k.wav")[0];
198         film->examine_and_add_content (content);
199         BOOST_REQUIRE (!wait_for_jobs());
200
201         auto playlist = make_shared<Playlist>();
202         playlist->add (film, content);
203         boost::signals2::connection c;
204         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
205         BOOST_CHECK (!wait_for_jobs());
206
207         AudioAnalysis analysis(film->audio_analysis_path(playlist));
208
209         /* The CLI tool of leqm_nrt gives this value for betty_stereo_48k.wav */
210         BOOST_CHECK_CLOSE (analysis.leqm().get_value_or(0), 88.276, 0.001);
211 }
212
213
214 /** Bug #2364; a file with a lot of silent video at the end (about 50s worth)
215  *  crashed the audio analysis with an OOM on Windows.
216  */
217 BOOST_AUTO_TEST_CASE(analyse_audio_with_long_silent_end)
218 {
219         auto content = content_factory(TestPaths::private_data() / "2364.mkv")[0];
220         auto film = new_test_film2("analyse_audio_with_long_silent_end", { content });
221
222         auto playlist = make_shared<Playlist>();
223         playlist->add(film, content);
224         boost::signals2::connection c;
225         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
226         BOOST_CHECK(!wait_for_jobs());
227 }
228
229
230 BOOST_AUTO_TEST_CASE(analyse_audio_with_strange_channel_count)
231 {
232         auto content = content_factory(TestPaths::private_data() / "mali.mkv")[0];
233         auto film = new_test_film2("analyse_audio_with_strange_channel_count", { content });
234
235         auto playlist = make_shared<Playlist>();
236         playlist->add(film, content);
237         boost::signals2::connection c;
238         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
239         BOOST_CHECK(!wait_for_jobs());
240 }
241