summaryrefslogtreecommitdiff
path: root/test/audio_analysis_test.cc
blob: c3a83186ed301befecc6771df7c2258ec39751c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

/** @defgroup selfcontained Self-contained tests of single classes / method sets */

/** @file  test/audio_analysis_test.cc
 *  @brief Test AudioAnalysis class.
 *  @ingroup selfcontained
 */

#include <boost/test/unit_test.hpp>
#include "lib/audio_analysis.h"
#include "lib/analyse_audio_job.h"
#include "lib/film.h"
#include "lib/ffmpeg_content.h"
#include "lib/dcp_content_type.h"
#include "lib/ffmpeg_content.h"
#include "lib/ratio.h"
#include "lib/job_manager.h"
#include "lib/audio_content.h"
#include "lib/content_factory.h"
#include "lib/playlist.h"
#include "test.h"
#include <iostream>

using std::vector;
using boost::shared_ptr;
using namespace dcpomatic;

static float
random_float ()
{
	return (float (rand ()) / RAND_MAX) * 2 - 1;
}

BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
{
	int const channels = 3;
	int const points = 4096;

	AudioAnalysis a (3);
	for (int i = 0; i < channels; ++i) {
		for (int j = 0; j < points; ++j) {
			AudioPoint p;
			p[AudioPoint::PEAK] = random_float ();
			p[AudioPoint::RMS] = random_float ();
			a.add_point (i, p);
		}
	}

	vector<AudioAnalysis::PeakTime> peak;
	for (int i = 0; i < channels; ++i) {
		peak.push_back (AudioAnalysis::PeakTime (random_float(), DCPTime (rand())));
	}
	a.set_sample_peak (peak);

	a.set_samples_per_point (100);
	a.set_sample_rate (48000);
	a.write ("build/test/audio_analysis_serialisation_test");

	AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
	for (int i = 0; i < channels; ++i) {
		BOOST_CHECK_EQUAL (b.points(i), points);
		for (int j = 0; j < points; ++j) {
			AudioPoint p = a.get_point (i, j);
			AudioPoint q = b.get_point (i, j);
			BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], q[AudioPoint::PEAK], 1);
			BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  q[AudioPoint::RMS], 1);
		}
	}

	BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3U);
	for (int i = 0; i < channels; ++i) {
		BOOST_CHECK_CLOSE (b.sample_peak()[i].peak, peak[i].peak, 1);
		BOOST_CHECK_EQUAL (b.sample_peak()[i].time.get(), peak[i].time.get());
	}

	BOOST_CHECK_EQUAL (a.samples_per_point(), 100);
	BOOST_CHECK_EQUAL (a.sample_rate(), 48000);
}

static void
finished ()
{

}

BOOST_AUTO_TEST_CASE (audio_analysis_test)
{
	shared_ptr<Film> film = new_test_film ("audio_analysis_test");
	film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
	film->set_container (Ratio::from_id ("185"));
	film->set_name ("audio_analysis_test");
	boost::filesystem::path p = TestPaths::private_data() / "betty_L.wav";

	shared_ptr<FFmpegContent> c (new FFmpegContent(p));
	film->examine_and_add_content (c);
	BOOST_REQUIRE (!wait_for_jobs());

	shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
	job->Finished.connect (boost::bind (&finished));
	JobManager::instance()->add (job);
	BOOST_REQUIRE (!wait_for_jobs());
}

/** Check that audio analysis works (i.e. runs without error) with a -ve delay */
BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
{
	shared_ptr<Film> film = new_test_film ("audio_analysis_negative_delay_test");
	film->set_name ("audio_analysis_negative_delay_test");
	shared_ptr<FFmpegContent> c (new FFmpegContent(TestPaths::private_data() / "boon_telly.mkv"));
	film->examine_and_add_content (c);
	BOOST_REQUIRE (!wait_for_jobs());

	c->audio->set_delay (-250);

	shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
	job->Finished.connect (boost::bind (&finished));
	JobManager::instance()->add (job);
	BOOST_REQUIRE (!wait_for_jobs());
}

/** Check audio analysis that is incorrect in 2e98263 */
BOOST_AUTO_TEST_CASE (audio_analysis_test2)
{
	shared_ptr<Film> film = new_test_film ("audio_analysis_test2");
	film->set_name ("audio_analysis_test2");
	shared_ptr<FFmpegContent> c (new FFmpegContent(TestPaths::private_data() / "3d_thx_broadway_2010_lossless.m2ts"));
	film->examine_and_add_content (c);
	BOOST_REQUIRE (!wait_for_jobs());

	shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
	job->Finished.connect (boost::bind (&finished));
	JobManager::instance()->add (job);
	BOOST_REQUIRE (!wait_for_jobs());
}


static bool done = false;

static void
analysis_finished ()
{
	done = true;
}

/* Test a case which was reported to throw an exception; analysing
 * a 12-channel DCP's audio.
 */
BOOST_AUTO_TEST_CASE (audio_analysis_test3)
{
	shared_ptr<Film> film = new_test_film ("analyse_audio_test");
	film->set_container (Ratio::from_id ("185"));
	film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
	film->set_name ("frobozz");

	shared_ptr<FFmpegContent> content (new FFmpegContent("test/data/white.wav"));
	film->examine_and_add_content (content);
	BOOST_REQUIRE (!wait_for_jobs());

	film->set_audio_channels (12);
	boost::signals2::connection connection;
	JobManager::instance()->analyse_audio (film, film->playlist(), false, connection, boost::bind (&analysis_finished));
	BOOST_REQUIRE (!wait_for_jobs());
	BOOST_CHECK (done);
}

/** Run an audio analysis that triggered an exception in the audio decoder at one point */
BOOST_AUTO_TEST_CASE (analyse_audio_test4)
{
	shared_ptr<Film> film = new_test_film ("analyse_audio_test");
	film->set_container (Ratio::from_id ("185"));
	film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
	film->set_name ("frobozz");
	shared_ptr<Content> content = content_factory(TestPaths::private_data() / "20 The Wedding Convoy Song.m4a").front();
	film->examine_and_add_content (content);
	BOOST_REQUIRE (!wait_for_jobs());

	shared_ptr<Playlist> playlist (new Playlist);
	playlist->add (film, content);
	boost::signals2::connection c;
	JobManager::instance()->analyse_audio (film, playlist, false, c, boost::bind (&finished));
	BOOST_CHECK (!wait_for_jobs ());
}

BOOST_AUTO_TEST_CASE (analyse_audio_leqm_test)
{
	shared_ptr<Film> film = new_test_film2 ("analyse_audio_leqm_test");
	film->set_audio_channels (2);
	shared_ptr<Content> content = content_factory(TestPaths::private_data() / "betty_stereo_48k.wav").front();
	film->examine_and_add_content (content);
	BOOST_REQUIRE (!wait_for_jobs());

	shared_ptr<Playlist> playlist (new Playlist);
	playlist->add (film, content);
	boost::signals2::connection c;
	JobManager::instance()->analyse_audio (film, playlist, false, c, boost::bind (&finished));
	BOOST_CHECK (!wait_for_jobs());

	AudioAnalysis analysis(film->audio_analysis_path(playlist));

	/* The CLI tool of leqm_nrt gives this value for betty_stereo_48k.wav */
	BOOST_CHECK_CLOSE (analysis.leqm().get_value_or(0), 88.276, 0.001);
}