summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2017-07-12 17:20:07 +0100
committerCarl Hetherington <cth@carlh.net>2017-07-12 17:20:07 +0100
commit6a37a2a1283cc3c73cb857dba32099cf12c8768a (patch)
tree32df98dae790a747541f09bfbdc0d8a39e29d323
parent51c755b4fdea854325a09f7658ccfe43b9fa7ef0 (diff)
Make ::get tell caller if there's been an underrun.
-rw-r--r--src/lib/audio_ring_buffers.cc7
-rw-r--r--src/lib/audio_ring_buffers.h2
-rw-r--r--test/audio_ring_buffers_test.cc191
3 files changed, 197 insertions, 3 deletions
diff --git a/src/lib/audio_ring_buffers.cc b/src/lib/audio_ring_buffers.cc
index 59f06a7c0..4e0b33382 100644
--- a/src/lib/audio_ring_buffers.cc
+++ b/src/lib/audio_ring_buffers.cc
@@ -45,7 +45,8 @@ AudioRingBuffers::put (shared_ptr<const AudioBuffers> data)
_buffers.push_back (data);
}
-void
+/** @return true if there was an underrun, otherwise false */
+bool
AudioRingBuffers::get (float* out, int channels, int frames)
{
boost::mutex::scoped_lock lm (_mutex);
@@ -58,7 +59,7 @@ AudioRingBuffers::get (float* out, int channels, int frames)
}
}
cout << "audio underrun; missing " << frames << "!\n";
- return;
+ return true;
}
shared_ptr<const AudioBuffers> front = _buffers.front ();
@@ -82,6 +83,8 @@ AudioRingBuffers::get (float* out, int channels, int frames)
_used_in_head = 0;
}
}
+
+ return false;
}
void
diff --git a/src/lib/audio_ring_buffers.h b/src/lib/audio_ring_buffers.h
index 8f180042e..3c726425c 100644
--- a/src/lib/audio_ring_buffers.h
+++ b/src/lib/audio_ring_buffers.h
@@ -34,7 +34,7 @@ public:
AudioRingBuffers ();
void put (boost::shared_ptr<const AudioBuffers> data);
- void get (float* out, int channels, int frames);
+ bool get (float* out, int channels, int frames);
void clear ();
Frame size () const;
diff --git a/test/audio_ring_buffers_test.cc b/test/audio_ring_buffers_test.cc
new file mode 100644
index 000000000..23491f6b3
--- /dev/null
+++ b/test/audio_ring_buffers_test.cc
@@ -0,0 +1,191 @@
+/*
+ Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
+
+ 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.
+
+ 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "lib/audio_ring_buffers.h"
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+using std::cout;
+using boost::shared_ptr;
+
+#define CANARY 9999
+
+/** Basic tests fetching the same number of channels as went in */
+BOOST_AUTO_TEST_CASE (audio_ring_buffers_test1)
+{
+ AudioRingBuffers rb;
+
+ /* Should start off empty */
+ BOOST_CHECK_EQUAL (rb.size(), 0);
+
+ /* Getting some data should give an underrun and write zeros */
+ float buffer[256 * 6];
+ buffer[240 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
+ for (int i = 0; i < 240 * 6; ++i) {
+ BOOST_REQUIRE_EQUAL (buffer[i], 0);
+ }
+ BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
+
+ /* clear() should give the same result */
+ rb.clear ();
+ BOOST_CHECK_EQUAL (rb.size(), 0);
+ buffer[240 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
+ for (int i = 0; i < 240 * 6; ++i) {
+ BOOST_REQUIRE_EQUAL (buffer[i], 0);
+ }
+ BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
+
+ /* Put some data in */
+ shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
+ int value = 0;
+ for (int i = 0; i < 91; ++i) {
+ for (int j = 0; j < 6; ++j) {
+ data->data(j)[i] = value++;
+ }
+ }
+ rb.put (data);
+ BOOST_CHECK_EQUAL (rb.size(), 91);
+
+ /* Get part of it out */
+ buffer[40 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 40), false);
+ int check = 0;
+ for (int i = 0; i < 40 * 6; ++i) {
+ BOOST_REQUIRE_EQUAL (buffer[i], check++);
+ }
+ BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 51);
+
+ /* Get the rest */
+ buffer[51 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 51), false);
+ for (int i = 0; i < 51 * 6; ++i) {
+ BOOST_REQUIRE_EQUAL (buffer[i], check++);
+ }
+ BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 0);
+
+ /* Now there should be an underrun */
+ buffer[240 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
+ BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
+}
+
+/** Similar tests but fetching more channels than were put in */
+BOOST_AUTO_TEST_CASE (audio_ring_buffers_test2)
+{
+ AudioRingBuffers rb;
+
+ /* Put some data in */
+ shared_ptr<AudioBuffers> data (new AudioBuffers (2, 91));
+ int value = 0;
+ for (int i = 0; i < 91; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ data->data(j)[i] = value++;
+ }
+ }
+ rb.put (data);
+ BOOST_CHECK_EQUAL (rb.size(), 91);
+
+ /* Get part of it out */
+ float buffer[256 * 6];
+ buffer[40 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 40), false);
+ int check = 0;
+ for (int i = 0; i < 40; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
+ }
+ for (int j = 2; j < 6; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
+ }
+ }
+ BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 51);
+
+ /* Get the rest */
+ buffer[51 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 51), false);
+ for (int i = 0; i < 51; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
+ }
+ for (int j = 2; j < 6; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
+ }
+ }
+ BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 0);
+
+ /* Now there should be an underrun */
+ buffer[240 * 6] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
+ BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
+}
+
+/** Similar tests but fetching fewer channels than were put in */
+BOOST_AUTO_TEST_CASE (audio_ring_buffers_test3)
+{
+ AudioRingBuffers rb;
+
+ /* Put some data in */
+ shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
+ int value = 0;
+ for (int i = 0; i < 91; ++i) {
+ for (int j = 0; j < 6; ++j) {
+ data->data(j)[i] = value++;
+ }
+ }
+ rb.put (data);
+ BOOST_CHECK_EQUAL (rb.size(), 91);
+
+ /* Get part of it out */
+ float buffer[256 * 6];
+ buffer[40 * 2] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 2, 40), false);
+ int check = 0;
+ for (int i = 0; i < 40; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
+ }
+ check += 4;
+ }
+ BOOST_CHECK_EQUAL (buffer[40 * 2], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 51);
+
+ /* Get the rest */
+ buffer[51 * 2] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 2, 51), false);
+ for (int i = 0; i < 51; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
+ }
+ check += 4;
+ }
+ BOOST_CHECK_EQUAL (buffer[51 * 2], CANARY);
+ BOOST_CHECK_EQUAL (rb.size(), 0);
+
+ /* Now there should be an underrun */
+ buffer[240 * 2] = CANARY;
+ BOOST_CHECK_EQUAL (rb.get (buffer, 2, 240), true);
+ BOOST_CHECK_EQUAL (buffer[240 * 2], CANARY);
+}