Handle end-trim correctly in player; there is scope to make this more efficient by...
[dcpomatic.git] / test / torture_test.cc
1 /*
2     Copyright (C) 2017 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 /** @file  test/torture_test.cc
22  *  @brief Entire projects that are programmatically created and checked.
23  */
24
25 #include "lib/audio_content.h"
26 #include "lib/film.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/content_factory.h"
30 #include "test.h"
31 #include <dcp/cpl.h>
32 #include <dcp/reel.h>
33 #include <dcp/reel_sound_asset.h>
34 #include <dcp/sound_asset.h>
35 #include <boost/test/unit_test.hpp>
36
37 using std::list;
38 using boost::shared_ptr;
39
40 /** Test a fairly complex arrangement of content */
41 BOOST_AUTO_TEST_CASE (torture_test1)
42 {
43         shared_ptr<Film> film = new_test_film ("torture_test");
44         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
45         film->set_name ("torture_test");
46         film->set_container (Ratio::from_id ("185"));
47         film->set_sequence (false);
48
49         /* Staircase at an offset of 2000 samples, trimmed both start and end, with a gain of 6dB */
50         shared_ptr<Content> staircase = content_factory(film, "test/data/staircase.wav").front ();
51         film->examine_and_add_content (staircase);
52         wait_for_jobs ();
53         staircase->set_position (DCPTime::from_frames (2000, film->audio_frame_rate()));
54         staircase->set_trim_start (ContentTime::from_frames (12, 48000));
55         staircase->set_trim_end (ContentTime::from_frames (35, 48000));
56         staircase->audio->set_gain (20 * log10(2));
57
58         /* And again at an offset of 50000 samples, trimmed both start and end, with a gain of 6dB */
59         staircase = content_factory(film, "test/data/staircase.wav").front ();
60         film->examine_and_add_content (staircase);
61         wait_for_jobs ();
62         staircase->set_position (DCPTime::from_frames (50000, film->audio_frame_rate()));
63         staircase->set_trim_start (ContentTime::from_frames (12, 48000));
64         staircase->set_trim_end (ContentTime::from_frames (35, 48000));
65         staircase->audio->set_gain (20 * log10(2));
66
67         film->set_video_frame_rate (24);
68         film->write_metadata ();
69         film->make_dcp ();
70         wait_for_jobs ();
71
72         dcp::DCP dcp ("build/test/torture_test/" + film->dcp_name(false));
73         dcp.read ();
74
75         list<shared_ptr<dcp::CPL> > cpls = dcp.cpls ();
76         BOOST_REQUIRE_EQUAL (cpls.size(), 1);
77         list<shared_ptr<dcp::Reel> > reels = cpls.front()->reels ();
78         BOOST_REQUIRE_EQUAL (reels.size(), 1);
79
80         shared_ptr<dcp::ReelSoundAsset> reel_sound = reels.front()->main_sound();
81         BOOST_REQUIRE (reel_sound);
82         shared_ptr<dcp::SoundAsset> sound = reel_sound->asset();
83         BOOST_REQUIRE (sound);
84
85         shared_ptr<dcp::SoundAssetReader> sound_reader = sound->start_read ();
86
87         /* First frame silent */
88         shared_ptr<const dcp::SoundFrame> fr = sound_reader->get_frame (0);
89         for (int i = 0; i < fr->samples(); ++i) {
90                 for (int j = 0; j < 6; ++j) {
91                         BOOST_CHECK_EQUAL (fr->get(j, i), 0);
92                 }
93         }
94
95         /* The first staircase is 4800 - 12 - 35 = 4753 samples.  One frame is 2000 samples, so we span 3 frames */
96
97         BOOST_REQUIRE_EQUAL (fr->samples(), 2000);
98
99         int stair = 12;
100
101         fr = sound_reader->get_frame (1);
102         for (int i = 0; i < fr->samples(); ++i) {
103                 for (int j = 0; j < 6; ++j) {
104                         if (j == 2) {
105                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
106                                 ++stair;
107                         } else {
108                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
109                         }
110                 }
111         }
112
113         fr = sound_reader->get_frame (2);
114         for (int i = 0; i < fr->samples(); ++i) {
115                 for (int j = 0; j < 6; ++j) {
116                         if (j == 2) {
117                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
118                                 ++stair;
119                         } else {
120                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
121                         }
122                 }
123         }
124
125         fr = sound_reader->get_frame (3);
126         for (int i = 0; i < fr->samples(); ++i) {
127                 for (int j = 0; j < 6; ++j) {
128                         if (j == 2 && i < (4753 - (2000 * 2))) {
129                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
130                                 ++stair;
131                         } else {
132                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
133                         }
134                 }
135         }
136
137         /* Then some silence */
138
139         for (int i = 4; i < 24; ++i) {
140                 fr = sound_reader->get_frame (i);
141                 for (int j = 0; j < fr->samples(); ++j) {
142                         for (int k = 0; k < 6; ++k) {
143                                 BOOST_CHECK_EQUAL (fr->get(k, j), 0);
144                         }
145                 }
146         }
147
148         /* Then the same thing again */
149         stair = 12;
150
151         fr = sound_reader->get_frame (25);
152         for (int i = 0; i < fr->samples(); ++i) {
153                 for (int j = 0; j < 6; ++j) {
154                         if (j == 2) {
155                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
156                                 ++stair;
157                         } else {
158                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
159                         }
160                 }
161         }
162
163         fr = sound_reader->get_frame (26);
164         for (int i = 0; i < fr->samples(); ++i) {
165                 for (int j = 0; j < 6; ++j) {
166                         if (j == 2) {
167                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
168                                 ++stair;
169                         } else {
170                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
171                         }
172                 }
173         }
174
175         fr = sound_reader->get_frame (27);
176         for (int i = 0; i < fr->samples(); ++i) {
177                 for (int j = 0; j < 6; ++j) {
178                         if (j == 2 && i < (4753 - (2000 * 2))) {
179                                 BOOST_CHECK_EQUAL ((fr->get(j, i) + 128) >> 8, stair * 2);
180                                 ++stair;
181                         } else {
182                                 BOOST_CHECK_EQUAL (fr->get(j, i), 0);
183                         }
184                 }
185         }
186 }