Initial butler work.
[dcpomatic.git] / src / lib / audio_ring_buffers.cc
1 /*
2     Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "audio_ring_buffers.h"
22 #include "dcpomatic_assert.h"
23 #include "exceptions.h"
24 #include <boost/foreach.hpp>
25
26 using std::min;
27 using std::cout;
28 using boost::shared_ptr;
29
30 AudioRingBuffers::AudioRingBuffers ()
31         : _used_in_head (0)
32 {
33
34 }
35
36 void
37 AudioRingBuffers::put (shared_ptr<const AudioBuffers> data, DCPTime time)
38 {
39         boost::mutex::scoped_lock lm (_mutex);
40
41         if (!_buffers.empty ()) {
42                 DCPOMATIC_ASSERT (_buffers.front()->channels() == data->channels());
43         }
44
45         _buffers.push_back (data);
46         _latest = time;
47 }
48
49 void
50 AudioRingBuffers::get (float* out, int channels, int frames)
51 {
52         boost::mutex::scoped_lock lm (_mutex);
53
54         while (frames > 0) {
55                 if (_buffers.empty ()) {
56                         for (int i = 0; i < frames; ++i) {
57                                 for (int j = 0; j < channels; ++j) {
58                                         *out++ = 0;
59                                 }
60                         }
61                         cout << "audio underrun; missing " << frames << "!\n";
62                         return;
63                 }
64
65                 int const to_do = min (frames, _buffers.front()->frames() - _used_in_head);
66                 float** p = _buffers.front()->data();
67                 int const c = min (_buffers.front()->channels(), channels);
68                 for (int i = 0; i < to_do; ++i) {
69                         for (int j = 0; j < c; ++j) {
70                                 *out++ = p[j][i + _used_in_head];
71                         }
72                         for (int j = c; j < channels; ++j) {
73                                 *out++ = 0;
74                         }
75                 }
76                 _used_in_head += to_do;
77                 frames -= to_do;
78
79                 if (_used_in_head == _buffers.front()->frames()) {
80                         _buffers.pop_front ();
81                         _used_in_head = 0;
82                 }
83         }
84 }
85
86 void
87 AudioRingBuffers::clear ()
88 {
89         boost::mutex::scoped_lock lm (_mutex);
90         _buffers.clear ();
91         _used_in_head = 0;
92         _latest = DCPTime ();
93 }
94
95 Frame
96 AudioRingBuffers::size ()
97 {
98         boost::mutex::scoped_lock lm (_mutex);
99         Frame s = 0;
100         BOOST_FOREACH (shared_ptr<const AudioBuffers> i, _buffers) {
101                 s += i->frames ();
102         }
103         return s - _used_in_head;
104 }