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