bbbf7e3b8612e19486e7d962e084659810f417e3
[ardour.git] / libs / ardour / buffer_set.cc
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 #include <algorithm>
20 #include <ardour/buffer_set.h>
21 #include <ardour/buffer.h>
22 #include <ardour/port.h>
23 #include <ardour/port_set.h>
24
25 namespace ARDOUR {
26
27 /** Create a new, empty BufferSet */
28 BufferSet::BufferSet()
29         : _is_mirror(false)
30 {
31         for (size_t i=0; i < DataType::num_types; ++i)
32                 _buffers.push_back( BufferVec() );
33
34         _count.reset();
35         _available.reset();
36 }
37
38 BufferSet::~BufferSet()
39 {
40         clear();
41 }
42
43 /** Destroy all contained buffers.
44  */
45 void
46 BufferSet::clear()
47 {
48         if (!_is_mirror) {
49                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
50                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
51                                 delete *j;
52                         }
53                         (*i).clear();
54                 }
55         }
56         _buffers.clear();
57         _count.reset();
58         _available.reset();
59 }
60
61 /** Make this BufferSet a direct mirror of a PortSet's buffers.
62  */
63 void
64 BufferSet::attach_buffers(PortSet& ports)
65 {
66         clear();
67
68         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
69                 _buffers.push_back(BufferVec());
70                 BufferVec& v = _buffers[(*t).to_index()];
71
72                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
73                         assert(p->type() == *t);
74                         v.push_back(&(p->get_buffer()));
75                 }
76
77         }
78         
79         _count = ports.count();
80
81         _is_mirror = true;
82 }
83
84 void
85 BufferSet::ensure_buffers(const ChanCount& count, size_t buffer_capacity)
86 {
87         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
88                 ensure_buffers(*t, count.get(*t), buffer_capacity);
89         }
90 }
91
92
93 /** Ensure that there are @a num_buffers buffers of type @a type available,
94  * each of size at least @a buffer_size
95  */
96 void
97 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
98 {
99         assert(type != DataType::NIL);
100         assert(type.to_index() < _buffers.size());
101
102         if (num_buffers == 0)
103                 return;
104
105         // The vector of buffers of the type we care about
106         BufferVec& bufs = _buffers[type.to_index()];
107         
108         // If we're a mirror just make sure we're ok
109         if (_is_mirror) {
110                 assert(_count.get(type) >= num_buffers);
111                 assert(bufs[0]->type() == type);
112                 return;
113         }
114
115         // If there's not enough or they're too small, just nuke the whole thing and
116         // rebuild it (so I'm lazy..)
117         if (bufs.size() < num_buffers
118                         || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
119
120                 // Nuke it
121                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
122                         delete (*i);
123                 }
124                 bufs.clear();
125
126                 // Rebuild it
127                 for (size_t i=0; i < num_buffers; ++i) {
128                         bufs.push_back(Buffer::create(type, buffer_capacity));
129                 }
130         
131                 _available.set(type, num_buffers);
132         }
133
134         // Post-conditions
135         assert(bufs[0]->type() == type);
136         assert(bufs.size() >= num_buffers);
137         assert(bufs.size() == _available.get(type));
138         assert(bufs[0]->capacity() >= buffer_capacity);
139 }
140
141 /** Get the capacity (size) of the available buffers of the given type.
142  *
143  * All buffers of a certain type always have the same capacity.
144  */
145 size_t
146 BufferSet::buffer_capacity(DataType type) const
147 {
148         assert(_available.get(type) > 0);
149         return _buffers[type.to_index()][0]->capacity();
150 }
151
152 // FIXME: make 'in' const
153 void
154 BufferSet::read_from(BufferSet& in, jack_nframes_t nframes, jack_nframes_t offset)
155 {
156         assert(available() >= in.count());
157
158         // Copy all buffers 1:1
159         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
160                 BufferSet::iterator o = begin(*t);
161                 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
162                         o->read_from(*i, nframes, offset);
163                 }
164         }
165
166         set_count(in.count());
167 }
168
169 } // namespace ARDOUR
170