Fix MIDI region loading.
[ardour.git] / libs / ardour / ardour / buffer.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_h__
20 #define __ardour_buffer_h__
21
22 #include <cstdlib>
23 #include <cassert>
24 #include <iostream>
25 #include <boost/utility.hpp>
26 #include <ardour/types.h>
27 #include <ardour/data_type.h>
28 #include <ardour/runtime_functions.h>
29
30 namespace ARDOUR {
31
32
33 /** A buffer of recordable/playable data.
34  *
35  * This is a datatype-agnostic base class for all buffers (there are no
36  * methods to actually access the data).  This provides a way for code that
37  * doesn't care about the data type to still deal with buffers (which is
38  * why the base class can't be a template).
39  * 
40  * To actually read/write buffer contents, use the appropriate derived class.
41  */
42 class Buffer : public boost::noncopyable
43 {
44 public:
45         virtual ~Buffer() {}
46
47         /** Factory function */
48         static Buffer* create(DataType type, size_t capacity);
49
50         /** Maximum capacity of buffer.
51          * Note in some cases the entire buffer may not contain valid data, use size. */
52         size_t capacity() const { return _capacity; }
53
54         /** Amount of valid data in buffer.  Use this over capacity almost always. */
55         size_t size() const { return _size; }
56
57         /** Type of this buffer.
58          * Based on this you can static cast a Buffer* to the desired type. */
59         DataType type() const { return _type; }
60
61         bool silent() const { return _silent; }
62
63         /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
64         virtual void silence(nframes_t len, nframes_t offset=0) = 0;
65         
66         /** Clear the entire buffer */
67         virtual void clear() { silence(_capacity, 0); }
68
69         virtual void read_from(const Buffer& src, nframes_t offset, nframes_t len) = 0;
70
71 protected:
72         Buffer(DataType type, size_t capacity)
73         : _type(type), _capacity(capacity), _size(0), _silent(true)
74         {}
75
76         DataType _type;
77         size_t   _capacity;
78         size_t   _size;
79         bool     _silent;
80 };
81
82
83 /* Inside every class with a type in it's name is a template waiting to get out... */
84
85
86 /** Buffer containing 32-bit floating point (audio) data. */
87 class AudioBuffer : public Buffer
88 {
89 public:
90         AudioBuffer(size_t capacity);
91         
92         ~AudioBuffer();
93
94         void silence(nframes_t len, nframes_t offset=0)
95         {
96                 if (!_silent) {
97                         assert(_capacity > 0);
98                         assert(offset + len <= _capacity);
99                         memset(_data + offset, 0, sizeof (Sample) * len);
100                         if (offset == 0 && len == _capacity) {
101                                 _silent = true;
102                         }
103                 }
104         }
105         
106         /** Read @a len frames FROM THE START OF @a src into self at @a offset */
107         void read_from(const Buffer& src, nframes_t len, nframes_t offset)
108         {
109                 assert(_capacity > 0);
110                 assert(src.type() == _type == DataType::AUDIO);
111                 assert(offset + len <= _capacity);
112                 memcpy(_data + offset, ((AudioBuffer&)src).data(), sizeof(Sample) * len);
113                 _silent = src.silent();
114         }
115         
116         /** Accumulate (add)@a len frames FROM THE START OF @a src into self at @a offset */
117         void accumulate_from(const AudioBuffer& src, nframes_t len, nframes_t offset)
118         {
119                 assert(_capacity > 0);
120                 assert(offset + len <= _capacity);
121
122                 Sample*       const dst_raw = _data + offset;
123                 const Sample* const src_raw = src.data();
124
125                 for (nframes_t n = 0; n < len; ++n) {
126                         dst_raw[n] += src_raw[n];
127                 }
128
129                 _silent = (src.silent() && _silent);
130         }
131         
132         /** Accumulate (add) @a len frames FROM THE START OF @a src into self at @a offset
133          * scaling by @a gain_coeff */
134         void accumulate_with_gain_from(const AudioBuffer& src, nframes_t len, nframes_t offset, gain_t gain_coeff)
135         {
136                 assert(_capacity > 0);
137                 assert(offset + len <= _capacity);
138
139                 Sample*       const dst_raw = _data + offset;
140                 const Sample* const src_raw = src.data();
141
142                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
143
144                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
145         }
146         
147         void apply_gain(gain_t gain, nframes_t len, nframes_t offset=0) {
148                 apply_gain_to_buffer (_data + offset, len, gain);
149         }
150
151         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
152          * 
153          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
154          */
155         void set_data(Sample* data, size_t size)
156         {
157                 assert(!_owns_data); // prevent leaks
158                 _capacity = size;
159                 _size = size;
160                 _data = data;
161                 _silent = false;
162         }
163
164         const Sample* data () const { return _data; }
165         Sample* data () { return _data; }
166
167         const Sample* data(nframes_t nframes, nframes_t offset) const
168                 { assert(offset + nframes <= _capacity); return _data + offset; }
169
170         Sample* data (nframes_t nframes, nframes_t offset)
171                 { assert(offset + nframes <= _capacity); return _data + offset; }
172
173 private:
174         bool    _owns_data;
175         Sample* _data; ///< Actual buffer contents
176 };
177
178
179
180 /** Buffer containing 8-bit unsigned char (MIDI) data. */
181 class MidiBuffer : public Buffer
182 {
183 public:
184         MidiBuffer(size_t capacity);
185         
186         ~MidiBuffer();
187
188         void silence(nframes_t dur, nframes_t offset=0);
189         
190         void read_from(const Buffer& src, nframes_t nframes, nframes_t offset);
191
192         bool  push_back(const ARDOUR::MidiEvent& event);
193         Byte* reserve(nframes_t time, size_t size);
194         
195         const MidiEvent& operator[](size_t i) const { assert(i < _size); return _events[i]; }
196         MidiEvent& operator[](size_t i) { assert(i < _size); return _events[i]; }
197
198         static size_t max_event_size() { return MAX_EVENT_SIZE; }
199
200 private:
201         // FIXME: Jack needs to tell us this
202         static const size_t MAX_EVENT_SIZE = 4; // bytes
203         
204         /* We use _size as "number of events", so the size of _data is
205          * (_size * MAX_EVENT_SIZE)
206          */
207
208         MidiEvent* _events; ///< Event structs that point to offsets in _data
209         Byte*      _data;   ///< MIDI, straight up.  No time stamps.
210 };
211
212 } // namespace ARDOUR
213
214 #endif // __ardour_buffer_h__