Add silence padding test.
[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 <stdexcept>
23 #include "audio_buffers.h"
24
25 using std::bad_alloc;
26 using boost::shared_ptr;
27
28 /** Construct an AudioBuffers.  Audio data is undefined after this constructor.
29  *  @param channels Number of channels.
30  *  @param frames Number of frames to reserve space for.
31  */
32 AudioBuffers::AudioBuffers (int channels, int frames)
33         : _channels (channels)
34         , _frames (frames)
35         , _allocated_frames (frames)
36 {
37         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
38         if (!_data) {
39                 throw bad_alloc ();
40         }
41         
42         for (int i = 0; i < _channels; ++i) {
43                 _data[i] = static_cast<float*> (malloc (frames * sizeof (float)));
44                 if (!_data[i]) {
45                         throw bad_alloc ();
46                 }
47         }
48 }
49
50 /** Copy constructor.
51  *  @param other Other AudioBuffers; data is copied.
52  */
53 AudioBuffers::AudioBuffers (AudioBuffers const & other)
54         : _channels (other._channels)
55         , _frames (other._frames)
56         , _allocated_frames (other._frames)
57 {
58         _data = static_cast<float**> (malloc (_channels * sizeof (float *)));
59         if (!_data) {
60                 throw bad_alloc ();
61         }
62         
63         for (int i = 0; i < _channels; ++i) {
64                 _data[i] = static_cast<float*> (malloc (_frames * sizeof (float)));
65                 if (!_data[i]) {
66                         throw bad_alloc ();
67                 }
68                 memcpy (_data[i], other._data[i], _frames * sizeof (float));
69         }
70 }
71
72 /* XXX: it's a shame that this is a copy-and-paste of the above;
73    probably fixable with c++0x.
74 */
75 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
76         : _channels (other->_channels)
77         , _frames (other->_frames)
78         , _allocated_frames (other->_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                 memcpy (_data[i], other->_data[i], _frames * sizeof (float));
91         }
92 }
93
94 /** AudioBuffers destructor */
95 AudioBuffers::~AudioBuffers ()
96 {
97         for (int i = 0; i < _channels; ++i) {
98                 free (_data[i]);
99         }
100
101         free (_data);
102 }
103
104 /** @param c Channel index.
105  *  @return Buffer for this channel.
106  */
107 float*
108 AudioBuffers::data (int c) const
109 {
110         assert (c >= 0 && c < _channels);
111         return _data[c];
112 }
113
114 /** Set the number of frames that these AudioBuffers will report themselves
115  *  as having.
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         _frames = f;
123 }
124
125 /** Make all samples on all channels silent */
126 void
127 AudioBuffers::make_silent ()
128 {
129         for (int i = 0; i < _channels; ++i) {
130                 make_silent (i);
131         }
132 }
133
134 /** Make all samples on a given channel silent.
135  *  @param c Channel.
136  */
137 void
138 AudioBuffers::make_silent (int c)
139 {
140         assert (c >= 0 && c < _channels);
141         
142         for (int i = 0; i < _frames; ++i) {
143                 _data[c][i] = 0;
144         }
145 }
146
147 void
148 AudioBuffers::make_silent (int from, int frames)
149 {
150         assert ((from + frames) <= _allocated_frames);
151
152         for (int c = 0; c < _channels; ++c) {
153                 for (int i = from; i < (from + frames); ++i) {
154                         _data[c][i] = 0;
155                 }
156         }
157 }
158
159 /** Copy data from another AudioBuffers to this one.  All channels are copied.
160  *  @param from AudioBuffers to copy from; must have the same number of channels as this.
161  *  @param frames_to_copy Number of frames to copy.
162  *  @param read_offset Offset to read from in `from'.
163  *  @param write_offset Offset to write to in `to'.
164  */
165 void
166 AudioBuffers::copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset)
167 {
168         assert (from->channels() == channels());
169
170         assert (from);
171         assert (read_offset >= 0 && (read_offset + frames_to_copy) <= from->_allocated_frames);
172         assert (write_offset >= 0 && (write_offset + frames_to_copy) <= _allocated_frames);
173
174         for (int i = 0; i < _channels; ++i) {
175                 memcpy (_data[i] + write_offset, from->_data[i] + read_offset, frames_to_copy * sizeof(float));
176         }
177 }
178
179 /** Move audio data around.
180  *  @param from Offset to move from.
181  *  @param to Offset to move to.
182  *  @param frames Number of frames to move.
183  */
184     
185 void
186 AudioBuffers::move (int from, int to, int frames)
187 {
188         if (frames == 0) {
189                 return;
190         }
191         
192         assert (from >= 0);
193         assert (from < _frames);
194         assert (to >= 0);
195         assert (to < _frames);
196         assert (frames > 0);
197         assert (frames <= _frames);
198         assert ((from + frames) <= _frames);
199         assert ((to + frames) <= _frames);
200         
201         for (int i = 0; i < _channels; ++i) {
202                 memmove (_data[i] + to, _data[i] + from, frames * sizeof(float));
203         }
204 }
205
206 /** Add data from from `from', `from_channel' to our channel `to_channel' */
207 void
208 AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel)
209 {
210         int const N = frames ();
211         assert (from->frames() == N);
212         assert (to_channel <= _channels);
213
214         float* s = from->data (from_channel);
215         float* d = _data[to_channel];
216
217         for (int i = 0; i < N; ++i) {
218                 *d++ += *s++;
219         }
220 }
221
222 /** Ensure we have space for at least a certain number of frames.  If we extend
223  *  the buffers, fill the new space with silence.
224  */
225 void
226 AudioBuffers::ensure_size (int frames)
227 {
228         if (_allocated_frames >= frames) {
229                 return;
230         }
231
232         for (int i = 0; i < _channels; ++i) {
233                 _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
234                 if (!_data[i]) {
235                         throw bad_alloc ();
236                 }
237                 for (int j = _allocated_frames; j < frames; ++j) {
238                         _data[i][j] = 0;
239                 }
240         }
241
242         _allocated_frames = frames;
243 }
244
245 void
246 AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
247 {
248         assert (_channels == from->channels ());
249
250         for (int i = 0; i < _channels; ++i) {
251                 for (int j = 0; j < frames; ++j) {
252                         _data[i][j + write_offset] += from->data()[i][j + read_offset];
253                 }
254         }
255 }
256