92c99a378065389c800ba459682ecb6e785d8983
[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 <boost/make_shared.hpp>
32 #include <cassert>
33 #include <iostream>
34
35 using std::string;
36 using std::cout;
37 using std::min;
38 using boost::shared_ptr;
39 using boost::make_shared;
40
41 class TestAudioContent : public Content
42 {
43 public:
44         TestAudioContent (shared_ptr<const Film> film)
45                 : Content (film)
46         {
47                 audio.reset (new AudioContent (this));
48                 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
49         }
50
51         std::string summary () const {
52                 return "";
53         }
54
55         DCPTime full_length () const {
56                 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
57         }
58
59         Frame audio_length () const {
60                 return llrint (61.2942 * 48000);
61         }
62 };
63
64 class TestAudioDecoder : public Decoder
65 {
66 public:
67         TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
68                 : _test_audio_content (content)
69                 , _position (0)
70         {
71                 audio.reset (new AudioDecoder (this, content->audio, false, log));
72         }
73
74         bool pass (PassReason, bool)
75         {
76                 Frame const N = min (
77                         Frame (2000),
78                         _test_audio_content->audio_length() - _position
79                         );
80
81                 shared_ptr<AudioBuffers> buffers = make_shared<AudioBuffers> (_test_audio_content->audio->stream()->channels(), N);
82                 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
83                         for (int j = 0; j < N; ++j) {
84                                 buffers->data(i)[j] = j + _position;
85                         }
86                 }
87
88                 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
89                 _position += N;
90
91                 return N < 2000;
92         }
93
94         void seek (ContentTime t, bool accurate)
95         {
96                 audio->seek (t, accurate);
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 }