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);
62 void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
64 const ChanCount& available() const { return _available; }
65 ChanCount& available() { return _available; }
67 const ChanCount& count() const { return _count; }
68 ChanCount& count() { return _count; }
70 void is_silent(bool yn) { _is_silent = yn; }
71 bool is_silent() const { return _is_silent; }
72 void silence (nframes_t nframes, nframes_t offset);
74 void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
76 size_t buffer_capacity(DataType type) const;
78 Buffer& get(DataType type, size_t i);
80 AudioBuffer& get_audio(size_t i) {
81 return (AudioBuffer&)get(DataType::AUDIO, i);
84 MidiBuffer& get_midi(size_t i) {
85 return (MidiBuffer&)get(DataType::MIDI, i);
89 /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with plugins.
90 * The index here corresponds directly to MIDI buffer numbers (i.e. the index
91 * passed to get_midi), translation back and forth will happen as needed */
92 LV2EventBuffer& get_lv2_midi(bool input, size_t i);
94 /** Flush modified LV2 event output buffers back to Ardour buffers */
95 void flush_lv2_midi(bool input, size_t i);
98 void read_from(BufferSet& in, nframes_t nframes);
99 void merge_from(BufferSet& in, nframes_t nframes);
102 // FIXME: possible to combine these? templates?
104 class audio_iterator {
106 AudioBuffer& operator*() { return _set.get_audio(_index); }
107 AudioBuffer* operator->() { return &_set.get_audio(_index); }
108 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
109 bool operator==(const audio_iterator& other) { return (_index == other._index); }
110 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
113 friend class BufferSet;
115 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
121 audio_iterator audio_begin() { return audio_iterator(*this, 0); }
122 audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
127 Buffer& operator*() { return _set.get(_type, _index); }
128 Buffer* operator->() { return &_set.get(_type, _index); }
129 iterator& operator++() { ++_index; return *this; } // yes, prefix only
130 bool operator==(const iterator& other) { return (_index == other._index); }
131 bool operator!=(const iterator& other) { return (_index != other._index); }
132 iterator operator=(const iterator& other) {
133 _set = other._set; _type = other._type; _index = other._index; return *this;
137 friend class BufferSet;
139 iterator(BufferSet& list, DataType type, size_t index)
140 : _set(list), _type(type), _index(index) {}
147 iterator begin(DataType type) { return iterator(*this, type, 0); }
148 iterator end(DataType type) { return iterator(*this, type, _count.get(type)); }
151 typedef std::vector<Buffer*> BufferVec;
153 /// Vector of vectors, indexed by DataType
154 std::vector<BufferVec> _buffers;
157 /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
158 typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
159 LV2Buffers _lv2_buffers;
162 /// Use counts (there may be more actual buffers than this)
165 /// Available counts (number of buffers actually allocated)
166 ChanCount _available;
168 /// Whether we (don't) 'own' the contained buffers (otherwise we mirror a PortSet)
171 /// Whether the buffer set should be considered silent
176 } // namespace ARDOUR
178 #endif // __ardour_buffer_set_h__