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