rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / ardour / buffer_set.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
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)
7     any later version.
8     
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
12     for more details.
13     
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.
17 */
18
19 #include <algorithm>
20 #include <ardour/buffer_set.h>
21 #include <ardour/buffer.h>
22 #include <ardour/port.h>
23 #include <ardour/port_set.h>
24
25 namespace ARDOUR {
26
27 /** Create a new, empty BufferSet */
28 BufferSet::BufferSet()
29         : _is_mirror(false)
30 {
31         for (size_t i=0; i < DataType::num_types; ++i)
32                 _buffers.push_back( BufferVec() );
33
34         _count.reset();
35         _available.reset();
36 }
37
38 BufferSet::~BufferSet()
39 {
40         clear();
41 }
42
43 /** Destroy all contained buffers.
44  */
45 void
46 BufferSet::clear()
47 {
48         if (!_is_mirror) {
49                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
50                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
51                                 delete *j;
52                         }
53                         (*i).clear();
54                 }
55         }
56         _buffers.clear();
57         _count.reset();
58         _available.reset();
59 }
60
61 /** Make this BufferSet a direct mirror of a PortSet's buffers.
62  */
63 void
64 BufferSet::attach_buffers(PortSet& ports)
65 {
66         clear();
67
68         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
69                 _buffers.push_back(BufferVec());
70                 BufferVec& v = _buffers[*t];
71
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()));
75                 }
76
77         }
78         
79         _count = ports.count();
80
81         _is_mirror = true;
82 }
83
84 void
85 BufferSet::ensure_buffers(const ChanCount& count, size_t buffer_capacity)
86 {
87         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
88                 ensure_buffers(*t, count.get(*t), buffer_capacity);
89         }
90 }
91
92
93 /** Ensure that there are @a num_buffers buffers of type @a type available,
94  * each of size at least @a buffer_size
95  */
96 void
97 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
98 {
99         assert(type != DataType::NIL);
100         assert(type < _buffers.size());
101         assert(buffer_capacity > 0);
102
103         if (num_buffers == 0)
104                 return;
105
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;
111
112         // The vector of buffers of the type we care about
113         BufferVec& bufs = _buffers[type];
114         
115         // If we're a mirror just make sure we're ok
116         if (_is_mirror) {
117                 assert(_count.get(type) >= num_buffers);
118                 assert(bufs[0]->type() == type);
119                 return;
120         }
121
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)) {
126
127                 // Nuke it
128                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
129                         delete (*i);
130                 }
131                 bufs.clear();
132
133                 // Rebuild it
134                 for (size_t i=0; i < num_buffers; ++i) {
135                         bufs.push_back(Buffer::create(type, buffer_capacity));
136                 }
137         
138                 _available.set(type, num_buffers);
139         }
140
141         // Post-conditions
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);
146 }
147
148 /** Get the capacity (size) of the available buffers of the given type.
149  *
150  * All buffers of a certain type always have the same capacity.
151  */
152 size_t
153 BufferSet::buffer_capacity(DataType type) const
154 {
155         assert(_available.get(type) > 0);
156         return _buffers[type][0]->capacity();
157 }
158
159 // FIXME: make 'in' const
160 void
161 BufferSet::read_from(BufferSet& in, nframes_t nframes, nframes_t offset)
162 {
163         assert(available() >= in.count());
164
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);
170                 }
171         }
172
173         set_count(in.count());
174 }
175
176 } // namespace ARDOUR
177