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.
19 #ifndef __ardour_buffer_set_h__
20 #define __ardour_buffer_set_h__
23 #include "libardour-config.h"
28 #include "ardour/chan_count.h"
29 #include "ardour/data_type.h"
30 #include "ardour/types.h"
42 /** A set of buffers of various types.
44 * These are mainly accessed from Session and passed around as scratch buffers
45 * (eg as parameters to run() methods) to do in-place signal processing.
47 * There are two types of counts associated with a BufferSet - available,
48 * and the 'use count'. Available is the actual number of allocated buffers
49 * (and so is the maximum acceptable value for the use counts).
51 * The use counts are how things determine the form of their input and inform
52 * others the form of their output (eg what they did to the BufferSet).
53 * Setting the use counts is realtime safe.
63 void attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset = 0);
65 void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
66 void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
68 const ChanCount& available() const { return _available; }
69 ChanCount& available() { return _available; }
71 const ChanCount& count() const { return _count; }
72 ChanCount& count() { return _count; }
74 void is_silent(bool yn) { _is_silent = yn; }
75 bool is_silent() const { return _is_silent; }
76 void silence (nframes_t nframes, nframes_t offset);
77 bool is_mirror() const { return _is_mirror; }
79 void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
81 size_t buffer_capacity(DataType type) const;
83 Buffer& get(DataType type, size_t i);
85 AudioBuffer& get_audio(size_t i) {
86 return (AudioBuffer&)get(DataType::AUDIO, i);
89 MidiBuffer& get_midi(size_t i) {
90 return (MidiBuffer&)get(DataType::MIDI, i);
94 /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with plugins.
95 * The index here corresponds directly to MIDI buffer numbers (i.e. the index
96 * passed to get_midi), translation back and forth will happen as needed */
97 LV2EventBuffer& get_lv2_midi(bool input, size_t i);
99 /** Flush modified LV2 event output buffers back to Ardour buffers */
100 void flush_lv2_midi(bool input, size_t i);
103 void read_from(BufferSet& in, nframes_t nframes);
104 void merge_from(BufferSet& in, nframes_t nframes);
107 // FIXME: possible to combine these? templates?
109 class audio_iterator {
111 AudioBuffer& operator*() { return _set.get_audio(_index); }
112 AudioBuffer* operator->() { return &_set.get_audio(_index); }
113 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
114 bool operator==(const audio_iterator& other) { return (_index == other._index); }
115 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
118 friend class BufferSet;
120 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
126 audio_iterator audio_begin() { return audio_iterator(*this, 0); }
127 audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
129 class midi_iterator {
131 MidiBuffer& operator*() { return _set.get_midi(_index); }
132 MidiBuffer* operator->() { return &_set.get_midi(_index); }
133 midi_iterator& operator++() { ++_index; return *this; } // yes, prefix only
134 bool operator==(const midi_iterator& other) { return (_index == other._index); }
135 bool operator!=(const midi_iterator& other) { return (_index != other._index); }
138 friend class BufferSet;
140 midi_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
146 midi_iterator midi_begin() { return midi_iterator(*this, 0); }
147 midi_iterator midi_end() { return midi_iterator(*this, _count.n_midi()); }
151 Buffer& operator*() { return _set.get(_type, _index); }
152 Buffer* operator->() { return &_set.get(_type, _index); }
153 iterator& operator++() { ++_index; return *this; } // yes, prefix only
154 bool operator==(const iterator& other) { return (_index == other._index); }
155 bool operator!=(const iterator& other) { return (_index != other._index); }
156 iterator operator=(const iterator& other) {
157 _set = other._set; _type = other._type; _index = other._index; return *this;
161 friend class BufferSet;
163 iterator(BufferSet& list, DataType type, size_t index)
164 : _set(list), _type(type), _index(index) {}
171 iterator begin(DataType type) { return iterator(*this, type, 0); }
172 iterator end(DataType type) { return iterator(*this, type, _count.get(type)); }
175 typedef std::vector<Buffer*> BufferVec;
177 /// Vector of vectors, indexed by DataType
178 std::vector<BufferVec> _buffers;
181 /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
182 typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
183 LV2Buffers _lv2_buffers;
186 /// Use counts (there may be more actual buffers than this)
189 /// Available counts (number of buffers actually allocated)
190 ChanCount _available;
192 /// False if we 'own' the contained buffers, if true we mirror a PortSet)
195 /// Whether the buffer set should be considered silent
200 } // namespace ARDOUR
202 #endif // __ardour_buffer_set_h__