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 // 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 assert(bufs[0]->type() == type);
137 assert(bufs.size() >= num_buffers);
138 assert(bufs.size() == _available.get(type));
139 assert(bufs[0]->capacity() >= buffer_capacity);
142 /** Get the capacity (size) of the available buffers of the given type.
144 * All buffers of a certain type always have the same capacity.
147 BufferSet::buffer_capacity(DataType type) const
149 assert(_available.get(type) > 0);
150 return _buffers[type][0]->capacity();
153 // FIXME: make 'in' const
155 BufferSet::read_from(BufferSet& in, nframes_t nframes, nframes_t offset)
157 assert(available() >= in.count());
159 // Copy all buffers 1:1
160 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
161 BufferSet::iterator o = begin(*t);
162 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
163 o->read_from(*i, nframes, offset);
167 set_count(in.count());
170 } // namespace ARDOUR