2 Copyright (C) 2014-2015 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 <boost/test/unit_test.hpp>
27 #include "lib/audio_decoder.h"
28 #include "lib/single_stream_audio_content.h"
34 using boost::shared_ptr;
36 class TestAudioContent : public SingleStreamAudioContent
39 TestAudioContent (shared_ptr<const Film> film)
41 , SingleStreamAudioContent (film)
43 _audio_stream.reset (new AudioStream (48000, 2));
46 std::string summary () const {
50 DCPTime full_length () const {
51 return DCPTime::from_seconds (float (audio_length()) / audio_stream()->frame_rate ());
54 Frame audio_length () const {
55 return llrint (61.2942 * audio_stream()->frame_rate ());
59 class TestAudioDecoder : public AudioDecoder
62 TestAudioDecoder (shared_ptr<TestAudioContent> content)
63 : AudioDecoder (content, false)
64 , _test_audio_content (content)
72 _test_audio_content->audio_length() - _position
75 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio_stream()->channels(), N));
76 for (int i = 0; i < _test_audio_content->audio_stream()->channels(); ++i) {
77 for (int j = 0; j < N; ++j) {
78 buffers->data(i)[j] = j + _position;
82 audio (_test_audio_content->audio_stream(), buffers, ContentTime::from_frames (_position, 48000));
88 void seek (ContentTime t, bool accurate)
90 AudioDecoder::seek (t, accurate);
91 _position = t.frames_round (_test_audio_content->resampled_audio_frame_rate ());
95 boost::shared_ptr<TestAudioContent> _test_audio_content;
99 shared_ptr<TestAudioContent> content;
100 shared_ptr<TestAudioDecoder> decoder;
103 get (Frame from, Frame length)
105 decoder->seek (ContentTime::from_frames (from, content->resampled_audio_frame_rate ()), true);
106 ContentAudio ca = decoder->get_audio (content->audio_stream(), from, length, true);
107 BOOST_CHECK_EQUAL (ca.frame, from);
112 check (Frame from, Frame length)
114 ContentAudio ca = get (from, length);
115 for (int i = 0; i < content->audio_stream()->channels(); ++i) {
116 for (int j = 0; j < length; ++j) {
117 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
122 /** Check the logic in AudioDecoder::get_audio */
123 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
125 shared_ptr<Film> film = new_test_film ("audio_decoder_test");
127 content.reset (new TestAudioContent (film));
128 decoder.reset (new TestAudioDecoder (content));
135 /* Read off the end */
137 Frame const from = content->resampled_audio_frame_rate() * 61;
138 Frame const length = content->resampled_audio_frame_rate() * 4;
139 ContentAudio ca = get (from, length);
141 for (int i = 0; i < content->audio_stream()->channels(); ++i) {
142 for (int j = 0; j < ca.audio->frames(); ++j) {
143 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);