separate solo & listen. some minor fixes and additional related fixes still to come
[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 <iostream>
20 #include <algorithm>
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"
27 #ifdef HAVE_SLV2
28 #include "ardour/lv2_plugin.h"
29 #include "ardour/lv2_event_buffer.h"
30 #endif
31
32 namespace ARDOUR {
33
34 /** Create a new, empty BufferSet */
35 BufferSet::BufferSet()
36         : _is_mirror(false)
37         , _is_silent(false)
38 {
39         for (size_t i=0; i < DataType::num_types; ++i) {
40                 _buffers.push_back(BufferVec());
41         }
42
43         _count.reset();
44         _available.reset();
45 }
46
47 BufferSet::~BufferSet()
48 {
49         clear();
50 }
51
52 /** Destroy all contained buffers.
53  */
54 void
55 BufferSet::clear()
56 {
57         if (!_is_mirror) {
58                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
59                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
60                                 delete *j;
61                         }
62                         (*i).clear();
63                 }
64         }
65         _buffers.clear();
66         _count.reset();
67         _available.reset();
68 }
69
70 /** Make this BufferSet a direct mirror of a PortSet's buffers.
71  */
72 void
73 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
74 {
75         clear();
76
77         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
78                 _buffers.push_back(BufferVec());
79                 BufferVec& v = _buffers[*t];
80
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)));
84                 }
85         }
86         
87         _count = ports.count();
88         _available = ports.count();
89
90         _is_mirror = true;
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
102         if (num_buffers == 0) {
103                 return;
104         }
105
106         // The vector of buffers of the type we care about
107         BufferVec& bufs = _buffers[type];
108         
109         // If we're a mirror just make sure we're ok
110         if (_is_mirror) {
111                 assert(_count.get(type) >= num_buffers);
112                 assert(bufs[0]->type() == type);
113                 return;
114         }
115
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)) {
120
121                 // Nuke it
122                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
123                         delete (*i);
124                 }
125                 bufs.clear();
126
127                 // Rebuild it
128                 for (size_t i = 0; i < num_buffers; ++i) {
129                         bufs.push_back(Buffer::create(type, buffer_capacity));
130                 }
131         
132                 _available.set(type, num_buffers);
133                 _count.set (type, num_buffers);
134         }
135
136 #ifdef HAVE_SLV2
137         // Ensure enough low level MIDI format buffers are available for conversion
138         // in both directions (input & output, out-of-place)
139         if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
140                 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
141                         _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
142                 }
143         }
144 #endif
145
146         // Post-conditions
147         assert(bufs[0]->type() == type);
148         assert(bufs.size() >= num_buffers);
149         assert(bufs.size() == _available.get(type));
150         assert(bufs[0]->capacity() >= buffer_capacity);
151 }
152
153 /** Ensure that the number of buffers of each type @a type matches @a chns
154  * and each buffer is of size at least @a buffer_capacity
155  */
156 void
157 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
158 {
159         if (chns == ChanCount::ZERO) {
160                 return;
161         }
162
163         // If we're a mirror just make sure we're ok
164         if (_is_mirror) {
165                 assert(_count >= chns);
166                 return;
167         }
168
169         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
170
171                 // The vector of buffers of this type
172                 BufferVec& bufs = _buffers[*t];
173
174                 uint32_t nbufs = chns.get (*t);
175                 
176                 if (nbufs == 0) {
177                         // Nuke it
178                         for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
179                                 delete (*i);
180                         }
181                         bufs.clear();
182                         continue;
183                 }
184
185                 // If there's not enough or they're too small, just nuke the whole thing and
186                 // rebuild it (so I'm lazy..)
187                 if (bufs.size() < nbufs
188                     || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
189                         
190                         // Nuke it
191                         for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
192                                 delete (*i);
193                         }
194                         bufs.clear();
195                         
196                         // Rebuild it
197                         for (size_t i = 0; i < nbufs; ++i) {
198                                 bufs.push_back(Buffer::create(*t, buffer_capacity));
199                         }
200                         
201                         _available.set (*t, nbufs);
202                 }
203                 
204 #ifdef HAVE_SLV2
205                 // Ensure enough low level MIDI format buffers are available for conversion
206                 // in both directions (input & output, out-of-place)
207                 if (*t == DataType::MIDI && _lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2 + 1) {
208                         while (_lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2) {
209                                 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
210                         }
211                 }
212 #endif
213                 
214                 // Post-conditions
215                 assert(bufs[0]->type() == *t);
216                 assert(bufs.size() == _available.get(*t));
217                 assert(bufs[0]->capacity() >= buffer_capacity);
218         }
219         
220         assert (available() == chns);
221 }
222
223 /** Get the capacity (size) of the available buffers of the given type.
224  *
225  * All buffers of a certain type always have the same capacity.
226  */
227 size_t
228 BufferSet::buffer_capacity(DataType type) const
229 {
230         assert(_available.get(type) > 0);
231         return _buffers[type][0]->capacity();
232 }
233
234 Buffer&
235 BufferSet::get(DataType type, size_t i)
236 {
237         assert(i < _available.get(type));
238         return *_buffers[type][i];
239 }
240
241 #ifdef HAVE_SLV2
242
243 LV2EventBuffer&
244 BufferSet::get_lv2_midi(bool input, size_t i)
245 {
246         MidiBuffer& mbuf = get_midi(i);
247         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
248         LV2EventBuffer* ebuf = b.second;
249         
250         ebuf->reset();
251         if (input) {
252                 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
253                         const Evoral::MIDIEvent<nframes_t> ev(*e, false);
254                         uint32_t type = LV2Plugin::midi_event_type();
255                         ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
256                 }
257         }
258         return *ebuf;
259 }
260
261 void
262 BufferSet::flush_lv2_midi(bool input, size_t i)
263 {
264         MidiBuffer& mbuf = get_midi(i);
265         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
266         LV2EventBuffer* ebuf = b.second;
267
268         mbuf.silence(0, 0);
269         for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
270                 uint32_t frames;
271                 uint32_t subframes;
272                 uint16_t type;
273                 uint16_t size;
274                 uint8_t* data;
275                 ebuf->get_event(&frames, &subframes, &type, &size, &data);
276                 mbuf.push_back(frames, size, data);
277         }
278 }
279
280 #endif
281
282 // FIXME: make 'in' const
283 void
284 BufferSet::read_from (BufferSet& in, nframes_t nframes)
285 {
286         assert(available() >= in.count());
287
288         // Copy all buffers 1:1
289         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
290                 BufferSet::iterator o = begin(*t);
291                 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
292                         o->read_from (*i, nframes);
293                 }
294         }
295
296         set_count(in.count());
297 }
298
299 // FIXME: make 'in' const
300 void
301 BufferSet::merge_from (BufferSet& in, nframes_t nframes)
302 {
303         /* merge all input buffers into out existing buffers.
304
305            NOTE: if "in" contains more buffers than this set,
306            we will drop the extra buffers.
307
308         */
309
310         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
311                 BufferSet::iterator o = begin(*t);
312                 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
313                         o->merge_from (*i, nframes);
314                 }
315         }
316 }
317
318 void
319 BufferSet::silence (nframes_t nframes, nframes_t offset)
320 {
321         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
322                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
323                         (*b)->silence (nframes, offset);
324                 }
325         }
326 }
327
328 } // namespace ARDOUR
329