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