Missing library from test link list.
[dcpomatic.git] / test / audio_delay_test.cc
1 /*
2     Copyright (C) 2013-2014 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 /** @defgroup specific Tests of fairly specific areas */
22
23 /** @file  test/audio_delay_test.cc
24  *  @brief Test encode using some FFmpegContents which have audio delays.
25  *  @ingroup specific
26  *
27  *  The output is checked algorithmically using knowledge of the input.
28  */
29
30 #include <boost/test/unit_test.hpp>
31 #include <dcp/sound_frame.h>
32 #include <dcp/cpl.h>
33 #include <dcp/reel.h>
34 #include <dcp/sound_asset.h>
35 #include <dcp/sound_asset_reader.h>
36 #include <dcp/reel_sound_asset.h>
37 #include "lib/ffmpeg_content.h"
38 #include "lib/dcp_content_type.h"
39 #include "lib/ratio.h"
40 #include "lib/film.h"
41 #include "lib/audio_content.h"
42 #include "test.h"
43 #include <iostream>
44
45 using std::string;
46 using std::cout;
47 using boost::lexical_cast;
48 using boost::shared_ptr;
49
50 static
51 void test_audio_delay (int delay_in_ms)
52 {
53         BOOST_TEST_MESSAGE ("Testing delay of " << delay_in_ms);
54
55         string const film_name = "audio_delay_test_" + lexical_cast<string> (delay_in_ms);
56         shared_ptr<Film> film = new_test_film (film_name);
57         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
58         film->set_container (Ratio::from_id ("185"));
59         film->set_name (film_name);
60
61         shared_ptr<FFmpegContent> content (new FFmpegContent("test/data/staircase.wav"));
62         film->examine_and_add_content (content);
63         BOOST_REQUIRE (!wait_for_jobs());
64         content->audio->set_delay (delay_in_ms);
65
66         film->make_dcp ();
67         BOOST_REQUIRE (!wait_for_jobs());
68
69         boost::filesystem::path path = "build/test";
70         path /= film_name;
71         path /= film->dcp_name ();
72         std::cout << "Loading " << path.string() << "\n";
73         dcp::DCP check (path.string ());
74         check.read ();
75
76         shared_ptr<const dcp::ReelSoundAsset> sound_asset = check.cpls().front()->reels().front()->main_sound ();
77         BOOST_CHECK (sound_asset);
78
79         /* Sample index in the DCP */
80         int n = 0;
81         /* DCP sound asset frame */
82         int frame = 0;
83         /* Delay in frames */
84         int const delay_in_frames = delay_in_ms * 48000 / 1000;
85
86         while (n < sound_asset->asset()->intrinsic_duration()) {
87                 shared_ptr<const dcp::SoundFrame> sound_frame = sound_asset->asset()->start_read()->get_frame (frame++);
88                 uint8_t const * d = sound_frame->data ();
89
90                 for (int i = 0; i < sound_frame->size(); i += (3 * sound_asset->asset()->channels())) {
91
92                         /* Mono input so it will appear on centre */
93                         int const sample = d[i + 7] | (d[i + 8] << 8);
94
95                         int delayed = n - delay_in_frames;
96                         if (delayed < 0 || delayed >= 4800) {
97                                 delayed = 0;
98                         }
99
100                         BOOST_REQUIRE_EQUAL (sample, delayed);
101                         ++n;
102                 }
103         }
104 }
105
106 /* Test audio delay when specified in a piece of audio content */
107 BOOST_AUTO_TEST_CASE (audio_delay_test)
108 {
109         test_audio_delay (0);
110         test_audio_delay (42);
111         test_audio_delay (-66);
112 }