Remove debugging code.
[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 /** @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 boost::shared_ptr;
45
46 static float
47 random_float ()
48 {
49         return (float (rand ()) / RAND_MAX) * 2 - 1;
50 }
51
52 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
53 {
54         int const channels = 3;
55         int const points = 4096;
56
57         srand (1);
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.write ("build/test/audio_analysis_serialisation_test");
76
77         srand (1);
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 = b.get_point (i, j);
84                         BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
85                         BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  random_float (), 1);
86                 }
87         }
88
89         BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3);
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
96 static void
97 finished ()
98 {
99
100 }
101
102 BOOST_AUTO_TEST_CASE (audio_analysis_test)
103 {
104         shared_ptr<Film> 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 = private_data / "betty_L.wav";
109
110         shared_ptr<FFmpegContent> c (new FFmpegContent (film, p));
111         film->examine_and_add_content (c);
112         wait_for_jobs ();
113
114         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
115         job->Finished.connect (boost::bind (&finished));
116         JobManager::instance()->add (job);
117         wait_for_jobs ();
118 }
119
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)
122 {
123         shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
124         film->set_name ("audio_analysis_negative_delay_test");
125         shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "boon_telly.mkv"));
126         film->examine_and_add_content (c);
127         wait_for_jobs ();
128
129         c->audio->set_delay (-250);
130
131         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
132         job->Finished.connect (boost::bind (&finished));
133         JobManager::instance()->add (job);
134         wait_for_jobs ();
135 }
136
137 /** Check audio analysis that is incorrect in 2e98263 */
138 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
139 {
140         shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
141         film->set_name ("audio_analysis_test2");
142         shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "3d_thx_broadway_2010_lossless.m2ts"));
143         film->examine_and_add_content (c);
144         wait_for_jobs ();
145
146         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
147         job->Finished.connect (boost::bind (&finished));
148         JobManager::instance()->add (job);
149         wait_for_jobs ();
150 }
151
152
153 static bool done = false;
154
155 static void
156 analysis_finished ()
157 {
158         done = true;
159 }
160
161 /* Test a case which was reported to throw an exception; analysing
162  * a 12-channel DCP's audio.
163  */
164 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
165 {
166         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
167         film->set_container (Ratio::from_id ("185"));
168         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
169         film->set_name ("frobozz");
170
171         shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/white.wav"));
172         film->examine_and_add_content (content);
173         wait_for_jobs ();
174
175         film->set_audio_channels (12);
176         boost::signals2::connection connection;
177         JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));
178         wait_for_jobs ();
179         BOOST_CHECK (done);
180 }
181
182 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
183 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
184 {
185         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
186         film->set_container (Ratio::from_id ("185"));
187         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
188         film->set_name ("frobozz");
189         shared_ptr<Content> content = content_factory(film, private_data / "20 The Wedding Convoy Song.m4a").front();
190         film->examine_and_add_content (content);
191         wait_for_jobs ();
192
193         shared_ptr<Playlist> playlist (new Playlist);
194         playlist->add (content);
195         boost::signals2::connection c;
196         JobManager::instance()->analyse_audio (film, playlist, c, boost::bind (&finished));
197         BOOST_CHECK (!wait_for_jobs ());
198 }