Rename some methods.
[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 Decoder
62 {
63 public:
64         TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
65                 : _test_audio_content (content)
66                 , _position (0)
67         {
68                 audio.reset (new AudioDecoder (this, content->audio, false, log));
69         }
70
71         bool pass (PassReason, bool)
72         {
73                 Frame const N = min (
74                         Frame (2000),
75                         _test_audio_content->audio_length() - _position
76                         );
77
78                 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio->stream()->channels(), N));
79                 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
80                         for (int j = 0; j < N; ++j) {
81                                 buffers->data(i)[j] = j + _position;
82                         }
83                 }
84
85                 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
86                 _position += N;
87
88                 return N < 2000;
89         }
90
91         void seek (ContentTime t, bool accurate)
92         {
93                 audio->seek (t, accurate);
94                 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
95         }
96
97 private:
98         boost::shared_ptr<TestAudioContent> _test_audio_content;
99         Frame _position;
100 };
101
102 shared_ptr<TestAudioContent> content;
103 shared_ptr<TestAudioDecoder> decoder;
104
105 static ContentAudio
106 get (Frame from, Frame length)
107 {
108         decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
109         ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
110         BOOST_CHECK_EQUAL (ca.frame, from);
111         return ca;
112 }
113
114 static void
115 check (Frame from, Frame length)
116 {
117         ContentAudio ca = get (from, length);
118         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
119                 for (int j = 0; j < length; ++j) {
120                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
121                 }
122         }
123 }
124
125 /** Check the logic in AudioDecoder::get_audio */
126 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
127 {
128         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
129
130         content.reset (new TestAudioContent (film));
131         decoder.reset (new TestAudioDecoder (content, film->log()));
132
133         /* Simple reads */
134
135         check (0, 48000);
136         check (44, 9123);
137         check (9991, 22);
138
139         /* Read off the end */
140
141         Frame const from = content->audio->resampled_frame_rate() * 61;
142         Frame const length = content->audio->resampled_frame_rate() * 4;
143         ContentAudio ca = get (from, length);
144
145         for (int i = 0; i < content->audio->stream()->channels(); ++i) {
146                 for (int j = 0; j < ca.audio->frames(); ++j) {
147                         BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
148                 }
149         }
150 }