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.
21 #include "libardour-config.h"
26 #include "pbd/compose.h"
27 #include "ardour/buffer.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/debug.h"
30 #include "ardour/midi_buffer.h"
31 #include "ardour/port.h"
32 #include "ardour/port_set.h"
33 #include "ardour/audioengine.h"
35 #include "ardour/lv2_plugin.h"
36 #include "ardour/lv2_event_buffer.h"
39 #include "vestige/aeffectx.h"
44 /** Create a new, empty BufferSet */
45 BufferSet::BufferSet()
48 for (size_t i
=0; i
< DataType::num_types
; ++i
) {
49 _buffers
.push_back(BufferVec());
56 BufferSet::~BufferSet()
61 /** Destroy all contained buffers.
67 for (std::vector
<BufferVec
>::iterator i
= _buffers
.begin(); i
!= _buffers
.end(); ++i
) {
68 for (BufferVec::iterator j
= (*i
).begin(); j
!= (*i
).end(); ++j
) {
79 for (VSTBuffers::iterator i
= _vst_buffers
.begin(); i
!= _vst_buffers
.end(); ++i
) {
83 _vst_buffers
.clear ();
87 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
88 * This is quite expensive and not RT-safe, so it should not be called in a process context;
89 * get_jack_port_addresses() will fill in a structure set up by this method.
92 BufferSet::attach_buffers (PortSet
& ports
)
96 for (DataType::iterator t
= DataType::begin(); t
!= DataType::end(); ++t
) {
97 _buffers
.push_back(BufferVec());
98 BufferVec
& v
= _buffers
[*t
];
100 for (PortSet::iterator p
= ports
.begin(*t
); p
!= ports
.end(*t
); ++p
) {
101 assert(p
->type() == *t
);
106 _count
= ports
.count();
107 _available
= ports
.count();
112 /** Write the JACK port addresses from a PortSet into our data structures. This
113 * call assumes that attach_buffers() has already been called for the same PortSet.
114 * Does not allocate, so RT-safe.
117 BufferSet::get_jack_port_addresses (PortSet
& ports
, framecnt_t nframes
)
119 assert (_count
== ports
.count ());
120 assert (_available
== ports
.count ());
123 assert (_buffers
.size() == DataType::num_types
);
125 for (DataType::iterator t
= DataType::begin(); t
!= DataType::end(); ++t
) {
126 BufferVec
& v
= _buffers
[*t
];
128 assert (v
.size() == ports
.num_ports (*t
));
131 for (PortSet::iterator p
= ports
.begin(*t
); p
!= ports
.end(*t
); ++p
) {
132 v
[i
] = &p
->get_buffer (nframes
);
138 /** Ensure that there are @a num_buffers buffers of type @a type available,
139 * each of size at least @a buffer_size
142 BufferSet::ensure_buffers(DataType type
, size_t num_buffers
, size_t buffer_capacity
)
144 assert(type
!= DataType::NIL
);
145 assert(type
< _buffers
.size());
147 if (num_buffers
== 0) {
151 // The vector of buffers of the type we care about
152 BufferVec
& bufs
= _buffers
[type
];
154 // If we're a mirror just make sure we're ok
156 assert(_count
.get(type
) >= num_buffers
);
157 assert(bufs
[0]->type() == type
);
161 // If there's not enough or they're too small, just nuke the whole thing and
162 // rebuild it (so I'm lazy..)
163 if (bufs
.size() < num_buffers
164 || (bufs
.size() > 0 && bufs
[0]->capacity() < buffer_capacity
)) {
167 for (BufferVec::iterator i
= bufs
.begin(); i
!= bufs
.end(); ++i
) {
173 for (size_t i
= 0; i
< num_buffers
; ++i
) {
174 bufs
.push_back(Buffer::create(type
, buffer_capacity
));
177 _available
.set(type
, num_buffers
);
178 _count
.set (type
, num_buffers
);
182 // Ensure enough low level MIDI format buffers are available for conversion
183 // in both directions (input & output, out-of-place)
184 if (type
== DataType::MIDI
&& _lv2_buffers
.size() < _buffers
[type
].size() * 2 + 1) {
185 while (_lv2_buffers
.size() < _buffers
[type
].size() * 2) {
186 _lv2_buffers
.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity
)));
192 // As above but for VST
193 if (type
== DataType::MIDI
) {
194 while (_vst_buffers
.size() < _buffers
[type
].size()) {
195 _vst_buffers
.push_back (new VSTBuffer (buffer_capacity
));
201 assert(bufs
[0]->type() == type
);
202 assert(bufs
.size() >= num_buffers
);
203 assert(bufs
.size() == _available
.get(type
));
204 assert(bufs
[0]->capacity() >= buffer_capacity
);
207 /** Ensure that the number of buffers of each type @a type matches @a chns
208 * and each buffer is of size at least @a buffer_capacity
211 BufferSet::ensure_buffers(const ChanCount
& chns
, size_t buffer_capacity
)
213 for (DataType::iterator i
= DataType::begin(); i
!= DataType::end(); ++i
) {
214 ensure_buffers (*i
, chns
.get (*i
), buffer_capacity
);
218 /** Get the capacity (size) of the available buffers of the given type.
220 * All buffers of a certain type always have the same capacity.
223 BufferSet::buffer_capacity(DataType type
) const
225 assert(_available
.get(type
) > 0);
226 return _buffers
[type
][0]->capacity();
230 BufferSet::get(DataType type
, size_t i
)
232 assert(i
< _available
.get(type
));
233 return *_buffers
[type
][i
];
237 BufferSet::get(DataType type
, size_t i
) const
239 assert(i
< _available
.get(type
));
240 return *_buffers
[type
][i
];
246 BufferSet::get_lv2_midi(bool input
, size_t i
)
248 assert (count().get(DataType::MIDI
) > i
);
250 MidiBuffer
& mbuf
= get_midi(i
);
251 LV2Buffers::value_type b
= _lv2_buffers
.at(i
* 2 + (input
? 0 : 1));
252 LV2EventBuffer
* ebuf
= b
.second
;
256 for (MidiBuffer::iterator e
= mbuf
.begin(); e
!= mbuf
.end(); ++e
) {
257 const Evoral::MIDIEvent
<framepos_t
> ev(*e
, false);
258 uint32_t type
= LV2Plugin::midi_event_type();
260 DEBUG_TRACE (PBD::DEBUG::LV2
, string_compose ("(FLUSH) MIDI event of size %1\n", ev
.size()));
261 for (uint16_t x
= 0; x
< ev
.size(); ++x
) {
262 DEBUG_TRACE (PBD::DEBUG::LV2
, string_compose ("\tByte[%1] = %2\n", x
, (int) ev
.buffer()[x
]));
265 ebuf
->append(ev
.time(), 0, type
, ev
.size(), ev
.buffer());
272 BufferSet::flush_lv2_midi(bool input
, size_t i
)
274 MidiBuffer
& mbuf
= get_midi(i
);
275 LV2Buffers::value_type b
= _lv2_buffers
.at(i
* 2 + (input
? 0 : 1));
276 LV2EventBuffer
* ebuf
= b
.second
;
279 for (ebuf
->rewind(); ebuf
->is_valid(); ebuf
->increment()) {
285 ebuf
->get_event(&frames
, &subframes
, &type
, &size
, &data
);
287 DEBUG_TRACE (PBD::DEBUG::LV2
, string_compose ("(FLUSH) MIDI event of size %1\n", size
));
288 for (uint16_t x
= 0; x
< size
; ++x
) {
289 DEBUG_TRACE (PBD::DEBUG::LV2
, string_compose ("\tByte[%1] = %2\n", x
, (int) data
[x
]));
292 mbuf
.push_back(frames
, size
, data
);
296 #endif /* LV2_SUPPORT */
301 BufferSet::get_vst_midi (size_t b
)
303 MidiBuffer
& m
= get_midi (b
);
304 VSTBuffer
* vst
= _vst_buffers
[b
];
308 for (MidiBuffer::iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
312 return vst
->events();
315 BufferSet::VSTBuffer::VSTBuffer (size_t c
)
318 _events
= static_cast<VstEvents
*> (malloc (sizeof (VstEvents
) + _capacity
* sizeof (VstEvent
*)));
319 _midi_events
= static_cast<VstMidiEvent
*> (malloc (sizeof (VstMidiEvent
) * _capacity
));
321 if (_events
== 0 || _midi_events
== 0) {
324 throw failed_constructor ();
327 _events
->numEvents
= 0;
328 _events
->reserved
= 0;
331 BufferSet::VSTBuffer::~VSTBuffer ()
338 BufferSet::VSTBuffer::clear ()
340 _events
->numEvents
= 0;
344 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent
<framepos_t
> const & ev
)
347 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
348 they won't be passed to VST plugins or VSTis
352 int const n
= _events
->numEvents
;
353 assert (n
< (int) _capacity
);
355 _events
->events
[n
] = reinterpret_cast<VstEvent
*> (_midi_events
+ n
);
356 VstMidiEvent
* v
= reinterpret_cast<VstMidiEvent
*> (_events
->events
[n
]);
358 v
->type
= kVstMidiType
;
359 v
->byteSize
= sizeof (VstMidiEvent
);
360 v
->deltaFrames
= ev
.time ();
368 v
->noteOffVelocity
= 0;
369 memcpy (v
->midiData
, ev
.buffer(), ev
.size());
372 _events
->numEvents
++;
375 #endif /* VST_SUPPORT */
378 BufferSet::read_from (const BufferSet
& in
, framecnt_t nframes
)
380 assert(available() >= in
.count());
382 // Copy all buffers 1:1
383 for (DataType::iterator t
= DataType::begin(); t
!= DataType::end(); ++t
) {
384 BufferSet::iterator o
= begin(*t
);
385 for (BufferSet::const_iterator i
= in
.begin(*t
); i
!= in
.end(*t
); ++i
, ++o
) {
386 o
->read_from (*i
, nframes
);
390 set_count(in
.count());
394 BufferSet::merge_from (const BufferSet
& in
, framecnt_t nframes
)
396 /* merge all input buffers into out existing buffers.
398 NOTE: if "in" contains more buffers than this set,
399 we will drop the extra buffers.
403 for (DataType::iterator t
= DataType::begin(); t
!= DataType::end(); ++t
) {
404 BufferSet::iterator o
= begin(*t
);
405 for (BufferSet::const_iterator i
= in
.begin(*t
); i
!= in
.end(*t
) && o
!= end (*t
); ++i
, ++o
) {
406 o
->merge_from (*i
, nframes
);
412 BufferSet::silence (framecnt_t nframes
, framecnt_t offset
)
414 for (std::vector
<BufferVec
>::iterator i
= _buffers
.begin(); i
!= _buffers
.end(); ++i
) {
415 for (BufferVec::iterator b
= i
->begin(); b
!= i
->end(); ++b
) {
416 (*b
)->silence (nframes
, offset
);
422 BufferSet::is_silent (bool yn
)
424 for (std::vector
<BufferVec
>::iterator i
= _buffers
.begin(); i
!= _buffers
.end(); ++i
) {
425 for (BufferVec::iterator b
= i
->begin(); b
!= i
->end(); ++b
) {
426 (*b
)->is_silent (yn
);
432 } // namespace ARDOUR