2 Copyright (C) 2012-2021 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/>.
22 #include "audio_buffers.h"
23 #include "dcpomatic_assert.h"
24 #include "maths_util.h"
32 using std::shared_ptr;
33 using std::make_shared;
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.
40 AudioBuffers::AudioBuffers (int channels, int32_t frames)
42 allocate (channels, frames);
47 * @param other Other AudioBuffers; data is copied.
49 AudioBuffers::AudioBuffers (AudioBuffers const & other)
51 allocate (other._channels, other._frames);
52 copy_from (&other, other._frames, 0, 0);
56 AudioBuffers::AudioBuffers (std::shared_ptr<const AudioBuffers> other)
58 allocate (other->_channels, other->_frames);
59 copy_from (other.get(), other->_frames, 0, 0);
63 AudioBuffers::AudioBuffers (std::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset)
65 allocate (other->_channels, frames_to_copy);
66 copy_from (other.get(), frames_to_copy, read_offset, 0);
71 AudioBuffers::operator= (AudioBuffers const & other)
78 allocate (other._channels, other._frames);
79 copy_from (&other, other._frames, 0, 0);
85 /** AudioBuffers destructor */
86 AudioBuffers::~AudioBuffers ()
93 AudioBuffers::allocate (int channels, int32_t frames)
95 DCPOMATIC_ASSERT (frames >= 0);
96 DCPOMATIC_ASSERT (channels >= 0);
100 _allocated_frames = frames;
102 _data = static_cast<float**> (malloc(_channels * sizeof(float *)));
107 for (int i = 0; i < _channels; ++i) {
108 _data[i] = static_cast<float*> (malloc(frames * sizeof(float)));
117 AudioBuffers::deallocate ()
119 for (int i = 0; i < _channels; ++i) {
127 /** @param channel Channel index.
128 * @return Buffer for this channel.
131 AudioBuffers::data (int channel) const
133 DCPOMATIC_ASSERT (channel >= 0 && channel < _channels);
134 return _data[channel];
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
141 * @param f Frames; must be less than or equal to the number of allocated frames.
144 AudioBuffers::set_frames (int32_t frames)
146 DCPOMATIC_ASSERT (frames <= _allocated_frames);
148 if (frames < _frames) {
149 make_silent (frames, _frames - frames);
155 /** Make all frames silent */
157 AudioBuffers::make_silent ()
159 for (int channel = 0; channel < _channels; ++channel) {
160 make_silent (channel);
165 /** Make all samples on a given channel silent */
167 AudioBuffers::make_silent (int channel)
169 DCPOMATIC_ASSERT (channel >= 0 && channel < _channels);
171 /* This isn't really allowed, as all-bits-0 is not guaranteed to mean a 0 float,
172 but it seems that we can get away with it.
174 memset (_data[channel], 0, _frames * sizeof(float));
178 /** Make some frames.
179 * @param from Start frame.
180 * @param frames Number of frames to silence.
183 AudioBuffers::make_silent (int32_t from, int32_t frames)
185 DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
187 for (int channel = 0; channel < _channels; ++channel) {
188 /* This isn't really allowed, as all-bits-0 is not guaranteed to mean a 0 float,
189 but it seems that we can get away with it.
191 memset (_data[channel] + from, 0, frames * sizeof(float));
196 /** Copy data from another AudioBuffers to this one. All channels are copied.
197 * @param from AudioBuffers to copy from; must have the same number of channels as this.
198 * @param frames_to_copy Number of frames to copy.
199 * @param read_offset Offset to read from in `from'.
200 * @param write_offset Offset to write to in `to'.
203 AudioBuffers::copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset)
205 if (frames_to_copy == 0) {
206 /* Prevent the asserts from firing if there is nothing to do */
210 DCPOMATIC_ASSERT (from);
211 DCPOMATIC_ASSERT (from->channels() == channels());
212 DCPOMATIC_ASSERT (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
213 DCPOMATIC_ASSERT (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
215 for (int i = 0; i < _channels; ++i) {
216 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
221 /** Move audio data around.
222 * @param from Offset to move from.
223 * @param to Offset to move to.
224 * @param frames Number of frames to move.
227 AudioBuffers::move (int32_t frames, int32_t from, int32_t to)
233 DCPOMATIC_ASSERT (from >= 0);
234 DCPOMATIC_ASSERT (from < _frames);
235 DCPOMATIC_ASSERT (to >= 0);
236 DCPOMATIC_ASSERT (to < _frames);
237 DCPOMATIC_ASSERT (frames > 0);
238 DCPOMATIC_ASSERT (frames <= _frames);
239 DCPOMATIC_ASSERT ((from + frames) <= _frames);
240 DCPOMATIC_ASSERT ((to + frames) <= _allocated_frames);
242 for (int i = 0; i < _channels; ++i) {
243 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
248 /** Add data from from `from', `from_channel' to our channel `to_channel'.
249 * @param from Buffers to copy data from.
250 * @param from_channel Channel index to read in \p from.
251 * @param to_channel Channel index to accumulate into.
252 * @param gain Linear gain to apply to the data before it is added.
255 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
257 int const N = frames ();
258 DCPOMATIC_ASSERT (from->frames() == N);
259 DCPOMATIC_ASSERT (to_channel <= _channels);
261 auto s = from->data (from_channel);
262 auto d = _data[to_channel];
264 for (int i = 0; i < N; ++i) {
265 *d++ += (*s++) * gain;
270 /** Ensure we have space for at least a certain number of frames. If we extend
271 * the buffers, fill the new space with silence.
274 AudioBuffers::ensure_size (int32_t frames)
276 if (_allocated_frames >= frames) {
280 /* Round up frames to the next power of 2 to reduce the number
281 of realloc()s that are necessary.
284 frames |= frames >> 1;
285 frames |= frames >> 2;
286 frames |= frames >> 4;
287 frames |= frames >> 8;
288 frames |= frames >> 16;
291 for (int i = 0; i < _channels; ++i) {
292 _data[i] = static_cast<float*> (realloc(_data[i], frames * sizeof(float)));
298 auto const old_allocated = _allocated_frames;
299 _allocated_frames = frames;
300 if (old_allocated < _allocated_frames) {
301 make_silent (old_allocated, _allocated_frames - old_allocated);
306 /** Mix some other buffers with these ones. The AudioBuffers must have the same number of channels.
307 * @param from Audio buffers to get data from.
308 * @param frames Number of frames to mix.
309 * @param read_offset Offset within `from' to read from.
310 * @param write_offset Offset within this to mix into.
313 AudioBuffers::accumulate_frames (AudioBuffers const * from, int32_t frames, int32_t read_offset, int32_t write_offset)
315 DCPOMATIC_ASSERT (_channels == from->channels ());
316 DCPOMATIC_ASSERT (read_offset >= 0);
317 DCPOMATIC_ASSERT (write_offset >= 0);
319 auto from_data = from->data ();
320 for (int i = 0; i < _channels; ++i) {
321 for (int j = 0; j < frames; ++j) {
322 _data[i][j + write_offset] += from_data[i][j + read_offset];
328 /** @param dB gain in dB */
330 AudioBuffers::apply_gain (float dB)
332 auto const linear = db_to_linear (dB);
334 for (int i = 0; i < _channels; ++i) {
335 for (int j = 0; j < _frames; ++j) {
336 _data[i][j] *= linear;
342 shared_ptr<AudioBuffers>
343 AudioBuffers::channel (int channel) const
345 auto output = make_shared<AudioBuffers>(1, frames());
346 output->copy_channel_from (this, channel, 0);
351 /** Copy all the samples from a channel on another AudioBuffers to a channel on this one.
352 * @param from AudioBuffers to copy from.
353 * @param from_channel Channel index in `from' to copy from.
354 * @param to_channel Channel index in this to copy into, overwriting what's already there.
357 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
359 DCPOMATIC_ASSERT (from->frames() == frames());
360 memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
364 /** Make a copy of these AudioBuffers */
365 shared_ptr<AudioBuffers>
366 AudioBuffers::clone () const
368 auto b = make_shared<AudioBuffers>(channels(), frames());
369 b->copy_from (this, frames(), 0, 0);
374 /** Extend these buffers with the data from another. The AudioBuffers must have the same number of channels. */
376 AudioBuffers::append (shared_ptr<const AudioBuffers> other)
378 DCPOMATIC_ASSERT (channels() == other->channels());
379 ensure_size (_frames + other->frames());
380 copy_from (other.get(), other->frames(), 0, _frames);
381 _frames += other->frames();
385 /** Remove some frames from the start of these AudioBuffers */
387 AudioBuffers::trim_start (int32_t frames)
389 DCPOMATIC_ASSERT (frames <= _frames);
390 move (_frames - frames, frames, 0);
391 set_frames (_frames - frames);