remove offset from process callback tree. some breakage may have occured. yes, really.
[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 <iostream>
20 #include <algorithm>
21 #include "ardour/buffer_set.h"
22 #include "ardour/buffer.h"
23 #include "ardour/port.h"
24 #include "ardour/port_set.h"
25
26 namespace ARDOUR {
27
28 /** Create a new, empty BufferSet */
29 BufferSet::BufferSet()
30         : _is_mirror(false)
31 {
32         for (size_t i=0; i < DataType::num_types; ++i)
33                 _buffers.push_back( BufferVec() );
34
35         _count.reset();
36         _available.reset();
37 }
38
39 BufferSet::~BufferSet()
40 {
41         clear();
42 }
43
44 /** Destroy all contained buffers.
45  */
46 void
47 BufferSet::clear()
48 {
49         if (!_is_mirror) {
50                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
51                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
52                                 delete *j;
53                         }
54                         (*i).clear();
55                 }
56         }
57         _buffers.clear();
58         _count.reset();
59         _available.reset();
60 }
61
62 /** Make this BufferSet a direct mirror of a PortSet's buffers.
63  */
64 void
65 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
66 {
67         clear();
68
69         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
70                 _buffers.push_back(BufferVec());
71                 BufferVec& v = _buffers[*t];
72
73                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
74                         assert(p->type() == *t);
75                         v.push_back(&(p->get_buffer(nframes, offset)));
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 < _buffers.size());
101         assert(buffer_capacity > 0);
102
103         if (num_buffers == 0)
104                 return;
105
106         // FIXME: Kludge to make MIDI buffers larger (size is bytes, not frames)
107         // See MidiPort::MidiPort
108         // We probably need a map<DataType, size_t> parameter for capacity
109         if (type == DataType::MIDI)
110                 buffer_capacity *= 8;
111
112         // The vector of buffers of the type we care about
113         BufferVec& bufs = _buffers[type];
114         
115         // If we're a mirror just make sure we're ok
116         if (_is_mirror) {
117                 assert(_count.get(type) >= num_buffers);
118                 assert(bufs[0]->type() == type);
119                 return;
120         }
121
122         // If there's not enough or they're too small, just nuke the whole thing and
123         // rebuild it (so I'm lazy..)
124         if (bufs.size() < num_buffers
125                         || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
126
127                 // Nuke it
128                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
129                         delete (*i);
130                 }
131                 bufs.clear();
132
133                 // Rebuild it
134                 for (size_t i=0; i < num_buffers; ++i) {
135                         bufs.push_back(Buffer::create(type, buffer_capacity));
136                 }
137         
138                 _available.set(type, num_buffers);
139         }
140
141         // Post-conditions
142         assert(bufs[0]->type() == type);
143         assert(bufs.size() >= num_buffers);
144         assert(bufs.size() == _available.get(type));
145         assert(bufs[0]->capacity() >= buffer_capacity);
146 }
147
148 /** Get the capacity (size) of the available buffers of the given type.
149  *
150  * All buffers of a certain type always have the same capacity.
151  */
152 size_t
153 BufferSet::buffer_capacity(DataType type) const
154 {
155         assert(_available.get(type) > 0);
156         return _buffers[type][0]->capacity();
157 }
158
159 // FIXME: make 'in' const
160 void
161 BufferSet::read_from (BufferSet& in, nframes_t nframes)
162 {
163         assert(available() >= in.count());
164
165         // Copy all buffers 1:1
166         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
167                 BufferSet::iterator o = begin(*t);
168                 for (BufferSet::iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
169                         o->read_from (*i, nframes);
170                 }
171         }
172
173         set_count(in.count());
174 }
175
176 } // namespace ARDOUR
177