2 Copyright (C) 2006 Paul Davis
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)
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
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.
21 #include "libardour-config.h"
28 #include "pbd/compose.h"
29 #include "pbd/failed_constructor.h"
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"
39 #include "ardour/lv2_plugin.h"
40 #include "lv2_evbuf.h"
42 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
43 #include "ardour/vestige/aeffectx.h"
48 /** Create a new, empty BufferSet */
49 BufferSet::BufferSet()
52 for (size_t i=0; i < DataType::num_types; ++i) {
53 _buffers.push_back(BufferVec());
60 BufferSet::~BufferSet()
65 /** Destroy all contained buffers.
71 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
72 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
82 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
83 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
87 _vst_buffers.clear ();
92 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
93 * This is quite expensive and not RT-safe, so it should not be called in a process context;
94 * get_backend_port_addresses() will fill in a structure set up by this method.
96 * XXX: this *is* called in a process context; I'm not sure quite what `should not' means above.
99 BufferSet::attach_buffers (PortSet& ports)
101 const ChanCount& count (ports.count());
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);
111 _count = ports.count();
112 _available = ports.count();
118 /** Write the backend port addresses from a PortSet into our data structures. This
119 * call assumes that attach_buffers() has already been called for the same PortSet.
120 * Does not allocate, so RT-safe BUT you can only call Port::get_buffer() from
121 * the process() callback tree anyway, so this has to be called in RT context.
124 BufferSet::get_backend_port_addresses (PortSet& ports, framecnt_t nframes)
126 assert (_count == ports.count ());
127 assert (_available == ports.count ());
130 assert (_buffers.size() == DataType::num_types);
132 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
133 BufferVec& v = _buffers[*t];
135 assert (v.size() == ports.num_ports (*t));
138 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
139 v[i] = &p->get_buffer (nframes);
145 /** Ensure that there are @a num_buffers buffers of type @a type available,
146 * each of size at least @a buffer_size
149 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
151 assert(type != DataType::NIL);
152 assert(type < _buffers.size());
154 if (num_buffers == 0) {
158 // The vector of buffers of the type we care about
159 BufferVec& bufs = _buffers[type];
161 // If we're a mirror just make sure we're ok
163 assert(_count.get(type) >= num_buffers);
164 assert(bufs[0]->type() == type);
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)) {
174 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
180 for (size_t i = 0; i < num_buffers; ++i) {
181 bufs.push_back(Buffer::create(type, buffer_capacity));
184 _available.set(type, num_buffers);
185 _count.set (type, num_buffers);
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,
197 URIMap::instance().urids.atom_Sequence)));
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));
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);
218 /** Ensure that the number of buffers of each type @a type matches @a chns
219 * and each buffer is of size at least @a buffer_capacity
222 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
224 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
225 ensure_buffers (*i, chns.get (*i), buffer_capacity);
229 /** Get the capacity (size) of the available buffers of the given type.
231 * All buffers of a certain type always have the same capacity.
234 BufferSet::buffer_capacity(DataType type) const
236 assert(_available.get(type) > 0);
237 return _buffers[type][0]->capacity();
241 BufferSet::get(DataType type, size_t i)
243 assert(i < _available.get(type));
244 return *_buffers[type][i];
248 BufferSet::get(DataType type, size_t i) const
250 assert(i < _available.get(type));
251 return *_buffers[type][i];
257 BufferSet::ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity)
259 assert(count().get(DataType::MIDI) > i);
261 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
262 LV2_Evbuf* evbuf = b.second;
264 if (lv2_evbuf_get_capacity(evbuf) >= buffer_capacity) return;
266 lv2_evbuf_free(b.second);
267 _lv2_buffers.at(i * 2 + (input ? 0 : 1)) =
268 std::make_pair(false, lv2_evbuf_new(
271 URIMap::instance().urids.atom_Chunk,
272 URIMap::instance().urids.atom_Sequence));
276 BufferSet::get_lv2_midi(bool input, size_t i, bool old_api)
278 assert(count().get(DataType::MIDI) > i);
280 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
281 LV2_Evbuf* evbuf = b.second;
283 lv2_evbuf_set_type(evbuf, old_api ? LV2_EVBUF_EVENT : LV2_EVBUF_ATOM);
284 lv2_evbuf_reset(evbuf, input);
289 BufferSet::forward_lv2_midi(LV2_Evbuf* buf, size_t i, bool purge_ardour_buffer)
291 MidiBuffer& mbuf = get_midi(i);
292 if (purge_ardour_buffer) {
295 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
296 lv2_evbuf_is_valid(i);
297 i = lv2_evbuf_next(i)) {
298 uint32_t frames, subframes, type, size;
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);
308 BufferSet::flush_lv2_midi(bool input, size_t i)
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;
315 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(evbuf);
316 lv2_evbuf_is_valid(i);
317 i = lv2_evbuf_next(i)) {
323 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
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]));
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);
337 #endif /* LV2_SUPPORT */
339 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
342 BufferSet::get_vst_midi (size_t b)
344 MidiBuffer& m = get_midi (b);
345 VSTBuffer* vst = _vst_buffers[b];
349 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
353 return vst->events();
356 BufferSet::VSTBuffer::VSTBuffer (size_t c)
359 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
360 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
362 if (_events == 0 || _midi_events == 0) {
365 throw failed_constructor ();
368 _events->numEvents = 0;
369 _events->reserved = 0;
372 BufferSet::VSTBuffer::~VSTBuffer ()
379 BufferSet::VSTBuffer::clear ()
381 _events->numEvents = 0;
385 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
388 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
389 they won't be passed to VST plugins or VSTis
393 int const n = _events->numEvents;
394 assert (n < (int) _capacity);
396 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
397 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
399 v->type = kVstMidiType;
400 v->byteSize = sizeof (VstMidiEvent);
401 v->deltaFrames = ev.time ();
409 v->noteOffVelocity = 0;
410 memcpy (v->midiData, ev.buffer(), ev.size());
413 _events->numEvents++;
416 #endif /* WINDOWS_VST_SUPPORT */
418 /** Copy buffers of one type from `in' to this BufferSet */
420 BufferSet::read_from (const BufferSet& in, framecnt_t nframes, DataType type)
422 assert (available().get (type) >= in.count().get (type));
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);
429 _count.set (type, in.count().get (type));
432 /** Copy buffers of all types from `in' to this BufferSet */
434 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
436 assert(available() >= in.count());
438 // Copy all buffers 1:1
439 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
440 read_from (in, nframes, *t);
445 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
447 /* merge all input buffers into out existing buffers.
449 NOTE: if "in" contains more buffers than this set,
450 we will drop the extra buffers.
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);
463 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
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);
472 } // namespace ARDOUR