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.
24 #include "audio_buffers.h"
27 using boost::shared_ptr;
29 /** Construct an AudioBuffers. Audio data is undefined after this constructor.
30 * @param channels Number of channels.
31 * @param frames Number of frames to reserve space for.
33 AudioBuffers::AudioBuffers (int channels, int frames)
35 allocate (channels, frames);
39 * @param other Other AudioBuffers; data is copied.
41 AudioBuffers::AudioBuffers (AudioBuffers const & other)
43 allocate (other._channels, other._frames);
44 copy_from (&other, other._frames, 0, 0);
47 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
49 allocate (other->_channels, other->_frames);
50 copy_from (other.get(), other->_frames, 0, 0);
54 AudioBuffers::operator= (AudioBuffers const & other)
61 allocate (other._channels, other._frames);
62 copy_from (&other, other._frames, 0, 0);
67 /** AudioBuffers destructor */
68 AudioBuffers::~AudioBuffers ()
74 AudioBuffers::allocate (int channels, int frames)
77 assert (channels >= 0);
81 _allocated_frames = frames;
83 _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
88 for (int i = 0; i < _channels; ++i) {
89 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
97 AudioBuffers::deallocate ()
99 for (int i = 0; i < _channels; ++i) {
106 /** @param c Channel index.
107 * @return Buffer for this channel.
110 AudioBuffers::data (int c) const
112 assert (c >= 0 && c < _channels);
116 /** Set the number of frames that these AudioBuffers will report themselves
117 * as having. If we reduce the number of frames, the `lost' frames will
119 * @param f Frames; must be less than or equal to the number of allocated frames.
122 AudioBuffers::set_frames (int f)
124 assert (f <= _allocated_frames);
126 for (int c = 0; c < _channels; ++c) {
127 for (int i = f; i < _frames; ++i) {
135 /** Make all samples on all channels silent */
137 AudioBuffers::make_silent ()
139 for (int i = 0; i < _channels; ++i) {
144 /** Make all samples on a given channel silent.
148 AudioBuffers::make_silent (int c)
150 assert (c >= 0 && c < _channels);
152 for (int i = 0; i < _frames; ++i) {
158 AudioBuffers::make_silent (int from, int frames)
160 assert ((from + frames) <= _allocated_frames);
162 for (int c = 0; c < _channels; ++c) {
163 for (int i = from; i < (from + frames); ++i) {
169 /** Copy data from another AudioBuffers to this one. All channels are copied.
170 * @param from AudioBuffers to copy from; must have the same number of channels as this.
171 * @param frames_to_copy Number of frames to copy.
172 * @param read_offset Offset to read from in `from'.
173 * @param write_offset Offset to write to in `to'.
176 AudioBuffers::copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset)
178 assert (from->channels() == channels());
181 assert (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
182 assert (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
184 for (int i = 0; i < _channels; ++i) {
185 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
189 /** Move audio data around.
190 * @param from Offset to move from.
191 * @param to Offset to move to.
192 * @param frames Number of frames to move.
196 AudioBuffers::move (int from, int to, int frames)
203 assert (from < _frames);
205 assert (to < _frames);
207 assert (frames <= _frames);
208 assert ((from + frames) <= _frames);
209 assert ((to + frames) <= _allocated_frames);
211 for (int i = 0; i < _channels; ++i) {
212 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
216 /** Add data from from `from', `from_channel' to our channel `to_channel'.
217 * @param gain Linear gain to apply to the data before it is added.
220 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
222 int const N = frames ();
223 assert (from->frames() == N);
224 assert (to_channel <= _channels);
226 float* s = from->data (from_channel);
227 float* d = _data[to_channel];
229 for (int i = 0; i < N; ++i) {
230 *d++ += (*s++) * gain;
234 /** Ensure we have space for at least a certain number of frames. If we extend
235 * the buffers, fill the new space with silence.
238 AudioBuffers::ensure_size (int frames)
240 if (_allocated_frames >= frames) {
244 for (int i = 0; i < _channels; ++i) {
245 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
249 for (int j = _allocated_frames; j < frames; ++j) {
254 _allocated_frames = frames;
258 AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
260 assert (_channels == from->channels ());
262 for (int i = 0; i < _channels; ++i) {
263 for (int j = 0; j < frames; ++j) {
264 _data[i][j + write_offset] += from->data()[i][j + read_offset];
269 /** @param dB gain in dB */
271 AudioBuffers::apply_gain (float dB)
273 float const linear = pow (10, dB / 20);
275 for (int i = 0; i < _channels; ++i) {
276 for (int j = 0; j < _frames; ++j) {
277 _data[i][j] *= linear;