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 "ardour/buffer.h"
22 #include "ardour/buffer_set.h"
23 #include "ardour/midi_buffer.h"
24 #include "ardour/port.h"
25 #include "ardour/port_set.h"
26 #include "ardour/audioengine.h"
28 #include "ardour/lv2_plugin.h"
29 #include "ardour/lv2_event_buffer.h"
34 /** Create a new, empty BufferSet */
35 BufferSet::BufferSet()
39 for (size_t i=0; i < DataType::num_types; ++i) {
40 _buffers.push_back(BufferVec());
47 BufferSet::~BufferSet()
52 /** Destroy all contained buffers.
58 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
59 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
70 /** Make this BufferSet a direct mirror of a PortSet's buffers.
73 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
77 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
78 _buffers.push_back(BufferVec());
79 BufferVec& v = _buffers[*t];
81 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
82 assert(p->type() == *t);
83 v.push_back(&(p->get_buffer(nframes, offset)));
87 _count = ports.count();
88 _available = ports.count();
93 /** Ensure that there are @a num_buffers buffers of type @a type available,
94 * each of size at least @a buffer_size
97 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
99 assert(type != DataType::NIL);
100 assert(type < _buffers.size());
102 if (num_buffers == 0) {
106 // The vector of buffers of the type we care about
107 BufferVec& bufs = _buffers[type];
109 // If we're a mirror just make sure we're ok
111 assert(_count.get(type) >= num_buffers);
112 assert(bufs[0]->type() == type);
116 // If there's not enough or they're too small, just nuke the whole thing and
117 // rebuild it (so I'm lazy..)
118 if (bufs.size() < num_buffers
119 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
122 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
128 for (size_t i = 0; i < num_buffers; ++i) {
129 bufs.push_back(Buffer::create(type, buffer_capacity));
132 _available.set(type, num_buffers);
136 // Ensure enough low level MIDI format buffers are available for conversion
137 // in both directions (input & output, out-of-place)
138 if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
139 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
140 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
146 assert(bufs[0]->type() == type);
147 assert(bufs.size() >= num_buffers);
148 assert(bufs.size() == _available.get(type));
149 assert(bufs[0]->capacity() >= buffer_capacity);
152 /** Get the capacity (size) of the available buffers of the given type.
154 * All buffers of a certain type always have the same capacity.
157 BufferSet::buffer_capacity(DataType type) const
159 assert(_available.get(type) > 0);
160 return _buffers[type][0]->capacity();
164 BufferSet::get(DataType type, size_t i)
166 assert(i < _available.get(type));
167 return *_buffers[type][i];
173 BufferSet::get_lv2_midi(bool input, size_t i)
175 MidiBuffer& mbuf = get_midi(i);
176 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
177 LV2EventBuffer* ebuf = b.second;
181 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
182 const Evoral::MIDIEvent<nframes_t> ev(*e, false);
183 uint32_t type = LV2Plugin::midi_event_type();
184 ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
191 BufferSet::flush_lv2_midi(bool input, size_t i)
193 MidiBuffer& mbuf = get_midi(i);
194 LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
195 LV2EventBuffer* ebuf = b.second;
198 for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
204 ebuf->get_event(&frames, &subframes, &type, &size, &data);
205 mbuf.push_back(frames, size, data);
211 // FIXME: make 'in' const
213 BufferSet::read_from (BufferSet& in, nframes_t nframes)
215 assert(available() >= in.count());
217 // Copy all buffers 1:1
218 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
219 BufferSet::iterator o = begin(*t);
220 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
221 o->read_from (*i, nframes);
225 set_count(in.count());
228 } // namespace ARDOUR