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__
24 #include "ardour/chan_count.h"
25 #include "ardour/data_type.h"
26 #include "ardour/types.h"
38 /** A set of buffers of various types.
40 * These are mainly accessed from Session and passed around as scratch buffers
41 * (eg as parameters to run() methods) to do in-place signal processing.
43 * There are two types of counts associated with a BufferSet - available,
44 * and the 'use count'. Available is the actual number of allocated buffers
45 * (and so is the maximum acceptable value for the use counts).
47 * The use counts are how things determine the form of their input and inform
48 * others the form of their output (eg what they did to the BufferSet).
49 * Setting the use counts is realtime safe.
59 void attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset = 0);
61 void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
63 const ChanCount& available() const { return _available; }
64 ChanCount& available() { return _available; }
66 const ChanCount& count() const { return _count; }
67 ChanCount& count() { return _count; }
69 void is_silent(bool yn) { _is_silent = yn; }
70 bool is_silent() const { return _is_silent; }
72 void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
74 size_t buffer_capacity(DataType type) const;
76 Buffer& get(DataType type, size_t i);
78 AudioBuffer& get_audio(size_t i) {
79 return (AudioBuffer&)get(DataType::AUDIO, i);
82 MidiBuffer& get_midi(size_t i) {
83 return (MidiBuffer&)get(DataType::MIDI, i);
87 /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with plugins.
88 * The index here corresponds directly to MIDI buffer numbers (i.e. the index
89 * passed to get_midi), translation back and forth will happen as needed */
90 LV2EventBuffer& get_lv2_midi(bool input, size_t i);
92 /** Flush modified LV2 event output buffers back to Ardour buffers */
93 void flush_lv2_midi(bool input, size_t i);
96 void read_from(BufferSet& in, nframes_t nframes);
99 // FIXME: possible to combine these? templates?
101 class audio_iterator {
103 AudioBuffer& operator*() { return _set.get_audio(_index); }
104 AudioBuffer* operator->() { return &_set.get_audio(_index); }
105 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
106 bool operator==(const audio_iterator& other) { return (_index == other._index); }
107 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
110 friend class BufferSet;
112 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
118 audio_iterator audio_begin() { return audio_iterator(*this, 0); }
119 audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
124 Buffer& operator*() { return _set.get(_type, _index); }
125 Buffer* operator->() { return &_set.get(_type, _index); }
126 iterator& operator++() { ++_index; return *this; } // yes, prefix only
127 bool operator==(const iterator& other) { return (_index == other._index); }
128 bool operator!=(const iterator& other) { return (_index != other._index); }
129 iterator operator=(const iterator& other) {
130 _set = other._set; _type = other._type; _index = other._index; return *this;
134 friend class BufferSet;
136 iterator(BufferSet& list, DataType type, size_t index)
137 : _set(list), _type(type), _index(index) {}
144 iterator begin(DataType type) { return iterator(*this, type, 0); }
145 iterator end(DataType type) { return iterator(*this, type, _count.get(type)); }
148 typedef std::vector<Buffer*> BufferVec;
150 /// Vector of vectors, indexed by DataType
151 std::vector<BufferVec> _buffers;
154 /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
155 typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
156 LV2Buffers _lv2_buffers;
159 /// Use counts (there may be more actual buffers than this)
162 /// Available counts (number of buffers actually allocated)
163 ChanCount _available;
165 /// Whether we (don't) 'own' the contained buffers (otherwise we mirror a PortSet)
168 /// Whether the buffer set should be considered silent
173 } // namespace ARDOUR
175 #endif // __ardour_buffer_set_h__