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 AudioBuffer : public Buffer
33 AudioBuffer(size_t capacity);
36 void silence (framecnt_t len, framecnt_t offset = 0) {
38 assert(_capacity > 0);
39 assert(offset + len <= _capacity);
40 memset(_data + offset, 0, sizeof (Sample) * len);
41 if (len == _capacity) {
48 /** Read @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
49 void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
51 assert(_capacity > 0);
52 assert(src.type() == DataType::AUDIO);
53 assert(len <= _capacity);
54 assert( src_offset <= ((framecnt_t) src.capacity()-len));
55 memcpy(_data + dst_offset, ((AudioBuffer&)src).data() + src_offset, sizeof(Sample) * len);
56 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
57 _silent = src.silent();
59 _silent = _silent && src.silent();
64 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
65 void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
66 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
68 accumulate_from (*ab, len, dst_offset, src_offset);
71 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
72 void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
73 assert(_capacity > 0);
74 assert(len <= _capacity);
76 Sample* const dst_raw = _data + dst_offset;
77 const Sample* const src_raw = src.data() + src_offset;
79 mix_buffers_no_gain(dst_raw, src_raw, len);
81 _silent = (src.silent() && _silent);
85 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @dst_offset
86 * scaling by @a gain_coeff */
87 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) {
89 assert(_capacity > 0);
90 assert(len <= _capacity);
96 Sample* const dst_raw = _data + dst_offset;
97 const Sample* const src_raw = src.data() + src_offset;
99 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
101 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
105 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
106 * scaling by @a gain_coeff */
107 void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
109 assert(_capacity > 0);
110 assert(len <= _capacity);
112 Sample* const dst_raw = _data + dst_offset;
114 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
116 _silent = (_silent && gain_coeff == 0);
120 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
121 * scaling by @a gain_coeff */
122 void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
124 assert(_capacity > 0);
125 assert(len <= _capacity);
127 Sample* dst = _data + dst_offset;
128 gain_t gain_delta = (target - initial)/len;
130 for (framecnt_t n = 0; n < len; ++n) {
131 *dst++ += (*src++ * initial);
132 initial += gain_delta;
135 _silent = (_silent && initial == 0 && target == 0);
139 void apply_gain (gain_t gain, framecnt_t len) {
140 apply_gain_to_buffer (_data, len, gain);
143 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
145 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
147 void set_data (Sample* data, size_t size) {
148 assert(!_owns_data); // prevent leaks
156 /** Reallocate the buffer used internally to handle at least @nframes of data
158 * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
160 void resize (size_t nframes);
162 bool empty() const { return _size == 0; }
164 const Sample* data (framecnt_t offset = 0) const {
165 assert(offset <= _capacity);
166 return _data + offset;
169 Sample* data (framecnt_t offset = 0) {
170 assert(offset <= _capacity);
171 return _data + offset;
174 void prepare () { _written = false; _silent = false; }
175 bool written() const { return _written; }
176 void set_written(bool w) { _written = w; }
181 Sample* _data; ///< Actual buffer contents
185 } // namespace ARDOUR
187 #endif // __ardour_audio_audio_buffer_h__