2 Copyright (C) 2016-2017 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 #include "audio_ring_buffers.h"
22 #include "dcpomatic_assert.h"
23 #include "exceptions.h"
24 #include <boost/foreach.hpp>
28 using boost::shared_ptr;
30 AudioRingBuffers::AudioRingBuffers ()
37 AudioRingBuffers::put (shared_ptr<const AudioBuffers> data)
39 boost::mutex::scoped_lock lm (_mutex);
41 if (!_buffers.empty ()) {
42 DCPOMATIC_ASSERT (_buffers.front()->channels() == data->channels());
45 _buffers.push_back (data);
49 AudioRingBuffers::get (float* out, int channels, int frames)
51 boost::mutex::scoped_lock lm (_mutex);
54 if (_buffers.empty ()) {
55 for (int i = 0; i < frames; ++i) {
56 for (int j = 0; j < channels; ++j) {
60 cout << "audio underrun; missing " << frames << "!\n";
64 shared_ptr<const AudioBuffers> front = _buffers.front ();
66 int const to_do = min (frames, front->frames() - _used_in_head);
67 float** p = front->data();
68 int const c = min (front->channels(), channels);
69 for (int i = 0; i < to_do; ++i) {
70 for (int j = 0; j < c; ++j) {
71 *out++ = p[j][i + _used_in_head];
73 for (int j = c; j < channels; ++j) {
77 _used_in_head += to_do;
80 if (_used_in_head == front->frames()) {
81 _buffers.pop_front ();
88 AudioRingBuffers::clear ()
90 boost::mutex::scoped_lock lm (_mutex);
96 AudioRingBuffers::size () const
98 boost::mutex::scoped_lock lm (_mutex);
100 BOOST_FOREACH (shared_ptr<const AudioBuffers> i, _buffers) {
103 return s - _used_in_head;