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"
27 #include "pbd/compose.h"
29 #include "ardour/buffer.h"
30 #include "ardour/buffer_set.h"
31 #include "ardour/debug.h"
32 #include "ardour/midi_buffer.h"
33 #include "ardour/port.h"
34 #include "ardour/port_set.h"
35 #include "ardour/audioengine.h"
37 #include "ardour/lv2_plugin.h"
38 #include "ardour/lv2_event_buffer.h"
40 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
41 #include "ardour/vestige/aeffectx.h"
46 /** Create a new, empty BufferSet */
47 BufferSet::BufferSet()
50 for (size_t i=0; i < DataType::num_types; ++i) {
51 _buffers.push_back(BufferVec());
58 BufferSet::~BufferSet()
63 /** Destroy all contained buffers.
69 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
70 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
80 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
81 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
85 _vst_buffers.clear ();
90 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
91 * This is quite expensive and not RT-safe, so it should not be called in a process context;
92 * get_jack_port_addresses() will fill in a structure set up by this method.
94 * XXX: this *is* called in a process context; I'm not sure quite what `should not' means above.
97 BufferSet::attach_buffers (PortSet& ports)
99 const ChanCount& count (ports.count());
103 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
104 _buffers.push_back (BufferVec());
105 BufferVec& v = _buffers[*t];
106 v.assign (count.n (*t), (Buffer*) 0);
109 _count = ports.count();
110 _available = ports.count();
116 /** Write the JACK port addresses from a PortSet into our data structures. This
117 * call assumes that attach_buffers() has already been called for the same PortSet.
118 * Does not allocate, so RT-safe BUT you can only call Port::get_buffer() from
119 * the process() callback tree anyway, so this has to be called in RT context.
122 BufferSet::get_jack_port_addresses (PortSet& ports, framecnt_t nframes)
124 assert (_count == ports.count ());
125 assert (_available == ports.count ());
128 assert (_buffers.size() == DataType::num_types);
130 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
131 BufferVec& v = _buffers[*t];
133 assert (v.size() == ports.num_ports (*t));
136 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
137 v[i] = &p->get_buffer (nframes);
143 /** Ensure that there are @a num_buffers buffers of type @a type available,
144 * each of size at least @a buffer_size
147 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
149 assert(type != DataType::NIL);
150 assert(type < _buffers.size());
152 if (num_buffers == 0) {
156 // The vector of buffers of the type we care about
157 BufferVec& bufs = _buffers[type];
159 // If we're a mirror just make sure we're ok
161 assert(_count.get(type) >= num_buffers);
162 assert(bufs[0]->type() == type);
166 // If there's not enough or they're too small, just nuke the whole thing and
167 // rebuild it (so I'm lazy..)
168 if (bufs.size() < num_buffers
169 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
172 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
178 for (size_t i = 0; i < num_buffers; ++i) {
179 bufs.push_back(Buffer::create(type, buffer_capacity));
182 _available.set(type, num_buffers);
183 _count.set (type, num_buffers);
187 // Ensure enough low level MIDI format buffers are available for conversion
188 // in both directions (input & output, out-of-place)
189 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
190 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
191 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
196 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
197 // As above but for VST
198 if (type == DataType::MIDI) {
199 while (_vst_buffers.size() < _buffers[type].size()) {
200 _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
206 assert(bufs[0]->type() == type);
207 assert(bufs.size() >= num_buffers);
208 assert(bufs.size() == _available.get(type));
209 assert(bufs[0]->capacity() >= buffer_capacity);
212 /** Ensure that the number of buffers of each type @a type matches @a chns
213 * and each buffer is of size at least @a buffer_capacity
216 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
218 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
219 ensure_buffers (*i, chns.get (*i), buffer_capacity);
223 /** Get the capacity (size) of the available buffers of the given type.
225 * All buffers of a certain type always have the same capacity.
228 BufferSet::buffer_capacity(DataType type) const
230 assert(_available.get(type) > 0);
231 return _buffers[type][0]->capacity();
235 BufferSet::get(DataType type, size_t i)
237 assert(i < _available.get(type));
238 return *_buffers[type][i];
242 BufferSet::get(DataType type, size_t i) const
244 assert(i < _available.get(type));
245 return *_buffers[type][i];
251 BufferSet::get_lv2_midi(bool input, size_t i)
253 assert (count().get(DataType::MIDI) > i);
255 MidiBuffer& mbuf = get_midi(i);
256 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
257 LV2EventBuffer* ebuf = b.second;
261 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
262 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
263 uint32_t type = LV2Plugin::midi_event_type();
265 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", ev.size()));
266 for (uint16_t x = 0; x < ev.size(); ++x) {
267 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) ev.buffer()[x]));
270 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
277 BufferSet::flush_lv2_midi(bool input, size_t i)
279 MidiBuffer& mbuf = get_midi(i);
280 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
281 LV2EventBuffer* ebuf = b.second;
284 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
290 ebuf->get_event(&frames, &subframes, &type, &size, &data);
292 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
293 for (uint16_t x = 0; x < size; ++x) {
294 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
297 mbuf.push_back(frames, size, data);
301 #endif /* LV2_SUPPORT */
303 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
306 BufferSet::get_vst_midi (size_t b)
308 MidiBuffer& m = get_midi (b);
309 VSTBuffer* vst = _vst_buffers[b];
313 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
317 return vst->events();
320 BufferSet::VSTBuffer::VSTBuffer (size_t c)
323 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
324 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
326 if (_events == 0 || _midi_events == 0) {
329 throw failed_constructor ();
332 _events->numEvents = 0;
333 _events->reserved = 0;
336 BufferSet::VSTBuffer::~VSTBuffer ()
343 BufferSet::VSTBuffer::clear ()
345 _events->numEvents = 0;
349 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
352 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
353 they won't be passed to VST plugins or VSTis
357 int const n = _events->numEvents;
358 assert (n < (int) _capacity);
360 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
361 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
363 v->type = kVstMidiType;
364 v->byteSize = sizeof (VstMidiEvent);
365 v->deltaFrames = ev.time ();
373 v->noteOffVelocity = 0;
374 memcpy (v->midiData, ev.buffer(), ev.size());
377 _events->numEvents++;
380 #endif /* WINDOWS_VST_SUPPORT */
382 /** Copy buffers of one type from `in' to this BufferSet */
384 BufferSet::read_from (const BufferSet& in, framecnt_t nframes, DataType type)
386 assert (available().get (type) >= in.count().get (type));
388 BufferSet::iterator o = begin (type);
389 for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
390 o->read_from (*i, nframes);
393 _count.set (type, in.count().get (type));
396 /** Copy buffers of all types from `in' to this BufferSet */
398 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
400 assert(available() >= in.count());
402 // Copy all buffers 1:1
403 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
404 read_from (in, nframes, *t);
409 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
411 /* merge all input buffers into out existing buffers.
413 NOTE: if "in" contains more buffers than this set,
414 we will drop the extra buffers.
418 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
419 BufferSet::iterator o = begin(*t);
420 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
421 o->merge_from (*i, nframes);
427 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
429 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
430 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
431 (*b)->silence (nframes, offset);
437 BufferSet::set_is_silent (bool yn)
439 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
440 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
441 (*b)->set_is_silent (yn);
447 } // namespace ARDOUR