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