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 /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
60 virtual void clear(jack_nframes_t offset = 0) = 0;
62 virtual void write(const Buffer& src, jack_nframes_t offset, jack_nframes_t len) = 0;
65 Buffer(DataType type, size_t capacity)
66 : _type(type), _capacity(capacity), _size(0)
74 // Prevent copies (undefined)
75 Buffer(const Buffer& copy);
76 void operator=(const Buffer& other);
80 /* Inside every class with a type in it's name is a template waiting to get out... */
83 /** Buffer containing 32-bit floating point (audio) data. */
84 class AudioBuffer : public Buffer
87 AudioBuffer(size_t capacity);
91 void clear(jack_nframes_t offset=0) { memset(_data + offset, 0, sizeof (Sample) * _capacity); }
93 /** Copy @a len frames starting at @a offset, from the start of @a src */
94 void write(const Buffer& src, jack_nframes_t offset, jack_nframes_t len)
96 assert(src.type() == _type == DataType::AUDIO);
97 assert(offset + len <= _capacity);
98 memcpy(_data + offset, ((AudioBuffer&)src).data(len, offset), sizeof(Sample) * len);
101 /** Copy @a len frames starting at @a offset, from the start of @a src */
102 void write(const Sample* src, jack_nframes_t offset, jack_nframes_t len)
104 assert(offset + len <= _capacity);
105 memcpy(_data + offset, src, sizeof(Sample) * len);
108 /** Set the data contained by this buffer manually (for setting directly to jack buffer).
110 * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
112 void set_data(Sample* data, size_t size)
114 assert(!_owns_data); // prevent leaks
120 const Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0) const
121 { assert(offset + nframes <= _capacity); return _data + offset; }
123 Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0)
124 { assert(offset + nframes <= _capacity); return _data + offset; }
127 // These are undefined (prevent copies)
128 AudioBuffer(const AudioBuffer& copy);
129 AudioBuffer& operator=(const AudioBuffer& copy);
132 Sample* _data; ///< Actual buffer contents
137 /** Buffer containing 8-bit unsigned char (MIDI) data. */
138 class MidiBuffer : public Buffer
141 MidiBuffer(size_t capacity);
145 // FIXME: clear events starting at offset
146 void clear(jack_nframes_t offset=0) { assert(offset == 0); _size = 0; }
148 void write(const Buffer& src, jack_nframes_t offset, jack_nframes_t nframes);
150 void set_size(size_t size) { _size = size; }
152 const RawMidi* data() const { return _data; }
153 RawMidi* data() { return _data; }
156 // These are undefined (prevent copies)
157 MidiBuffer(const MidiBuffer& copy);
158 MidiBuffer& operator=(const MidiBuffer& copy);
161 RawMidi* _data; ///< Actual buffer contents
164 } // namespace ARDOUR
166 #endif // __ardour_buffer_h__