separate solo & listen. some minor fixes and additional related fixes still to come
[ardour.git] / libs / ardour / ardour / buffer_set.h
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 #ifndef __ardour_buffer_set_h__
20 #define __ardour_buffer_set_h__
21
22 #include <cassert>
23 #include <vector>
24 #include "ardour/chan_count.h"
25 #include "ardour/data_type.h"
26 #include "ardour/types.h"
27
28 namespace ARDOUR {
29
30 class Buffer;
31 class AudioBuffer;
32 class MidiBuffer;
33 class PortSet;
34 #ifdef HAVE_SLV2
35 class LV2EventBuffer;
36 #endif
37
38 /** A set of buffers of various types.
39  *
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.
42  *
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).
46  *
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.
50  */
51 class BufferSet
52 {
53 public:
54         BufferSet();
55         ~BufferSet();
56
57         void clear();
58         
59         void attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset = 0);
60
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);
63
64         const ChanCount& available() const { return _available; }
65         ChanCount&       available()       { return _available; }
66
67         const ChanCount& count() const { return _count; }
68         ChanCount&       count()       { return _count; }
69
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);
73
74         void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
75         
76         size_t buffer_capacity(DataType type) const;
77
78         Buffer& get(DataType type, size_t i);
79
80         AudioBuffer& get_audio(size_t i) {
81                 return (AudioBuffer&)get(DataType::AUDIO, i);
82         }
83         
84         MidiBuffer& get_midi(size_t i) {
85                 return (MidiBuffer&)get(DataType::MIDI, i);
86         }
87
88 #ifdef HAVE_SLV2
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);
93
94         /** Flush modified LV2 event output buffers back to Ardour buffers */
95         void flush_lv2_midi(bool input, size_t i);
96 #endif
97
98         void read_from(BufferSet& in, nframes_t nframes);
99         void merge_from(BufferSet& in, nframes_t nframes);
100
101         // ITERATORS
102         // FIXME: possible to combine these?  templates?
103         
104         class audio_iterator {
105         public:
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); }
111
112         private:
113                 friend class BufferSet;
114
115                 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
116
117                 BufferSet& _set;
118                 size_t    _index;
119         };
120
121         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
122         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
123
124
125         class iterator {
126         public:
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;
134                 }
135
136         private:
137                 friend class BufferSet;
138
139                 iterator(BufferSet& list, DataType type, size_t index)
140                         : _set(list), _type(type), _index(index) {}
141
142                 BufferSet& _set;
143                 DataType   _type;
144                 size_t     _index;
145         };
146
147         iterator begin(DataType type) { return iterator(*this, type, 0); }
148         iterator end(DataType type)   { return iterator(*this, type, _count.get(type)); }
149         
150 private:
151         typedef std::vector<Buffer*> BufferVec;
152
153         /// Vector of vectors, indexed by DataType
154         std::vector<BufferVec> _buffers;
155         
156 #ifdef HAVE_SLV2
157         /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
158         typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
159         LV2Buffers _lv2_buffers;
160 #endif
161
162         /// Use counts (there may be more actual buffers than this)
163         ChanCount _count;
164
165         /// Available counts (number of buffers actually allocated)
166         ChanCount _available;
167
168         /// Whether we (don't) 'own' the contained buffers (otherwise we mirror a PortSet)
169         bool _is_mirror;
170
171         /// Whether the buffer set should be considered silent
172         bool _is_silent;
173 };
174
175
176 } // namespace ARDOUR
177
178 #endif // __ardour_buffer_set_h__