X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Faudio_merger_test.cc;h=1484213e72b76d54b617355252414a86faa9d589;hb=d0a0a4839f0e1e8d37f9f9ad41784c4d18210a6b;hp=31d055ab703b2961e5d447198e7758c5cc53075e;hpb=85986ebc99aaae8507d889a027da6e3177e1348b;p=dcpomatic.git diff --git a/test/audio_merger_test.cc b/test/audio_merger_test.cc index 31d055ab7..1484213e7 100644 --- a/test/audio_merger_test.cc +++ b/test/audio_merger_test.cc @@ -1,69 +1,79 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2017 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ +/** @file test/audio_merger_test.cc + * @brief Test AudioMerger class. + * @ingroup selfcontained + */ + +#include "lib/audio_merger.h" +#include "lib/audio_buffers.h" #include #include #include #include -#include "lib/audio_merger.h" -#include "lib/audio_buffers.h" +#include +using std::pair; +using std::list; +using std::cout; using boost::shared_ptr; using boost::bind; static shared_ptr last_audio; -static int -pass_through (int x) +int const sampling_rate = 48000; + +static void +push (AudioMerger& merger, int from, int to, int at) { - return x; + shared_ptr buffers (new AudioBuffers (1, to - from)); + for (int i = 0; i < (to - from); ++i) { + buffers->data()[0][i] = from + i; + } + merger.push (buffers, DCPTime(at, sampling_rate)); } +/* Basic mixing, 2 overlapping pushes */ BOOST_AUTO_TEST_CASE (audio_merger_test1) { - AudioMerger merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1)); - - /* Push 64 samples, 0 -> 63 at time 0 */ - shared_ptr buffers (new AudioBuffers (1, 64)); - for (int i = 0; i < 64; ++i) { - buffers->data()[0][i] = i; - } - merger.push (buffers, 0); + AudioMerger merger (sampling_rate); - /* Push 64 samples, 0 -> 63 at time 22 */ - merger.push (buffers, 22); + push (merger, 0, 64, 0); + push (merger, 0, 64, 22); - TimedAudioBuffers tb = merger.pull (22); - BOOST_CHECK (tb.audio != shared_ptr ()); - BOOST_CHECK_EQUAL (tb.audio->frames(), 22); - BOOST_CHECK_EQUAL (tb.time, 0); + list, DCPTime> > tb = merger.pull (DCPTime::from_frames (22, sampling_rate)); + BOOST_REQUIRE (tb.size() == 1); + BOOST_CHECK (tb.front().first != shared_ptr ()); + BOOST_CHECK_EQUAL (tb.front().first->frames(), 22); + BOOST_CHECK_EQUAL (tb.front().second.get(), 0); /* And they should be a staircase */ for (int i = 0; i < 22; ++i) { - BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i); + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } - tb = merger.flush (); - - /* That flush should give us 64 samples at 22 */ - BOOST_CHECK_EQUAL (tb.audio->frames(), 64); - BOOST_CHECK_EQUAL (tb.time, 22); + tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate)); + BOOST_REQUIRE (tb.size() == 1); + BOOST_CHECK_EQUAL (tb.front().first->frames(), 64); + BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get()); /* Check the sample values */ for (int i = 0; i < 64; ++i) { @@ -71,37 +81,56 @@ BOOST_AUTO_TEST_CASE (audio_merger_test1) if (i < (64 - 22)) { correct += i + 22; } - BOOST_CHECK_EQUAL (tb.audio->data()[0][i], correct); + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct); } } +/* Push at non-zero time */ BOOST_AUTO_TEST_CASE (audio_merger_test2) { - AudioMerger merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1)); + AudioMerger merger (sampling_rate); + + push (merger, 0, 64, 9); - /* Push 64 samples, 0 -> 63 at time 9 */ - shared_ptr buffers (new AudioBuffers (1, 64)); + /* There's nothing from 0 to 9 */ + list, DCPTime> > tb = merger.pull (DCPTime::from_frames (9, sampling_rate)); + BOOST_CHECK_EQUAL (tb.size(), 0); + + /* Then there's our data at 9 */ + tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate)); + + BOOST_CHECK_EQUAL (tb.front().first->frames(), 64); + BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get()); + + /* Check the sample values */ for (int i = 0; i < 64; ++i) { - buffers->data()[0][i] = i; + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } - merger.push (buffers, 9); - - TimedAudioBuffers tb = merger.pull (9); - BOOST_CHECK_EQUAL (tb.audio->frames(), 9); - BOOST_CHECK_EQUAL (tb.time, 0); - - for (int i = 0; i < 9; ++i) { - BOOST_CHECK_EQUAL (tb.audio->data()[0][i], 0); +} + +/* Push two non contiguous blocks */ +BOOST_AUTO_TEST_CASE (audio_merger_test3) +{ + AudioMerger merger (sampling_rate); + + push (merger, 0, 64, 17); + push (merger, 0, 64, 114); + + /* Get them back */ + + list, DCPTime> > tb = merger.pull (DCPTime::from_frames (100, sampling_rate)); + BOOST_REQUIRE (tb.size() == 1); + BOOST_CHECK_EQUAL (tb.front().first->frames(), 64); + BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get()); + for (int i = 0; i < 64; ++i) { + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } - - tb = merger.flush (); - /* That flush should give us 64 samples at 9 */ - BOOST_CHECK_EQUAL (tb.audio->frames(), 64); - BOOST_CHECK_EQUAL (tb.time, 9); - - /* Check the sample values */ + tb = merger.pull (DCPTime::from_frames (200, sampling_rate)); + BOOST_REQUIRE (tb.size() == 1); + BOOST_CHECK_EQUAL (tb.front().first->frames(), 64); + BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get()); for (int i = 0; i < 64; ++i) { - BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i); + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } }