2 Copyright (C) 2012-2013 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"
23 #include <boost/make_shared.hpp>
30 using boost::shared_ptr;
31 using boost::make_shared;
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.
37 AudioBuffers::AudioBuffers (int channels, int32_t frames)
39 allocate (channels, frames);
43 * @param other Other AudioBuffers; data is copied.
45 AudioBuffers::AudioBuffers (AudioBuffers const & other)
47 allocate (other._channels, other._frames);
48 copy_from (&other, other._frames, 0, 0);
51 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
53 allocate (other->_channels, other->_frames);
54 copy_from (other.get(), other->_frames, 0, 0);
58 AudioBuffers::operator= (AudioBuffers const & other)
65 allocate (other._channels, other._frames);
66 copy_from (&other, other._frames, 0, 0);
71 /** AudioBuffers destructor */
72 AudioBuffers::~AudioBuffers ()
78 AudioBuffers::allocate (int channels, int32_t frames)
80 DCPOMATIC_ASSERT (frames >= 0);
81 DCPOMATIC_ASSERT (channels >= 0);
85 _allocated_frames = frames;
87 _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
92 for (int i = 0; i < _channels; ++i) {
93 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
101 AudioBuffers::deallocate ()
103 for (int i = 0; i < _channels; ++i) {
110 /** @param c Channel index.
111 * @return Buffer for this channel.
114 AudioBuffers::data (int c) const
116 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
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
123 * @param f Frames; must be less than or equal to the number of allocated frames.
126 AudioBuffers::set_frames (int32_t f)
128 DCPOMATIC_ASSERT (f <= _allocated_frames);
130 for (int c = 0; c < _channels; ++c) {
131 for (int i = f; i < _frames; ++i) {
139 /** Make all samples on all channels silent */
141 AudioBuffers::make_silent ()
143 for (int i = 0; i < _channels; ++i) {
148 /** Make all samples on a given channel silent.
152 AudioBuffers::make_silent (int c)
154 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
156 for (int i = 0; i < _frames; ++i) {
162 AudioBuffers::make_silent (int32_t from, int32_t frames)
164 DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
166 for (int c = 0; c < _channels; ++c) {
167 for (int i = from; i < (from + frames); ++i) {
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'.
180 AudioBuffers::copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset)
182 if (frames_to_copy == 0) {
183 /* Prevent the asserts from firing if there is nothing to do */
187 DCPOMATIC_ASSERT (from->channels() == channels());
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);
193 for (int i = 0; i < _channels; ++i) {
194 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
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.
205 AudioBuffers::move (int32_t from, int32_t to, int32_t frames)
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);
220 for (int i = 0; i < _channels; ++i) {
221 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
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.
229 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
231 int const N = frames ();
232 DCPOMATIC_ASSERT (from->frames() == N);
233 DCPOMATIC_ASSERT (to_channel <= _channels);
235 float* s = from->data (from_channel);
236 float* d = _data[to_channel];
238 for (int i = 0; i < N; ++i) {
239 *d++ += (*s++) * gain;
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.
247 AudioBuffers::ensure_size (int32_t frames)
249 if (_allocated_frames >= frames) {
253 /* Round up frames to the next power of 2 to reduce the number
254 of realloc()s that are necessary.
257 frames |= frames >> 1;
258 frames |= frames >> 2;
259 frames |= frames >> 4;
260 frames |= frames >> 8;
261 frames |= frames >> 16;
264 for (int i = 0; i < _channels; ++i) {
265 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
269 for (int j = _allocated_frames; j < frames; ++j) {
274 _allocated_frames = frames;
278 AudioBuffers::accumulate_frames (AudioBuffers const * from, int32_t read_offset, int32_t write_offset, int32_t frames)
280 DCPOMATIC_ASSERT (_channels == from->channels ());
281 DCPOMATIC_ASSERT (read_offset >= 0);
282 DCPOMATIC_ASSERT (write_offset >= 0);
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];
291 /** @param dB gain in dB */
293 AudioBuffers::apply_gain (float dB)
295 float const linear = pow (10, dB / 20);
297 for (int i = 0; i < _channels; ++i) {
298 for (int j = 0; j < _frames; ++j) {
299 _data[i][j] *= linear;
304 /** @param c Channel index.
305 * @return AudioBuffers object containing only channel `c' from this AudioBuffers.
307 shared_ptr<AudioBuffers>
308 AudioBuffers::channel (int c) const
310 shared_ptr<AudioBuffers> o = make_shared<AudioBuffers> (1, frames ());
311 o->copy_channel_from (this, c, 0);
316 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
318 DCPOMATIC_ASSERT (from->frames() == frames());
319 memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
322 shared_ptr<AudioBuffers>
323 AudioBuffers::clone () const
325 shared_ptr<AudioBuffers> b = make_shared<AudioBuffers> (channels (), frames ());
326 b->copy_from (this, frames (), 0, 0);