Assume .jpf is an image (JPEG2000).
[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 #include <iostream>
26
27 using std::min;
28 using std::cout;
29 using boost::shared_ptr;
30
31 AudioRingBuffers::AudioRingBuffers ()
32         : _used_in_head (0)
33 {
34
35 }
36
37 void
38 AudioRingBuffers::put (shared_ptr<const AudioBuffers> data)
39 {
40         boost::mutex::scoped_lock lm (_mutex);
41
42         if (!_buffers.empty ()) {
43                 DCPOMATIC_ASSERT (_buffers.front()->channels() == data->channels());
44         }
45
46         _buffers.push_back (data);
47 }
48
49 /** @return true if there was an underrun, otherwise false */
50 bool
51 AudioRingBuffers::get (float* out, int channels, int frames)
52 {
53         boost::mutex::scoped_lock lm (_mutex);
54
55         while (frames > 0) {
56                 if (_buffers.empty ()) {
57                         for (int i = 0; i < frames; ++i) {
58                                 for (int j = 0; j < channels; ++j) {
59                                         *out++ = 0;
60                                 }
61                         }
62                         cout << "audio underrun; missing " << frames << "!\n";
63                         return true;
64                 }
65
66                 shared_ptr<const AudioBuffers> front = _buffers.front ();
67
68                 int const to_do = min (frames, front->frames() - _used_in_head);
69                 float** p = front->data();
70                 int const c = min (front->channels(), channels);
71                 for (int i = 0; i < to_do; ++i) {
72                         for (int j = 0; j < c; ++j) {
73                                 *out++ = p[j][i + _used_in_head];
74                         }
75                         for (int j = c; j < channels; ++j) {
76                                 *out++ = 0;
77                         }
78                 }
79                 _used_in_head += to_do;
80                 frames -= to_do;
81
82                 if (_used_in_head == front->frames()) {
83                         _buffers.pop_front ();
84                         _used_in_head = 0;
85                 }
86         }
87
88         return false;
89 }
90
91 void
92 AudioRingBuffers::clear ()
93 {
94         boost::mutex::scoped_lock lm (_mutex);
95         _buffers.clear ();
96         _used_in_head = 0;
97 }
98
99 Frame
100 AudioRingBuffers::size () const
101 {
102         boost::mutex::scoped_lock lm (_mutex);
103         Frame s = 0;
104         BOOST_FOREACH (shared_ptr<const AudioBuffers> i, _buffers) {
105                 s += i->frames ();
106         }
107         return s - _used_in_head;
108 }