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"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ratio.h"
33 #include <boost/test/unit_test.hpp>
40 using boost::shared_ptr;
42 class TestAudioContent : public Content
45 TestAudioContent (shared_ptr<const Film> film)
48 audio.reset (new AudioContent (this));
49 audio->set_stream (AudioStreamPtr (new AudioStream (48000, audio_length(), 2)));
52 std::string summary () const {
56 DCPTime full_length () const {
57 return DCPTime::from_seconds (float (audio_length()) / audio->stream()->frame_rate ());
60 Frame audio_length () const {
61 return llrint (61.2942 * 48000);
65 class TestAudioDecoder : public Decoder
68 TestAudioDecoder (shared_ptr<TestAudioContent> content, shared_ptr<Log> log)
69 : _test_audio_content (content)
72 audio.reset (new AudioDecoder (this, content->audio, log));
75 bool pass (PassReason, bool)
79 _test_audio_content->audio_length() - _position
82 shared_ptr<AudioBuffers> buffers (new AudioBuffers (_test_audio_content->audio->stream()->channels(), N));
83 for (int i = 0; i < _test_audio_content->audio->stream()->channels(); ++i) {
84 for (int j = 0; j < N; ++j) {
85 buffers->data(i)[j] = j + _position;
89 audio->give (_test_audio_content->audio->stream(), buffers, ContentTime::from_frames (_position, 48000));
95 void seek (ContentTime t, bool accurate)
97 audio->seek (t, accurate);
98 _position = t.frames_round (_test_audio_content->audio->resampled_frame_rate ());
102 boost::shared_ptr<TestAudioContent> _test_audio_content;
106 shared_ptr<TestAudioContent> content;
107 shared_ptr<TestAudioDecoder> decoder;
110 get (Frame from, Frame length)
112 decoder->seek (ContentTime::from_frames (from, content->audio->resampled_frame_rate ()), true);
113 ContentAudio ca = decoder->audio->get (content->audio->stream(), from, length, true);
114 BOOST_CHECK_EQUAL (ca.frame, from);
119 check (Frame from, Frame length)
121 ContentAudio ca = get (from, length);
122 for (int i = 0; i < content->audio->stream()->channels(); ++i) {
123 for (int j = 0; j < length; ++j) {
124 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
129 /** Check the logic in AudioDecoder::get_audio */
130 BOOST_AUTO_TEST_CASE (audio_decoder_get_audio_test)
132 shared_ptr<Film> film = new_test_film ("audio_decoder_test");
134 content.reset (new TestAudioContent (film));
135 decoder.reset (new TestAudioDecoder (content, film->log()));
143 /* Read off the end */
145 Frame const from = content->audio->resampled_frame_rate() * 61;
146 Frame const length = content->audio->resampled_frame_rate() * 4;
147 ContentAudio ca = get (from, length);
149 for (int i = 0; i < content->audio->stream()->channels(); ++i) {
150 for (int j = 0; j < ca.audio->frames(); ++j) {
151 BOOST_REQUIRE_EQUAL (ca.audio->data(i)[j], j + from);
156 BOOST_AUTO_TEST_CASE (audio_decoder_test)
158 shared_ptr<Film> film = new_test_film ("analyse_audio_test");
159 film->set_container (Ratio::from_id ("185"));
160 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
161 film->set_name ("frobozz");
162 shared_ptr<Content> content = content_factory(film, private_data / "20 The Wedding Convoy Song.m4a").front();
163 film->examine_and_add_content (content);
166 content->set_trim_start (ContentTime::from_seconds (60));
168 BOOST_CHECK (!wait_for_jobs ());