FFmpegContent does not need audio_length().
[dcpomatic.git] / test / audio_decoder_test.cc
1 /*
2     Copyright (C) 2014-2015 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 <cassert>
25 #include <boost/test/unit_test.hpp>
26 #include "test.h"
27 #include "lib/audio_decoder.h"
28 #include "lib/audio_content.h"
29
30 using std::string;
31 using std::cout;
32 using std::min;
33 using boost::shared_ptr;
34
35 class TestAudioContent : public AudioContent
36 {
37 public:
38         TestAudioContent (shared_ptr<Film> film)
39                 : Content (film)
40                 , AudioContent (film, DCPTime ())
41         {}
42
43         string summary () const {
44                 return "";
45         }
46
47         string information () const {
48                 return "";
49         }
50
51         DCPTime full_length () const {
52                 return DCPTime::from_seconds (float (audio_length()) / audio_frame_rate ());
53         }
54
55         int audio_channels () const {
56                 return 2;
57         }
58
59         Frame audio_length () const {
60                 return rint (61.2942 * audio_frame_rate ());
61         }
62
63         int audio_frame_rate () const {
64                 return 48000;
65         }
66
67         AudioMapping audio_mapping () const {
68                 return AudioMapping (audio_channels ());
69         }
70
71         void set_audio_mapping (AudioMapping) {}
72 };
73
74 class TestAudioDecoder : public AudioDecoder
75 {
76 public:
77         TestAudioDecoder (shared_ptr<TestAudioContent> content)
78                 : AudioDecoder (content)
79                 , _test_audio_content (content)
80                 , _position (0)
81         {}
82
83         bool pass (PassReason)
84         {
85                 Frame const N = min (
86                         Frame (2000),
87                         _test_audio_content->audio_length() - _position
88                         );
89
90                 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_audio_content->audio_channels(), N));
91                 for (int i = 0; i < _audio_content->audio_channels(); ++i) {
92                         for (int j = 0; j < N; ++j) {
93                                 buffers->data(i)[j] = j + _position;
94                         }
95                 }
96
97                 audio (buffers, ContentTime::from_frames (_position, _audio_content->resampled_audio_frame_rate ()));
98                 _position += N;
99
100                 return N < 2000;
101         }
102
103         void seek (ContentTime t, bool accurate)
104         {
105                 AudioDecoder::seek (t, accurate);
106                 _position = t.frames (_audio_content->resampled_audio_frame_rate ());
107         }
108
109 private:
110         boost::shared_ptr<TestAudioContent> _test_audio_content;
111         Frame _position;
112 };
113
114 shared_ptr<TestAudioContent> content;
115 shared_ptr<TestAudioDecoder> decoder;
116
117 static shared_ptr<ContentAudio>
118 get (Frame from, Frame length)
119 {
120         decoder->seek (ContentTime::from_frames (from, content->resampled_audio_frame_rate ()), true);
121         shared_ptr<ContentAudio> ca = decoder->get_audio (from, length, true);
122         BOOST_CHECK_EQUAL (ca->frame, from);
123         return ca;
124 }
125
126 static void
127 check (Frame from, Frame length)
128 {
129         shared_ptr<ContentAudio> ca = get (from, length);
130         for (int i = 0; i < content->audio_channels(); ++i) {
131                 for (int j = 0; j < length; ++j) {
132                         BOOST_CHECK_EQUAL (ca->audio->data(i)[j], j + from);
133                         assert (ca->audio->data(i)[j] == j + from);
134                 }
135         }
136 }
137
138 /** Check the logic in AudioDecoder::get_audio */
139 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
140 {
141         shared_ptr<Film> film = new_test_film ("audio_decoder_test");
142
143         content.reset (new TestAudioContent (film));
144         decoder.reset (new TestAudioDecoder (content));
145         
146         /* Simple reads */
147         check (0, 48000);
148         check (44, 9123);
149         check (9991, 22);
150
151         /* Read off the end */
152
153         Frame const from = content->resampled_audio_frame_rate() * 61;
154         Frame const length = content->resampled_audio_frame_rate() * 4;
155         shared_ptr<ContentAudio> ca = get (from, length);
156         
157         for (int i = 0; i < content->audio_channels(); ++i) {
158                 for (int j = 0; j < ca->audio->frames(); ++j) {
159                         BOOST_REQUIRE_EQUAL (ca->audio->data(i)[j], j + from);
160                 }
161         }
162 }