Basic grunt-work, untested and unfinished, but it compiles.
[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 ()
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                 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
98         }
99
100 private:
101         boost::shared_ptr<TestAudioContent> _test_audio_content;
102         Frame _position;
103 };
104
105 shared_ptr<TestAudioContent> content;
106 shared_ptr<TestAudioDecoder> decoder;
107
108 static ContentAudio
109 get (Frame from, Frame length)
110 {
111         decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
112         ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
113         BOOST_CHECK_EQUAL (ca.frame, from);
114         return ca;
115 }
116
117 static void
118 check (Frame from, Frame length)
119 {
120         ContentAudio ca = get (from, length);
121         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
122                 for (int j = 0; j < length; ++j) {
123                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
124                 }
125         }
126 }
127
128 /** Check the logic in AudioDecoder::get_audio */
129 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
130 {
131         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
132
133         content.reset (new TestAudioContent (film));
134         decoder.reset (new TestAudioDecoder (content, film->log()));
135
136         /* Simple reads */
137
138         check (0, 48000);
139         check (44, 9123);
140         check (9991, 22);
141
142         /* Read off the end */
143
144         Frame const from = content->audio->resampled_frame_rate() * 61;
145         Frame const length = content->audio->resampled_frame_rate() * 4;
146         ContentAudio ca = get (from, length);
147
148         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
149                 for (int j = 0; j < ca.audio->frames(); ++j) {
150                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
151                 }
152         }
153 }
154
155 BOOST_AUTO_TEST_CASE (audio_decoder_test)
156 {
157         shared_ptr<Film> film = new_test_film ("analyse_audio_test");
158         film->set_container (Ratio::from_id ("185"));
159         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
160         film->set_name ("frobozz");
161         shared_ptr<Content> content = content_factory(film, private_data / "20 The Wedding Convoy Song.m4a").front();
162         film->examine_and_add_content (content);
163         wait_for_jobs ();
164
165         content->set_trim_start (ContentTime::from_seconds (60));
166         film->make_dcp ();
167         BOOST_CHECK (!wait_for_jobs ());
168 }