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()
43 for (size_t i=0; i < DataType::num_types; ++i) {
44 _buffers.push_back(BufferVec());
51 BufferSet::~BufferSet()
56 /** Destroy all contained buffers.
62 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
63 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
74 /** Make this BufferSet a direct mirror of a PortSet's buffers.
77 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
81 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
82 _buffers.push_back(BufferVec());
83 BufferVec& v = _buffers[*t];
85 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
86 assert(p->type() == *t);
87 v.push_back(&(p->get_buffer(nframes, offset)));
91 _count = ports.count();
92 _available = ports.count();
97 /** Ensure that there are @a num_buffers buffers of type @a type available,
98 * each of size at least @a buffer_size
101 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
103 assert(type != DataType::NIL);
104 assert(type < _buffers.size());
106 if (num_buffers == 0) {
110 // The vector of buffers of the type we care about
111 BufferVec& bufs = _buffers[type];
113 // If we're a mirror just make sure we're ok
115 assert(_count.get(type) >= num_buffers);
116 assert(bufs[0]->type() == type);
120 // If there's not enough or they're too small, just nuke the whole thing and
121 // rebuild it (so I'm lazy..)
122 if (bufs.size() < num_buffers
123 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
126 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
132 for (size_t i = 0; i < num_buffers; ++i) {
133 bufs.push_back(Buffer::create(type, buffer_capacity));
136 _available.set(type, num_buffers);
137 _count.set (type, num_buffers);
141 // Ensure enough low level MIDI format buffers are available for conversion
142 // in both directions (input & output, out-of-place)
143 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
144 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
145 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
151 assert(bufs[0]->type() == type);
152 assert(bufs.size() >= num_buffers);
153 assert(bufs.size() == _available.get(type));
154 assert(bufs[0]->capacity() >= buffer_capacity);
157 /** Ensure that the number of buffers of each type @a type matches @a chns
158 * and each buffer is of size at least @a buffer_capacity
161 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
163 if (chns == ChanCount::ZERO) {
167 // If we're a mirror just make sure we're ok
169 assert(_count >= chns);
173 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
175 // The vector of buffers of this type
176 BufferVec& bufs = _buffers[*t];
178 uint32_t nbufs = chns.get (*t);
182 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
189 // If there's not enough or they're too small, just nuke the whole thing and
190 // rebuild it (so I'm lazy..)
191 if (bufs.size() < nbufs
192 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
195 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
201 for (size_t i = 0; i < nbufs; ++i) {
202 bufs.push_back(Buffer::create(*t, buffer_capacity));
205 _available.set (*t, nbufs);
209 // Ensure enough low level MIDI format buffers are available for conversion
210 // in both directions (input & output, out-of-place)
211 if (*t == DataType::MIDI && _lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2 + 1) {
212 while (_lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2) {
213 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
219 assert(bufs[0]->type() == *t);
220 assert(bufs.size() == _available.get(*t));
221 assert(bufs[0]->capacity() >= buffer_capacity);
224 assert (available() >= chns);
227 /** Get the capacity (size) of the available buffers of the given type.
229 * All buffers of a certain type always have the same capacity.
232 BufferSet::buffer_capacity(DataType type) const
234 assert(_available.get(type) > 0);
235 return _buffers[type][0]->capacity();
239 BufferSet::get(DataType type, size_t i)
241 assert(i < _available.get(type));
242 return *_buffers[type][i];
246 BufferSet::get(DataType type, size_t i) const
248 assert(i < _available.get(type));
249 return *_buffers[type][i];
255 BufferSet::get_lv2_midi(bool input, size_t i)
257 MidiBuffer& mbuf = get_midi(i);
258 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
259 LV2EventBuffer* ebuf = b.second;
263 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
264 const Evoral::MIDIEvent<nframes_t> ev(*e, false);
265 uint32_t type = LV2Plugin::midi_event_type();
266 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
273 BufferSet::flush_lv2_midi(bool input, size_t i)
275 MidiBuffer& mbuf = get_midi(i);
276 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
277 LV2EventBuffer* ebuf = b.second;
280 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
286 ebuf->get_event(&frames, &subframes, &type, &size, &data);
287 mbuf.push_back(frames, size, data);
294 BufferSet::read_from (const BufferSet& in, nframes_t nframes)
296 assert(available() >= in.count());
298 // Copy all buffers 1:1
299 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
300 BufferSet::iterator o = begin(*t);
301 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
302 o->read_from (*i, nframes);
306 set_count(in.count());
310 BufferSet::merge_from (const BufferSet& in, nframes_t nframes)
312 /* merge all input buffers into out existing buffers.
314 NOTE: if "in" contains more buffers than this set,
315 we will drop the extra buffers.
319 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
320 BufferSet::iterator o = begin(*t);
321 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
322 o->merge_from (*i, nframes);
328 BufferSet::silence (nframes_t nframes, nframes_t offset)
330 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
331 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
332 (*b)->silence (nframes, offset);
338 BufferSet::is_silent (bool yn)
340 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
341 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
342 (*b)->is_silent (yn);
348 } // namespace ARDOUR