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"
30 #include "ardour/buffer.h"
31 #include "ardour/buffer_set.h"
32 #include "ardour/debug.h"
33 #include "ardour/midi_buffer.h"
34 #include "ardour/port.h"
35 #include "ardour/port_set.h"
36 #include "ardour/audioengine.h"
38 #include "ardour/lv2_plugin.h"
39 #include "ardour/lv2_event_buffer.h"
41 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
42 #include "ardour/vestige/aeffectx.h"
47 /** Create a new, empty BufferSet */
48 BufferSet::BufferSet()
51 for (size_t i=0; i < DataType::num_types; ++i) {
52 _buffers.push_back(BufferVec());
59 BufferSet::~BufferSet()
64 /** Destroy all contained buffers.
70 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
71 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
81 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
82 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
86 _vst_buffers.clear ();
91 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
92 * This is quite expensive and not RT-safe, so it should not be called in a process context;
93 * get_jack_port_addresses() will fill in a structure set up by this method.
95 * XXX: this *is* called in a process context; I'm not sure quite what `should not' means above.
98 BufferSet::attach_buffers (PortSet& ports)
100 const ChanCount& count (ports.count());
104 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
105 _buffers.push_back (BufferVec());
106 BufferVec& v = _buffers[*t];
107 v.assign (count.n (*t), (Buffer*) 0);
110 _count = ports.count();
111 _available = ports.count();
117 /** Write the JACK port addresses from a PortSet into our data structures. This
118 * call assumes that attach_buffers() has already been called for the same PortSet.
119 * Does not allocate, so RT-safe BUT you can only call Port::get_buffer() from
120 * the process() callback tree anyway, so this has to be called in RT context.
123 BufferSet::get_jack_port_addresses (PortSet& ports, framecnt_t nframes)
125 assert (_count == ports.count ());
126 assert (_available == ports.count ());
129 assert (_buffers.size() == DataType::num_types);
131 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
132 BufferVec& v = _buffers[*t];
134 assert (v.size() == ports.num_ports (*t));
137 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
138 v[i] = &p->get_buffer (nframes);
144 /** Ensure that there are @a num_buffers buffers of type @a type available,
145 * each of size at least @a buffer_size
148 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
150 assert(type != DataType::NIL);
151 assert(type < _buffers.size());
153 if (num_buffers == 0) {
157 // The vector of buffers of the type we care about
158 BufferVec& bufs = _buffers[type];
160 // If we're a mirror just make sure we're ok
162 assert(_count.get(type) >= num_buffers);
163 assert(bufs[0]->type() == type);
167 // If there's not enough or they're too small, just nuke the whole thing and
168 // rebuild it (so I'm lazy..)
169 if (bufs.size() < num_buffers
170 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
173 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
179 for (size_t i = 0; i < num_buffers; ++i) {
180 bufs.push_back(Buffer::create(type, buffer_capacity));
183 _available.set(type, num_buffers);
184 _count.set (type, num_buffers);
188 // Ensure enough low level MIDI format buffers are available for conversion
189 // in both directions (input & output, out-of-place)
190 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
191 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
192 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
197 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
198 // As above but for VST
199 if (type == DataType::MIDI) {
200 while (_vst_buffers.size() < _buffers[type].size()) {
201 _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
207 assert(bufs[0]->type() == type);
208 assert(bufs.size() >= num_buffers);
209 assert(bufs.size() == _available.get(type));
210 assert(bufs[0]->capacity() >= buffer_capacity);
213 /** Ensure that the number of buffers of each type @a type matches @a chns
214 * and each buffer is of size at least @a buffer_capacity
217 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
219 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
220 ensure_buffers (*i, chns.get (*i), buffer_capacity);
224 /** Get the capacity (size) of the available buffers of the given type.
226 * All buffers of a certain type always have the same capacity.
229 BufferSet::buffer_capacity(DataType type) const
231 assert(_available.get(type) > 0);
232 return _buffers[type][0]->capacity();
236 BufferSet::get(DataType type, size_t i)
238 assert(i < _available.get(type));
239 return *_buffers[type][i];
243 BufferSet::get(DataType type, size_t i) const
245 assert(i < _available.get(type));
246 return *_buffers[type][i];
252 BufferSet::get_lv2_midi(bool input, size_t i)
254 assert (count().get(DataType::MIDI) > i);
256 MidiBuffer& mbuf = get_midi(i);
257 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
258 LV2EventBuffer* ebuf = b.second;
262 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("%1 bytes of MIDI waiting @ %2\n", mbuf.size(), (void*) mbuf.data()));
263 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
264 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
265 uint32_t type = LV2Plugin::midi_event_type();
267 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tMIDI event of size %1 @ %2\n", ev.size(), ev.time()));
268 for (uint16_t x = 0; x < ev.size(); ++x) {
269 std::stringstream ss;
270 ss << "\t\tByte[" << x << "] = " << std::hex << (int) ev.buffer()[x] << std::dec << std::endl;
271 DEBUG_TRACE (PBD::DEBUG::LV2, ss.str());
274 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
281 BufferSet::flush_lv2_midi(bool input, size_t i)
283 MidiBuffer& mbuf = get_midi(i);
284 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
285 LV2EventBuffer* ebuf = b.second;
288 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
294 ebuf->get_event(&frames, &subframes, &type, &size, &data);
296 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
297 for (uint16_t x = 0; x < size; ++x) {
298 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
301 mbuf.push_back(frames, size, data);
305 #endif /* LV2_SUPPORT */
307 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
310 BufferSet::get_vst_midi (size_t b)
312 MidiBuffer& m = get_midi (b);
313 VSTBuffer* vst = _vst_buffers[b];
317 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
321 return vst->events();
324 BufferSet::VSTBuffer::VSTBuffer (size_t c)
327 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
328 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
330 if (_events == 0 || _midi_events == 0) {
333 throw failed_constructor ();
336 _events->numEvents = 0;
337 _events->reserved = 0;
340 BufferSet::VSTBuffer::~VSTBuffer ()
347 BufferSet::VSTBuffer::clear ()
349 _events->numEvents = 0;
353 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
356 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
357 they won't be passed to VST plugins or VSTis
361 int const n = _events->numEvents;
362 assert (n < (int) _capacity);
364 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
365 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
367 v->type = kVstMidiType;
368 v->byteSize = sizeof (VstMidiEvent);
369 v->deltaFrames = ev.time ();
377 v->noteOffVelocity = 0;
378 memcpy (v->midiData, ev.buffer(), ev.size());
381 _events->numEvents++;
384 #endif /* WINDOWS_VST_SUPPORT */
386 /** Copy buffers of one type from `in' to this BufferSet */
388 BufferSet::read_from (const BufferSet& in, framecnt_t nframes, DataType type)
390 assert (available().get (type) >= in.count().get (type));
392 BufferSet::iterator o = begin (type);
393 for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
394 o->read_from (*i, nframes);
397 _count.set (type, in.count().get (type));
400 /** Copy buffers of all types from `in' to this BufferSet */
402 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
404 assert(available() >= in.count());
406 // Copy all buffers 1:1
407 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
408 read_from (in, nframes, *t);
413 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
415 /* merge all input buffers into out existing buffers.
417 NOTE: if "in" contains more buffers than this set,
418 we will drop the extra buffers.
422 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
423 BufferSet::iterator o = begin(*t);
424 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
425 o->merge_from (*i, nframes);
431 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
433 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
434 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
435 (*b)->silence (nframes, offset);
441 BufferSet::set_is_silent (bool yn)
443 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
444 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
445 (*b)->set_is_silent (yn);
451 } // namespace ARDOUR