2 Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "audio_buffers.h"
22 #include "dcpomatic_assert.h"
29 using boost::shared_ptr;
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.
35 AudioBuffers::AudioBuffers (int channels, int32_t frames)
37 allocate (channels, frames);
41 * @param other Other AudioBuffers; data is copied.
43 AudioBuffers::AudioBuffers (AudioBuffers const & other)
45 allocate (other._channels, other._frames);
46 copy_from (&other, other._frames, 0, 0);
49 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
51 allocate (other->_channels, other->_frames);
52 copy_from (other.get(), other->_frames, 0, 0);
56 AudioBuffers::operator= (AudioBuffers const & other)
63 allocate (other._channels, other._frames);
64 copy_from (&other, other._frames, 0, 0);
69 /** AudioBuffers destructor */
70 AudioBuffers::~AudioBuffers ()
76 AudioBuffers::allocate (int channels, int32_t frames)
78 DCPOMATIC_ASSERT (frames >= 0);
79 DCPOMATIC_ASSERT (channels >= 0);
83 _allocated_frames = frames;
85 _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
90 for (int i = 0; i < _channels; ++i) {
91 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
99 AudioBuffers::deallocate ()
101 for (int i = 0; i < _channels; ++i) {
108 /** @param c Channel index.
109 * @return Buffer for this channel.
112 AudioBuffers::data (int c) const
114 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
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
121 * @param f Frames; must be less than or equal to the number of allocated frames.
124 AudioBuffers::set_frames (int32_t f)
126 DCPOMATIC_ASSERT (f <= _allocated_frames);
128 for (int c = 0; c < _channels; ++c) {
129 for (int i = f; i < _frames; ++i) {
137 /** Make all frames silent */
139 AudioBuffers::make_silent ()
141 for (int i = 0; i < _channels; ++i) {
146 /** Make all samples on a given channel silent.
150 AudioBuffers::make_silent (int c)
152 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
154 for (int i = 0; i < _frames; ++i) {
159 /** Make some frames.
160 * @param from Start frame.
161 * @param frames Number of frames to silence.
164 AudioBuffers::make_silent (int32_t from, int32_t frames)
166 DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
168 for (int c = 0; c < _channels; ++c) {
169 for (int i = from; i < (from + frames); ++i) {
175 /** Copy data from another AudioBuffers to this one. All channels are copied.
176 * @param from AudioBuffers to copy from; must have the same number of channels as this.
177 * @param frames_to_copy Number of frames to copy.
178 * @param read_offset Offset to read from in `from'.
179 * @param write_offset Offset to write to in `to'.
182 AudioBuffers::copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset)
184 if (frames_to_copy == 0) {
185 /* Prevent the asserts from firing if there is nothing to do */
189 DCPOMATIC_ASSERT (from->channels() == channels());
191 DCPOMATIC_ASSERT (from);
192 DCPOMATIC_ASSERT (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
193 DCPOMATIC_ASSERT (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
195 for (int i = 0; i < _channels; ++i) {
196 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
200 /** Move audio data around.
201 * @param from Offset to move from.
202 * @param to Offset to move to.
203 * @param frames Number of frames to move.
206 AudioBuffers::move (int32_t frames, int32_t from, int32_t to)
212 DCPOMATIC_ASSERT (from >= 0);
213 DCPOMATIC_ASSERT (from < _frames);
214 DCPOMATIC_ASSERT (to >= 0);
215 DCPOMATIC_ASSERT (to < _frames);
216 DCPOMATIC_ASSERT (frames > 0);
217 DCPOMATIC_ASSERT (frames <= _frames);
218 DCPOMATIC_ASSERT ((from + frames) <= _frames);
219 DCPOMATIC_ASSERT ((to + frames) <= _allocated_frames);
221 for (int i = 0; i < _channels; ++i) {
222 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
226 /** Add data from from `from', `from_channel' to our channel `to_channel'.
227 * @param gain Linear gain to apply to the data before it is added.
230 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
232 int const N = frames ();
233 DCPOMATIC_ASSERT (from->frames() == N);
234 DCPOMATIC_ASSERT (to_channel <= _channels);
236 float* s = from->data (from_channel);
237 float* d = _data[to_channel];
239 for (int i = 0; i < N; ++i) {
240 *d++ += (*s++) * gain;
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.
248 AudioBuffers::ensure_size (int32_t frames)
250 if (_allocated_frames >= frames) {
254 /* Round up frames to the next power of 2 to reduce the number
255 of realloc()s that are necessary.
258 frames |= frames >> 1;
259 frames |= frames >> 2;
260 frames |= frames >> 4;
261 frames |= frames >> 8;
262 frames |= frames >> 16;
265 for (int i = 0; i < _channels; ++i) {
266 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
270 for (int j = _allocated_frames; j < frames; ++j) {
275 _allocated_frames = frames;
278 /** Mix some other buffers with these ones. The AudioBuffers must have the same number of channels.
279 * @param from Audio buffers to get data from.
280 * @param frames Number of frames to mix.
281 * @param read_offset Offset within `from' to read from.
282 * @param write_offset Offset within this to mix into.
285 AudioBuffers::accumulate_frames (AudioBuffers const * from, int32_t frames, int32_t read_offset, int32_t write_offset)
287 DCPOMATIC_ASSERT (_channels == from->channels ());
288 DCPOMATIC_ASSERT (read_offset >= 0);
289 DCPOMATIC_ASSERT (write_offset >= 0);
291 float** from_data = from->data ();
292 for (int i = 0; i < _channels; ++i) {
293 for (int j = 0; j < frames; ++j) {
294 _data[i][j + write_offset] += from_data[i][j + read_offset];
299 /** @param dB gain in dB */
301 AudioBuffers::apply_gain (float dB)
303 float const linear = pow (10, dB / 20);
305 for (int i = 0; i < _channels; ++i) {
306 for (int j = 0; j < _frames; ++j) {
307 _data[i][j] *= linear;
312 /** @param c Channel index.
313 * @return AudioBuffers object containing only channel `c' from this AudioBuffers.
315 shared_ptr<AudioBuffers>
316 AudioBuffers::channel (int c) const
318 shared_ptr<AudioBuffers> o (new AudioBuffers (1, frames ()));
319 o->copy_channel_from (this, c, 0);
323 /** Copy all the samples from a channel on another AudioBuffers to a channel on this one.
324 * @param from AudioBuffers to copy from.
325 * @param from_channel Channel index in `from' to copy from.
326 * @param to_channel Channel index in this to copy into, overwriting what's already there.
329 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
331 DCPOMATIC_ASSERT (from->frames() == frames());
332 memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
335 /** Make a copy of these AudioBuffers */
336 shared_ptr<AudioBuffers>
337 AudioBuffers::clone () const
339 shared_ptr<AudioBuffers> b (new AudioBuffers (channels (), frames ()));
340 b->copy_from (this, frames (), 0, 0);
344 /** Extend these buffers with the data from another. The AudioBuffers must have the same number of channels. */
346 AudioBuffers::append (shared_ptr<const AudioBuffers> other)
348 DCPOMATIC_ASSERT (channels() == other->channels());
349 ensure_size (_frames + other->frames());
350 copy_from (other.get(), other->frames(), 0, _frames);
351 _frames += other->frames();
354 /** Remove some frames from the start of these AudioBuffers */
356 AudioBuffers::trim_start (int32_t frames)
358 DCPOMATIC_ASSERT (frames <= _frames);
359 move (_frames - frames, frames, 0);
360 set_frames (_frames - frames);