2 Copyright (C) 2006 Paul Davis
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)
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
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.
19 #ifndef __ardour_audio_buffer_h__
20 #define __ardour_audio_buffer_h__
24 #include "ardour/buffer.h"
25 #include "ardour/runtime_functions.h"
29 /** Buffer containing audio data. */
30 class LIBARDOUR_API AudioBuffer : public Buffer
33 AudioBuffer(size_t capacity);
37 * @param len number of samples to clear
38 * @laram offset start offset
40 void silence (samplecnt_t len, samplecnt_t offset = 0);
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
48 void read_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
50 assert(_capacity > 0);
51 assert(len <= _capacity);
52 memcpy(_data + dst_offset, src + src_offset, sizeof(Sample) * len);
57 void read_from_with_gain (const Sample* src, samplecnt_t len, gain_t gain, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
59 assert(_capacity > 0);
60 assert(len <= _capacity);
62 for (samplecnt_t n = 0; n < len; ++n) {
63 _data[dst_offset+n] = src[n] * gain;
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
75 void read_from (const Buffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
77 assert(_capacity > 0);
78 assert(src.type() == DataType::AUDIO);
79 assert(dst_offset + len <= _capacity);
80 assert( src_offset <= ((samplecnt_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();
85 _silent = _silent && src.silent();
90 /** Accumulate (add) @a len samples @a src starting at @a src_offset into self starting at @a dst_offset */
91 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) @a len samples @a src starting at @a src_offset into self starting at @a dst_offset */
98 void accumulate_from (const AudioBuffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
99 assert(_capacity > 0);
100 assert(len <= _capacity);
102 Sample* const dst_raw = _data + dst_offset;
103 const Sample* const src_raw = src.data() + src_offset;
105 mix_buffers_no_gain(dst_raw, src_raw, len);
107 _silent = (src.silent() && _silent);
111 /** Accumulate (add) @a len samples @a src starting at @a src_offset into self starting at @a dst_offset */
112 void accumulate_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
113 assert(_capacity > 0);
114 assert(len <= _capacity);
116 Sample* const dst_raw = _data + dst_offset;
117 const Sample* const src_raw = src + src_offset;
119 mix_buffers_no_gain(dst_raw, src_raw, len);
125 /** Accumulate (add) @a len samples @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, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0) {
129 assert(_capacity > 0);
130 assert(len <= _capacity);
136 Sample* const dst_raw = _data + dst_offset;
137 const Sample* const src_raw = src.data() + src_offset;
139 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
141 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
145 /** Accumulate (add) @a len samples FROM THE START OF @a src into self
146 * scaling by @a gain_coeff */
147 void accumulate_with_gain_from (const Sample* src_raw, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0) {
149 assert(_capacity > 0);
150 assert(len <= _capacity);
152 Sample* const dst_raw = _data + dst_offset;
154 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
156 _silent = (_silent && gain_coeff == 0);
160 /** Accumulate (add) @a len samples FROM THE START OF @a src into self
161 * scaling by @a gain_coeff */
162 void accumulate_with_ramped_gain_from (const Sample* src, samplecnt_t len, gain_t initial, gain_t target, sampleoffset_t dst_offset = 0) {
164 assert(_capacity > 0);
165 assert(len <= _capacity);
167 Sample* dst = _data + dst_offset;
168 gain_t gain_delta = (target - initial)/len;
170 for (samplecnt_t n = 0; n < len; ++n) {
171 *dst++ += (*src++ * initial);
172 initial += gain_delta;
175 _silent = (_silent && initial == 0 && target == 0);
179 /** apply a fixed gain factor to the audio buffer
180 * @param gain gain factor
181 * @param len number of samples to amplify
183 void apply_gain (gain_t gain, samplecnt_t len) {
184 apply_gain_to_buffer (_data, len, gain);
187 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
189 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
191 void set_data (Sample* data, size_t size) {
192 assert(!_owns_data); // prevent leaks
199 /** Reallocate the buffer used internally to handle at least @nframes of data
201 * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
203 void resize (size_t nframes);
205 const Sample* data (samplecnt_t offset = 0) const {
206 assert(offset <= _capacity);
207 return _data + offset;
210 Sample* data (samplecnt_t offset = 0) {
211 assert(offset <= _capacity);
213 return _data + offset;
216 /** check buffer for silence
217 * @param nframes number of samples to check
218 * @param n first non zero sample (if any)
219 * @return true if all samples are zero
221 bool check_silence (pframes_t nframes, pframes_t& n) const;
230 bool written() const { return _written; }
231 void set_written(bool w) { _written = w; }
236 Sample* _data; ///< Actual buffer contents
240 } // namespace ARDOUR
242 #endif // __ardour_audio_audio_buffer_h__