Logging improvements to allow prettier displays in the server GUI.
[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 "audio_buffers.h"
21 #include "dcpomatic_assert.h"
22 #include <cassert>
23 #include <cstring>
24 #include <cmath>
25 #include <stdexcept>
26
27 using std::bad_alloc;
28 using boost::shared_ptr;
29
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.
33  */
34 AudioBuffers::AudioBuffers (int channels, int32_t frames)
35 {
36         allocate (channels, frames);
37 }
38
39 /** Copy constructor.
40  *  @param other Other AudioBuffers; data is copied.
41  */
42 AudioBuffers::AudioBuffers (AudioBuffers const & other)
43 {
44         allocate (other._channels, other._frames);
45         copy_from (&other, other._frames, 0, 0);
46 }
47
48 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
49 {
50         allocate (other->_channels, other->_frames);
51         copy_from (other.get(), other->_frames, 0, 0);
52 }
53
54 AudioBuffers &
55 AudioBuffers::operator= (AudioBuffers const & other)
56 {
57         if (this == &other) {
58                 return *this;
59         }
60
61         deallocate ();
62         allocate (other._channels, other._frames);
63         copy_from (&other, other._frames, 0, 0);
64
65         return *this;
66 }
67
68 /** AudioBuffers destructor */
69 AudioBuffers::~AudioBuffers ()
70 {
71         deallocate ();
72 }
73
74 void
75 AudioBuffers::allocate (int channels, int32_t frames)
76 {
77         DCPOMATIC_ASSERT (frames >= 0);
78         DCPOMATIC_ASSERT (channels >= 0);
79
80         _channels = channels;
81         _frames = frames;
82         _allocated_frames = frames;
83
84         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
85         if (!_data) {
86                 throw bad_alloc ();
87         }
88
89         for (int i = 0; i < _channels; ++i) {
90                 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
91                 if (!_data[i]) {
92                         throw bad_alloc ();
93                 }
94         }
95 }
96
97 void
98 AudioBuffers::deallocate ()
99 {
100         for (int i = 0; i < _channels; ++i) {
101                 free (_data[i]);
102         }
103
104         free (_data);
105 }
106
107 /** @param c Channel index.
108  *  @return Buffer for this channel.
109  */
110 float*
111 AudioBuffers::data (int c) const
112 {
113         DCPOMATIC_ASSERT (c >= 0 && c < _channels);
114         return _data[c];
115 }
116
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
119  *  be silenced.
120  *  @param f Frames; must be less than or equal to the number of allocated frames.
121  */
122 void
123 AudioBuffers::set_frames (int32_t f)
124 {
125         DCPOMATIC_ASSERT (f <= _allocated_frames);
126
127         for (int c = 0; c < _channels; ++c) {
128                 for (int i = f; i < _frames; ++i) {
129                         _data[c][i] = 0;
130                 }
131         }
132
133         _frames = f;
134 }
135
136 /** Make all samples on all channels silent */
137 void
138 AudioBuffers::make_silent ()
139 {
140         for (int i = 0; i < _channels; ++i) {
141                 make_silent (i);
142         }
143 }
144
145 /** Make all samples on a given channel silent.
146  *  @param c Channel.
147  */
148 void
149 AudioBuffers::make_silent (int c)
150 {
151         DCPOMATIC_ASSERT (c >= 0 && c < _channels);
152
153         for (int i = 0; i < _frames; ++i) {
154                 _data[c][i] = 0;
155         }
156 }
157
158 void
159 AudioBuffers::make_silent (int32_t from, int32_t frames)
160 {
161         DCPOMATIC_ASSERT ((from + frames) <= _allocated_frames);
162
163         for (int c = 0; c < _channels; ++c) {
164                 for (int i = from; i < (from + frames); ++i) {
165                         _data[c][i] = 0;
166                 }
167         }
168 }
169
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'.
175  */
176 void
177 AudioBuffers::copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset)
178 {
179         if (frames_to_copy == 0) {
180                 /* Prevent the asserts from firing if there is nothing to do */
181                 return;
182         }
183
184         DCPOMATIC_ASSERT (from->channels() == channels());
185
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);
189
190         for (int i = 0; i < _channels; ++i) {
191                 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
192         }
193 }
194
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.
199  */
200
201 void
202 AudioBuffers::move (int32_t from, int32_t to, int32_t frames)
203 {
204         if (frames == 0) {
205                 return;
206         }
207
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);
216
217         for (int i = 0; i < _channels; ++i) {
218                 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
219         }
220 }
221
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.
224  */
225 void
226 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain)
227 {
228         int const N = frames ();
229         DCPOMATIC_ASSERT (from->frames() == N);
230         DCPOMATIC_ASSERT (to_channel <= _channels);
231
232         float* s = from->data (from_channel);
233         float* d = _data[to_channel];
234
235         for (int i = 0; i < N; ++i) {
236                 *d++ += (*s++) * gain;
237         }
238 }
239
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.
242  */
243 void
244 AudioBuffers::ensure_size (int32_t frames)
245 {
246         if (_allocated_frames >= frames) {
247                 return;
248         }
249
250         /* Round up frames to the next power of 2 to reduce the number
251            of realloc()s that are necessary.
252         */
253         frames--;
254         frames |= frames >> 1;
255         frames |= frames >> 2;
256         frames |= frames >> 4;
257         frames |= frames >> 8;
258         frames |= frames >> 16;
259         frames++;
260
261         for (int i = 0; i < _channels; ++i) {
262                 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
263                 if (!_data[i]) {
264                         throw bad_alloc ();
265                 }
266                 for (int j = _allocated_frames; j < frames; ++j) {
267                         _data[i][j] = 0;
268                 }
269         }
270
271         _allocated_frames = frames;
272 }
273
274 void
275 AudioBuffers::accumulate_frames (AudioBuffers const * from, int32_t read_offset, int32_t write_offset, int32_t frames)
276 {
277         DCPOMATIC_ASSERT (_channels == from->channels ());
278         DCPOMATIC_ASSERT (read_offset >= 0);
279         DCPOMATIC_ASSERT (write_offset >= 0);
280
281         for (int i = 0; i < _channels; ++i) {
282                 for (int j = 0; j < frames; ++j) {
283                         _data[i][j + write_offset] += from->data()[i][j + read_offset];
284                 }
285         }
286 }
287
288 /** @param dB gain in dB */
289 void
290 AudioBuffers::apply_gain (float dB)
291 {
292         float const linear = pow (10, dB / 20);
293
294         for (int i = 0; i < _channels; ++i) {
295                 for (int j = 0; j < _frames; ++j) {
296                         _data[i][j] *= linear;
297                 }
298         }
299 }
300
301 /** @param c Channel index.
302  *  @return AudioBuffers object containing only channel `c' from this AudioBuffers.
303  */
304 shared_ptr<AudioBuffers>
305 AudioBuffers::channel (int c) const
306 {
307         shared_ptr<AudioBuffers> o (new AudioBuffers (1, frames ()));
308         o->copy_channel_from (this, c, 0);
309         return o;
310 }
311
312 void
313 AudioBuffers::copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel)
314 {
315         DCPOMATIC_ASSERT (from->frames() == frames());
316         memcpy (data(to_channel), from->data(from_channel), frames() * sizeof (float));
317 }
318
319 shared_ptr<AudioBuffers>
320 AudioBuffers::clone () const
321 {
322         shared_ptr<AudioBuffers> b (new AudioBuffers (channels (), frames ()));
323         b->copy_from (this, frames (), 0, 0);
324         return b;
325 }