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