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>
31 /** A buffer of recordable/playable data.
33 * This is a datatype-agnostic base class for all buffers (there are no
34 * methods to actually access the data). This provides a way for code that
35 * doesn't care about the data type to still deal with buffers (which is
36 * why the base class can't be a template).
38 * To actually read/write buffer contents, use the appropriate derived class.
45 /** Factory function */
46 static Buffer* create(DataType type, size_t capacity);
48 /** Maximum capacity of buffer.
49 * Note in some cases the entire buffer may not contain valid data, use size. */
50 size_t capacity() const { return _capacity; }
52 /** Amount of valid data in buffer. Use this over capacity almost always. */
53 size_t size() const { return _size; }
55 /** Type of this buffer.
56 * Based on this you can static cast a Buffer* to the desired type. */
57 DataType type() const { return _type; }
59 bool silent() const { return _silent; }
61 /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
62 virtual void silence(jack_nframes_t len, jack_nframes_t offset=0) = 0;
64 /** Clear the entire buffer */
65 virtual void clear() { silence(_capacity, 0); }
67 virtual void read_from(const Buffer& src, jack_nframes_t offset, jack_nframes_t len) = 0;
70 Buffer(DataType type, size_t capacity)
71 : _type(type), _capacity(capacity), _size(0), _silent(true)
80 // Prevent copies (undefined)
81 Buffer(const Buffer& copy);
82 void operator=(const Buffer& other);
86 /* Inside every class with a type in it's name is a template waiting to get out... */
89 /** Buffer containing 32-bit floating point (audio) data. */
90 class AudioBuffer : public Buffer
93 AudioBuffer(size_t capacity);
97 void silence(jack_nframes_t len, jack_nframes_t offset=0)
100 assert(_capacity > 0);
101 assert(offset + len <= _capacity);
102 memset(_data + offset, 0, sizeof (Sample) * len);
103 if (offset == 0 && len == _capacity) {
109 /** Read @a len frames FROM THE START OF @a src into self at @a offset */
110 void read_from(const Buffer& src, jack_nframes_t len, jack_nframes_t offset)
112 assert(_capacity > 0);
113 assert(src.type() == _type == DataType::AUDIO);
114 assert(offset + len <= _capacity);
115 memcpy(_data + offset, ((AudioBuffer&)src).data(len), sizeof(Sample) * len);
116 _silent = src.silent();
119 /** Accumulate (add)@a len frames FROM THE START OF @a src into self at @a offset */
120 void accumulate_from(const AudioBuffer& src, jack_nframes_t len, jack_nframes_t offset)
122 assert(_capacity > 0);
123 assert(offset + len <= _capacity);
125 Sample* const dst_raw = _data + offset;
126 const Sample* const src_raw = src.data(len);
128 for (jack_nframes_t n = 0; n < len; ++n) {
129 dst_raw[n] += src_raw[n];
132 _silent = (src.silent() && _silent);
135 /** Accumulate (add) @a len frames FROM THE START OF @a src into self at @a offset
136 * scaling by @a gain_coeff */
137 void accumulate_with_gain_from(const AudioBuffer& src, jack_nframes_t len, jack_nframes_t offset, gain_t gain_coeff)
139 assert(_capacity > 0);
140 assert(offset + len <= _capacity);
142 Sample* const dst_raw = _data + offset;
143 const Sample* const src_raw = src.data(len);
145 for (jack_nframes_t n = 0; n < len; ++n) {
146 dst_raw[n] += src_raw[n] * gain_coeff;
149 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
152 void apply_gain(gain_t gain, jack_nframes_t len, jack_nframes_t offset=0) {
153 Sample* const buf = _data + offset;
155 for (jack_nframes_t n = 0; n < len; ++n) {
159 _silent = (_silent || gain == 0);
162 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
164 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
166 void set_data(Sample* data, size_t size)
168 assert(!_owns_data); // prevent leaks
175 const Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0) const
176 { assert(offset + nframes <= _capacity); return _data + offset; }
178 Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0)
179 { assert(offset + nframes <= _capacity); return _data + offset; }
182 // These are undefined (prevent copies)
183 AudioBuffer(const AudioBuffer& copy);
184 AudioBuffer& operator=(const AudioBuffer& copy);
187 Sample* _data; ///< Actual buffer contents
192 /** Buffer containing 8-bit unsigned char (MIDI) data. */
193 class MidiBuffer : public Buffer
196 MidiBuffer(size_t capacity);
200 void silence(jack_nframes_t dur, jack_nframes_t offset=0);
202 void read_from(const Buffer& src, jack_nframes_t nframes, jack_nframes_t offset);
204 bool push_back(const MidiEvent& event);
206 const MidiEvent& operator[](size_t i) const { assert(i < _size); return _events[i]; }
207 MidiEvent& operator[](size_t i) { assert(i < _size); return _events[i]; }
209 static size_t max_event_size() { return MAX_EVENT_SIZE; }
211 //void set_size(size_t size) { _size = size; }
213 //const RawMidi* data() const { return _data; }
214 //RawMidi* data() { return _data; }
217 // These are undefined (prevent copies)
218 MidiBuffer(const MidiBuffer& copy);
219 MidiBuffer& operator=(const MidiBuffer& copy);
221 // FIXME: Jack needs to tell us this
222 static const size_t MAX_EVENT_SIZE = 4; // bytes
224 /* We use _size as "number of events", so the size of _data is
225 * (_size * MAX_EVENT_SIZE)
228 MidiEvent* _events; ///< Event structs that point to offsets in _data
229 RawMidi* _data; ///< MIDI, straight up. No time stamps.
232 } // namespace ARDOUR
234 #endif // __ardour_buffer_h__