2 * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2013-2016 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef __ardour_audio_buffer_h__
23 #define __ardour_audio_buffer_h__
27 #include "ardour/buffer.h"
28 #include "ardour/runtime_functions.h"
32 /** Buffer containing audio data. */
33 class LIBARDOUR_API AudioBuffer : public Buffer
36 AudioBuffer (size_t capacity);
40 * @param len number of samples to clear
41 * @param offset start offset
43 void silence (samplecnt_t len, samplecnt_t offset = 0);
45 /** Copy samples from src array starting at src_offset into self starting at dst_offset
46 * @param src array to read from
47 * @param len number of samples to copy
48 * @param dst_offset offset in destination buffer
49 * @param src_offset start offset in src buffer
51 void read_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
54 assert (_capacity > 0);
55 assert (len <= _capacity);
56 copy_vector (_data + dst_offset, src + src_offset, len);
61 /** Copy samples from src buffer starting at src_offset into self starting at dst_offset
62 * @param src buffer to read from
63 * @param len number of samples to copy
64 * @param dst_offset offset in destination buffer
65 * @param src_offset start offset in src buffer
67 void read_from (const Buffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
69 assert (&src != this);
70 assert (_capacity > 0);
71 assert (src.type () == DataType::AUDIO);
72 assert (dst_offset + len <= _capacity);
73 assert (src_offset <= ((samplecnt_t)src.capacity () - len));
76 memset (_data + dst_offset, 0, sizeof (Sample) * len);
78 copy_vector (_data + dst_offset, ((const AudioBuffer&)src).data () + src_offset, len);
81 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
82 _silent = src.silent ();
84 _silent = _silent && src.silent ();
89 /** Accumulate (add) \param len samples from \param src starting at \param src_offset into self starting at \param dst_offset */
90 void merge_from (const Buffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
92 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*> (&src);
94 accumulate_from (*ab, len, dst_offset, src_offset);
97 /** Accumulate (add) \param len samples from \param src starting at \param src_offset into self starting at \param dst_offset */
98 void accumulate_from (const AudioBuffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
100 assert (_capacity > 0);
101 assert (len <= _capacity);
105 Sample* const dst_raw = _data + dst_offset;
106 const Sample* const src_raw = src.data () + src_offset;
108 mix_buffers_no_gain (dst_raw, src_raw, len);
110 _silent = (src.silent () && _silent);
114 /** Accumulate (add) \param len samples of \param src starting at \param src_offset into self
115 * starting at \param dst_offset
117 void accumulate_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
119 assert (_capacity > 0);
120 assert (len <= _capacity);
122 Sample* const dst_raw = _data + dst_offset;
123 const Sample* const src_raw = src + src_offset;
125 mix_buffers_no_gain (dst_raw, src_raw, len);
131 /** Accumulate (add) \param len samples if \param src starting at \param src_offset into self
132 * starting at \param dst_offset scaling by \param gain_coeff
134 void accumulate_with_gain_from (const AudioBuffer& src, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
136 assert (_capacity > 0);
137 assert (len <= _capacity);
139 if (src.silent () || gain_coeff == 0) {
143 Sample* const dst_raw = _data + dst_offset;
144 const Sample* const src_raw = src.data () + src_offset;
146 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
148 _silent = ((src.silent () && _silent) || (_silent && gain_coeff == 0));
152 /** Accumulate (add) \param len samples from the start of \param src_raw into self at \param dst_offset
153 * scaling by \param gain_coeff
155 void accumulate_with_gain_from (const Sample* src_raw, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0)
157 assert (_capacity > 0);
158 assert (len <= _capacity);
160 Sample* const dst_raw = _data + dst_offset;
162 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
164 _silent = (_silent && gain_coeff == 0);
168 /** Accumulate (add) \param len samples from the start of \param src into self at \param dst_offset
169 * using a linear gain ramp from \param initial to \param target .
171 void accumulate_with_ramped_gain_from (const Sample* src, samplecnt_t len, gain_t initial, gain_t target, sampleoffset_t dst_offset = 0)
173 assert (_capacity > 0);
174 assert (len <= _capacity);
176 if (initial == 0 && target == 0) {
180 Sample* dst = _data + dst_offset;
181 gain_t gain_delta = (target - initial) / len;
183 for (samplecnt_t n = 0; n < len; ++n) {
184 *dst++ += (*src++ * initial);
185 initial += gain_delta;
188 _silent = (_silent && initial == 0 && target == 0);
192 /** Apply a fixed gain factor to the audio buffer
194 * @param gain gain factor
195 * @param len number of samples to amplify
197 void apply_gain (gain_t gain, samplecnt_t len)
200 memset (_data, 0, sizeof (Sample) * len);
201 if (len == _capacity) {
206 apply_gain_to_buffer (_data, len, gain);
209 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
211 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
213 void set_data (Sample* data, size_t size)
215 assert (!_owns_data); // prevent leaks
222 /** Reallocate the buffer used internally to handle at least \param nframes of data
224 * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
226 void resize (size_t nframes);
228 const Sample* data (samplecnt_t offset = 0) const
230 assert (offset <= _capacity);
231 return _data + offset;
234 Sample* data (samplecnt_t offset = 0)
236 assert (offset <= _capacity);
238 return _data + offset;
241 /** Check buffer for silence
243 * @param nframes number of samples to check
244 * @param n first non zero sample (if any)
245 * @return true if all samples are zero
247 bool check_silence (pframes_t nframes, pframes_t& n) const;
258 bool written () const { return _written; }
259 void set_written (bool w) { _written = w; }
264 Sample* _data; ///< Actual buffer contents
267 } // namespace ARDOUR
269 #endif // __ardour_audio_audio_buffer_h__