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