Merge master.
[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         _channels = channels;
77         _frames = frames;
78         _allocated_frames = frames;
79         
80         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
81         if (!_data) {
82                 throw bad_alloc ();
83         }
84         
85         for (int i = 0; i < _channels; ++i) {
86                 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
87                 if (!_data[i]) {
88                         throw bad_alloc ();
89                 }
90         }
91 }
92
93 void
94 AudioBuffers::deallocate ()
95 {
96         for (int i = 0; i < _channels; ++i) {
97                 free (_data[i]);
98         }
99
100         free (_data);
101 }
102
103 /** @param c Channel index.
104  *  @return Buffer for this channel.
105  */
106 float*
107 AudioBuffers::data (int c) const
108 {
109         assert (c >= 0 && c < _channels);
110         return _data[c];
111 }
112
113 /** Set the number of frames that these AudioBuffers will report themselves
114  *  as having.  If we reduce the number of frames, the `lost' frames will
115  *  be silenced.
116  *  @param f Frames; must be less than or equal to the number of allocated frames.
117  */
118 void
119 AudioBuffers::set_frames (int f)
120 {
121         assert (f <= _allocated_frames);
122
123         for (int c = 0; c < _channels; ++c) {
124                 for (int i = f; i < _frames; ++i) {
125                         _data[c][i] = 0;
126                 }
127         }
128         
129         _frames = f;
130 }
131
132 /** Make all samples on all channels silent */
133 void
134 AudioBuffers::make_silent ()
135 {
136         for (int i = 0; i < _channels; ++i) {
137                 make_silent (i);
138         }
139 }
140
141 /** Make all samples on a given channel silent.
142  *  @param c Channel.
143  */
144 void
145 AudioBuffers::make_silent (int c)
146 {
147         assert (c >= 0 && c < _channels);
148         
149         for (int i = 0; i < _frames; ++i) {
150                 _data[c][i] = 0;
151         }
152 }
153
154 void
155 AudioBuffers::make_silent (int from, int frames)
156 {
157         assert ((from + frames) <= _allocated_frames);
158
159         for (int c = 0; c < _channels; ++c) {
160                 for (int i = from; i < (from + frames); ++i) {
161                         _data[c][i] = 0;
162                 }
163         }
164 }
165
166 /** Copy data from another AudioBuffers to this one.  All channels are copied.
167  *  @param from AudioBuffers to copy from; must have the same number of channels as this.
168  *  @param frames_to_copy Number of frames to copy.
169  *  @param read_offset Offset to read from in `from'.
170  *  @param write_offset Offset to write to in `to'.
171  */
172 void
173 AudioBuffers::copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset)
174 {
175         assert (from->channels() == channels());
176
177         assert (from);
178         assert (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
179         assert (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
180
181         for (int i = 0; i < _channels; ++i) {
182                 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
183         }
184 }
185
186 /** Move audio data around.
187  *  @param from Offset to move from.
188  *  @param to Offset to move to.
189  *  @param frames Number of frames to move.
190  */
191     
192 void
193 AudioBuffers::move (int from, int to, int frames)
194 {
195         if (frames == 0) {
196                 return;
197         }
198         
199         assert (from >= 0);
200         assert (from < _frames);
201         assert (to >= 0);
202         assert (to < _frames);
203         assert (frames > 0);
204         assert (frames <= _frames);
205         assert ((from + frames) <= _frames);
206         assert ((to + frames) <= _allocated_frames);
207         
208         for (int i = 0; i < _channels; ++i) {
209                 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
210         }
211 }
212
213 /** Add data from from `from', `from_channel' to our channel `to_channel'.
214  *  @param gain Linear gain to apply to the data before it is added.
215  */
216 void
217 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
218 {
219         int const N = frames ();
220         assert (from->frames() == N);
221         assert (to_channel <= _channels);
222
223         float* s = from->data (from_channel);
224         float* d = _data[to_channel];
225
226         for (int i = 0; i < N; ++i) {
227                 *d++ += (*s++) * gain;
228         }
229 }
230
231 /** Ensure we have space for at least a certain number of frames.  If we extend
232  *  the buffers, fill the new space with silence.
233  */
234 void
235 AudioBuffers::ensure_size (int frames)
236 {
237         if (_allocated_frames >= frames) {
238                 return;
239         }
240
241         for (int i = 0; i < _channels; ++i) {
242                 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
243                 if (!_data[i]) {
244                         throw bad_alloc ();
245                 }
246                 for (int j = _allocated_frames; j < frames; ++j) {
247                         _data[i][j] = 0;
248                 }
249         }
250
251         _allocated_frames = frames;
252 }
253
254 void
255 AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
256 {
257         assert (_channels == from->channels ());
258
259         for (int i = 0; i < _channels; ++i) {
260                 for (int j = 0; j < frames; ++j) {
261                         _data[i][j + write_offset] += from->data()[i][j + read_offset];
262                 }
263         }
264 }
265
266 /** @param dB gain in dB */
267 void
268 AudioBuffers::apply_gain (float dB)
269 {
270         float const linear = pow (10, dB / 20);
271         
272         for (int i = 0; i < _channels; ++i) {
273                 for (int j = 0; j < _frames; ++j) {
274                         _data[i][j] *= linear;
275                 }
276         }
277 }