ardour
buffer_set.cc
Go to the documentation of this file.
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 
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23 
24 #include <iostream>
25 #include <algorithm>
26 #include <sstream>
27 
28 #include "pbd/compose.h"
29 #include "pbd/failed_constructor.h"
30 
31 #include "ardour/buffer.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/debug.h"
34 #include "ardour/midi_buffer.h"
35 #include "ardour/port.h"
36 #include "ardour/port_set.h"
37 #include "ardour/uri_map.h"
38 #ifdef LV2_SUPPORT
39 #include "ardour/lv2_plugin.h"
40 #include "lv2_evbuf.h"
41 #endif
42 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
44 #endif
45 
46 namespace ARDOUR {
47 
50  : _is_mirror(false)
51 {
52  for (size_t i=0; i < DataType::num_types; ++i) {
53  _buffers.push_back(BufferVec());
54  }
55 
56  _count.reset();
57  _available.reset();
58 }
59 
61 {
62  clear();
63 }
64 
67 void
69 {
70  if (!_is_mirror) {
71  for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
72  for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
73  delete *j;
74  }
75  (*i).clear();
76  }
77  }
78  _buffers.clear();
79  _count.reset();
80  _available.reset();
81 
82 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
83  for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
84  delete *i;
85  }
86 
87  _vst_buffers.clear ();
88 #endif
89 
90 }
91 
98 void
100 {
101  const ChanCount& count (ports.count());
102 
103  clear ();
104 
105  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
106  _buffers.push_back (BufferVec());
107  BufferVec& v = _buffers[*t];
108  v.assign (count.n (*t), (Buffer*) 0);
109  }
110 
111  _count = ports.count();
112  _available = ports.count();
113 
114 
115  _is_mirror = true;
116 }
117 
123 void
125 {
126  assert (_count == ports.count ());
127  assert (_available == ports.count ());
128  assert (_is_mirror);
129 
130  assert (_buffers.size() == DataType::num_types);
131 
132  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
133  BufferVec& v = _buffers[*t];
134 
135  assert (v.size() == ports.num_ports (*t));
136 
137  int i = 0;
138  for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
139  v[i] = &p->get_buffer (nframes);
140  ++i;
141  }
142  }
143 }
144 
148 void
149 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
150 {
151  assert(type != DataType::NIL);
152  assert(type < _buffers.size());
153 
154  if (num_buffers == 0) {
155  return;
156  }
157 
158  // The vector of buffers of the type we care about
159  BufferVec& bufs = _buffers[type];
160 
161  // If we're a mirror just make sure we're ok
162  if (_is_mirror) {
163  assert(_count.get(type) >= num_buffers);
164  assert(bufs[0]->type() == type);
165  return;
166  }
167 
168  // If there's not enough or they're too small, just nuke the whole thing and
169  // rebuild it (so I'm lazy..)
170  if (bufs.size() < num_buffers
171  || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
172 
173  // Nuke it
174  for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
175  delete (*i);
176  }
177  bufs.clear();
178 
179  // Rebuild it
180  for (size_t i = 0; i < num_buffers; ++i) {
181  bufs.push_back(Buffer::create(type, buffer_capacity));
182  }
183 
184  _available.set(type, num_buffers);
185  _count.set (type, num_buffers);
186  }
187 
188 #ifdef LV2_SUPPORT
189  // Ensure enough low level MIDI format buffers are available for conversion
190  // in both directions (input & output, out-of-place)
191  if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
192  while (_lv2_buffers.size() < _buffers[type].size() * 2) {
193  _lv2_buffers.push_back(
194  std::make_pair(false, lv2_evbuf_new(buffer_capacity,
196  URIMap::instance().urids.atom_Chunk,
198  }
199  }
200 #endif
201 
202 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
203  // As above but for VST
204  if (type == DataType::MIDI) {
205  while (_vst_buffers.size() < _buffers[type].size()) {
206  _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
207  }
208  }
209 #endif
210 
211  // Post-conditions
212  assert(bufs[0]->type() == type);
213  assert(bufs.size() >= num_buffers);
214  assert(bufs.size() == _available.get(type));
215  assert(bufs[0]->capacity() >= buffer_capacity);
216 }
217 
221 void
222 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
223 {
224  for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
225  ensure_buffers (*i, chns.get (*i), buffer_capacity);
226  }
227 }
228 
233 size_t
235 {
236  assert(_available.get(type) > 0);
237  return _buffers[type][0]->capacity();
238 }
239 
240 Buffer&
241 BufferSet::get(DataType type, size_t i)
242 {
243  assert(i < _available.get(type));
244  return *_buffers[type][i];
245 }
246 
247 const Buffer&
248 BufferSet::get(DataType type, size_t i) const
249 {
250  assert(i < _available.get(type));
251  return *_buffers[type][i];
252 }
253 
254 #ifdef LV2_SUPPORT
255 
256 void
257 BufferSet::ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity)
258 {
259  assert(count().get(DataType::MIDI) > i);
260 
261  LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
262  LV2_Evbuf* evbuf = b.second;
263 
264  if (lv2_evbuf_get_capacity(evbuf) >= buffer_capacity) return;
265 
266  lv2_evbuf_free(b.second);
267  _lv2_buffers.at(i * 2 + (input ? 0 : 1)) =
268  std::make_pair(false, lv2_evbuf_new(
269  buffer_capacity,
271  URIMap::instance().urids.atom_Chunk,
273 }
274 
275 LV2_Evbuf*
276 BufferSet::get_lv2_midi(bool input, size_t i, bool old_api)
277 {
278  assert(count().get(DataType::MIDI) > i);
279 
280  LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
281  LV2_Evbuf* evbuf = b.second;
282 
284  lv2_evbuf_reset(evbuf, input);
285  return evbuf;
286 }
287 
288 void
289 BufferSet::forward_lv2_midi(LV2_Evbuf* buf, size_t i, bool purge_ardour_buffer)
290 {
291  MidiBuffer& mbuf = get_midi(i);
292  if (purge_ardour_buffer) {
293  mbuf.silence(0, 0);
294  }
295  for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
297  i = lv2_evbuf_next(i)) {
298  uint32_t frames, subframes, type, size;
299  uint8_t* data;
300  lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
301  if (type == URIMap::instance().urids.midi_MidiEvent) {
302  mbuf.push_back(frames, size, data);
303  }
304  }
305 }
306 
307 void
308 BufferSet::flush_lv2_midi(bool input, size_t i)
309 {
310  MidiBuffer& mbuf = get_midi(i);
311  LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
312  LV2_Evbuf* evbuf = b.second;
313 
314  mbuf.silence(0, 0);
315  for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(evbuf);
317  i = lv2_evbuf_next(i)) {
318  uint32_t frames;
319  uint32_t subframes;
320  uint32_t type;
321  uint32_t size;
322  uint8_t* data;
323  lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
324 #ifndef NDEBUG
325  DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
326  for (uint16_t x = 0; x < size; ++x) {
327  DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
328  }
329 #endif
330  if (type == URIMap::instance().urids.midi_MidiEvent) {
331  // TODO: Make Ardour event buffers generic so plugins can communicate
332  mbuf.push_back(frames, size, data);
333  }
334  }
335 }
336 
337 #endif /* LV2_SUPPORT */
338 
339 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
340 
341 VstEvents*
342 BufferSet::get_vst_midi (size_t b)
343 {
344  MidiBuffer& m = get_midi (b);
345  VSTBuffer* vst = _vst_buffers[b];
346 
347  vst->clear ();
348 
349  for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
350  vst->push_back (*i);
351  }
352 
353  return vst->events();
354 }
355 
356 BufferSet::VSTBuffer::VSTBuffer (size_t c)
357  : _capacity (c)
358 {
359  _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
360  _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
361 
362  if (_events == 0 || _midi_events == 0) {
363  free (_events);
364  free (_midi_events);
365  throw failed_constructor ();
366  }
367 
368  _events->numEvents = 0;
369  _events->reserved = 0;
370 }
371 
372 BufferSet::VSTBuffer::~VSTBuffer ()
373 {
374  free (_events);
375  free (_midi_events);
376 }
377 
378 void
379 BufferSet::VSTBuffer::clear ()
380 {
381  _events->numEvents = 0;
382 }
383 
384 void
385 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
386 {
387  if (ev.size() > 3) {
388  /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
389  they won't be passed to VST plugins or VSTis
390  */
391  return;
392  }
393  int const n = _events->numEvents;
394  assert (n < (int) _capacity);
395 
396  _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
397  VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
398 
399  v->type = kVstMidiType;
400  v->byteSize = sizeof (VstMidiEvent);
401  v->deltaFrames = ev.time ();
402 
403  v->flags = 0;
404  v->detune = 0;
405  v->noteLength = 0;
406  v->noteOffset = 0;
407  v->reserved1 = 0;
408  v->reserved2 = 0;
409  v->noteOffVelocity = 0;
410  memcpy (v->midiData, ev.buffer(), ev.size());
411  v->midiData[3] = 0;
412 
413  _events->numEvents++;
414 }
415 
416 #endif /* WINDOWS_VST_SUPPORT */
417 
419 void
421 {
422  assert (available().get (type) >= in.count().get (type));
423 
424  BufferSet::iterator o = begin (type);
425  for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
426  o->read_from (*i, nframes);
427  }
428 
429  _count.set (type, in.count().get (type));
430 }
431 
433 void
435 {
436  assert(available() >= in.count());
437 
438  // Copy all buffers 1:1
439  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
440  read_from (in, nframes, *t);
441  }
442 }
443 
444 void
446 {
447  /* merge all input buffers into out existing buffers.
448 
449  NOTE: if "in" contains more buffers than this set,
450  we will drop the extra buffers.
451 
452  */
453 
454  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
455  BufferSet::iterator o = begin(*t);
456  for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
457  o->merge_from (*i, nframes);
458  }
459  }
460 }
461 
462 void
464 {
465  for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
466  for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
467  (*b)->silence (nframes, offset);
468  }
469  }
470 }
471 
472 } // namespace ARDOUR
473 
char reserved2
Definition: aeffectx.h:172
bool lv2_evbuf_is_valid(LV2_Evbuf_Iterator iter)
Definition: lv2_evbuf.c:152
char noteOffVelocity
Definition: aeffectx.h:168
LV2_Evbuf_Iterator lv2_evbuf_begin(LV2_Evbuf *evbuf)
Definition: lv2_evbuf.c:137
void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
Definition: buffer_set.cc:149
MidiBuffer & get_midi(size_t i)
Definition: buffer_set.h:107
ChanCount _count
Use counts (there may be more actual buffers than this)
Definition: buffer_set.h:218
bool _is_mirror
False if we 'own' the contained buffers, if true we mirror a PortSet)
Definition: buffer_set.h:224
URIDs urids
Definition: uri_map.h:88
int noteOffset
Definition: aeffectx.h:162
int noteLength
Definition: aeffectx.h:160
std::vector< Buffer * > BufferVec
Definition: buffer_set.h:181
iterator end(DataType type=DataType::NIL)
Definition: port_set.h:100
void lv2_evbuf_set_type(LV2_Evbuf *evbuf, LV2_Evbuf_Type type)
Definition: lv2_evbuf.c:67
void read_from(const BufferSet &in, framecnt_t nframes)
Definition: buffer_set.cc:434
static URIMap & instance()
Definition: uri_map.cc:66
#define kVstMidiType
Definition: aeffectx.h:134
size_t num_ports() const
Definition: port_set.cc:123
bool lv2_evbuf_get(LV2_Evbuf_Iterator iter, uint32_t *frames, uint32_t *subframes, uint32_t *type, uint32_t *size, uint8_t **data)
Definition: lv2_evbuf.c:185
LV2_Evbuf_Iterator lv2_evbuf_next(LV2_Evbuf_Iterator iter)
Definition: lv2_evbuf.c:158
char reserved1
Definition: aeffectx.h:170
Buffer & get(DataType type, size_t i)
Definition: buffer_set.cc:241
void silence(framecnt_t nframes, framecnt_t offset)
Definition: buffer_set.cc:463
static iterator end()
Definition: data_type.h:109
LV2_Evbuf * lv2_evbuf_new(uint32_t capacity, LV2_Evbuf_Type type, uint32_t atom_Chunk, uint32_t atom_Sequence)
Definition: lv2_evbuf.c:44
int64_t framecnt_t
Definition: types.h:76
uint32_t size() const
Definition: Event.hpp:134
iterator begin(DataType type)
Definition: buffer_set.h:165
uint32_t lv2_evbuf_get_capacity(LV2_Evbuf *evbuf)
Definition: lv2_evbuf.c:119
iterator end(DataType type)
Definition: buffer_set.h:166
std::vector< BufferVec > _buffers
Vector of vectors, indexed by DataType.
Definition: buffer_set.h:184
Definition: amp.h:29
Time time() const
Definition: Event.hpp:132
#define DEBUG_TRACE(bits, str)
Definition: debug.h:55
static const uint32_t num_types
Definition: data_type.h:58
void lv2_evbuf_free(LV2_Evbuf *evbuf)
Definition: lv2_evbuf.c:61
void get_backend_port_addresses(PortSet &, framecnt_t)
Definition: buffer_set.cc:124
LIBARDOUR_API uint64_t LV2
Definition: debug.cc:51
void merge_from(const BufferSet &in, framecnt_t nframes)
Definition: buffer_set.cc:445
uint32_t get(DataType t) const
Definition: chan_count.h:59
const ChanCount & count() const
Definition: buffer_set.h:87
void set(DataType t, uint32_t count)
Definition: chan_count.h:58
const ChanCount & available() const
Definition: buffer_set.h:84
int deltaFrames
Definition: aeffectx.h:156
char midiData[4]
Definition: aeffectx.h:164
size_t buffer_capacity(DataType type) const
Definition: buffer_set.cc:234
iterator_base< MidiBuffer, Evoral::MIDIEvent< TimeType > > iterator
Definition: midi_buffer.h:124
const uint8_t * buffer() const
Definition: Event.hpp:135
iterator begin(DataType type=DataType::NIL)
Definition: port_set.h:96
uint32_t atom_Sequence
Definition: uri_map.h:64
void lv2_evbuf_reset(LV2_Evbuf *evbuf, bool input)
Definition: lv2_evbuf.c:82
void attach_buffers(PortSet &ports)
Definition: buffer_set.cc:99
uint32_t n(DataType t) const
Definition: chan_count.h:61
const ChanCount & count() const
Definition: port_set.h:69
ChanCount _available
Available counts (number of buffers actually allocated)
Definition: buffer_set.h:221
static iterator begin()
Definition: data_type.h:108
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
static Buffer * create(DataType type, size_t capacity)
Definition: buffer.cc:27
struct _VstMidiEvent VstMidiEvent
Definition: aeffectx.h:175