Test start-trim of audio-only content.
[dcpomatic.git] / test / audio_decoder_test.cc
1 /*
2     Copyright (C) 2014-2016 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/audio_decoder_test.cc
22  *  @brief Tests of the AudioDecoder class.
23  */
24
25 #include "test.h"
26 #include "lib/content.h"
27 #include "lib/audio_decoder.h"
28 #include "lib/audio_content.h"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ratio.h"
32 #include "lib/film.h"
33 #include <boost/test/unit_test.hpp>
34 #include <cassert>
35 #include <iostream>
36
37 using std::string;
38 using std::cout;
39 using std::min;
40 using boost::shared_ptr;
41
42 class TestAudioContent : public Content
43 {
44 public:
45         TestAudioContent (shared_ptr<const Film> film)
46                 : Content (film)
47         {
48                 audio.reset (new AudioContent (this));
49                 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
50         }
51
52         std::string summary () const {
53                 return "";
54         }
55
56         DCPTime full_length () const {
57                 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
58         }
59
60         Frame audio_length () const {
61                 return llrint (61.2942 * 48000);
62         }
63 };
64
65 class TestAudioDecoder : public Decoder
66 {
67 public:
68         TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
69                 : _test_audio_content (content)
70                 , _position (0)
71         {
72                 audio.reset (new AudioDecoder (this, content->audio, log));
73         }
74
75         bool pass (PassReason, bool)
76         {
77                 Frame const N = min (
78                         Frame (2000),
79                         _test_audio_content->audio_length() - _position
80                         );
81
82                 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio->stream()->channels(), N));
83                 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
84                         for (int j = 0; j < N; ++j) {
85                                 buffers->data(i)[j] = j + _position;
86                         }
87                 }
88
89                 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
90                 _position += N;
91
92                 return N < 2000;
93         }
94
95         void seek (ContentTime t, bool accurate)
96         {
97                 audio->seek (t, accurate);
98                 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
99         }
100
101 private:
102         boost::shared_ptr<TestAudioContent> _test_audio_content;
103         Frame _position;
104 };
105
106 shared_ptr<TestAudioContent> content;
107 shared_ptr<TestAudioDecoder> decoder;
108
109 static ContentAudio
110 get (Frame from, Frame length)
111 {
112         decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
113         ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
114         BOOST_CHECK_EQUAL (ca.frame, from);
115         return ca;
116 }
117
118 static void
119 check (Frame from, Frame length)
120 {
121         ContentAudio ca = get (from, length);
122         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
123                 for (int j = 0; j < length; ++j) {
124                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
125                 }
126         }
127 }
128
129 /** Check the logic in AudioDecoder::get_audio */
130 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
131 {
132         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
133
134         content.reset (new TestAudioContent (film));
135         decoder.reset (new TestAudioDecoder (content, film->log()));
136
137         /* Simple reads */
138
139         check (0, 48000);
140         check (44, 9123);
141         check (9991, 22);
142
143         /* Read off the end */
144
145         Frame const from = content->audio->resampled_frame_rate() * 61;
146         Frame const length = content->audio->resampled_frame_rate() * 4;
147         ContentAudio ca = get (from, length);
148
149         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
150                 for (int j = 0; j < ca.audio->frames(); ++j) {
151                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
152                 }
153         }
154 }
155
156 BOOST_AUTO_TEST_CASE (audio_decoder_test)
157 {
158         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
159         film->set_container (Ratio::from_id ("185"));
160         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
161         film->set_name ("frobozz");
162         shared_ptr<Content> content = content_factory (film, private_data / "20 The Wedding Convoy Song.m4a");
163         film->examine_and_add_content (content);
164         wait_for_jobs ();
165
166         content->set_trim_start (ContentTime::from_seconds (60));
167         film->make_dcp ();
168         BOOST_CHECK (!wait_for_jobs ());
169 }