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