2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file test/audio_decoder_test.cc
21 * @brief Tests of the AudioDecoder class.
25 #include "lib/content.h"
26 #include "lib/audio_decoder.h"
27 #include "lib/audio_content.h"
29 #include <boost/test/unit_test.hpp>
36 using boost::shared_ptr;
38 class TestAudioContent : public Content
41 TestAudioContent (shared_ptr<const Film> film)
44 audio.reset (new AudioContent (this));
45 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
48 std::string summary () const {
52 DCPTime full_length () const {
53 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
56 Frame audio_length () const {
57 return llrint (61.2942 * 48000);
61 class TestAudioDecoder : public Decoder
64 TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
65 : _test_audio_content (content)
68 audio.reset (new AudioDecoder (this, content->audio, false, log));
71 bool pass (PassReason, bool)
75 _test_audio_content->audio_length() - _position
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;
85 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
91 void seek (ContentTime t, bool accurate)
93 audio->seek (t, accurate);
94 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
98 boost::shared_ptr<TestAudioContent> _test_audio_content;
102 shared_ptr<TestAudioContent> content;
103 shared_ptr<TestAudioDecoder> decoder;
106 get (Frame from, Frame length)
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);
115 check (Frame from, Frame length)
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);
125 /** Check the logic in AudioDecoder::get_audio */
126 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
128 shared_ptr<Film> film = new_test_film ("audio_decoder_test");
130 content.reset (new TestAudioContent (film));
131 decoder.reset (new TestAudioDecoder (content, film->log()));
139 /* Read off the end */
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);
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);