Merge remote-tracking branch 'origin/master' into 2.0
[dcpomatic.git] / src / lib / audio_buffers.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cassert>
21 #include <cstring>
22 #include <cmath>
23 #include <stdexcept>
24 #include "audio_buffers.h"
25
26 using std::bad_alloc;
27 using boost::shared_ptr;
28
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.
32  */
33 AudioBuffers::AudioBuffers (int channels, int frames)
34 {
35         allocate (channels, frames);
36 }
37
38 /** Copy constructor.
39  *  @param other Other AudioBuffers; data is copied.
40  */
41 AudioBuffers::AudioBuffers (AudioBuffers const & other)
42 {
43         allocate (other._channels, other._frames);
44         copy_from (&other, other._frames, 0, 0);
45 }
46
47 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
48 {
49         allocate (other->_channels, other->_frames);
50         copy_from (other.get(), other->_frames, 0, 0);
51 }
52
53 AudioBuffers &
54 AudioBuffers::operator= (AudioBuffers const & other)
55 {
56         if (this == &other) {
57                 return *this;
58         }
59                 
60         deallocate ();
61         allocate (other._channels, other._frames);
62         copy_from (&other, other._frames, 0, 0);
63
64         return *this;
65 }
66
67 /** AudioBuffers destructor */
68 AudioBuffers::~AudioBuffers ()
69 {
70         deallocate ();
71 }
72
73 void
74 AudioBuffers::allocate (int channels, int frames)
75 {
76         assert (frames >= 0);
77         assert (channels >= 0);
78
79         _channels = channels;
80         _frames = frames;
81         _allocated_frames = frames;
82         
83         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
84         if (!_data) {
85                 throw bad_alloc ();
86         }
87         
88         for (int i = 0; i < _channels; ++i) {
89                 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
90                 if (!_data[i]) {
91                         throw bad_alloc ();
92                 }
93         }
94 }
95
96 void
97 AudioBuffers::deallocate ()
98 {
99         for (int i = 0; i < _channels; ++i) {
100                 free (_data[i]);
101         }
102
103         free (_data);
104 }
105
106 /** @param c Channel index.
107  *  @return Buffer for this channel.
108  */
109 float*
110 AudioBuffers::data (int c) const
111 {
112         assert (c >= 0 && c < _channels);
113         return _data[c];
114 }
115
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
118  *  be silenced.
119  *  @param f Frames; must be less than or equal to the number of allocated frames.
120  */
121 void
122 AudioBuffers::set_frames (int f)
123 {
124         assert (f <= _allocated_frames);
125
126         for (int c = 0; c < _channels; ++c) {
127                 for (int i = f; i < _frames; ++i) {
128                         _data[c][i] = 0;
129                 }
130         }
131         
132         _frames = f;
133 }
134
135 /** Make all samples on all channels silent */
136 void
137 AudioBuffers::make_silent ()
138 {
139         for (int i = 0; i < _channels; ++i) {
140                 make_silent (i);
141         }
142 }
143
144 /** Make all samples on a given channel silent.
145  *  @param c Channel.
146  */
147 void
148 AudioBuffers::make_silent (int c)
149 {
150         assert (c >= 0 && c < _channels);
151         
152         for (int i = 0; i < _frames; ++i) {
153                 _data[c][i] = 0;
154         }
155 }
156
157 void
158 AudioBuffers::make_silent (int from, int frames)
159 {
160         assert ((from + frames) <= _allocated_frames);
161
162         for (int c = 0; c < _channels; ++c) {
163                 for (int i = from; i < (from + frames); ++i) {
164                         _data[c][i] = 0;
165                 }
166         }
167 }
168
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'.
174  */
175 void
176 AudioBuffers::copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset)
177 {
178         if (frames_to_copy == 0) {
179                 /* Prevent the asserts from firing if there is nothing to do */
180                 return;
181         }
182         
183         assert (from->channels() == channels());
184
185         assert (from);
186         assert (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
187         assert (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
188
189         for (int i = 0; i < _channels; ++i) {
190                 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
191         }
192 }
193
194 /** Move audio data around.
195  *  @param from Offset to move from.
196  *  @param to Offset to move to.
197  *  @param frames Number of frames to move.
198  */
199     
200 void
201 AudioBuffers::move (int from, int to, int frames)
202 {
203         if (frames == 0) {
204                 return;
205         }
206         
207         assert (from >= 0);
208         assert (from < _frames);
209         assert (to >= 0);
210         assert (to < _frames);
211         assert (frames > 0);
212         assert (frames <= _frames);
213         assert ((from + frames) <= _frames);
214         assert ((to + frames) <= _allocated_frames);
215         
216         for (int i = 0; i < _channels; ++i) {
217                 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
218         }
219 }
220
221 /** Add data from from `from', `from_channel' to our channel `to_channel'.
222  *  @param gain Linear gain to apply to the data before it is added.
223  */
224 void
225 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
226 {
227         int const N = frames ();
228         assert (from->frames() == N);
229         assert (to_channel <= _channels);
230
231         float* s = from->data (from_channel);
232         float* d = _data[to_channel];
233
234         for (int i = 0; i < N; ++i) {
235                 *d++ += (*s++) * gain;
236         }
237 }
238
239 /** Ensure we have space for at least a certain number of frames.  If we extend
240  *  the buffers, fill the new space with silence.
241  */
242 void
243 AudioBuffers::ensure_size (int frames)
244 {
245         if (_allocated_frames >= frames) {
246                 return;
247         }
248
249         for (int i = 0; i < _channels; ++i) {
250                 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
251                 if (!_data[i]) {
252                         throw bad_alloc ();
253                 }
254                 for (int j = _allocated_frames; j < frames; ++j) {
255                         _data[i][j] = 0;
256                 }
257         }
258
259         _allocated_frames = frames;
260 }
261
262 void
263 AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
264 {
265         assert (_channels == from->channels ());
266         assert (read_offset >= 0);
267         assert (write_offset >= 0);
268
269         for (int i = 0; i < _channels; ++i) {
270                 for (int j = 0; j < frames; ++j) {
271                         _data[i][j + write_offset] += from->data()[i][j + read_offset];
272                 }
273         }
274 }
275
276 /** @param dB gain in dB */
277 void
278 AudioBuffers::apply_gain (float dB)
279 {
280         float const linear = pow (10, dB / 20);
281         
282         for (int i = 0; i < _channels; ++i) {
283                 for (int j = 0; j < _frames; ++j) {
284                         _data[i][j] *= linear;
285                 }
286         }
287 }
288
289 /** @param c Channel index.
290  *  @return AudioBuffers object containing only channel `c' from this AudioBuffers.
291  */
292 shared_ptr<AudioBuffers>
293 AudioBuffers::channel (int c) const
294 {
295         shared_ptr<AudioBuffers> o (new AudioBuffers (1, frames ()));
296         o->copy_channel_from (this, c, 0);
297         return o;
298 }
299
300 void
301 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
302 {
303         assert (from->frames() == frames());
304         memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
305 }
306
307 shared_ptr<AudioBuffers>
308 AudioBuffers::clone () const
309 {
310         shared_ptr<AudioBuffers> b (new AudioBuffers (channels (), frames ()));
311         b->copy_from (this, frames (), 0, 0);
312         return b;
313 }