2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file test/audio_analysis_test.cc
21 * @brief Check audio analysis code.
24 #include <boost/test/unit_test.hpp>
25 #include "lib/audio_analysis.h"
26 #include "lib/analyse_audio_job.h"
28 #include "lib/sndfile_content.h"
29 #include "lib/dcp_content_type.h"
30 #include "lib/ffmpeg_content.h"
31 #include "lib/ratio.h"
32 #include "lib/job_manager.h"
35 using boost::shared_ptr;
40 return (float (rand ()) / RAND_MAX) * 2 - 1;
43 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
45 int const channels = 3;
46 int const points = 4096;
51 for (int i = 0; i < channels; ++i) {
52 for (int j = 0; j < points; ++j) {
54 p[AudioPoint::PEAK] = random_float ();
55 p[AudioPoint::RMS] = random_float ();
60 float const peak = random_float ();
61 DCPTime const peak_time = DCPTime (rand ());
62 a.set_sample_peak (peak, peak_time);
64 a.write ("build/test/audio_analysis_serialisation_test");
68 AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
69 for (int i = 0; i < channels; ++i) {
70 BOOST_CHECK_EQUAL (b.points(i), points);
71 for (int j = 0; j < points; ++j) {
72 AudioPoint p = b.get_point (i, j);
73 BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
74 BOOST_CHECK_CLOSE (p[AudioPoint::RMS], random_float (), 1);
78 BOOST_CHECK (b.sample_peak ());
79 BOOST_CHECK_CLOSE (b.sample_peak().get(), peak, 1);
80 BOOST_CHECK (b.sample_peak_time ());
81 BOOST_CHECK_EQUAL (b.sample_peak_time().get(), peak_time);
90 BOOST_AUTO_TEST_CASE (audio_analysis_test)
92 shared_ptr<Film> film = new_test_film ("audio_analysis_test");
93 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
94 film->set_container (Ratio::from_id ("185"));
95 film->set_name ("audio_analysis_test");
96 boost::filesystem::path p = private_data / "betty_L.wav";
98 shared_ptr<SndfileContent> c (new SndfileContent (film, p));
99 film->examine_and_add_content (c);
102 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
103 job->Finished.connect (boost::bind (&finished));
104 JobManager::instance()->add (job);
108 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
109 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
111 shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
112 film->set_name ("audio_analysis_negative_delay_test");
113 shared_ptr<AudioContent> c (new FFmpegContent (film, private_data / "boon_telly.mkv"));
114 c->set_audio_delay (-250);
115 film->examine_and_add_content (c);
118 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
119 job->Finished.connect (boost::bind (&finished));
120 JobManager::instance()->add (job);
124 /** Check audio analysis that is incorrect in 2e98263 */
125 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
127 shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
128 film->set_name ("audio_analysis_test2");
129 shared_ptr<AudioContent> c (new FFmpegContent (film, private_data / "3d_thx_broadway_2010_lossless.m2ts"));
130 film->examine_and_add_content (c);
133 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
134 job->Finished.connect (boost::bind (&finished));
135 JobManager::instance()->add (job);
140 static bool done = false;
148 /* Test a case which was reported to throw an exception; analysing
149 * a 12-channel DCP's audio.
151 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
153 shared_ptr<Film> film = new_test_film ("analyse_audio_test");
154 film->set_container (Ratio::from_id ("185"));
155 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
156 film->set_name ("frobozz");
158 shared_ptr<SndfileContent> content (new SndfileContent (film, "test/data/white.wav"));
159 film->examine_and_add_content (content);
162 film->set_audio_channels (12);
163 boost::signals2::connection connection;
164 JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));