2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /** @file test/audio_decoder_test.cc
22 * @brief Tests of the AudioDecoder class.
26 #include "lib/content.h"
27 #include "lib/audio_decoder.h"
28 #include "lib/audio_content.h"
30 #include <boost/test/unit_test.hpp>
31 #include <boost/make_shared.hpp>
38 using boost::shared_ptr;
39 using boost::make_shared;
41 class TestAudioContent : public Content
44 TestAudioContent (shared_ptr<const Film> film)
47 audio.reset (new AudioContent (this));
48 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
51 std::string summary () const {
55 DCPTime full_length () const {
56 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
59 Frame audio_length () const {
60 return llrint (61.2942 * 48000);
64 class TestAudioDecoder : public Decoder
67 TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
68 : _test_audio_content (content)
71 audio.reset (new AudioDecoder (this, content->audio, false, log));
74 bool pass (PassReason, bool)
78 _test_audio_content->audio_length() - _position
81 shared_ptr<AudioBuffers> buffers = make_shared<AudioBuffers> (_test_audio_content->audio->stream()->channels(), N);
82 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
83 for (int j = 0; j < N; ++j) {
84 buffers->data(i)[j] = j + _position;
88 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
94 void seek (ContentTime t, bool accurate)
96 audio->seek (t, accurate);
97 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
101 boost::shared_ptr<TestAudioContent> _test_audio_content;
105 shared_ptr<TestAudioContent> content;
106 shared_ptr<TestAudioDecoder> decoder;
109 get (Frame from, Frame length)
111 decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
112 ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
113 BOOST_CHECK_EQUAL (ca.frame, from);
118 check (Frame from, Frame length)
120 ContentAudio ca = get (from, length);
121 for (int i = 0; i < content->audio->stream()->channels(); ++i) {
122 for (int j = 0; j < length; ++j) {
123 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
128 /** Check the logic in AudioDecoder::get_audio */
129 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
131 shared_ptr<Film> film = new_test_film ("audio_decoder_test");
133 content.reset (new TestAudioContent (film));
134 decoder.reset (new TestAudioDecoder (content, film->log()));
142 /* Read off the end */
144 Frame const from = content->audio->resampled_frame_rate() * 61;
145 Frame const length = content->audio->resampled_frame_rate() * 4;
146 ContentAudio ca = get (from, length);
148 for (int i = 0; i < content->audio->stream()->channels(); ++i) {
149 for (int j = 0; j < ca.audio->frames(); ++j) {
150 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);