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