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