051b75ab4bf4b1821557f20f31f19c2ebfe9ec78
[ardour.git] / libs / ardour / ardour / audio_buffer.h
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifndef __ardour_audio_buffer_h__
20 #define __ardour_audio_buffer_h__
21
22 #include <cstring>
23
24 #include "ardour/buffer.h"
25 #include "ardour/runtime_functions.h"
26
27 namespace ARDOUR {
28
29 /** Buffer containing audio data. */
30 class LIBARDOUR_API AudioBuffer : public Buffer
31 {
32 public:
33         AudioBuffer(size_t capacity);
34         ~AudioBuffer();
35
36         /** silence buffer
37          * @param len number of samples to clear
38          * @laram offset start offset
39          */
40         void silence (framecnt_t len, framecnt_t offset = 0);
41
42         /** Copy samples from src array starting at src_offset into self starting at dst_offset
43          * @param src array to read from
44          * @param len number of samples to copy
45          * @param dst_offset offset in destination buffer
46          * @param src_offset start offset in src buffer
47          */
48         void read_from (const Sample* src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
49                 assert(src != 0);
50                 assert(_capacity > 0);
51                 assert(len <= _capacity);
52                 memcpy(_data + dst_offset, src + src_offset, sizeof(Sample) * len);
53                 _silent = false;
54                 _written = true;
55         }
56
57         void read_from_with_gain (const Sample* src, framecnt_t len, gain_t gain, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
58                 assert(src != 0);
59                 assert(_capacity > 0);
60                 assert(len <= _capacity);
61                 src += src_offset;
62                 for (framecnt_t n = 0; n < len; ++n) {
63                         _data[dst_offset+n] = src[n] * gain;
64                 }
65                 _silent = false;
66                 _written = true;
67         }
68
69         /** Copy samples from src buffer starting at src_offset into self starting at dst_offset
70          * @param src buffer to read from
71          * @param len number of samples to copy
72          * @param dst_offset offset in destination buffer
73          * @param src_offset start offset in src buffer
74          */
75         void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
76                 assert(&src != this);
77                 assert(_capacity > 0);
78                 assert(src.type() == DataType::AUDIO);
79                 assert(dst_offset + len <= _capacity);
80                 assert( src_offset <= ((framecnt_t) src.capacity()-len));
81                 memcpy(_data + dst_offset, ((const AudioBuffer&)src).data() + src_offset, sizeof(Sample) * len);
82                 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
83                         _silent = src.silent();
84                 } else {
85                         _silent = _silent && src.silent();
86                 }
87                 _written = true;
88         }
89
90         /** Accumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
91         void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
92                 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
93                 assert (ab);
94                 accumulate_from (*ab, len, dst_offset, src_offset);
95         }
96
97         /** Accumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
98         void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
99                 assert(_capacity > 0);
100                 assert(len <= _capacity);
101
102                 Sample*       const dst_raw = _data + dst_offset;
103                 const Sample* const src_raw = src.data() + src_offset;
104
105                 mix_buffers_no_gain(dst_raw, src_raw, len);
106
107                 _silent = (src.silent() && _silent);
108                 _written = true;
109         }
110
111         /** Accumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
112         void accumulate_from (const Sample* src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
113                 assert(_capacity > 0);
114                 assert(len <= _capacity);
115
116                 Sample*       const dst_raw = _data + dst_offset;
117                 const Sample* const src_raw = src + src_offset;
118
119                 mix_buffers_no_gain(dst_raw, src_raw, len);
120
121                 _silent = false;
122                 _written = true;
123         }
124
125         /** Accumulate (add) @a len frames @a src starting at @a src_offset into self starting at @dst_offset
126          * scaling by @a gain_coeff */
127         void accumulate_with_gain_from (const AudioBuffer& src, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
128
129                 assert(_capacity > 0);
130                 assert(len <= _capacity);
131
132                 if (src.silent()) {
133                         return;
134                 }
135
136                 Sample*       const dst_raw = _data + dst_offset;
137                 const Sample* const src_raw = src.data() + src_offset;
138
139                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
140
141                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
142                 _written = true;
143         }
144
145         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
146          * scaling by @a gain_coeff */
147         void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
148
149                 assert(_capacity > 0);
150                 assert(len <= _capacity);
151
152                 Sample*       const dst_raw = _data + dst_offset;
153
154                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
155
156                 _silent = (_silent && gain_coeff == 0);
157                 _written = true;
158         }
159
160         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
161          * scaling by @a gain_coeff */
162         void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
163
164                 assert(_capacity > 0);
165                 assert(len <= _capacity);
166
167                 Sample* dst = _data + dst_offset;
168                 gain_t  gain_delta = (target - initial)/len;
169
170                 for (framecnt_t n = 0; n < len; ++n) {
171                         *dst++ += (*src++ * initial);
172                         initial += gain_delta;
173                 }
174
175                 _silent = (_silent && initial == 0 && target == 0);
176                 _written = true;
177         }
178
179         /** apply a fixed gain factor to the audio buffer
180          * @param gain gain factor
181          * @param len number of frames to amplify
182          */
183         void apply_gain (gain_t gain, framecnt_t len) {
184                 apply_gain_to_buffer (_data, len, gain);
185         }
186
187         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
188          *
189          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
190          */
191         void set_data (Sample* data, size_t size) {
192                 assert(!_owns_data); // prevent leaks
193                 _capacity = size;
194                 _data = data;
195                 _silent = false;
196                 _written = false;
197         }
198
199         /** Reallocate the buffer used internally to handle at least @nframes of data
200          *
201          * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
202          */
203         void resize (size_t nframes);
204
205         const Sample* data (framecnt_t offset = 0) const {
206                 assert(offset <= _capacity);
207                 return _data + offset;
208         }
209
210         Sample* data (framecnt_t offset = 0) {
211                 assert(offset <= _capacity);
212                 _silent = false;
213                 return _data + offset;
214         }
215
216         /** check buffer for silence
217          * @param nframes  number of frames to check
218          * @param n first non zero sample (if any)
219          * @return true if all samples are zero
220          */
221         bool check_silence (pframes_t nframes, pframes_t& n) const;
222
223         void prepare () { _written = false; _silent = false; }
224         bool written() const { return _written; }
225         void set_written(bool w) { _written = w; }
226
227   private:
228         bool    _owns_data;
229         bool    _written;
230         Sample* _data; ///< Actual buffer contents
231 };
232
233
234 } // namespace ARDOUR
235
236 #endif // __ardour_audio_audio_buffer_h__