Strip trailing whitespace from waf scripts.
[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 {
38         for (size_t i=0; i < DataType::num_types; ++i) {
39                 _buffers.push_back(BufferVec());
40         }
41
42         _count.reset();
43         _available.reset();
44 }
45
46 BufferSet::~BufferSet()
47 {
48         clear();
49 }
50
51 /** Destroy all contained buffers.
52  */
53 void
54 BufferSet::clear()
55 {
56         if (!_is_mirror) {
57                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
58                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
59                                 delete *j;
60                         }
61                         (*i).clear();
62                 }
63         }
64         _buffers.clear();
65         _count.reset();
66         _available.reset();
67 }
68
69 /** Make this BufferSet a direct mirror of a PortSet's buffers.
70  */
71 void
72 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
73 {
74         clear();
75
76         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
77                 _buffers.push_back(BufferVec());
78                 BufferVec& v = _buffers[*t];
79
80                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
81                         assert(p->type() == *t);
82                         v.push_back(&(p->get_buffer(nframes, offset)));
83                 }
84         }
85         
86         _count = ports.count();
87
88         _is_mirror = true;
89 }
90
91 /** Ensure that there are @a num_buffers buffers of type @a type available,
92  * each of size at least @a buffer_size
93  */
94 void
95 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
96 {
97         assert(type != DataType::NIL);
98         assert(type < _buffers.size());
99
100         if (num_buffers == 0)
101                 return;
102
103         // The vector of buffers of the type we care about
104         BufferVec& bufs = _buffers[type];
105         
106         // If we're a mirror just make sure we're ok
107         if (_is_mirror) {
108                 assert(_count.get(type) >= num_buffers);
109                 assert(bufs[0]->type() == type);
110                 return;
111         }
112
113         // If there's not enough or they're too small, just nuke the whole thing and
114         // rebuild it (so I'm lazy..)
115         if (bufs.size() < num_buffers
116                         || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
117
118                 // Nuke it
119                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
120                         delete (*i);
121                 }
122                 bufs.clear();
123
124                 // Rebuild it
125                 for (size_t i = 0; i < num_buffers; ++i) {
126                         bufs.push_back(Buffer::create(type, buffer_capacity));
127                 }
128         
129                 _available.set(type, num_buffers);
130         }
131
132 #ifdef HAVE_SLV2
133         // Ensure enough low level MIDI format buffers are available for conversion
134         // in both directions (input & output, out-of-place)
135         if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2) {
136                 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
137                         _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
138                 }
139         }
140 #endif
141
142         // Post-conditions
143         assert(bufs[0]->type() == type);
144         assert(bufs.size() >= num_buffers);
145         assert(bufs.size() == _available.get(type));
146         assert(bufs[0]->capacity() >= buffer_capacity);
147 }
148
149 /** Get the capacity (size) of the available buffers of the given type.
150  *
151  * All buffers of a certain type always have the same capacity.
152  */
153 size_t
154 BufferSet::buffer_capacity(DataType type) const
155 {
156         assert(_available.get(type) > 0);
157         return _buffers[type][0]->capacity();
158 }
159
160 Buffer&
161 BufferSet::get(DataType type, size_t i)
162 {
163         assert(i <= _count.get(type));
164         return *_buffers[type][i];
165 }
166
167 #ifdef HAVE_SLV2
168
169 LV2EventBuffer&
170 BufferSet::get_lv2_midi(bool input, size_t i)
171 {
172         MidiBuffer& mbuf = get_midi(i);
173         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
174         LV2EventBuffer* ebuf = b.second;
175         
176         ebuf->reset();
177         if (input) {
178                 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
179                         const Evoral::MIDIEvent<nframes_t> ev(*e, false);
180                         uint32_t type = LV2Plugin::midi_event_type();
181                         ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
182                 }
183         }
184         return *ebuf;
185 }
186
187 void
188 BufferSet::flush_lv2_midi(bool input, size_t i)
189 {
190         MidiBuffer& mbuf = get_midi(i);
191         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
192         LV2EventBuffer* ebuf = b.second;
193
194         mbuf.silence(0, 0);
195         for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
196                 uint32_t frames;
197                 uint32_t subframes;
198                 uint16_t type;
199                 uint16_t size;
200                 uint8_t* data;
201                 ebuf->get_event(&frames, &subframes, &type, &size, &data);
202                 mbuf.push_back(frames, size, data);
203         }
204 }
205
206 #endif
207
208 // FIXME: make 'in' const
209 void
210 BufferSet::read_from (BufferSet& in, nframes_t nframes)
211 {
212         assert(available() >= in.count());
213
214         // Copy all buffers 1:1
215         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
216                 BufferSet::iterator o = begin(*t);
217                 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
218                         o->read_from (*i, nframes);
219                 }
220         }
221
222         set_count(in.count());
223 }
224
225 } // namespace ARDOUR
226