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__
22 #define _XOPEN_SOURCE 600
23 #include <cstdlib> // for posix_memalign
25 #include <ardour/types.h>
26 #include <ardour/data_type.h>
31 /* Yes, this is a bit of a mess right now. I'll clean it up when everything
32 * using it works out.. */
35 /** A buffer of recordable/playable data.
37 * This is a datatype-agnostic base class for all buffers (there are no
38 * methods to actually access the data). This provides a way for code that
39 * doesn't care about the data type to still deal with buffers (which is
40 * why the base class can't be a template).
42 * To actually read/write buffer contents, use the appropriate derived class.
47 Buffer(DataType type, size_t capacity)
48 : _type(type), _capacity(capacity), _size(0)
53 /** Maximum capacity of buffer.
54 * Note in some cases the entire buffer may not contain valid data, use size. */
55 size_t capacity() const { return _capacity; }
57 /** Amount of valid data in buffer. Use this over capacity almost always. */
58 size_t size() const { return _size; }
60 /** Type of this buffer.
61 * Based on this you can static cast a Buffer* to the desired type. */
62 DataType type() const { return _type; }
71 /* Inside every class with a type in it's name is a template waiting to get out... */
74 /** Buffer containing 32-bit floating point (audio) data. */
75 class AudioBuffer : public Buffer
78 AudioBuffer(size_t capacity)
79 : Buffer(DataType::AUDIO, capacity)
82 _size = capacity; // For audio buffers, size = capacity (always)
83 #ifdef NO_POSIX_MEMALIGN
84 _data = (Sample *) malloc(sizeof(Sample) * capacity);
86 posix_memalign((void**)_data, 16, sizeof(Sample) * capacity);
89 memset(_data, 0, sizeof(Sample) * capacity);
92 const Sample* data() const { return _data; }
93 Sample* data() { return _data; }
96 // These are undefined (prevent copies)
97 AudioBuffer(const AudioBuffer& copy);
98 AudioBuffer& operator=(const AudioBuffer& copy);
100 Sample* _data; ///< Actual buffer contents
104 } // namespace ARDOUR
106 #endif // __ardour_buffer_h__