2 Copyright (C) 2016-2018 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>
32 using boost::shared_ptr;
33 using boost::optional;
35 AudioRingBuffers::AudioRingBuffers ()
42 AudioRingBuffers::put (shared_ptr<const AudioBuffers> data, DCPTime time)
44 boost::mutex::scoped_lock lm (_mutex);
46 if (!_buffers.empty()) {
47 DCPOMATIC_ASSERT (_buffers.front().first->channels() == data->channels());
48 DCPOMATIC_ASSERT ((_buffers.back().second + DCPTime::from_frames(_buffers.back().first->frames(), 48000)) == time);
51 _buffers.push_back(make_pair(data, time));
54 /** @return time of the returned data; if it's not set this indicates an underrun */
56 AudioRingBuffers::get (float* out, int channels, int frames)
58 boost::mutex::scoped_lock lm (_mutex);
60 optional<DCPTime> time;
63 if (_buffers.empty ()) {
64 for (int i = 0; i < frames; ++i) {
65 for (int j = 0; j < channels; ++j) {
69 cout << "audio underrun; missing " << frames << "!\n";
73 pair<shared_ptr<const AudioBuffers>, DCPTime> front = _buffers.front ();
75 time = front.second + DCPTime::from_frames(_used_in_head, 48000);
78 int const to_do = min (frames, front.first->frames() - _used_in_head);
79 float** p = front.first->data();
80 int const c = min (front.first->channels(), channels);
81 for (int i = 0; i < to_do; ++i) {
82 for (int j = 0; j < c; ++j) {
83 *out++ = p[j][i + _used_in_head];
85 for (int j = c; j < channels; ++j) {
89 _used_in_head += to_do;
92 if (_used_in_head == front.first->frames()) {
93 _buffers.pop_front ();
102 AudioRingBuffers::clear ()
104 boost::mutex::scoped_lock lm (_mutex);
110 AudioRingBuffers::size () const
112 boost::mutex::scoped_lock lm (_mutex);
114 for (list<pair<shared_ptr<const AudioBuffers>, DCPTime> >::const_iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
115 s += i->first->frames();
117 return s - _used_in_head;