Split audio; builds.
[dcpomatic.git] / test / audio_analysis_test.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  test/audio_analysis_test.cc
21  *  @brief Check audio analysis code.
22  */
23
24 #include <boost/test/unit_test.hpp>
25 #include "lib/audio_analysis.h"
26 #include "lib/analyse_audio_job.h"
27 #include "lib/film.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"
34 #include "test.h"
35
36 using boost::shared_ptr;
37
38 static float
39 random_float ()
40 {
41         return (float (rand ()) / RAND_MAX) * 2 - 1;
42 }
43
44 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
45 {
46         int const channels = 3;
47         int const points = 4096;
48
49         srand (1);
50
51         AudioAnalysis a (3);
52         for (int i = 0; i < channels; ++i) {
53                 for (int j = 0; j < points; ++j) {
54                         AudioPoint p;
55                         p[AudioPoint::PEAK] = random_float ();
56                         p[AudioPoint::RMS] = random_float ();
57                         a.add_point (i, p);
58                 }
59         }
60
61         float const peak = random_float ();
62         DCPTime const peak_time = DCPTime (rand ());
63         a.set_sample_peak (peak, peak_time);
64
65         a.write ("build/test/audio_analysis_serialisation_test");
66
67         srand (1);
68
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);
76                 }
77         }
78
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);
83 }
84
85 static void
86 finished ()
87 {
88
89 }
90
91 BOOST_AUTO_TEST_CASE (audio_analysis_test)
92 {
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";
98
99         shared_ptr<SndfileContent> c (new SndfileContent (film, p));
100         film->examine_and_add_content (c);
101         wait_for_jobs ();
102
103         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
104         job->Finished.connect (boost::bind (&finished));
105         JobManager::instance()->add (job);
106         wait_for_jobs ();
107 }
108
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)
111 {
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);
117         wait_for_jobs ();
118
119         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
120         job->Finished.connect (boost::bind (&finished));
121         JobManager::instance()->add (job);
122         wait_for_jobs ();
123 }
124
125 /** Check audio analysis that is incorrect in 2e98263 */
126 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
127 {
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);
132         wait_for_jobs ();
133
134         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
135         job->Finished.connect (boost::bind (&finished));
136         JobManager::instance()->add (job);
137         wait_for_jobs ();
138 }
139
140
141 static bool done = false;
142
143 static void
144 analysis_finished ()
145 {
146         done = true;
147 }
148
149 /* Test a case which was reported to throw an exception; analysing
150  * a 12-channel DCP's audio.
151  */
152 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
153 {
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");
158
159         shared_ptr<SndfileContent> content (new SndfileContent (film, "test/data/white.wav"));
160         film->examine_and_add_content (content);
161         wait_for_jobs ();
162
163         film->set_audio_channels (12);
164         boost::signals2::connection connection;
165         JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));
166         wait_for_jobs ();
167         BOOST_CHECK (done);
168 }