Reading of MIDI CC from MIDI regions (MidiModel). UI still needs work though..
[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
27 namespace ARDOUR {
28
29 class Buffer;
30 class AudioBuffer;
31 class MidiBuffer;
32 class PortSet;
33
34
35 /** A set of buffers of various types.
36  *
37  * These are mainly accessed from Session and passed around as scratch buffers
38  * (eg as parameters to run() methods) to do in-place signal processing.
39  *
40  * There are two types of counts associated with a BufferSet - available,
41  * and the 'use count'.  Available is the actual number of allocated buffers
42  * (and so is the maximum acceptable value for the use counts).
43  *
44  * The use counts are how things determine the form of their input and inform
45  * others the form of their output (eg what they did to the BufferSet).
46  * Setting the use counts is realtime safe.
47  */
48 class BufferSet
49 {
50 public:
51         BufferSet();
52         ~BufferSet();
53
54         void clear();
55         
56         void attach_buffers(PortSet& ports);
57
58         void ensure_buffers(const ChanCount& count, size_t buffer_capacity);
59         void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
60
61         const ChanCount& available() const { return _available; }
62         ChanCount&       available()       { return _available; }
63
64         const ChanCount& count() const { return _count; }
65         ChanCount&       count()       { return _count; }
66
67         void set_count(const ChanCount& count) { _count = count; }
68         
69         size_t buffer_capacity(DataType type) const;
70
71         Buffer& get(DataType type, size_t i)
72         {
73                 assert(i <= _count.get(type));
74                 return *_buffers[type][i];
75         }
76
77         AudioBuffer& get_audio(size_t i)
78         {
79                 return (AudioBuffer&)get(DataType::AUDIO, i);
80         }
81         
82         MidiBuffer& get_midi(size_t i)
83         {
84                 return (MidiBuffer&)get(DataType::MIDI, i);
85         }
86
87         void read_from(BufferSet& in, jack_nframes_t nframes, jack_nframes_t offset=0);
88
89         // ITERATORS
90         
91         // FIXME: this is a filthy copy-and-paste mess
92         // FIXME: litter these with assertions
93         
94         class audio_iterator {
95         public:
96
97                 AudioBuffer& operator*()  { return _set.get_audio(_index); }
98                 AudioBuffer* operator->() { return &_set.get_audio(_index); }
99                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
100                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
101                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
102
103         private:
104                 friend class BufferSet;
105
106                 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
107
108                 BufferSet& _set;
109                 size_t    _index;
110         };
111
112         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
113         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
114
115         class iterator {
116         public:
117
118                 Buffer& operator*()  { return _set.get(_type, _index); }
119                 Buffer* operator->() { return &_set.get(_type, _index); }
120                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
121                 bool operator==(const iterator& other) { return (_index == other._index); }
122                 bool operator!=(const iterator& other) { return (_index != other._index); }
123                 iterator operator=(const iterator& other) { _set = other._set; _type = other._type; _index = other._index; return *this; }
124
125         private:
126                 friend class BufferSet;
127
128                 iterator(BufferSet& list, DataType type, size_t index)
129                         : _set(list), _type(type), _index(index) {}
130
131                 BufferSet& _set;
132                 DataType   _type;
133                 size_t     _index;
134         };
135
136         iterator begin(DataType type) { return iterator(*this, type, 0); }
137         iterator end(DataType type)   { return iterator(*this, type, _count.get(type)); }
138
139         
140 private:
141         typedef std::vector<Buffer*> BufferVec;
142
143         /// Vector of vectors, indexed by DataType
144         std::vector<BufferVec> _buffers;
145
146         /// Use counts (there may be more actual buffers than this)
147         ChanCount _count;
148
149         /// Available counts (number of buffers actually allocated)
150         ChanCount _available;
151
152         /// Whether we (don't) 'own' the contained buffers (otherwise we mirror a PortSet)
153         bool _is_mirror;
154 };
155
156
157 } // namespace ARDOUR
158
159 #endif // __ardour_buffer_set_h__