fix all manner of wrongness with port buffer offsets
[ardour.git] / libs / ardour / ardour / audio_buffer.h
1 /*
2     Copyright (C) 2006 Paul Davis
3
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)
7     any later version.
8
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
12     for more details.
13
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.
17 */
18
19 #ifndef __ardour_audio_buffer_h__
20 #define __ardour_audio_buffer_h__
21
22 #include <cstring>
23 #include "ardour/buffer.h"
24
25 namespace ARDOUR {
26
27 class AudioBuffer : public Buffer
28 {
29 public:
30         AudioBuffer(size_t capacity);
31         ~AudioBuffer();
32
33         void silence (framecnt_t len, framecnt_t offset = 0) {
34                 if (!_silent) {
35                         assert(_capacity > 0);
36                         assert(offset + len <= _capacity);
37                         memset(_data + offset, 0, sizeof (Sample) * len);
38                         if (len == _capacity) {
39                                 _silent = true;
40                         }
41                 }
42                 _written = true;
43         }
44
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) {
47                 assert(&src != this);
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 (next_write_addr >= _data + _capacity) {
53                         next_write_addr = _data;
54                 }
55                 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
56                         _silent = src.silent();
57                 } else {
58                         _silent = _silent && src.silent();
59                 }
60                 _written = true;
61         }
62
63         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
64         void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
65                 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
66                 assert (ab);
67                 accumulate_from (*ab, len, dst_offset, src_offset);
68         }
69
70         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
71         void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
72                 assert(_capacity > 0);
73                 assert(len <= _capacity);
74
75                 Sample*       const dst_raw = _data + dst_offset;
76                 const Sample* const src_raw = src.data() + src_offset;
77
78                 mix_buffers_no_gain(dst_raw, src_raw, len);
79
80                 _silent = (src.silent() && _silent);
81                 _written = true;
82         }
83
84         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset
85          * scaling by @a gain_coeff */
86         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) {
87
88                 assert(_capacity > 0);
89                 assert(len <= _capacity);
90
91                 if (src.silent()) {
92                         return;
93                 }
94
95                 Sample*       const dst_raw = _data + dst_offset;
96                 const Sample* const src_raw = src.data() + src_offset;
97
98                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
99
100                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
101                 _written = true;
102         }
103
104         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
105          * scaling by @a gain_coeff */
106         void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
107
108                 assert(_capacity > 0);
109                 assert(len <= _capacity);
110
111                 Sample*       const dst_raw = _data + dst_offset;
112
113                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
114
115                 _silent = (_silent && gain_coeff == 0);
116                 _written = true;
117         }
118
119         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
120          * scaling by @a gain_coeff */
121         void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
122
123                 assert(_capacity > 0);
124                 assert(len <= _capacity);
125
126                 Sample* dst = _data + dst_offset;
127                 gain_t  gain_delta = (target - initial)/len;
128
129                 for (framecnt_t n = 0; n < len; ++n) {
130                         *dst++ += (*src++ * initial);
131                         initial += gain_delta;
132                 }
133
134                 _silent = (_silent && initial == 0 && target == 0);
135                 _written = true;
136         }
137
138         void apply_gain (gain_t gain, framecnt_t len) {
139                 apply_gain_to_buffer (_data, len, gain);
140         }
141
142         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
143          *
144          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
145          */
146         void set_data (Sample* data, size_t size) {
147                 assert(!_owns_data); // prevent leaks
148                 _capacity = size;
149                 _size = size;
150                 _data = data;
151                 _silent = false;
152                 _written = false;
153         }
154
155         /** Reallocate the buffer used internally to handle at least @nframes of data
156          *
157          * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
158          */
159         void resize (size_t nframes);
160
161
162         const Sample* data (framecnt_t offset = 0) const {
163                 assert(offset <= _capacity);
164                 return _data + offset;
165         }
166
167         Sample* data (framecnt_t offset = 0) {
168                 assert(offset <= _capacity);
169                 return _data + offset;
170         }
171
172         void prepare () { _written = false; }
173         bool written() const { return _written; }
174
175         void set_marked_for_write (bool yn) { mfw = yn; }
176
177   private:
178         bool    _owns_data;
179         bool    _written;
180         Sample* _data; ///< Actual buffer contents
181 };
182
183
184 } // namespace ARDOUR
185
186 #endif // __ardour_audio_audio_buffer_h__