No-op; fix GPL address and use the explicit-program-name version.
[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/film.h"
30 #include <boost/test/unit_test.hpp>
31 #include <cassert>
32 #include <iostream>
33
34 using std::string;
35 using std::cout;
36 using std::min;
37 using boost::shared_ptr;
38
39 class TestAudioContent : public Content
40 {
41 public:
42         TestAudioContent (shared_ptr<const Film> film)
43                 : Content (film)
44         {
45                 audio.reset (new AudioContent (this));
46                 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
47         }
48
49         std::string summary () const {
50                 return "";
51         }
52
53         DCPTime full_length () const {
54                 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
55         }
56
57         Frame audio_length () const {
58                 return llrint (61.2942 * 48000);
59         }
60 };
61
62 class TestAudioDecoder : public Decoder
63 {
64 public:
65         TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
66                 : _test_audio_content (content)
67                 , _position (0)
68         {
69                 audio.reset (new AudioDecoder (this, content->audio, false, log));
70         }
71
72         bool pass (PassReason, bool)
73         {
74                 Frame const N = min (
75                         Frame (2000),
76                         _test_audio_content->audio_length() - _position
77                         );
78
79                 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio->stream()->channels(), N));
80                 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
81                         for (int j = 0; j < N; ++j) {
82                                 buffers->data(i)[j] = j + _position;
83                         }
84                 }
85
86                 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
87                 _position += N;
88
89                 return N < 2000;
90         }
91
92         void seek (ContentTime t, bool accurate)
93         {
94                 audio->seek (t, accurate);
95                 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
96         }
97
98 private:
99         boost::shared_ptr<TestAudioContent> _test_audio_content;
100         Frame _position;
101 };
102
103 shared_ptr<TestAudioContent> content;
104 shared_ptr<TestAudioDecoder> decoder;
105
106 static ContentAudio
107 get (Frame from, Frame length)
108 {
109         decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
110         ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
111         BOOST_CHECK_EQUAL (ca.frame, from);
112         return ca;
113 }
114
115 static void
116 check (Frame from, Frame length)
117 {
118         ContentAudio ca = get (from, length);
119         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
120                 for (int j = 0; j < length; ++j) {
121                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
122                 }
123         }
124 }
125
126 /** Check the logic in AudioDecoder::get_audio */
127 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
128 {
129         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
130
131         content.reset (new TestAudioContent (film));
132         decoder.reset (new TestAudioDecoder (content, film->log()));
133
134         /* Simple reads */
135
136         check (0, 48000);
137         check (44, 9123);
138         check (9991, 22);
139
140         /* Read off the end */
141
142         Frame const from = content->audio->resampled_frame_rate() * 61;
143         Frame const length = content->audio->resampled_frame_rate() * 4;
144         ContentAudio ca = get (from, length);
145
146         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
147                 for (int j = 0; j < ca.audio->frames(); ++j) {
148                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
149                 }
150         }
151 }