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