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"
39 /** Create a new, empty BufferSet */
40 BufferSet::BufferSet()
44 for (size_t i=0; i < DataType::num_types; ++i) {
45 _buffers.push_back(BufferVec());
52 BufferSet::~BufferSet()
57 /** Destroy all contained buffers.
63 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
64 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
75 /** Make this BufferSet a direct mirror of a PortSet's buffers.
78 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
82 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
83 _buffers.push_back(BufferVec());
84 BufferVec& v = _buffers[*t];
86 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
87 assert(p->type() == *t);
88 v.push_back(&(p->get_buffer(nframes, offset)));
92 _count = ports.count();
93 _available = ports.count();
98 /** Ensure that there are @a num_buffers buffers of type @a type available,
99 * each of size at least @a buffer_size
102 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
104 assert(type != DataType::NIL);
105 assert(type < _buffers.size());
107 if (num_buffers == 0) {
111 // The vector of buffers of the type we care about
112 BufferVec& bufs = _buffers[type];
114 // If we're a mirror just make sure we're ok
116 assert(_count.get(type) >= num_buffers);
117 assert(bufs[0]->type() == type);
121 // If there's not enough or they're too small, just nuke the whole thing and
122 // rebuild it (so I'm lazy..)
123 if (bufs.size() < num_buffers
124 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
127 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
133 for (size_t i = 0; i < num_buffers; ++i) {
134 bufs.push_back(Buffer::create(type, buffer_capacity));
137 _available.set(type, num_buffers);
138 _count.set (type, num_buffers);
142 // Ensure enough low level MIDI format buffers are available for conversion
143 // in both directions (input & output, out-of-place)
144 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
145 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
146 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
152 assert(bufs[0]->type() == type);
153 assert(bufs.size() >= num_buffers);
154 assert(bufs.size() == _available.get(type));
155 assert(bufs[0]->capacity() >= buffer_capacity);
158 /** Ensure that the number of buffers of each type @a type matches @a chns
159 * and each buffer is of size at least @a buffer_capacity
162 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
164 if (chns == ChanCount::ZERO) {
168 // If we're a mirror just make sure we're ok
170 assert(_count >= chns);
174 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
176 // The vector of buffers of this type
177 BufferVec& bufs = _buffers[*t];
179 uint32_t nbufs = chns.get (*t);
183 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
190 // If there's not enough or they're too small, just nuke the whole thing and
191 // rebuild it (so I'm lazy..)
192 if (bufs.size() < nbufs
193 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
196 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
202 for (size_t i = 0; i < nbufs; ++i) {
203 bufs.push_back(Buffer::create(*t, buffer_capacity));
206 _available.set (*t, nbufs);
210 // Ensure enough low level MIDI format buffers are available for conversion
211 // in both directions (input & output, out-of-place)
212 if (*t == DataType::MIDI && _lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2 + 1) {
213 while (_lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2) {
214 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
220 assert(bufs[0]->type() == *t);
221 assert(bufs.size() == _available.get(*t));
222 assert(bufs[0]->capacity() >= buffer_capacity);
225 assert (available() == chns);
228 /** Get the capacity (size) of the available buffers of the given type.
230 * All buffers of a certain type always have the same capacity.
233 BufferSet::buffer_capacity(DataType type) const
235 assert(_available.get(type) > 0);
236 return _buffers[type][0]->capacity();
240 BufferSet::get(DataType type, size_t i)
242 assert(i < _available.get(type));
243 return *_buffers[type][i];
247 BufferSet::get(DataType type, size_t i) const
249 assert(i < _available.get(type));
250 return *_buffers[type][i];
256 BufferSet::get_lv2_midi(bool input, size_t i)
258 MidiBuffer& mbuf = get_midi(i);
259 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
260 LV2EventBuffer* ebuf = b.second;
264 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
265 const Evoral::MIDIEvent<nframes_t> ev(*e, false);
266 uint32_t type = LV2Plugin::midi_event_type();
267 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
274 BufferSet::flush_lv2_midi(bool input, size_t i)
276 MidiBuffer& mbuf = get_midi(i);
277 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
278 LV2EventBuffer* ebuf = b.second;
281 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
287 ebuf->get_event(&frames, &subframes, &type, &size, &data);
288 mbuf.push_back(frames, size, data);
295 BufferSet::read_from (const BufferSet& in, nframes_t nframes)
297 assert(available() >= in.count());
299 // Copy all buffers 1:1
300 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
301 BufferSet::iterator o = begin(*t);
302 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
303 o->read_from (*i, nframes);
307 set_count(in.count());
311 BufferSet::merge_from (const BufferSet& in, nframes_t nframes)
313 /* merge all input buffers into out existing buffers.
315 NOTE: if "in" contains more buffers than this set,
316 we will drop the extra buffers.
320 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
321 BufferSet::iterator o = begin(*t);
322 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
323 o->merge_from (*i, nframes);
329 BufferSet::silence (nframes_t nframes, nframes_t offset)
331 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
332 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
333 (*b)->silence (nframes, offset);
338 } // namespace ARDOUR