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__
23 #include "ardour/buffer.h"
27 class AudioBuffer : public Buffer
30 AudioBuffer(size_t capacity);
33 void silence (framecnt_t len, framecnt_t offset = 0) {
35 assert(_capacity > 0);
36 assert(offset + len <= _capacity);
37 memset(_data + offset, 0, sizeof (Sample) * len);
38 if (len == _capacity) {
45 /** Read @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
46 void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
48 assert(_capacity > 0);
49 assert(src.type() == DataType::AUDIO);
50 assert(len <= _capacity);
51 memcpy(_data + dst_offset, ((AudioBuffer&)src).data() + src_offset, sizeof(Sample) * len);
52 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
53 _silent = src.silent();
55 _silent = _silent && src.silent();
60 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
61 void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
62 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
64 accumulate_from (*ab, len, dst_offset, src_offset);
67 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
68 void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
69 assert(_capacity > 0);
70 assert(len <= _capacity);
72 Sample* const dst_raw = _data + dst_offset;
73 const Sample* const src_raw = src.data() + src_offset;
75 mix_buffers_no_gain(dst_raw, src_raw, len);
77 _silent = (src.silent() && _silent);
81 /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset
82 * scaling by @a gain_coeff */
83 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) {
85 assert(_capacity > 0);
86 assert(len <= _capacity);
92 Sample* const dst_raw = _data + dst_offset;
93 const Sample* const src_raw = src.data() + src_offset;
95 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
97 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
101 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
102 * scaling by @a gain_coeff */
103 void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
105 assert(_capacity > 0);
106 assert(len <= _capacity);
108 Sample* const dst_raw = _data + dst_offset;
110 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
112 _silent = (_silent && gain_coeff == 0);
116 /** Accumulate (add) @a len frames FROM THE START OF @a src into self
117 * scaling by @a gain_coeff */
118 void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
120 assert(_capacity > 0);
121 assert(len <= _capacity);
123 Sample* dst = _data + dst_offset;
124 gain_t gain_delta = (target - initial)/len;
126 for (framecnt_t n = 0; n < len; ++n) {
127 *dst++ += (*src++ * initial);
128 initial += gain_delta;
131 _silent = (_silent && initial == 0 && target == 0);
135 void apply_gain (gain_t gain, framecnt_t len) {
136 apply_gain_to_buffer (_data, len, gain);
139 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
141 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
143 void set_data (Sample* data, size_t size) {
144 assert(!_owns_data); // prevent leaks
152 /** Reallocate the buffer used internally to handle at least @nframes of data
154 * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
156 void resize (size_t nframes);
159 const Sample* data (framecnt_t offset = 0) const {
160 assert(offset <= _capacity);
161 return _data + offset;
164 Sample* data (framecnt_t offset = 0) {
165 assert(offset <= _capacity);
166 return _data + offset;
169 void prepare () { _written = false; _silent = false; }
170 bool written() const { return _written; }
175 Sample* _data; ///< Actual buffer contents
179 } // namespace ARDOUR
181 #endif // __ardour_audio_audio_buffer_h__