+ return _buffers[type][0]->capacity();
+}
+
+Buffer&
+BufferSet::get(DataType type, size_t i)
+{
+ assert(i < _available.get(type));
+ return *_buffers[type][i];
+}
+
+const Buffer&
+BufferSet::get(DataType type, size_t i) const
+{
+ assert(i < _available.get(type));
+ return *_buffers[type][i];
+}
+
+#ifdef LV2_SUPPORT
+
+void
+BufferSet::ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity)
+{
+ assert(count().get(DataType::MIDI) > i);
+
+ LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
+ LV2_Evbuf* evbuf = b.second;
+
+ if (lv2_evbuf_get_capacity(evbuf) >= buffer_capacity) return;
+
+ lv2_evbuf_free(b.second);
+ _lv2_buffers.at(i * 2 + (input ? 0 : 1)) =
+ std::make_pair(false, lv2_evbuf_new(
+ buffer_capacity,
+ LV2_EVBUF_EVENT,
+ URIMap::instance().urids.atom_Chunk,
+ URIMap::instance().urids.atom_Sequence));
+}
+
+LV2_Evbuf*
+BufferSet::get_lv2_midi(bool input, size_t i, bool old_api)
+{
+ assert(count().get(DataType::MIDI) > i);
+
+ LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
+ LV2_Evbuf* evbuf = b.second;
+
+ lv2_evbuf_set_type(evbuf, old_api ? LV2_EVBUF_EVENT : LV2_EVBUF_ATOM);
+ lv2_evbuf_reset(evbuf, input);
+ return evbuf;
+}
+
+void
+BufferSet::forward_lv2_midi(LV2_Evbuf* buf, size_t i, bool purge_ardour_buffer)
+{
+ MidiBuffer& mbuf = get_midi(i);
+ if (purge_ardour_buffer) {
+ mbuf.silence(0, 0);
+ }
+ for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
+ lv2_evbuf_is_valid(i);
+ i = lv2_evbuf_next(i)) {
+ uint32_t frames, subframes, type, size;
+ uint8_t* data;
+ lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
+ if (type == URIMap::instance().urids.midi_MidiEvent) {
+ mbuf.push_back(frames, size, data);
+ }
+ }
+}
+
+void
+BufferSet::flush_lv2_midi(bool input, size_t i)
+{
+ MidiBuffer& mbuf = get_midi(i);
+ LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
+ LV2_Evbuf* evbuf = b.second;
+
+ mbuf.silence(0, 0);
+ for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(evbuf);
+ lv2_evbuf_is_valid(i);
+ i = lv2_evbuf_next(i)) {
+ uint32_t frames;
+ uint32_t subframes;
+ uint32_t type;
+ uint32_t size;
+ uint8_t* data;
+ lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
+#ifndef NDEBUG
+ DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
+ for (uint16_t x = 0; x < size; ++x) {
+ DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
+ }
+#endif
+ if (type == URIMap::instance().urids.midi_MidiEvent) {
+ // TODO: Make Ardour event buffers generic so plugins can communicate
+ mbuf.push_back(frames, size, data);
+ }
+ }
+}
+
+#endif /* LV2_SUPPORT */
+
+#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
+
+VstEvents*
+BufferSet::get_vst_midi (size_t b)
+{
+ MidiBuffer& m = get_midi (b);
+ VSTBuffer* vst = _vst_buffers[b];
+
+ vst->clear ();
+
+ for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
+ vst->push_back (*i);
+ }
+
+ return vst->events();
+}
+
+BufferSet::VSTBuffer::VSTBuffer (size_t c)
+ : _capacity (c)
+{
+ _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
+ _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
+
+ if (_events == 0 || _midi_events == 0) {
+ free (_events);
+ free (_midi_events);
+ throw failed_constructor ();
+ }
+
+ _events->numEvents = 0;
+ _events->reserved = 0;
+}
+
+BufferSet::VSTBuffer::~VSTBuffer ()
+{
+ free (_events);
+ free (_midi_events);
+}
+
+void
+BufferSet::VSTBuffer::clear ()
+{
+ _events->numEvents = 0;
+}
+
+void
+BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
+{
+ if (ev.size() > 3) {
+ /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
+ they won't be passed to VST plugins or VSTis
+ */
+ return;
+ }
+ int const n = _events->numEvents;
+ assert (n < (int) _capacity);
+
+ _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
+ VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
+
+ v->type = kVstMidiType;
+ v->byteSize = sizeof (VstMidiEvent);
+ v->deltaFrames = ev.time ();
+
+ v->flags = 0;
+ v->detune = 0;
+ v->noteLength = 0;
+ v->noteOffset = 0;
+ v->reserved1 = 0;
+ v->reserved2 = 0;
+ v->noteOffVelocity = 0;
+ memcpy (v->midiData, ev.buffer(), ev.size());
+ v->midiData[3] = 0;
+
+ _events->numEvents++;
+}
+
+#endif /* WINDOWS_VST_SUPPORT */
+
+/** Copy buffers of one type from `in' to this BufferSet */
+void
+BufferSet::read_from (const BufferSet& in, framecnt_t nframes, DataType type)
+{
+ assert (available().get (type) >= in.count().get (type));
+
+ BufferSet::iterator o = begin (type);
+ for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
+ o->read_from (*i, nframes);
+ }
+
+ _count.set (type, in.count().get (type));