X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Faudio_merger_test.cc;h=9292584ab7a80b098f71b4ffad51cff2cbdf4dda;hb=fd225e3afb0bcf3e7bfce1018dbc0a4c4bd275f2;hp=a03d3b30fab575ec0a161ceb1e78bcb8101abfcb;hpb=ee70e6c7c5c6a14605f3b7b17c01e2f41f0f10dd;p=dcpomatic.git diff --git a/test/audio_merger_test.cc b/test/audio_merger_test.cc index a03d3b30f..9292584ab 100644 --- a/test/audio_merger_test.cc +++ b/test/audio_merger_test.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2017 Carl Hetherington + Copyright (C) 2013-2020 Carl Hetherington This file is part of DCP-o-matic. @@ -18,14 +18,27 @@ */ +/** @file test/audio_merger_test.cc + * @brief Test AudioMerger class. + * @ingroup selfcontained + */ + +#include "lib/cross.h" #include "lib/audio_merger.h" #include "lib/audio_buffers.h" +#include "lib/dcpomatic_time.h" +#include "test.h" +#include #include #include #include #include +#include using std::pair; +using std::list; +using std::cout; +using std::string; using boost::shared_ptr; using boost::bind; @@ -33,34 +46,39 @@ static shared_ptr last_audio; int const sampling_rate = 48000; -BOOST_AUTO_TEST_CASE (audio_merger_test1) +static void +push (AudioMerger& merger, int from, int to, int at) { - AudioMerger merger (1, sampling_rate); - - /* 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; + 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()); + merger.push (buffers, DCPTime(at, sampling_rate)); +} + +/* Basic mixing, 2 overlapping pushes */ +BOOST_AUTO_TEST_CASE (audio_merger_test1) +{ + AudioMerger merger (sampling_rate); - /* Push 64 samples, 0 -> 63 at time 22 */ - merger.push (buffers, DCPTime::from_frames (22, sampling_rate)); + push (merger, 0, 64, 0); + push (merger, 0, 64, 22); - pair, DCPTime> tb = merger.pull (DCPTime::from_frames (22, sampling_rate)); - BOOST_CHECK (tb.first != shared_ptr ()); - BOOST_CHECK_EQUAL (tb.first->frames(), 22); - BOOST_CHECK_EQUAL (tb.second.get(), 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.first->data()[0][i], i); + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate)); - - BOOST_CHECK_EQUAL (tb.first->frames(), 64); - BOOST_CHECK_EQUAL (tb.second.get(), DCPTime::from_frames(22, sampling_rate).get()); + 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) { @@ -68,36 +86,95 @@ BOOST_AUTO_TEST_CASE (audio_merger_test1) if (i < (64 - 22)) { correct += i + 22; } - BOOST_CHECK_EQUAL (tb.first->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, sampling_rate); + AudioMerger merger (sampling_rate); + + push (merger, 0, 64, 9); + + /* 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()); - /* Push 64 samples, 0 -> 63 at time 9 */ - shared_ptr buffers (new AudioBuffers (1, 64)); + /* 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, DCPTime::from_frames (9, sampling_rate)); +} - pair, DCPTime> tb = merger.pull (DCPTime::from_frames (9, sampling_rate)); - BOOST_CHECK_EQUAL (tb.first->frames(), 9); - BOOST_CHECK_EQUAL (tb.second.get(), 0); +/* Push two non contiguous blocks */ +BOOST_AUTO_TEST_CASE (audio_merger_test3) +{ + AudioMerger merger (sampling_rate); - for (int i = 0; i < 9; ++i) { - BOOST_CHECK_EQUAL (tb.first->data()[0][i], 0); - } + push (merger, 0, 64, 17); + push (merger, 0, 64, 114); - tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate)); + /* Get them back */ - BOOST_CHECK_EQUAL (tb.first->frames(), 64); - BOOST_CHECK_EQUAL (tb.second.get(), DCPTime::from_frames(9, sampling_rate).get()); + 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); + } - /* 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.first->data()[0][i], i); + BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i); } } + +/* Reply a sequence of calls to AudioMerger that resulted in a crash */ +BOOST_AUTO_TEST_CASE (audio_merger_test4) +{ + FILE* f = fopen_boost("test/data/audio_merger_bug1.log", "r"); + BOOST_REQUIRE (f); + list tokens; + char buf[64]; + while (fscanf(f, "%63s", buf) == 1) { + tokens.push_back (buf); + } + + shared_ptr merger; + list::const_iterator i = tokens.begin (); + while (i != tokens.end()) { + BOOST_CHECK (*i++ == "I/AM"); + string const cmd = *i++; + if (cmd == "frame_rate") { + BOOST_REQUIRE (i != tokens.end()); + merger.reset (new AudioMerger(dcp::raw_convert(*i++))); + } else if (cmd == "clear") { + merger->clear (); + } else if (cmd == "push") { + BOOST_REQUIRE (i != tokens.end()); + DCPTime time(dcp::raw_convert(*i++)); + BOOST_REQUIRE (i != tokens.end()); + int const frames = dcp::raw_convert(*i++); + shared_ptr buffers(new AudioBuffers(1, frames)); + BOOST_REQUIRE (merger); + merger->push (buffers, time); + } else if (cmd == "pull") { + BOOST_REQUIRE (i != tokens.end()); + DCPTime time(dcp::raw_convert(*i++)); + merger->pull (time); + } + } +} + +