cceb1267213450ed8fc6cc3c4dbc3d8c913c032c
[dcpomatic.git] / src / lib / audio_buffers.cc
1 /*
2     Copyright (C) 2012-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_buffers.h"
22 #include "dcpomatic_assert.h"
23 #include <cassert>
24 #include <cstring>
25 #include <cmath>
26 #include <stdexcept>
27
28 using std::bad_alloc;
29 using boost::shared_ptr;
30
31 /** Construct an AudioBuffers.  Audio data is undefined after this constructor.
32  *  @param channels Number of channels.
33  *  @param frames Number of frames to reserve space for.
34  */
35 AudioBuffers::AudioBuffers (int channels, int32_t frames)
36 {
37         allocate (channels, frames);
38 }
39
40 /** Copy constructor.
41  *  @param other Other AudioBuffers; data is copied.
42  */
43 AudioBuffers::AudioBuffers (AudioBuffers const & other)
44 {
45         allocate (other._channels, other._frames);
46         copy_from (&other, other._frames, 0, 0);
47 }
48
49 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
50 {
51         allocate (other->_channels, other->_frames);
52         copy_from (other.get(), other->_frames, 0, 0);
53 }
54
55 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset)
56 {
57         allocate (other->_channels, frames_to_copy);
58         copy_from (other.get(), frames_to_copy, read_offset, 0);
59 }
60
61 AudioBuffers &
62 AudioBuffers::operator= (AudioBuffers const & other)
63 {
64         if (this == &other) {
65                 return *this;
66         }
67
68         deallocate ();
69         allocate (other._channels, other._frames);
70         copy_from (&other, other._frames, 0, 0);
71
72         return *this;
73 }
74
75 /** AudioBuffers destructor */
76 AudioBuffers::~AudioBuffers ()
77 {
78         deallocate ();
79 }
80
81 void
82 AudioBuffers::allocate (int channels, int32_t frames)
83 {
84         DCPOMATIC_ASSERT (frames >= 0);
85         DCPOMATIC_ASSERT (channels >= 0);
86
87         _channels = channels;
88         _frames = frames;
89         _allocated_frames = frames;
90
91         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
92         if (!_data) {
93                 throw bad_alloc ();
94         }
95
96         for (int i = 0; i < _channels; ++i) {
97                 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
98                 if (!_data[i]) {
99                         throw bad_alloc ();
100                 }
101         }
102 }
103
104 void
105 AudioBuffers::deallocate ()
106 {
107         for (int i = 0; i < _channels; ++i) {
108                 free (_data[i]);
109         }
110
111         free (_data);
112 }
113
114 /** @param c Channel index.
115  *  @return Buffer for this channel.
116  */
117 float*
118 AudioBuffers::data (int c) const
119 {
120         DCPOMATIC_ASSERT (c >= 0 && c < _channels);
121         return _data[c];
122 }
123
124 /** Set the number of frames that these AudioBuffers will report themselves
125  *  as having.  If we reduce the number of frames, the `lost' frames will
126  *  be silenced.
127  *  @param f Frames; must be less than or equal to the number of allocated frames.
128  */
129 void
130 AudioBuffers::set_frames (int32_t f)
131 {
132         DCPOMATIC_ASSERT (f <= _allocated_frames);
133
134         if (f < _frames) {
135                 make_silent (f, _frames - f);
136         }
137         _frames = f;
138 }
139
140 /** Make all frames silent */
141 void
142 AudioBuffers::make_silent ()
143 {
144         for (int i = 0; i < _channels; ++i) {
145                 make_silent (i);
146         }
147 }
148
149 /** Make all samples on a given channel silent.
150  *  @param c Channel.
151  */
152 void
153 AudioBuffers::make_silent (int c)
154 {
155         DCPOMATIC_ASSERT (c >= 0 && c < _channels);
156
157         /* This isn't really allowed, as all-bits-0 is not guaranteed to mean a 0 float,
158            but it seems that we can get away with it.
159         */
160         memset (_data[c], 0, _frames * sizeof(float));
161 }
162
163 /** Make some frames.
164  *  @param from Start frame.
165  *  @param frames Number of frames to silence.
166  */
167 void
168 AudioBuffers::make_silent (int32_t from, int32_t frames)
169 {
170         DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
171
172         for (int c = 0; c < _channels; ++c) {
173                 /* This isn't really allowed, as all-bits-0 is not guaranteed to mean a 0 float,
174                    but it seems that we can get away with it.
175                 */
176                 memset (_data[c] + from, 0, frames * sizeof(float));
177         }
178 }
179
180 /** Copy data from another AudioBuffers to this one.  All channels are copied.
181  *  @param from AudioBuffers to copy from; must have the same number of channels as this.
182  *  @param frames_to_copy Number of frames to copy.
183  *  @param read_offset Offset to read from in `from'.
184  *  @param write_offset Offset to write to in `to'.
185  */
186 void
187 AudioBuffers::copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset)
188 {
189         if (frames_to_copy == 0) {
190                 /* Prevent the asserts from firing if there is nothing to do */
191                 return;
192         }
193
194         DCPOMATIC_ASSERT (from->channels() == channels());
195
196         DCPOMATIC_ASSERT (from);
197         DCPOMATIC_ASSERT (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
198         DCPOMATIC_ASSERT (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
199
200         for (int i = 0; i < _channels; ++i) {
201                 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
202         }
203 }
204
205 /** Move audio data around.
206  *  @param from Offset to move from.
207  *  @param to Offset to move to.
208  *  @param frames Number of frames to move.
209  */
210 void
211 AudioBuffers::move (int32_t frames, int32_t from, int32_t to)
212 {
213         if (frames == 0) {
214                 return;
215         }
216
217         DCPOMATIC_ASSERT (from >= 0);
218         DCPOMATIC_ASSERT (from < _frames);
219         DCPOMATIC_ASSERT (to >= 0);
220         DCPOMATIC_ASSERT (to < _frames);
221         DCPOMATIC_ASSERT (frames > 0);
222         DCPOMATIC_ASSERT (frames <= _frames);
223         DCPOMATIC_ASSERT ((from + frames) <= _frames);
224         DCPOMATIC_ASSERT ((to + frames) <= _allocated_frames);
225
226         for (int i = 0; i < _channels; ++i) {
227                 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
228         }
229 }
230
231 /** Add data from from `from', `from_channel' to our channel `to_channel'.
232  *  @param from Buffers to copy data from.
233  *  @param from_channel Channel index to read in \p from.
234  *  @param to_channel Channel index to accumulate into.
235  *  @param gain Linear gain to apply to the data before it is added.
236  */
237 void
238 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
239 {
240         int const N = frames ();
241         DCPOMATIC_ASSERT (from->frames() == N);
242         DCPOMATIC_ASSERT (to_channel <= _channels);
243
244         float* s = from->data (from_channel);
245         float* d = _data[to_channel];
246
247         for (int i = 0; i < N; ++i) {
248                 *d++ += (*s++) * gain;
249         }
250 }
251
252 /** Ensure we have space for at least a certain number of frames.  If we extend
253  *  the buffers, fill the new space with silence.
254  */
255 void
256 AudioBuffers::ensure_size (int32_t frames)
257 {
258         if (_allocated_frames >= frames) {
259                 return;
260         }
261
262         /* Round up frames to the next power of 2 to reduce the number
263            of realloc()s that are necessary.
264         */
265         frames--;
266         frames |= frames >> 1;
267         frames |= frames >> 2;
268         frames |= frames >> 4;
269         frames |= frames >> 8;
270         frames |= frames >> 16;
271         frames++;
272
273         for (int i = 0; i < _channels; ++i) {
274                 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
275                 if (!_data[i]) {
276                         throw bad_alloc ();
277                 }
278         }
279
280         int32_t const old_allocated = _allocated_frames;
281         _allocated_frames = frames;
282         if (old_allocated < _allocated_frames) {
283                 make_silent (old_allocated, _allocated_frames - old_allocated);
284         }
285 }
286
287 /** Mix some other buffers with these ones.  The AudioBuffers must have the same number of channels.
288  *  @param from Audio buffers to get data from.
289  *  @param frames Number of frames to mix.
290  *  @param read_offset Offset within `from' to read from.
291  *  @param write_offset Offset within this to mix into.
292  */
293 void
294 AudioBuffers::accumulate_frames (AudioBuffers const * from, int32_t frames, int32_t read_offset, int32_t write_offset)
295 {
296         DCPOMATIC_ASSERT (_channels == from->channels ());
297         DCPOMATIC_ASSERT (read_offset >= 0);
298         DCPOMATIC_ASSERT (write_offset >= 0);
299
300         float** from_data = from->data ();
301         for (int i = 0; i < _channels; ++i) {
302                 for (int j = 0; j < frames; ++j) {
303                         _data[i][j + write_offset] += from_data[i][j + read_offset];
304                 }
305         }
306 }
307
308 /** @param dB gain in dB */
309 void
310 AudioBuffers::apply_gain (float dB)
311 {
312         float const linear = pow (10, dB / 20);
313
314         for (int i = 0; i < _channels; ++i) {
315                 for (int j = 0; j < _frames; ++j) {
316                         _data[i][j] *= linear;
317                 }
318         }
319 }
320
321 /** @param c Channel index.
322  *  @return AudioBuffers object containing only channel `c' from this AudioBuffers.
323  */
324 shared_ptr<AudioBuffers>
325 AudioBuffers::channel (int c) const
326 {
327         shared_ptr<AudioBuffers> o (new AudioBuffers (1, frames ()));
328         o->copy_channel_from (this, c, 0);
329         return o;
330 }
331
332 /** Copy all the samples from a channel on another AudioBuffers to a channel on this one.
333  *  @param from AudioBuffers to copy from.
334  *  @param from_channel Channel index in `from' to copy from.
335  *  @param to_channel Channel index in this to copy into, overwriting what's already there.
336  */
337 void
338 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
339 {
340         DCPOMATIC_ASSERT (from->frames() == frames());
341         memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
342 }
343
344 /** Make a copy of these AudioBuffers */
345 shared_ptr<AudioBuffers>
346 AudioBuffers::clone () const
347 {
348         shared_ptr<AudioBuffers> b (new AudioBuffers (channels (), frames ()));
349         b->copy_from (this, frames (), 0, 0);
350         return b;
351 }
352
353 /** Extend these buffers with the data from another.  The AudioBuffers must have the same number of channels. */
354 void
355 AudioBuffers::append (shared_ptr<const AudioBuffers> other)
356 {
357         DCPOMATIC_ASSERT (channels() == other->channels());
358         ensure_size (_frames + other->frames());
359         copy_from (other.get(), other->frames(), 0, _frames);
360         _frames += other->frames();
361 }
362
363 /** Remove some frames from the start of these AudioBuffers */
364 void
365 AudioBuffers::trim_start (int32_t frames)
366 {
367         DCPOMATIC_ASSERT (frames <= _frames);
368         move (_frames - frames, frames, 0);
369         set_frames (_frames - frames);
370 }