new record-step-edit icon for track/bus list
[ardour2.git] / libs / ardour / buffer_set.cc
blob2f091854cf6d7abeb3a98f1fa1d6aa7d7084be8f
1 /*
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)
7 any later version.
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
12 for more details.
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.
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
24 #include <iostream>
25 #include <algorithm>
26 #include "pbd/compose.h"
27 #include "ardour/buffer.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/debug.h"
30 #include "ardour/midi_buffer.h"
31 #include "ardour/port.h"
32 #include "ardour/port_set.h"
33 #include "ardour/audioengine.h"
34 #ifdef LV2_SUPPORT
35 #include "ardour/lv2_plugin.h"
36 #include "ardour/lv2_event_buffer.h"
37 #endif
38 #ifdef VST_SUPPORT
39 #include "vestige/aeffectx.h"
40 #endif
42 namespace ARDOUR {
44 /** Create a new, empty BufferSet */
45 BufferSet::BufferSet()
46 : _is_mirror(false)
48 for (size_t i=0; i < DataType::num_types; ++i) {
49 _buffers.push_back(BufferVec());
52 _count.reset();
53 _available.reset();
56 BufferSet::~BufferSet()
58 clear();
61 /** Destroy all contained buffers.
63 void
64 BufferSet::clear()
66 if (!_is_mirror) {
67 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
68 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
69 delete *j;
71 (*i).clear();
74 _buffers.clear();
75 _count.reset();
76 _available.reset();
78 #ifdef VST_SUPPORT
79 for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
80 delete *i;
83 _vst_buffers.clear ();
84 #endif
87 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
88 * This is quite expensive and not RT-safe, so it should not be called in a process context;
89 * get_jack_port_addresses() will fill in a structure set up by this method.
91 void
92 BufferSet::attach_buffers (PortSet& ports)
94 clear();
96 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
97 _buffers.push_back(BufferVec());
98 BufferVec& v = _buffers[*t];
100 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
101 assert(p->type() == *t);
102 v.push_back (0);
106 _count = ports.count();
107 _available = ports.count();
109 _is_mirror = true;
112 /** Write the JACK port addresses from a PortSet into our data structures. This
113 * call assumes that attach_buffers() has already been called for the same PortSet.
114 * Does not allocate, so RT-safe.
116 void
117 BufferSet::get_jack_port_addresses (PortSet& ports, framecnt_t nframes)
119 assert (_count == ports.count ());
120 assert (_available == ports.count ());
121 assert (_is_mirror);
123 assert (_buffers.size() == DataType::num_types);
125 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
126 BufferVec& v = _buffers[*t];
128 assert (v.size() == ports.num_ports (*t));
130 int i = 0;
131 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
132 v[i] = &p->get_buffer (nframes);
133 ++i;
138 /** Ensure that there are @a num_buffers buffers of type @a type available,
139 * each of size at least @a buffer_size
141 void
142 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
144 assert(type != DataType::NIL);
145 assert(type < _buffers.size());
147 if (num_buffers == 0) {
148 return;
151 // The vector of buffers of the type we care about
152 BufferVec& bufs = _buffers[type];
154 // If we're a mirror just make sure we're ok
155 if (_is_mirror) {
156 assert(_count.get(type) >= num_buffers);
157 assert(bufs[0]->type() == type);
158 return;
161 // If there's not enough or they're too small, just nuke the whole thing and
162 // rebuild it (so I'm lazy..)
163 if (bufs.size() < num_buffers
164 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
166 // Nuke it
167 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
168 delete (*i);
170 bufs.clear();
172 // Rebuild it
173 for (size_t i = 0; i < num_buffers; ++i) {
174 bufs.push_back(Buffer::create(type, buffer_capacity));
177 _available.set(type, num_buffers);
178 _count.set (type, num_buffers);
181 #ifdef LV2_SUPPORT
182 // Ensure enough low level MIDI format buffers are available for conversion
183 // in both directions (input & output, out-of-place)
184 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
185 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
186 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
189 #endif
191 #ifdef VST_SUPPORT
192 // As above but for VST
193 if (type == DataType::MIDI) {
194 while (_vst_buffers.size() < _buffers[type].size()) {
195 _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
198 #endif
200 // Post-conditions
201 assert(bufs[0]->type() == type);
202 assert(bufs.size() >= num_buffers);
203 assert(bufs.size() == _available.get(type));
204 assert(bufs[0]->capacity() >= buffer_capacity);
207 /** Ensure that the number of buffers of each type @a type matches @a chns
208 * and each buffer is of size at least @a buffer_capacity
210 void
211 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
213 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
214 ensure_buffers (*i, chns.get (*i), buffer_capacity);
218 /** Get the capacity (size) of the available buffers of the given type.
220 * All buffers of a certain type always have the same capacity.
222 size_t
223 BufferSet::buffer_capacity(DataType type) const
225 assert(_available.get(type) > 0);
226 return _buffers[type][0]->capacity();
229 Buffer&
230 BufferSet::get(DataType type, size_t i)
232 assert(i < _available.get(type));
233 return *_buffers[type][i];
236 const Buffer&
237 BufferSet::get(DataType type, size_t i) const
239 assert(i < _available.get(type));
240 return *_buffers[type][i];
243 #ifdef LV2_SUPPORT
245 LV2EventBuffer&
246 BufferSet::get_lv2_midi(bool input, size_t i)
248 assert (count().get(DataType::MIDI) > i);
250 MidiBuffer& mbuf = get_midi(i);
251 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
252 LV2EventBuffer* ebuf = b.second;
254 ebuf->reset();
255 if (input) {
256 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
257 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
258 uint32_t type = LV2Plugin::midi_event_type();
259 #ifndef NDEBUG
260 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", ev.size()));
261 for (uint16_t x = 0; x < ev.size(); ++x) {
262 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) ev.buffer()[x]));
264 #endif
265 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
268 return *ebuf;
271 void
272 BufferSet::flush_lv2_midi(bool input, size_t i)
274 MidiBuffer& mbuf = get_midi(i);
275 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
276 LV2EventBuffer* ebuf = b.second;
278 mbuf.silence(0, 0);
279 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
280 uint32_t frames;
281 uint32_t subframes;
282 uint16_t type;
283 uint16_t size;
284 uint8_t* data;
285 ebuf->get_event(&frames, &subframes, &type, &size, &data);
286 #ifndef NDEBUG
287 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
288 for (uint16_t x = 0; x < size; ++x) {
289 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
291 #endif
292 mbuf.push_back(frames, size, data);
296 #endif /* LV2_SUPPORT */
298 #ifdef VST_SUPPORT
300 VstEvents*
301 BufferSet::get_vst_midi (size_t b)
303 MidiBuffer& m = get_midi (b);
304 VSTBuffer* vst = _vst_buffers[b];
306 vst->clear ();
308 for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
309 vst->push_back (*i);
312 return vst->events();
315 BufferSet::VSTBuffer::VSTBuffer (size_t c)
316 : _capacity (c)
318 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
319 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
321 if (_events == 0 || _midi_events == 0) {
322 free (_events);
323 free (_midi_events);
324 throw failed_constructor ();
327 _events->numEvents = 0;
328 _events->reserved = 0;
331 BufferSet::VSTBuffer::~VSTBuffer ()
333 free (_events);
334 free (_midi_events);
337 void
338 BufferSet::VSTBuffer::clear ()
340 _events->numEvents = 0;
343 void
344 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
346 if (ev.size() > 3) {
347 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
348 they won't be passed to VST plugins or VSTis
350 return;
352 int const n = _events->numEvents;
353 assert (n < (int) _capacity);
355 _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
356 VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
358 v->type = kVstMidiType;
359 v->byteSize = sizeof (VstMidiEvent);
360 v->deltaFrames = ev.time ();
362 v->flags = 0;
363 v->detune = 0;
364 v->noteLength = 0;
365 v->noteOffset = 0;
366 v->reserved1 = 0;
367 v->reserved2 = 0;
368 v->noteOffVelocity = 0;
369 memcpy (v->midiData, ev.buffer(), ev.size());
370 v->midiData[3] = 0;
372 _events->numEvents++;
375 #endif /* VST_SUPPORT */
377 void
378 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
380 assert(available() >= in.count());
382 // Copy all buffers 1:1
383 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
384 BufferSet::iterator o = begin(*t);
385 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
386 o->read_from (*i, nframes);
390 set_count(in.count());
393 void
394 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
396 /* merge all input buffers into out existing buffers.
398 NOTE: if "in" contains more buffers than this set,
399 we will drop the extra buffers.
403 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
404 BufferSet::iterator o = begin(*t);
405 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
406 o->merge_from (*i, nframes);
411 void
412 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
414 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
415 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
416 (*b)->silence (nframes, offset);
421 void
422 BufferSet::is_silent (bool yn)
424 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
425 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
426 (*b)->is_silent (yn);
432 } // namespace ARDOUR