2 * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2013-2016 Robin Gareus <robin@gareus.org>
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.
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.
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.
24 #include "libardour-config.h"
31 #include "pbd/compose.h"
32 #include "pbd/failed_constructor.h"
34 #include "ardour/buffer.h"
35 #include "ardour/buffer_set.h"
36 #include "ardour/debug.h"
37 #include "ardour/midi_buffer.h"
38 #include "ardour/port.h"
39 #include "ardour/port_set.h"
41 #include "ardour/lv2_plugin.h"
42 #include "lv2_evbuf.h"
43 #include "ardour/uri_map.h"
45 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
46 #include "ardour/vestige/vestige.h"
51 /** Create a new, empty BufferSet */
52 BufferSet::BufferSet()
55 for (size_t i=0; i < DataType::num_types; ++i) {
56 _buffers.push_back(BufferVec());
63 BufferSet::~BufferSet()
68 /** Destroy all contained buffers.
74 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
75 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
85 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
86 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
90 _vst_buffers.clear ();
94 for (LV2Buffers::iterator i = _lv2_buffers.begin(); i != _lv2_buffers.end(); ++i) {
97 _lv2_buffers.clear ();
102 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
103 * This is quite expensive and not RT-safe, so it should not be called in a process context;
104 * get_backend_port_addresses() will fill in a structure set up by this method.
106 * XXX: this *is* called in a process context; I'm not sure quite what `should not' means above.
109 BufferSet::attach_buffers (PortSet& ports)
111 const ChanCount& count (ports.count());
115 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
116 _buffers.push_back (BufferVec());
117 BufferVec& v = _buffers[*t];
118 v.assign (count.n (*t), (Buffer*) 0);
121 _count = ports.count();
122 _available = ports.count();
128 /** Write the backend port addresses from a PortSet into our data structures. This
129 * call assumes that attach_buffers() has already been called for the same PortSet.
130 * Does not allocate, so RT-safe BUT you can only call Port::get_buffer() from
131 * the process() callback tree anyway, so this has to be called in RT context.
134 BufferSet::get_backend_port_addresses (PortSet& ports, samplecnt_t nframes)
136 assert (_count == ports.count ());
137 assert (_available == ports.count ());
140 assert (_buffers.size() == DataType::num_types);
142 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
143 BufferVec& v = _buffers[*t];
145 assert (v.size() == ports.num_ports (*t));
148 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
149 v[i] = &p->get_buffer (nframes);
155 /** Ensure that there are @a num_buffers buffers of type @a type available,
156 * each of size at least @a buffer_size
159 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
161 assert(type != DataType::NIL);
162 assert(type < _buffers.size());
164 if (num_buffers == 0) {
168 // The vector of buffers of the type we care about
169 BufferVec& bufs = _buffers[type];
171 // If we're a mirror just make sure we're ok
173 assert(_count.get(type) >= num_buffers);
174 assert(bufs[0]->type() == type);
178 // If there's not enough or they're too small, just nuke the whole thing and
179 // rebuild it (so I'm lazy..)
180 if (bufs.size() < num_buffers
181 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
184 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
190 for (size_t i = 0; i < num_buffers; ++i) {
191 bufs.push_back(Buffer::create(type, buffer_capacity));
194 _available.set(type, num_buffers);
195 _count.set (type, num_buffers);
199 // Ensure enough low level MIDI format buffers are available for conversion
200 // in both directions (input & output, out-of-place)
201 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
202 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
203 _lv2_buffers.push_back(
204 std::make_pair(false, lv2_evbuf_new(buffer_capacity,
206 URIMap::instance().urids.atom_Chunk,
207 URIMap::instance().urids.atom_Sequence)));
212 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
213 // As above but for VST
214 if (type == DataType::MIDI) {
215 while (_vst_buffers.size() < _buffers[type].size()) {
216 _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
222 assert(bufs[0]->type() == type);
223 assert(bufs.size() >= num_buffers);
224 assert(bufs.size() == _available.get(type));
225 assert(bufs[0]->capacity() >= buffer_capacity);
228 /** Ensure that the number of buffers of each type @a type matches @a chns
229 * and each buffer is of size at least @a buffer_capacity
232 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
234 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
235 ensure_buffers (*i, chns.get (*i), buffer_capacity);
239 /** Get the capacity (size) of the available buffers of the given type.
241 * All buffers of a certain type always have the same capacity.
244 BufferSet::buffer_capacity(DataType type) const
246 assert(_available.get(type) > 0);
247 return _buffers[type][0]->capacity();
251 BufferSet::get_available(DataType type, size_t i)
253 assert(i < _available.get(type));
254 return *_buffers[type][i];
258 BufferSet::get_available(DataType type, size_t i) const
260 assert(i < _available.get(type));
261 return *_buffers[type][i];
267 BufferSet::ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity)
269 assert(count().get(DataType::MIDI) > i);
271 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
272 LV2_Evbuf* evbuf = b.second;
274 if (lv2_evbuf_get_capacity(evbuf) >= buffer_capacity) return;
276 lv2_evbuf_free(b.second);
277 _lv2_buffers.at(i * 2 + (input ? 0 : 1)) =
278 std::make_pair(false, lv2_evbuf_new(
281 URIMap::instance().urids.atom_Chunk,
282 URIMap::instance().urids.atom_Sequence));
286 BufferSet::get_lv2_midi(bool input, size_t i, bool old_api)
288 assert(count().get(DataType::MIDI) > i);
290 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
291 LV2_Evbuf* evbuf = b.second;
293 lv2_evbuf_set_type(evbuf, old_api ? LV2_EVBUF_EVENT : LV2_EVBUF_ATOM);
294 lv2_evbuf_reset(evbuf, input);
299 BufferSet::forward_lv2_midi(LV2_Evbuf* buf, size_t i, bool purge_ardour_buffer)
301 MidiBuffer& mbuf = get_midi(i);
302 if (purge_ardour_buffer) {
305 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
306 lv2_evbuf_is_valid(i);
307 i = lv2_evbuf_next(i)) {
308 uint32_t samples, subframes, type, size;
310 lv2_evbuf_get(i, &samples, &subframes, &type, &size, &data);
311 if (type == URIMap::instance().urids.midi_MidiEvent) {
312 mbuf.push_back(samples, size, data);
318 BufferSet::flush_lv2_midi(bool input, size_t i)
320 MidiBuffer& mbuf = get_midi(i);
321 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
322 LV2_Evbuf* evbuf = b.second;
325 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(evbuf);
326 lv2_evbuf_is_valid(i);
327 i = lv2_evbuf_next(i)) {
333 lv2_evbuf_get(i, &samples, &subframes, &type, &size, &data);
335 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
336 for (uint16_t x = 0; x < size; ++x) {
337 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
340 if (type == URIMap::instance().urids.midi_MidiEvent) {
341 // TODO: Make Ardour event buffers generic so plugins can communicate
342 mbuf.push_back(samples, size, data);
347 #endif /* LV2_SUPPORT */
349 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
352 BufferSet::get_vst_midi (size_t b)
354 MidiBuffer& m = get_midi (b);
355 assert (b <= _vst_buffers.size());
356 VSTBuffer* vst = _vst_buffers[b];
360 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
364 return vst->events();
367 BufferSet::VSTBuffer::VSTBuffer (size_t c)
373 /* from `man malloc`: "If size is 0, then malloc() returns either NULL, or a
374 * unique pointer value that can later be successfully passed to free()."
376 * The latter will cause trouble here.
378 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
379 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
382 if (_events == 0 || _midi_events == 0) {
387 throw failed_constructor ();
390 _events->numEvents = 0;
391 _events->reserved = 0;
394 BufferSet::VSTBuffer::~VSTBuffer ()
401 BufferSet::VSTBuffer::clear ()
403 _events->numEvents = 0;
407 BufferSet::VSTBuffer::push_back (Evoral::Event<samplepos_t> const & ev)
410 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
411 they won't be passed to VST plugins or VSTis
415 uint32_t const n = _events->numEvents;
416 assert (n < _capacity);
417 if (n >= _capacity) {
421 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
422 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
424 v->type = kVstMidiType;
425 v->byteSize = sizeof (VstMidiEvent);
426 v->deltaSamples = ev.time ();
434 v->noteOffVelocity = 0;
435 memcpy (v->midiData, ev.buffer(), ev.size());
438 _events->numEvents++;
441 #endif /* WINDOWS_VST_SUPPORT */
443 /** Copy buffers of one type from `in' to this BufferSet */
445 BufferSet::read_from (const BufferSet& in, samplecnt_t nframes, DataType type)
447 assert (available().get (type) >= in.count().get (type));
449 BufferSet::iterator o = begin (type);
450 for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
451 o->read_from (*i, nframes);
454 _count.set (type, in.count().get (type));
457 /** Copy buffers of all types from `in' to this BufferSet */
459 BufferSet::read_from (const BufferSet& in, samplecnt_t nframes)
461 assert(available() >= in.count());
463 // Copy all buffers 1:1
464 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
465 read_from (in, nframes, *t);
470 BufferSet::merge_from (const BufferSet& in, samplecnt_t nframes)
472 /* merge all input buffers into out existing buffers.
474 NOTE: if "in" contains more buffers than this set,
475 we will drop the extra buffers.
479 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
480 BufferSet::iterator o = begin(*t);
481 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
482 o->merge_from (*i, nframes);
488 BufferSet::silence (samplecnt_t nframes, samplecnt_t offset)
490 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
491 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
492 (*b)->silence (nframes, offset);
497 } // namespace ARDOUR