Rename DEVELOPMENT -> DEVELOP.md and add some stuff about player stress testing.
[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 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         srand (1);
59
60         AudioAnalysis a (3);
61         for (int i = 0; i < channels; ++i) {
62                 for (int j = 0; j < points; ++j) {
63                         AudioPoint p;
64                         p[AudioPoint::PEAK] = random_float ();
65                         p[AudioPoint::RMS] = random_float ();
66                         a.add_point (i, p);
67                 }
68         }
69
70         vector<AudioAnalysis::PeakTime> peak;
71         for (int i = 0; i < channels; ++i) {
72                 peak.push_back (AudioAnalysis::PeakTime (random_float(), DCPTime (rand())));
73         }
74         a.set_sample_peak (peak);
75
76         a.set_samples_per_point (100);
77         a.set_sample_rate (48000);
78         a.write ("build/test/audio_analysis_serialisation_test");
79
80         srand (1);
81
82         AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
83         for (int i = 0; i < channels; ++i) {
84                 BOOST_CHECK_EQUAL (b.points(i), points);
85                 for (int j = 0; j < points; ++j) {
86                         AudioPoint p = b.get_point (i, j);
87                         BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
88                         BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  random_float (), 1);
89                 }
90         }
91
92         BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3);
93         for (int i = 0; i < channels; ++i) {
94                 BOOST_CHECK_CLOSE (b.sample_peak()[i].peak, peak[i].peak, 1);
95                 BOOST_CHECK_EQUAL (b.sample_peak()[i].time.get(), peak[i].time.get());
96         }
97
98         BOOST_CHECK_EQUAL (a.samples_per_point(), 100);
99         BOOST_CHECK_EQUAL (a.sample_rate(), 48000);
100 }
101
102 static void
103 finished ()
104 {
105
106 }
107
108 BOOST_AUTO_TEST_CASE (audio_analysis_test)
109 {
110         shared_ptr<Film> film = new_test_film ("audio_analysis_test");
111         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
112         film->set_container (Ratio::from_id ("185"));
113         film->set_name ("audio_analysis_test");
114         boost::filesystem::path p = TestPaths::private_data / "betty_L.wav";
115
116         shared_ptr<FFmpegContent> c (new FFmpegContent(p));
117         film->examine_and_add_content (c);
118         BOOST_REQUIRE (!wait_for_jobs());
119
120         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
121         job->Finished.connect (boost::bind (&finished));
122         JobManager::instance()->add (job);
123         BOOST_REQUIRE (!wait_for_jobs());
124 }
125
126 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
127 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
128 {
129         shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
130         film->set_name ("audio_analysis_negative_delay_test");
131         shared_ptr<FFmpegContent> c (new FFmpegContent(TestPaths::private_data / "boon_telly.mkv"));
132         film->examine_and_add_content (c);
133         BOOST_REQUIRE (!wait_for_jobs());
134
135         c->audio->set_delay (-250);
136
137         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
138         job->Finished.connect (boost::bind (&finished));
139         JobManager::instance()->add (job);
140         BOOST_REQUIRE (!wait_for_jobs());
141 }
142
143 /** Check audio analysis that is incorrect in 2e98263 */
144 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
145 {
146         shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
147         film->set_name ("audio_analysis_test2");
148         shared_ptr<FFmpegContent> c (new FFmpegContent(TestPaths::private_data / "3d_thx_broadway_2010_lossless.m2ts"));
149         film->examine_and_add_content (c);
150         BOOST_REQUIRE (!wait_for_jobs());
151
152         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
153         job->Finished.connect (boost::bind (&finished));
154         JobManager::instance()->add (job);
155         BOOST_REQUIRE (!wait_for_jobs());
156 }
157
158
159 static bool done = false;
160
161 static void
162 analysis_finished ()
163 {
164         done = true;
165 }
166
167 /* Test a case which was reported to throw an exception; analysing
168  * a 12-channel DCP's audio.
169  */
170 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
171 {
172         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
173         film->set_container (Ratio::from_id ("185"));
174         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
175         film->set_name ("frobozz");
176
177         shared_ptr<FFmpegContent> content (new FFmpegContent("test/data/white.wav"));
178         film->examine_and_add_content (content);
179         BOOST_REQUIRE (!wait_for_jobs());
180
181         film->set_audio_channels (12);
182         boost::signals2::connection connection;
183         JobManager::instance()->analyse_audio (film, film->playlist(), false, connection, boost::bind (&analysis_finished));
184         BOOST_REQUIRE (!wait_for_jobs());
185         BOOST_CHECK (done);
186 }
187
188 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
189 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
190 {
191         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
192         film->set_container (Ratio::from_id ("185"));
193         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
194         film->set_name ("frobozz");
195         shared_ptr<Content> content = content_factory(TestPaths::private_data / "20 The Wedding Convoy Song.m4a").front();
196         film->examine_and_add_content (content);
197         BOOST_REQUIRE (!wait_for_jobs());
198
199         shared_ptr<Playlist> playlist (new Playlist);
200         playlist->add (film, content);
201         boost::signals2::connection c;
202         JobManager::instance()->analyse_audio (film, playlist, false, c, boost::bind (&finished));
203         BOOST_CHECK (!wait_for_jobs ());
204 }