21480a5c3f1da89c5246054f4e217039786680ec
[dcpomatic.git] / test / audio_decoder_test.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  test/audio_decoder_test.cc
21  *  @brief Tests of the AudioDecoder class.
22  */
23
24 #include "test.h"
25 #include "lib/content.h"
26 #include "lib/audio_decoder.h"
27 #include "lib/audio_content.h"
28 #include "lib/film.h"
29 #include <boost/test/unit_test.hpp>
30 #include <cassert>
31 #include <iostream>
32
33 using std::string;
34 using std::cout;
35 using std::min;
36 using boost::shared_ptr;
37
38 class TestAudioContent : public Content
39 {
40 public:
41         TestAudioContent (shared_ptr<const Film> film)
42                 : Content (film)
43         {
44                 audio.reset (new AudioContent (this, film));
45                 audio->set_stream (AudioStreamPtr (new AudioStream (48000, 2)));
46         }
47
48         std::string summary () const {
49                 return "";
50         }
51
52         DCPTime full_length () const {
53                 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
54         }
55
56         Frame audio_length () const {
57                 return llrint (61.2942 * audio->stream()->frame_rate ());
58         }
59 };
60
61 class TestAudioDecoder : public AudioDecoder
62 {
63 public:
64         TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
65                 : AudioDecoder (content->audio, false, log)
66                 , _test_audio_content (content)
67                 , _position (0)
68         {}
69
70         bool pass (PassReason, bool)
71         {
72                 Frame const N = min (
73                         Frame (2000),
74                         _test_audio_content->audio_length() - _position
75                         );
76
77                 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio->stream()->channels(), N));
78                 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
79                         for (int j = 0; j < N; ++j) {
80                                 buffers->data(i)[j] = j + _position;
81                         }
82                 }
83
84                 audio (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
85                 _position += N;
86
87                 return N < 2000;
88         }
89
90         void seek (ContentTime t, bool accurate)
91         {
92                 AudioDecoder::seek (t, accurate);
93                 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
94         }
95
96 private:
97         boost::shared_ptr<TestAudioContent> _test_audio_content;
98         Frame _position;
99 };
100
101 shared_ptr<TestAudioContent> content;
102 shared_ptr<TestAudioDecoder> decoder;
103
104 static ContentAudio
105 get (Frame from, Frame length)
106 {
107         decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
108         ContentAudio ca = decoder->get_audio (content->audio->stream(), from, length, true);
109         BOOST_CHECK_EQUAL (ca.frame, from);
110         return ca;
111 }
112
113 static void
114 check (Frame from, Frame length)
115 {
116         ContentAudio ca = get (from, length);
117         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
118                 for (int j = 0; j < length; ++j) {
119                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
120                 }
121         }
122 }
123
124 /** Check the logic in AudioDecoder::get_audio */
125 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
126 {
127         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
128
129         content.reset (new TestAudioContent (film));
130         decoder.reset (new TestAudioDecoder (content, film->log()));
131
132         /* Simple reads */
133
134         check (0, 48000);
135         check (44, 9123);
136         check (9991, 22);
137
138         /* Read off the end */
139
140         Frame const from = content->audio->resampled_frame_rate() * 61;
141         Frame const length = content->audio->resampled_frame_rate() * 4;
142         ContentAudio ca = get (from, length);
143
144         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
145                 for (int j = 0; j < ca.audio->frames(); ++j) {
146                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
147                 }
148         }
149 }