Make ::get tell caller if there's been an underrun.
authorCarl Hetherington <cth@carlh.net>
Wed, 12 Jul 2017 16:20:07 +0000 (17:20 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 12 Jul 2017 16:20:07 +0000 (17:20 +0100)
src/lib/audio_ring_buffers.cc
src/lib/audio_ring_buffers.h
test/audio_ring_buffers_test.cc [new file with mode: 0644]

index 59f06a7c06ab378aabc75415faa55d66404c4a56..4e0b3338237fd268c322d90bbacd03ec19a3bec9 100644 (file)
@@ -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
index 8f180042eef35d6018415d7ea1acff3178048068..3c726425c9b749b6807a19da2eaf38f218d379c1 100644 (file)
@@ -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 (file)
index 0000000..23491f6
--- /dev/null
@@ -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);
+}