Rename TYPE_DEBUG_PLAYER to TYPE_DEBUG_VIDEO_VIEW.
[dcpomatic.git] / src / lib / audio_ring_buffers.cc
1 /*
2     Copyright (C) 2016-2019 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 std::make_pair;
30 using std::pair;
31 using std::list;
32 using boost::shared_ptr;
33 using boost::optional;
34 using namespace dcpomatic;
35
36 AudioRingBuffers::AudioRingBuffers ()
37         : _used_in_head (0)
38 {
39
40 }
41
42 /** @param frame_rate Frame rate in use; this is only used to check timing consistency of the incoming data */
43 void
44 AudioRingBuffers::put (shared_ptr<const AudioBuffers> data, DCPTime time, int frame_rate)
45 {
46         boost::mutex::scoped_lock lm (_mutex);
47
48         if (!_buffers.empty()) {
49                 DCPOMATIC_ASSERT (_buffers.front().first->channels() == data->channels());
50                 DCPTime const end = (_buffers.back().second + DCPTime::from_frames(_buffers.back().first->frames(), frame_rate));
51                 if (labs(end.get() - time.get()) > 1) {
52                         cout << "bad put " << to_string(_buffers.back().second) << " " << _buffers.back().first->frames() << " " << to_string(time) << "\n";
53                 }
54                 DCPOMATIC_ASSERT (labs(end.get() - time.get()) < 2);
55         }
56
57         _buffers.push_back(make_pair(data, time));
58 }
59
60 /** @return time of the returned data; if it's not set this indicates an underrun */
61 optional<DCPTime>
62 AudioRingBuffers::get (float* out, int channels, int frames)
63 {
64         boost::mutex::scoped_lock lm (_mutex);
65
66         optional<DCPTime> time;
67
68         while (frames > 0) {
69                 if (_buffers.empty ()) {
70                         for (int i = 0; i < frames; ++i) {
71                                 for (int j = 0; j < channels; ++j) {
72                                         *out++ = 0;
73                                 }
74                         }
75                         cout << "audio underrun; missing " << frames << "!\n";
76                         return time;
77                 }
78
79                 pair<shared_ptr<const AudioBuffers>, DCPTime> front = _buffers.front ();
80                 if (!time) {
81                         time = front.second + DCPTime::from_frames(_used_in_head, 48000);
82                 }
83
84                 int const to_do = min (frames, front.first->frames() - _used_in_head);
85                 float** p = front.first->data();
86                 int const c = min (front.first->channels(), channels);
87                 for (int i = 0; i < to_do; ++i) {
88                         for (int j = 0; j < c; ++j) {
89                                 *out++ = p[j][i + _used_in_head];
90                         }
91                         for (int j = c; j < channels; ++j) {
92                                 *out++ = 0;
93                         }
94                 }
95                 _used_in_head += to_do;
96                 frames -= to_do;
97
98                 if (_used_in_head == front.first->frames()) {
99                         _buffers.pop_front ();
100                         _used_in_head = 0;
101                 }
102         }
103
104         return time;
105 }
106
107 optional<DCPTime>
108 AudioRingBuffers::peek () const
109 {
110         boost::mutex::scoped_lock lm (_mutex);
111         if (_buffers.empty()) {
112                 return optional<DCPTime>();
113         }
114         return _buffers.front().second;
115 }
116
117 void
118 AudioRingBuffers::clear ()
119 {
120         boost::mutex::scoped_lock lm (_mutex);
121         _buffers.clear ();
122         _used_in_head = 0;
123 }
124
125 Frame
126 AudioRingBuffers::size () const
127 {
128         boost::mutex::scoped_lock lm (_mutex);
129         Frame s = 0;
130         for (list<pair<shared_ptr<const AudioBuffers>, DCPTime> >::const_iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
131                 s += i->first->frames();
132         }
133         return s - _used_in_head;
134 }