Restore correct setup of fast resampler when the player is set to fast.
[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)
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 }
47
48 /** @return true if there was an underrun, otherwise false */
49 bool
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 true;
63                 }
64
65                 shared_ptr<const AudioBuffers> front = _buffers.front ();
66
67                 int const to_do = min (frames, front->frames() - _used_in_head);
68                 float** p = front->data();
69                 int const c = min (front->channels(), channels);
70                 for (int i = 0; i < to_do; ++i) {
71                         for (int j = 0; j < c; ++j) {
72                                 *out++ = p[j][i + _used_in_head];
73                         }
74                         for (int j = c; j < channels; ++j) {
75                                 *out++ = 0;
76                         }
77                 }
78                 _used_in_head += to_do;
79                 frames -= to_do;
80
81                 if (_used_in_head == front->frames()) {
82                         _buffers.pop_front ();
83                         _used_in_head = 0;
84                 }
85         }
86
87         return false;
88 }
89
90 void
91 AudioRingBuffers::clear ()
92 {
93         boost::mutex::scoped_lock lm (_mutex);
94         _buffers.clear ();
95         _used_in_head = 0;
96 }
97
98 Frame
99 AudioRingBuffers::size () const
100 {
101         boost::mutex::scoped_lock lm (_mutex);
102         Frame s = 0;
103         BOOST_FOREACH (shared_ptr<const AudioBuffers> i, _buffers) {
104                 s += i->frames ();
105         }
106         return s - _used_in_head;
107 }