2 Copyright (C) 2006 Paul Davis
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)
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
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.
19 #ifndef __ardour_buffer_h__
20 #define __ardour_buffer_h__
25 #include <ardour/types.h>
26 #include <ardour/data_type.h>
27 #include <ardour/runtime_functions.h>
32 /** A buffer of recordable/playable data.
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).
39 * To actually read/write buffer contents, use the appropriate derived class.
46 /** Factory function */
47 static Buffer* create(DataType type, size_t capacity);
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; }
53 /** Amount of valid data in buffer. Use this over capacity almost always. */
54 size_t size() const { return _size; }
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; }
60 bool silent() const { return _silent; }
62 /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
63 virtual void silence(nframes_t len, nframes_t offset=0) = 0;
65 /** Clear the entire buffer */
66 virtual void clear() { silence(_capacity, 0); }
68 virtual void read_from(const Buffer& src, nframes_t offset, nframes_t len) = 0;
71 Buffer(DataType type, size_t capacity)
72 : _type(type), _capacity(capacity), _size(0), _silent(true)
81 // Prevent copies (undefined)
82 Buffer(const Buffer& copy);
83 void operator=(const Buffer& other);
87 /* Inside every class with a type in it's name is a template waiting to get out... */
90 /** Buffer containing 32-bit floating point (audio) data. */
91 class AudioBuffer : public Buffer
94 AudioBuffer(size_t capacity);
98 void silence(nframes_t len, nframes_t offset=0)
101 assert(_capacity > 0);
102 assert(offset + len <= _capacity);
103 memset(_data + offset, 0, sizeof (Sample) * len);
104 if (offset == 0 && len == _capacity) {
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)
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();
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)
123 assert(_capacity > 0);
124 assert(offset + len <= _capacity);
126 Sample* const dst_raw = _data + offset;
127 const Sample* const src_raw = src.data();
129 for (nframes_t n = 0; n < len; ++n) {
130 dst_raw[n] += src_raw[n];
133 _silent = (src.silent() && _silent);
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)
140 assert(_capacity > 0);
141 assert(offset + len <= _capacity);
143 Sample* const dst_raw = _data + offset;
144 const Sample* const src_raw = src.data();
146 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
148 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
151 void apply_gain(gain_t gain, nframes_t len, nframes_t offset=0) {
152 apply_gain_to_buffer (_data + offset, len, gain);
155 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
157 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
159 void set_data(Sample* data, size_t size)
161 assert(!_owns_data); // prevent leaks
168 const Sample* data () const { return _data; }
169 Sample* data () { return _data; }
171 const Sample* data(nframes_t nframes, nframes_t offset) const
172 { assert(offset + nframes <= _capacity); return _data + offset; }
174 Sample* data (nframes_t nframes, nframes_t offset)
175 { assert(offset + nframes <= _capacity); return _data + offset; }
178 // These are undefined (prevent copies)
179 AudioBuffer(const AudioBuffer& copy);
180 AudioBuffer& operator=(const AudioBuffer& copy);
183 Sample* _data; ///< Actual buffer contents
188 /** Buffer containing 8-bit unsigned char (MIDI) data. */
189 class MidiBuffer : public Buffer
192 MidiBuffer(size_t capacity);
196 void silence(nframes_t dur, nframes_t offset=0);
198 void read_from(const Buffer& src, nframes_t nframes, nframes_t offset);
200 bool push_back(const MidiEvent& event);
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]; }
205 static size_t max_event_size() { return MAX_EVENT_SIZE; }
207 //void set_size(size_t size) { _size = size; }
209 //const RawMidi* data() const { return _data; }
210 //RawMidi* data() { return _data; }
213 // These are undefined (prevent copies)
214 MidiBuffer(const MidiBuffer& copy);
215 MidiBuffer& operator=(const MidiBuffer& copy);
217 // FIXME: Jack needs to tell us this
218 static const size_t MAX_EVENT_SIZE = 4; // bytes
220 /* We use _size as "number of events", so the size of _data is
221 * (_size * MAX_EVENT_SIZE)
224 MidiEvent* _events; ///< Event structs that point to offsets in _data
225 RawMidi* _data; ///< MIDI, straight up. No time stamps.
228 } // namespace ARDOUR
230 #endif // __ardour_buffer_h__