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