2 Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_buffers.h"
21 #include "dcpomatic_assert.h"
28 using boost::shared_ptr;
30 /** Construct an AudioBuffers. Audio data is undefined after this constructor.
31 * @param channels Number of channels.
32 * @param frames Number of frames to reserve space for.
34 AudioBuffers::AudioBuffers (int channels, int frames)
36 allocate (channels, frames);
40 * @param other Other AudioBuffers; data is copied.
42 AudioBuffers::AudioBuffers (AudioBuffers const & other)
44 allocate (other._channels, other._frames);
45 copy_from (&other, other._frames, 0, 0);
48 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
50 allocate (other->_channels, other->_frames);
51 copy_from (other.get(), other->_frames, 0, 0);
55 AudioBuffers::operator= (AudioBuffers const & other)
62 allocate (other._channels, other._frames);
63 copy_from (&other, other._frames, 0, 0);
68 /** AudioBuffers destructor */
69 AudioBuffers::~AudioBuffers ()
75 AudioBuffers::allocate (int channels, int frames)
77 DCPOMATIC_ASSERT (frames >= 0);
78 DCPOMATIC_ASSERT (channels >= 0);
82 _allocated_frames = frames;
84 _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
89 for (int i = 0; i < _channels; ++i) {
90 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
98 AudioBuffers::deallocate ()
100 for (int i = 0; i < _channels; ++i) {
107 /** @param c Channel index.
108 * @return Buffer for this channel.
111 AudioBuffers::data (int c) const
113 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
117 /** Set the number of frames that these AudioBuffers will report themselves
118 * as having. If we reduce the number of frames, the `lost' frames will
120 * @param f Frames; must be less than or equal to the number of allocated frames.
123 AudioBuffers::set_frames (int f)
125 DCPOMATIC_ASSERT (f <= _allocated_frames);
127 for (int c = 0; c < _channels; ++c) {
128 for (int i = f; i < _frames; ++i) {
136 /** Make all samples on all channels silent */
138 AudioBuffers::make_silent ()
140 for (int i = 0; i < _channels; ++i) {
145 /** Make all samples on a given channel silent.
149 AudioBuffers::make_silent (int c)
151 DCPOMATIC_ASSERT (c >= 0 && c < _channels);
153 for (int i = 0; i < _frames; ++i) {
159 AudioBuffers::make_silent (int from, int frames)
161 DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
163 for (int c = 0; c < _channels; ++c) {
164 for (int i = from; i < (from + frames); ++i) {
170 /** Copy data from another AudioBuffers to this one. All channels are copied.
171 * @param from AudioBuffers to copy from; must have the same number of channels as this.
172 * @param frames_to_copy Number of frames to copy.
173 * @param read_offset Offset to read from in `from'.
174 * @param write_offset Offset to write to in `to'.
177 AudioBuffers::copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset)
179 if (frames_to_copy == 0) {
180 /* Prevent the asserts from firing if there is nothing to do */
184 DCPOMATIC_ASSERT (from->channels() == channels());
186 DCPOMATIC_ASSERT (from);
187 DCPOMATIC_ASSERT (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
188 DCPOMATIC_ASSERT (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
190 for (int i = 0; i < _channels; ++i) {
191 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
195 /** Move audio data around.
196 * @param from Offset to move from.
197 * @param to Offset to move to.
198 * @param frames Number of frames to move.
202 AudioBuffers::move (int from, int to, int frames)
208 DCPOMATIC_ASSERT (from >= 0);
209 DCPOMATIC_ASSERT (from < _frames);
210 DCPOMATIC_ASSERT (to >= 0);
211 DCPOMATIC_ASSERT (to < _frames);
212 DCPOMATIC_ASSERT (frames > 0);
213 DCPOMATIC_ASSERT (frames <= _frames);
214 DCPOMATIC_ASSERT ((from + frames) <= _frames);
215 DCPOMATIC_ASSERT ((to + frames) <= _allocated_frames);
217 for (int i = 0; i < _channels; ++i) {
218 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
222 /** Add data from from `from', `from_channel' to our channel `to_channel'.
223 * @param gain Linear gain to apply to the data before it is added.
226 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
228 int const N = frames ();
229 DCPOMATIC_ASSERT (from->frames() == N);
230 DCPOMATIC_ASSERT (to_channel <= _channels);
232 float* s = from->data (from_channel);
233 float* d = _data[to_channel];
235 for (int i = 0; i < N; ++i) {
236 *d++ += (*s++) * gain;
240 /** Ensure we have space for at least a certain number of frames. If we extend
241 * the buffers, fill the new space with silence.
244 AudioBuffers::ensure_size (int frames)
246 if (_allocated_frames >= frames) {
250 for (int i = 0; i < _channels; ++i) {
251 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
255 for (int j = _allocated_frames; j < frames; ++j) {
260 _allocated_frames = frames;
264 AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
266 DCPOMATIC_ASSERT (_channels == from->channels ());
267 DCPOMATIC_ASSERT (read_offset >= 0);
268 DCPOMATIC_ASSERT (write_offset >= 0);
270 for (int i = 0; i < _channels; ++i) {
271 for (int j = 0; j < frames; ++j) {
272 _data[i][j + write_offset] += from->data()[i][j + read_offset];
277 /** @param dB gain in dB */
279 AudioBuffers::apply_gain (float dB)
281 float const linear = pow (10, dB / 20);
283 for (int i = 0; i < _channels; ++i) {
284 for (int j = 0; j < _frames; ++j) {
285 _data[i][j] *= linear;
290 /** @param c Channel index.
291 * @return AudioBuffers object containing only channel `c' from this AudioBuffers.
293 shared_ptr<AudioBuffers>
294 AudioBuffers::channel (int c) const
296 shared_ptr<AudioBuffers> o (new AudioBuffers (1, frames ()));
297 o->copy_channel_from (this, c, 0);
302 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
304 DCPOMATIC_ASSERT (from->frames() == frames());
305 memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
308 shared_ptr<AudioBuffers>
309 AudioBuffers::clone () const
311 shared_ptr<AudioBuffers> b (new AudioBuffers (channels (), frames ()));
312 b->copy_from (this, frames (), 0, 0);