Fix broken whitespace. I'd apologize for the compile times if it was my fault :D
[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 /** Buffer containing audio data. */
28 class AudioBuffer : public Buffer
29 {
30 public:
31         AudioBuffer(size_t capacity);
32         ~AudioBuffer();
33
34         void silence (framecnt_t len, framecnt_t offset = 0) {
35                 if (!_silent) {
36                         assert(_capacity > 0);
37                         assert(offset + len <= _capacity);
38                         memset(_data + offset, 0, sizeof (Sample) * len);
39                         if (len == _capacity) {
40                                 _silent = true;
41                         }
42                 }
43                 _written = true;
44         }
45
46         /** Read @a len frames @a src starting at @a src_offset into self starting at @ dst_offset*/
47         void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
48                 assert(&src != this);
49                 assert(_capacity > 0);
50                 assert(src.type() == DataType::AUDIO);
51                 assert(len <= _capacity);
52                 memcpy(_data + dst_offset, ((AudioBuffer&)src).data() + src_offset, sizeof(Sample) * len);
53                 if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
54                         _silent = src.silent();
55                 } else {
56                         _silent = _silent && src.silent();
57                 }
58                 _written = true;
59         }
60
61         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
62         void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
63                 const AudioBuffer* ab = dynamic_cast<const AudioBuffer*>(&src);
64                 assert (ab);
65                 accumulate_from (*ab, len, dst_offset, src_offset);
66         }
67
68         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @a dst_offset */
69         void accumulate_from (const AudioBuffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) {
70                 assert(_capacity > 0);
71                 assert(len <= _capacity);
72
73                 Sample*       const dst_raw = _data + dst_offset;
74                 const Sample* const src_raw = src.data() + src_offset;
75
76                 mix_buffers_no_gain(dst_raw, src_raw, len);
77
78                 _silent = (src.silent() && _silent);
79                 _written = true;
80         }
81
82         /** Acumulate (add) @a len frames @a src starting at @a src_offset into self starting at @dst_offset
83          * scaling by @a gain_coeff */
84         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
86                 assert(_capacity > 0);
87                 assert(len <= _capacity);
88
89                 if (src.silent()) {
90                         return;
91                 }
92
93                 Sample*       const dst_raw = _data + dst_offset;
94                 const Sample* const src_raw = src.data() + src_offset;
95
96                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
97
98                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
99                 _written = true;
100         }
101
102         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
103          * scaling by @a gain_coeff */
104         void accumulate_with_gain_from (const Sample* src_raw, framecnt_t len, gain_t gain_coeff, framecnt_t dst_offset = 0) {
105
106                 assert(_capacity > 0);
107                 assert(len <= _capacity);
108
109                 Sample*       const dst_raw = _data + dst_offset;
110
111                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
112
113                 _silent = (_silent && gain_coeff == 0);
114                 _written = true;
115         }
116
117         /** Accumulate (add) @a len frames FROM THE START OF @a src into self
118          * scaling by @a gain_coeff */
119         void accumulate_with_ramped_gain_from (const Sample* src, framecnt_t len, gain_t initial, gain_t target, framecnt_t dst_offset = 0) {
120
121                 assert(_capacity > 0);
122                 assert(len <= _capacity);
123
124                 Sample* dst = _data + dst_offset;
125                 gain_t  gain_delta = (target - initial)/len;
126
127                 for (framecnt_t n = 0; n < len; ++n) {
128                         *dst++ += (*src++ * initial);
129                         initial += gain_delta;
130                 }
131
132                 _silent = (_silent && initial == 0 && target == 0);
133                 _written = true;
134         }
135
136         void apply_gain (gain_t gain, framecnt_t len) {
137                 apply_gain_to_buffer (_data, len, gain);
138         }
139
140         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
141          *
142          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
143          */
144         void set_data (Sample* data, size_t size) {
145                 assert(!_owns_data); // prevent leaks
146                 _capacity = size;
147                 _size = size;
148                 _data = data;
149                 _silent = false;
150                 _written = false;
151         }
152
153         /** Reallocate the buffer used internally to handle at least @nframes of data
154          *
155          * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
156          */
157         void resize (size_t nframes);
158
159
160         const Sample* data (framecnt_t offset = 0) const {
161                 assert(offset <= _capacity);
162                 return _data + offset;
163         }
164
165         Sample* data (framecnt_t offset = 0) {
166                 assert(offset <= _capacity);
167                 return _data + offset;
168         }
169
170         void prepare () { _written = false; _silent = false; }
171         bool written() const { return _written; }
172
173   private:
174         bool    _owns_data;
175         bool    _written;
176         Sample* _data; ///< Actual buffer contents
177 };
178
179
180 } // namespace ARDOUR
181
182 #endif // __ardour_audio_audio_buffer_h__