2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/audio_analysis_test.cc
22 * @brief Check audio analysis code.
25 #include <boost/test/unit_test.hpp>
26 #include "lib/audio_analysis.h"
27 #include "lib/analyse_audio_job.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"
39 using boost::shared_ptr;
44 return (float (rand ()) / RAND_MAX) * 2 - 1;
47 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
49 int const channels = 3;
50 int const points = 4096;
55 for (int i = 0; i < channels; ++i) {
56 for (int j = 0; j < points; ++j) {
58 p[AudioPoint::PEAK] = random_float ();
59 p[AudioPoint::RMS] = random_float ();
64 float const peak = random_float ();
65 DCPTime const peak_time = DCPTime (rand ());
66 a.set_sample_peak (peak, peak_time);
68 a.write ("build/test/audio_analysis_serialisation_test");
72 AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
73 for (int i = 0; i < channels; ++i) {
74 BOOST_CHECK_EQUAL (b.points(i), points);
75 for (int j = 0; j < points; ++j) {
76 AudioPoint p = b.get_point (i, j);
77 BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
78 BOOST_CHECK_CLOSE (p[AudioPoint::RMS], random_float (), 1);
82 BOOST_CHECK (b.sample_peak ());
83 BOOST_CHECK_CLOSE (b.sample_peak().get(), peak, 1);
84 BOOST_CHECK (b.sample_peak_time ());
85 BOOST_CHECK_EQUAL (b.sample_peak_time().get().get(), peak_time.get());
94 BOOST_AUTO_TEST_CASE (audio_analysis_test)
96 shared_ptr<Film> film = new_test_film ("audio_analysis_test");
97 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
98 film->set_container (Ratio::from_id ("185"));
99 film->set_name ("audio_analysis_test");
100 boost::filesystem::path p = private_data / "betty_L.wav";
102 shared_ptr<FFmpegContent> c (new FFmpegContent (film, p));
103 film->examine_and_add_content (c);
106 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
107 job->Finished.connect (boost::bind (&finished));
108 JobManager::instance()->add (job);
112 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
113 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
115 shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
116 film->set_name ("audio_analysis_negative_delay_test");
117 shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "boon_telly.mkv"));
118 film->examine_and_add_content (c);
121 c->audio->set_delay (-250);
123 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
124 job->Finished.connect (boost::bind (&finished));
125 JobManager::instance()->add (job);
129 /** Check audio analysis that is incorrect in 2e98263 */
130 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
132 shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
133 film->set_name ("audio_analysis_test2");
134 shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "3d_thx_broadway_2010_lossless.m2ts"));
135 film->examine_and_add_content (c);
138 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
139 job->Finished.connect (boost::bind (&finished));
140 JobManager::instance()->add (job);
145 static bool done = false;
153 /* Test a case which was reported to throw an exception; analysing
154 * a 12-channel DCP's audio.
156 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
158 shared_ptr<Film> film = new_test_film ("analyse_audio_test");
159 film->set_container (Ratio::from_id ("185"));
160 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
161 film->set_name ("frobozz");
163 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/white.wav"));
164 film->examine_and_add_content (content);
167 film->set_audio_channels (12);
168 boost::signals2::connection connection;
169 JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));
174 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
175 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
177 shared_ptr<Film> film = new_test_film ("analyse_audio_test");
178 film->set_container (Ratio::from_id ("185"));
179 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
180 film->set_name ("frobozz");
181 shared_ptr<Content> content = content_factory (film, private_data / "20 The Wedding Convoy Song.m4a");
182 film->examine_and_add_content (content);
185 shared_ptr<Playlist> playlist (new Playlist);
186 playlist->add (content);
187 boost::signals2::connection c;
188 JobManager::instance()->analyse_audio (film, playlist, c, boost::bind (&finished));
189 BOOST_CHECK (!wait_for_jobs ());