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