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, framecnt_t nframes, framecnt_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 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
184 ensure_buffers (*i, chns.get (*i), buffer_capacity);
188 /** Get the capacity (size) of the available buffers of the given type.
190 * All buffers of a certain type always have the same capacity.
193 BufferSet::buffer_capacity(DataType type) const
195 assert(_available.get(type) > 0);
196 return _buffers[type][0]->capacity();
200 BufferSet::get(DataType type, size_t i)
202 assert(i < _available.get(type));
203 return *_buffers[type][i];
207 BufferSet::get(DataType type, size_t i) const
209 assert(i < _available.get(type));
210 return *_buffers[type][i];
216 BufferSet::get_lv2_midi(bool input, size_t i)
218 assert (count().get(DataType::MIDI) > i);
220 MidiBuffer& mbuf = get_midi(i);
221 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
222 LV2EventBuffer* ebuf = b.second;
226 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
227 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
228 uint32_t type = LV2Plugin::midi_event_type();
229 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
236 BufferSet::flush_lv2_midi(bool input, size_t i)
238 MidiBuffer& mbuf = get_midi(i);
239 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
240 LV2EventBuffer* ebuf = b.second;
243 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
249 ebuf->get_event(&frames, &subframes, &type, &size, &data);
250 mbuf.push_back(frames, size, data);
254 #endif /* HAVE_SLV2 */
259 BufferSet::get_vst_midi (size_t b)
261 MidiBuffer& m = get_midi (b);
262 VSTBuffer* vst = _vst_buffers[b];
266 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
270 return vst->events();
273 BufferSet::VSTBuffer::VSTBuffer (size_t c)
276 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
277 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
279 if (_events == 0 || _midi_events == 0) {
282 throw failed_constructor ();
285 _events->numEvents = 0;
286 _events->reserved = 0;
289 BufferSet::VSTBuffer::~VSTBuffer ()
296 BufferSet::VSTBuffer::clear ()
298 _events->numEvents = 0;
302 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
305 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
306 they won't be passed to VST plugins or VSTis
310 int const n = _events->numEvents;
311 assert (n < (int) _capacity);
313 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
314 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
316 v->type = kVstMidiType;
317 v->byteSize = sizeof (VstMidiEvent);
318 v->deltaFrames = ev.time ();
326 v->noteOffVelocity = 0;
327 memcpy (v->midiData, ev.buffer(), ev.size());
330 _events->numEvents++;
333 #endif /* VST_SUPPORT */
336 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
338 assert(available() >= in.count());
340 // Copy all buffers 1:1
341 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
342 BufferSet::iterator o = begin(*t);
343 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
344 o->read_from (*i, nframes);
348 set_count(in.count());
352 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
354 /* merge all input buffers into out existing buffers.
356 NOTE: if "in" contains more buffers than this set,
357 we will drop the extra buffers.
361 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
362 BufferSet::iterator o = begin(*t);
363 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
364 o->merge_from (*i, nframes);
370 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
372 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
373 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
374 (*b)->silence (nframes, offset);
380 BufferSet::is_silent (bool yn)
382 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
383 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
384 (*b)->is_silent (yn);
390 } // namespace ARDOUR