Ardour  8.12
audio_buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2016 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef __ardour_audio_buffer_h__
23 #define __ardour_audio_buffer_h__
24 
25 #include <cstring>
26 
27 #include "ardour/buffer.h"
29 
30 namespace ARDOUR
31 {
34 {
35 public:
36  AudioBuffer (size_t capacity);
38 
43  void silence (samplecnt_t len, samplecnt_t offset = 0);
44 
45  bool silent_data() const;
46 
53  void read_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
54  {
55  assert (src != 0);
56  assert (_capacity > 0);
57  assert (len <= _capacity);
58  copy_vector (_data + dst_offset, src + src_offset, len);
59  _silent = false;
60  _written = true;
61  }
62 
69  void read_from (const Buffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
70  {
71  assert (&src != this);
72  assert (_capacity > 0);
73  assert (src.type () == DataType::AUDIO);
74  assert (dst_offset + len <= _capacity);
75  assert (src_offset <= ((samplecnt_t)src.capacity () - len));
76 
77  if (src.silent ()) {
78  memset (_data + dst_offset, 0, sizeof (Sample) * len);
79  } else {
80  copy_vector (_data + dst_offset, ((const AudioBuffer&)src).data () + src_offset, len);
81  }
82 
83  if (dst_offset == 0 && src_offset == 0 && len == _capacity) {
84  _silent = src.silent ();
85  } else {
86  _silent = _silent && src.silent ();
87  }
88  _written = true;
89  }
90 
92  void merge_from (const Buffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
93  {
94  const AudioBuffer* ab = dynamic_cast<const AudioBuffer*> (&src);
95  assert (ab);
96  accumulate_from (*ab, len, dst_offset, src_offset);
97  }
98 
100  void accumulate_from (const AudioBuffer& src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
101  {
102  assert (_capacity > 0);
103  assert (len <= _capacity);
104  if (src.silent ()) {
105  return;
106  }
107  Sample* const dst_raw = _data + dst_offset;
108  const Sample* const src_raw = src.data () + src_offset;
109 
110  mix_buffers_no_gain (dst_raw, src_raw, len);
111 
112  _silent = (src.silent () && _silent);
113  _written = true;
114  }
115 
119  void accumulate_from (const Sample* src, samplecnt_t len, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
120  {
121  assert (_capacity > 0);
122  assert (len <= _capacity);
123 
124  Sample* const dst_raw = _data + dst_offset;
125  const Sample* const src_raw = src + src_offset;
126 
127  mix_buffers_no_gain (dst_raw, src_raw, len);
128 
129  _silent = false;
130  _written = true;
131  }
132 
136  void accumulate_with_gain_from (const AudioBuffer& src, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0)
137  {
138  assert (_capacity > 0);
139  assert (len <= _capacity);
140 
141  if (src.silent () || gain_coeff == 0) {
142  return;
143  }
144 
145  Sample* const dst_raw = _data + dst_offset;
146  const Sample* const src_raw = src.data () + src_offset;
147 
148  mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
149 
150  _silent = ((src.silent () && _silent) || (_silent && gain_coeff == 0));
151  _written = true;
152  }
153 
157  void accumulate_with_gain_from (const Sample* src_raw, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset = 0)
158  {
159  assert (_capacity > 0);
160  assert (len <= _capacity);
161 
162  Sample* const dst_raw = _data + dst_offset;
163 
164  mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
165 
166  _silent = (_silent && gain_coeff == 0);
167  _written = true;
168  }
169 
173  void accumulate_with_ramped_gain_from (const Sample* src, samplecnt_t len, gain_t initial, gain_t target, sampleoffset_t dst_offset = 0)
174  {
175  assert (_capacity > 0);
176  assert (len <= _capacity);
177 
178  if (initial == 0 && target == 0) {
179  return;
180  }
181 
182  Sample* dst = _data + dst_offset;
183  gain_t gain_delta = (target - initial) / len;
184 
185  for (samplecnt_t n = 0; n < len; ++n) {
186  *dst++ += (*src++ * initial);
187  initial += gain_delta;
188  }
189 
190  _silent = (_silent && initial == 0 && target == 0);
191  _written = true;
192  }
193 
200  {
201  if (gain == 0) {
202  memset (_data, 0, sizeof (Sample) * len);
203  if (len == _capacity) {
204  _silent = true;
205  }
206  return;
207  }
208  apply_gain_to_buffer (_data, len, gain);
209  }
210 
215  void set_data (Sample* data, size_t size)
216  {
217  assert (!_owns_data); // prevent leaks
218  _capacity = size;
219  _data = data;
220  _silent = false;
221  _written = false;
222  }
223 
228  void resize (size_t nframes);
229 
230  const Sample* data (samplecnt_t offset = 0) const
231  {
232  assert (offset <= _capacity);
233  return _data + offset;
234  }
235 
236  Sample* data (samplecnt_t offset = 0)
237  {
238  assert (offset <= _capacity);
239  _silent = false;
240  return _data + offset;
241  }
242 
249  bool check_silence (pframes_t nframes, pframes_t& n) const;
250 
251  void prepare ()
252  {
253  if (!_owns_data) {
254  _data = 0;
255  }
256  _written = false;
257  _silent = false;
258  }
259 
260  bool written () const { return _written; }
261  void set_written (bool w) { _written = w; }
262 
263 private:
265  bool _written;
267 };
268 
269 } // namespace ARDOUR
270 
271 #endif // __ardour_audio_audio_buffer_h__
Sample * _data
Actual buffer contents.
Definition: audio_buffer.h:266
Sample * data(samplecnt_t offset=0)
Definition: audio_buffer.h:236
bool silent_data() const
bool check_silence(pframes_t nframes, pframes_t &n) const
void read_from(const Buffer &src, samplecnt_t len, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:69
AudioBuffer(size_t capacity)
void set_data(Sample *data, size_t size)
Definition: audio_buffer.h:215
void apply_gain(gain_t gain, samplecnt_t len)
Definition: audio_buffer.h:199
void accumulate_with_gain_from(const AudioBuffer &src, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:136
void merge_from(const Buffer &src, samplecnt_t len, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:92
const Sample * data(samplecnt_t offset=0) const
Definition: audio_buffer.h:230
void set_written(bool w)
Definition: audio_buffer.h:261
void accumulate_from(const Sample *src, samplecnt_t len, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:119
void accumulate_with_gain_from(const Sample *src_raw, samplecnt_t len, gain_t gain_coeff, sampleoffset_t dst_offset=0)
Definition: audio_buffer.h:157
void silence(samplecnt_t len, samplecnt_t offset=0)
void accumulate_from(const AudioBuffer &src, samplecnt_t len, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:100
void accumulate_with_ramped_gain_from(const Sample *src, samplecnt_t len, gain_t initial, gain_t target, sampleoffset_t dst_offset=0)
Definition: audio_buffer.h:173
void read_from(const Sample *src, samplecnt_t len, sampleoffset_t dst_offset=0, sampleoffset_t src_offset=0)
Definition: audio_buffer.h:53
void resize(size_t nframes)
bool written() const
Definition: audio_buffer.h:260
DataType type() const
Definition: buffer.h:58
bool silent() const
Definition: buffer.h:60
size_t capacity() const
Definition: buffer.h:54
#define LIBARDOUR_API
void memset(float *data, const float val, const uint32_t n_samples)
PBD::PropertyDescriptor< gain_t > gain
uint32_t pframes_t
Temporal::samplecnt_t samplecnt_t
apply_gain_to_buffer_t apply_gain_to_buffer
mix_buffers_with_gain_t mix_buffers_with_gain
Temporal::sampleoffset_t sampleoffset_t
mix_buffers_no_gain_t mix_buffers_no_gain
copy_vector_t copy_vector