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