Revert "Use make_shared<>."
[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 /** @file  test/audio_analysis_test.cc
22  *  @brief Check audio analysis code.
23  */
24
25 #include <boost/test/unit_test.hpp>
26 #include "lib/audio_analysis.h"
27 #include "lib/analyse_audio_job.h"
28 #include "lib/film.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 "test.h"
36
37 using boost::shared_ptr;
38
39 static float
40 random_float ()
41 {
42         return (float (rand ()) / RAND_MAX) * 2 - 1;
43 }
44
45 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
46 {
47         int const channels = 3;
48         int const points = 4096;
49
50         srand (1);
51
52         AudioAnalysis a (3);
53         for (int i = 0; i < channels; ++i) {
54                 for (int j = 0; j < points; ++j) {
55                         AudioPoint p;
56                         p[AudioPoint::PEAK] = random_float ();
57                         p[AudioPoint::RMS] = random_float ();
58                         a.add_point (i, p);
59                 }
60         }
61
62         float const peak = random_float ();
63         DCPTime const peak_time = DCPTime (rand ());
64         a.set_sample_peak (peak, peak_time);
65
66         a.write ("build/test/audio_analysis_serialisation_test");
67
68         srand (1);
69
70         AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
71         for (int i = 0; i < channels; ++i) {
72                 BOOST_CHECK_EQUAL (b.points(i), points);
73                 for (int j = 0; j < points; ++j) {
74                         AudioPoint p = b.get_point (i, j);
75                         BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], random_float (), 1);
76                         BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  random_float (), 1);
77                 }
78         }
79
80         BOOST_CHECK (b.sample_peak ());
81         BOOST_CHECK_CLOSE (b.sample_peak().get(), peak, 1);
82         BOOST_CHECK (b.sample_peak_time ());
83         BOOST_CHECK_EQUAL (b.sample_peak_time().get(), peak_time);
84 }
85
86 static void
87 finished ()
88 {
89
90 }
91
92 BOOST_AUTO_TEST_CASE (audio_analysis_test)
93 {
94         shared_ptr<Film> film = new_test_film ("audio_analysis_test");
95         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
96         film->set_container (Ratio::from_id ("185"));
97         film->set_name ("audio_analysis_test");
98         boost::filesystem::path p = private_data / "betty_L.wav";
99
100         shared_ptr<FFmpegContent> c (new FFmpegContent (film, p));
101         film->examine_and_add_content (c);
102         wait_for_jobs ();
103
104         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
105         job->Finished.connect (boost::bind (&finished));
106         JobManager::instance()->add (job);
107         wait_for_jobs ();
108 }
109
110 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
111 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
112 {
113         shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
114         film->set_name ("audio_analysis_negative_delay_test");
115         shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "boon_telly.mkv"));
116         film->examine_and_add_content (c);
117         wait_for_jobs ();
118
119         c->audio->set_delay (-250);
120
121         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
122         job->Finished.connect (boost::bind (&finished));
123         JobManager::instance()->add (job);
124         wait_for_jobs ();
125 }
126
127 /** Check audio analysis that is incorrect in 2e98263 */
128 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
129 {
130         shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
131         film->set_name ("audio_analysis_test2");
132         shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "3d_thx_broadway_2010_lossless.m2ts"));
133         film->examine_and_add_content (c);
134         wait_for_jobs ();
135
136         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
137         job->Finished.connect (boost::bind (&finished));
138         JobManager::instance()->add (job);
139         wait_for_jobs ();
140 }
141
142
143 static bool done = false;
144
145 static void
146 analysis_finished ()
147 {
148         done = true;
149 }
150
151 /* Test a case which was reported to throw an exception; analysing
152  * a 12-channel DCP's audio.
153  */
154 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
155 {
156         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
157         film->set_container (Ratio::from_id ("185"));
158         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
159         film->set_name ("frobozz");
160
161         shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/white.wav"));
162         film->examine_and_add_content (content);
163         wait_for_jobs ();
164
165         film->set_audio_channels (12);
166         boost::signals2::connection connection;
167         JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));
168         wait_for_jobs ();
169         BOOST_CHECK (done);
170 }