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.
20 #include <ardour/buffer_set.h>
21 #include <ardour/buffer.h>
22 #include <ardour/port.h>
23 #include <ardour/port_set.h>
27 /** Create a new, empty BufferSet */
28 BufferSet::BufferSet()
31 for (size_t i=0; i < DataType::num_types; ++i)
32 _buffers.push_back( BufferVec() );
38 BufferSet::~BufferSet()
43 /** Destroy all contained buffers.
49 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
50 for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
61 /** Make this BufferSet a direct mirror of a PortSet's buffers.
64 BufferSet::attach_buffers(PortSet& ports)
68 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
69 _buffers.push_back(BufferVec());
70 BufferVec& v = _buffers[*t];
72 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
73 assert(p->type() == *t);
74 v.push_back(&(p->get_buffer()));
79 _count = ports.count();
85 BufferSet::ensure_buffers(const ChanCount& count, size_t buffer_capacity)
87 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
88 ensure_buffers(*t, count.get(*t), buffer_capacity);
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());
101 assert(buffer_capacity > 0);
103 if (num_buffers == 0)
106 // FIXME: Kludge to make MIDI buffers larger (size is bytes, not frames)
107 // See MidiPort::MidiPort
108 // We probably need a map<DataType, size_t> parameter for capacity
109 if (type == DataType::MIDI)
110 buffer_capacity *= 8;
112 // The vector of buffers of the type we care about
113 BufferVec& bufs = _buffers[type];
115 // If we're a mirror just make sure we're ok
117 assert(_count.get(type) >= num_buffers);
118 assert(bufs[0]->type() == type);
122 // If there's not enough or they're too small, just nuke the whole thing and
123 // rebuild it (so I'm lazy..)
124 if (bufs.size() < num_buffers
125 || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
128 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
134 for (size_t i=0; i < num_buffers; ++i) {
135 bufs.push_back(Buffer::create(type, buffer_capacity));
138 _available.set(type, num_buffers);
142 assert(bufs[0]->type() == type);
143 assert(bufs.size() >= num_buffers);
144 assert(bufs.size() == _available.get(type));
145 assert(bufs[0]->capacity() >= buffer_capacity);
148 /** Get the capacity (size) of the available buffers of the given type.
150 * All buffers of a certain type always have the same capacity.
153 BufferSet::buffer_capacity(DataType type) const
155 assert(_available.get(type) > 0);
156 return _buffers[type][0]->capacity();
159 // FIXME: make 'in' const
161 BufferSet::read_from(BufferSet& in, nframes_t nframes, nframes_t offset)
163 assert(available() >= in.count());
165 // Copy all buffers 1:1
166 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
167 BufferSet::iterator o = begin(*t);
168 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
169 o->read_from(*i, nframes, offset);
173 set_count(in.count());
176 } // namespace ARDOUR