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"
26 #include "ardour/buffer.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/port.h"
30 #include "ardour/port_set.h"
31 #include "ardour/audioengine.h"
33 #include "ardour/lv2_plugin.h"
34 #include "ardour/lv2_event_buffer.h"
37 #include "vestige/aeffectx.h"
42 /** Create a new, empty BufferSet */
43 BufferSet::BufferSet()
46 for (size_t i=0; i < DataType::num_types; ++i) {
47 _buffers.push_back(BufferVec());
54 BufferSet::~BufferSet()
59 /** Destroy all contained buffers.
65 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
66 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
77 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
81 _vst_buffers.clear ();
85 /** Make this BufferSet a direct mirror of a PortSet's buffers.
88 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
92 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
93 _buffers.push_back(BufferVec());
94 BufferVec& v = _buffers[*t];
96 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
97 assert(p->type() == *t);
98 v.push_back(&(p->get_buffer(nframes, offset)));
102 _count = ports.count();
103 _available = ports.count();
108 /** Ensure that there are @a num_buffers buffers of type @a type available,
109 * each of size at least @a buffer_size
112 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
114 assert(type != DataType::NIL);
115 assert(type < _buffers.size());
117 if (num_buffers == 0) {
121 // The vector of buffers of the type we care about
122 BufferVec& bufs = _buffers[type];
124 // If we're a mirror just make sure we're ok
126 assert(_count.get(type) >= num_buffers);
127 assert(bufs[0]->type() == type);
131 // If there's not enough or they're too small, just nuke the whole thing and
132 // rebuild it (so I'm lazy..)
133 if (bufs.size() < num_buffers
134 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
137 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
143 for (size_t i = 0; i < num_buffers; ++i) {
144 bufs.push_back(Buffer::create(type, buffer_capacity));
147 _available.set(type, num_buffers);
148 _count.set (type, num_buffers);
152 // Ensure enough low level MIDI format buffers are available for conversion
153 // in both directions (input & output, out-of-place)
154 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
155 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
156 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
162 // As above but for VST
163 if (type == DataType::MIDI) {
164 while (_vst_buffers.size() < _buffers[type].size()) {
165 _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
171 assert(bufs[0]->type() == type);
172 assert(bufs.size() >= num_buffers);
173 assert(bufs.size() == _available.get(type));
174 assert(bufs[0]->capacity() >= buffer_capacity);
177 /** Ensure that the number of buffers of each type @a type matches @a chns
178 * and each buffer is of size at least @a buffer_capacity
181 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
183 if (chns == ChanCount::ZERO) {
187 // If we're a mirror just make sure we're ok
189 assert(_count >= chns);
193 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
195 // The vector of buffers of this type
196 BufferVec& bufs = _buffers[*t];
198 uint32_t nbufs = chns.get (*t);
202 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
209 // If there's not enough or they're too small, just nuke the whole thing and
210 // rebuild it (so I'm lazy..)
211 if (bufs.size() < nbufs
212 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
215 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
221 for (size_t i = 0; i < nbufs; ++i) {
222 bufs.push_back(Buffer::create(*t, buffer_capacity));
225 _available.set (*t, nbufs);
229 // Ensure enough low level MIDI format buffers are available for conversion
230 // in both directions (input & output, out-of-place)
231 if (*t == DataType::MIDI && _lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2 + 1) {
232 while (_lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2) {
233 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
239 assert(bufs[0]->type() == *t);
240 assert(bufs.size() == _available.get(*t));
241 assert(bufs[0]->capacity() >= buffer_capacity);
244 assert (available() >= chns);
247 /** Get the capacity (size) of the available buffers of the given type.
249 * All buffers of a certain type always have the same capacity.
252 BufferSet::buffer_capacity(DataType type) const
254 assert(_available.get(type) > 0);
255 return _buffers[type][0]->capacity();
259 BufferSet::get(DataType type, size_t i)
261 assert(i < _available.get(type));
262 return *_buffers[type][i];
266 BufferSet::get(DataType type, size_t i) const
268 assert(i < _available.get(type));
269 return *_buffers[type][i];
275 BufferSet::get_lv2_midi(bool input, size_t i)
277 MidiBuffer& mbuf = get_midi(i);
278 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
279 LV2EventBuffer* ebuf = b.second;
283 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
284 const Evoral::MIDIEvent<nframes_t> ev(*e, false);
285 uint32_t type = LV2Plugin::midi_event_type();
286 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
293 BufferSet::flush_lv2_midi(bool input, size_t i)
295 MidiBuffer& mbuf = get_midi(i);
296 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
297 LV2EventBuffer* ebuf = b.second;
300 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
306 ebuf->get_event(&frames, &subframes, &type, &size, &data);
307 mbuf.push_back(frames, size, data);
311 #endif /* HAVE_SLV2 */
316 BufferSet::get_vst_midi (size_t b)
318 MidiBuffer& m = get_midi (b);
319 VSTBuffer* vst = _vst_buffers[b];
323 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
327 return vst->events();
330 BufferSet::VSTBuffer::VSTBuffer (size_t c)
333 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
334 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
336 if (_events == 0 || _midi_events == 0) {
339 throw failed_constructor ();
342 _events->numEvents = 0;
343 _events->reserved = 0;
346 BufferSet::VSTBuffer::~VSTBuffer ()
353 BufferSet::VSTBuffer::clear ()
355 _events->numEvents = 0;
359 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<nframes_t> const & ev)
362 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
363 they won't be passed to VST plugins or VSTis
367 int const n = _events->numEvents;
368 assert (n < (int) _capacity);
370 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
371 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
373 v->type = kVstMidiType;
374 v->byteSize = sizeof (VstMidiEvent);
375 v->deltaFrames = ev.time ();
383 v->noteOffVelocity = 0;
384 memcpy (v->midiData, ev.buffer(), ev.size());
387 _events->numEvents++;
390 #endif /* VST_SUPPORT */
393 BufferSet::read_from (const BufferSet& in, nframes_t nframes)
395 assert(available() >= in.count());
397 // Copy all buffers 1:1
398 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
399 BufferSet::iterator o = begin(*t);
400 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
401 o->read_from (*i, nframes);
405 set_count(in.count());
409 BufferSet::merge_from (const BufferSet& in, nframes_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 (nframes_t nframes, nframes_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::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)->is_silent (yn);
447 } // namespace ARDOUR