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