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);
36 void silence (framecnt_t len, framecnt_t offset = 0);
38 /** Read @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
39 void read_from (const Sample* src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
41 assert(_capacity > 0);
42 assert(len <= _capacity);
43 memcpy(_data + dst_offset, src + src_offset, sizeof(Sample) * len);
48 void read_from_with_gain (const Sample* src, framecnt_t len, gain_t gain, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
50 assert(_capacity > 0);
51 assert(len <= _capacity);
53 for (framecnt_t n = 0; n < len; ++n) {
54 _data[dst_offset+n] = src[n] * gain;
60 /** Read @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
61 void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
63 assert(_capacity > 0);
64 assert(src.type() == DataType::AUDIO);
65 assert(dst_offset + len <= _capacity);
66 assert( src_offset <= ((framecnt_t) src.capacity()-len));
67 memcpy(_data + dst_offset, ((const AudioBuffer&)src).data() + src_offset, sizeof(Sample) * len);
68 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
69 _silent = src.silent();
71 _silent = _silent && src.silent();
76 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
77 void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
78 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
80 accumulate_from (*ab, len, dst_offset, src_offset);
83 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
84 void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
85 assert(_capacity > 0);
86 assert(len <= _capacity);
88 Sample* const dst_raw = _data + dst_offset;
89 const Sample* const src_raw = src.data() + src_offset;
91 mix_buffers_no_gain(dst_raw, src_raw, len);
93 _silent = (src.silent() && _silent);
97 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
98 void accumulate_from (const Sample* src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_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 + src_offset;
105 mix_buffers_no_gain(dst_raw, src_raw, len);
111 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @dst_offset
112 * scaling by @a gain_coeff */
113 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) {
115 assert(_capacity > 0);
116 assert(len <= _capacity);
122 Sample* const dst_raw = _data + dst_offset;
123 const Sample* const src_raw = src.data() + src_offset;
125 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
127 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
131 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
132 * scaling by @a gain_coeff */
133 void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
135 assert(_capacity > 0);
136 assert(len <= _capacity);
138 Sample* const dst_raw = _data + dst_offset;
140 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
142 _silent = (_silent && gain_coeff == 0);
146 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
147 * scaling by @a gain_coeff */
148 void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
150 assert(_capacity > 0);
151 assert(len <= _capacity);
153 Sample* dst = _data + dst_offset;
154 gain_t gain_delta = (target - initial)/len;
156 for (framecnt_t n = 0; n < len; ++n) {
157 *dst++ += (*src++ * initial);
158 initial += gain_delta;
161 _silent = (_silent && initial == 0 && target == 0);
165 void apply_gain (gain_t gain, framecnt_t len) {
166 apply_gain_to_buffer (_data, len, gain);
169 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
171 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
173 void set_data (Sample* data, size_t size) {
174 assert(!_owns_data); // prevent leaks
181 /** Reallocate the buffer used internally to handle at least @nframes of data
183 * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
185 void resize (size_t nframes);
187 const Sample* data (framecnt_t offset = 0) const {
188 assert(offset <= _capacity);
189 return _data + offset;
192 Sample* data (framecnt_t offset = 0) {
193 assert(offset <= _capacity);
195 return _data + offset;
198 bool check_silence (pframes_t, pframes_t&) const;
200 void prepare () { _written = false; _silent = false; }
201 bool written() const { return _written; }
202 void set_written(bool w) { _written = w; }
207 Sample* _data; ///< Actual buffer contents
211 } // namespace ARDOUR
213 #endif // __ardour_audio_audio_buffer_h__