Mostly-merge master.
[dcpomatic.git] / test / audio_delay_test.cc
1 /*
2     Copyright (C) 2013 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 #include <boost/test/unit_test.hpp>
21 #include <dcp/sound_frame.h>
22 #include <dcp/cpl.h>
23 #include <dcp/reel.h>
24 #include <dcp/sound_mxf.h>
25 #include <dcp/reel_sound_asset.h>
26 #include "lib/sndfile_content.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/film.h"
30 #include "test.h"
31
32 using std::string;
33 using std::cout;
34 using boost::lexical_cast;
35 using boost::shared_ptr;
36
37 static
38 void test_audio_delay (int delay_in_ms)
39 {
40         string const film_name = "audio_delay_test_" + lexical_cast<string> (delay_in_ms);
41         shared_ptr<Film> film = new_test_film (film_name);
42         film->set_dcp_content_type (DCPContentType::from_dci_name ("FTR"));
43         film->set_container (Ratio::from_id ("185"));
44         film->set_name (film_name);
45
46         shared_ptr<SndfileContent> content (new SndfileContent (film, "test/data/staircase.wav"));
47         content->set_audio_delay (delay_in_ms);
48         film->examine_and_add_content (content);
49         wait_for_jobs ();
50
51         film->make_dcp ();
52         wait_for_jobs ();
53
54         boost::filesystem::path path = "build/test";
55         path /= film_name;
56         path /= film->dcp_name ();
57         dcp::DCP check (path.string ());
58         check.read ();
59
60         shared_ptr<const dcp::ReelSoundAsset> sound_asset = check.cpls().front()->reels().front()->main_sound ();
61         BOOST_CHECK (sound_asset);
62
63         /* Sample index in the DCP */
64         int n = 0;
65         /* DCP sound asset frame */
66         int frame = 0;
67         /* Delay in frames */
68         int const delay_in_frames = delay_in_ms * 48000 / 1000;
69
70         while (n < sound_asset->mxf()->intrinsic_duration()) {
71                 shared_ptr<const dcp::SoundFrame> sound_frame = sound_asset->mxf()->get_frame (frame++);
72                 uint8_t const * d = sound_frame->data ();
73                 
74                 for (int i = 0; i < sound_frame->size(); i += (3 * sound_asset->mxf()->channels())) {
75
76                         /* Mono input so it will appear on centre */
77                         int const sample = d[i + 7] | (d[i + 8] << 8);
78
79                         int delayed = n - delay_in_frames;
80                         if (delayed < 0 || delayed >= 4800) {
81                                 delayed = 0;
82                         }
83
84                         BOOST_CHECK_EQUAL (sample, delayed);
85                         ++n;
86                 }
87         }
88 }
89
90
91 /* Test audio delay when specified in a piece of audio content */
92 BOOST_AUTO_TEST_CASE (audio_delay_test)
93 {
94         test_audio_delay (0);
95         test_audio_delay (42);
96         test_audio_delay (-66);
97 }