remove offset from process callback tree. some breakage may have occured. yes, really.
[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 (nframes_t len, nframes_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, nframes_t len, nframes_t dst_offset = 0, nframes_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 (dst_offset == 0 && src_offset == 0 && len == _capacity) {
53                         _silent = src.silent();
54                 } else {
55                         _silent = _silent && src.silent();
56                 }
57                 _written = true;
58         }
59         
60         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
61         void accumulate_from (const AudioBuffer& src, nframes_t len, nframes_t dst_offset = 0, nframes_t src_offset = 0) {
62                 assert(_capacity > 0);
63                 assert(len <= _capacity);
64
65                 Sample*       const dst_raw = _data + dst_offset;
66                 const Sample* const src_raw = src.data() + src_offset;
67
68                 mix_buffers_no_gain(dst_raw, src_raw, len);
69
70                 _silent = (src.silent() && _silent);
71                 _written = true;
72         }
73         
74         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @ dst_offset
75          * scaling by @a gain_coeff */
76         void accumulate_with_gain_from (const AudioBuffer& src, nframes_t len, gain_t gain_coeff, nframes_t dst_offset = 0, nframes_t src_offset = 0) {
77
78                 assert(_capacity > 0);
79                 assert(len <= _capacity);
80
81                 if (src.silent()) {
82                         return;
83                 }
84
85                 Sample*       const dst_raw = _data + dst_offset;
86                 const Sample* const src_raw = src.data() + src_offset;
87
88                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
89
90                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
91                 _written = true;
92         }
93
94         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
95          * scaling by @a gain_coeff */
96         void accumulate_with_gain_from (const Sample* src_raw, nframes_t len, gain_t gain_coeff, nframes_t dst_offset = 0) {
97
98                 assert(_capacity > 0);
99                 assert(len <= _capacity);
100
101                 Sample*       const dst_raw = _data + dst_offset;
102
103                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
104
105                 _silent = (_silent && gain_coeff == 0);
106                 _written = true;
107         }
108         
109         void apply_gain (gain_t gain, nframes_t len) {
110                 apply_gain_to_buffer (_data, len, gain);
111         }
112
113         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
114          * 
115          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
116          */
117         void set_data (Sample* data, size_t size) {
118                 assert(!_owns_data); // prevent leaks
119                 _capacity = size;
120                 _size = size;
121                 _data = data;
122                 _silent = false;
123                 _written = false;
124         }
125
126         /** Reallocate the buffer used internally to handle at least @nframes of data
127          * 
128          * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
129          */
130         void resize (size_t nframes);
131
132
133         const Sample* data (nframes_t offset = 0) const { 
134                 assert(offset <= _capacity); 
135                 return _data + offset; 
136         }
137
138         Sample* data (nframes_t offset = 0) { 
139                 assert(offset <= _capacity); 
140                 return _data + offset; 
141         }
142
143         void prepare () { _written = false; }
144         bool written() const { return _written; }
145
146   private:
147         bool    _owns_data;
148         bool    _written;
149         Sample* _data; ///< Actual buffer contents
150         
151 };
152
153
154 } // namespace ARDOUR
155
156 #endif // __ardour_audio_audio_buffer_h__