2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/audio_delay_test.cc
22 * @brief Test encode using some FFmpegContents which have audio delays.
24 * The output is checked algorithmically using knowledge of the input.
27 #include <boost/test/unit_test.hpp>
28 #include <dcp/sound_frame.h>
31 #include <dcp/sound_asset.h>
32 #include <dcp/reel_sound_asset.h>
33 #include "lib/ffmpeg_content.h"
34 #include "lib/dcp_content_type.h"
35 #include "lib/ratio.h"
37 #include "lib/audio_content.h"
43 using boost::lexical_cast;
44 using boost::shared_ptr;
47 void test_audio_delay (int delay_in_ms)
49 BOOST_TEST_MESSAGE ("Testing delay of " << delay_in_ms);
51 string const film_name = "audio_delay_test_" + lexical_cast<string> (delay_in_ms);
52 shared_ptr<Film> film = new_test_film (film_name);
53 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
54 film->set_container (Ratio::from_id ("185"));
55 film->set_name (film_name);
57 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/staircase.wav"));
58 film->examine_and_add_content (content);
60 content->audio->set_delay (delay_in_ms);
65 boost::filesystem::path path = "build/test";
67 path /= film->dcp_name ();
68 dcp::DCP check (path.string ());
71 shared_ptr<const dcp::ReelSoundAsset> sound_asset = check.cpls().front()->reels().front()->main_sound ();
72 BOOST_CHECK (sound_asset);
74 /* Sample index in the DCP */
76 /* DCP sound asset frame */
79 int const delay_in_frames = delay_in_ms * 48000 / 1000;
81 while (n < sound_asset->asset()->intrinsic_duration()) {
82 shared_ptr<const dcp::SoundFrame> sound_frame = sound_asset->asset()->get_frame (frame++);
83 uint8_t const * d = sound_frame->data ();
85 for (int i = 0; i < sound_frame->size(); i += (3 * sound_asset->asset()->channels())) {
87 /* Mono input so it will appear on centre */
88 int const sample = d[i + 7] | (d[i + 8] << 8);
90 int delayed = n - delay_in_frames;
91 if (delayed < 0 || delayed >= 4800) {
95 BOOST_REQUIRE_EQUAL (sample, delayed);
101 /* Test audio delay when specified in a piece of audio content */
102 BOOST_AUTO_TEST_CASE (audio_delay_test)
104 test_audio_delay (0);
105 test_audio_delay (42);
106 test_audio_delay (-66);