2 Copyright (C) 2012-2016 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"
33 #include "lib/audio_content.h"
36 using boost::shared_ptr;
41 return (float (rand ()) / RAND_MAX) * 2 - 1;
44 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
46 int const channels = 3;
47 int const points = 4096;
52 for (int i = 0; i < channels; ++i) {
53 for (int j = 0; j < points; ++j) {
55 p[AudioPoint::PEAK] = random_float ();
56 p[AudioPoint::RMS] = random_float ();
61 float const peak = random_float ();
62 DCPTime const peak_time = DCPTime (rand ());
63 a.set_sample_peak (peak, peak_time);
65 a.write ("build/test/audio_analysis_serialisation_test");
69 AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
70 for (int i = 0; i < channels; ++i) {
71 BOOST_CHECK_EQUAL (b.points(i), points);
72 for (int j = 0; j < points; ++j) {
73 AudioPoint p = b.get_point (i, j);
74 BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
75 BOOST_CHECK_CLOSE (p[AudioPoint::RMS], random_float (), 1);
79 BOOST_CHECK (b.sample_peak ());
80 BOOST_CHECK_CLOSE (b.sample_peak().get(), peak, 1);
81 BOOST_CHECK (b.sample_peak_time ());
82 BOOST_CHECK_EQUAL (b.sample_peak_time().get(), peak_time);
91 BOOST_AUTO_TEST_CASE (audio_analysis_test)
93 shared_ptr<Film> film = new_test_film ("audio_analysis_test");
94 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
95 film->set_container (Ratio::from_id ("185"));
96 film->set_name ("audio_analysis_test");
97 boost::filesystem::path p = private_data / "betty_L.wav";
99 shared_ptr<SndfileContent> c (new SndfileContent (film, p));
100 film->examine_and_add_content (c);
103 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
104 job->Finished.connect (boost::bind (&finished));
105 JobManager::instance()->add (job);
109 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
110 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
112 shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
113 film->set_name ("audio_analysis_negative_delay_test");
114 shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "boon_telly.mkv"));
115 c->audio->set_audio_delay (-250);
116 film->examine_and_add_content (c);
119 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
120 job->Finished.connect (boost::bind (&finished));
121 JobManager::instance()->add (job);
125 /** Check audio analysis that is incorrect in 2e98263 */
126 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
128 shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
129 film->set_name ("audio_analysis_test2");
130 shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "3d_thx_broadway_2010_lossless.m2ts"));
131 film->examine_and_add_content (c);
134 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
135 job->Finished.connect (boost::bind (&finished));
136 JobManager::instance()->add (job);
141 static bool done = false;
149 /* Test a case which was reported to throw an exception; analysing
150 * a 12-channel DCP's audio.
152 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
154 shared_ptr<Film> film = new_test_film ("analyse_audio_test");
155 film->set_container (Ratio::from_id ("185"));
156 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
157 film->set_name ("frobozz");
159 shared_ptr<SndfileContent> content (new SndfileContent (film, "test/data/white.wav"));
160 film->examine_and_add_content (content);
163 film->set_audio_channels (12);
164 boost::signals2::connection connection;
165 JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));