Merged from trunk R743
[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 #define _XOPEN_SOURCE 600
23 #include <cstdlib> // for posix_memalign
24 #include <cassert>
25 #include <ardour/types.h>
26 #include <ardour/data_type.h>
27
28 namespace ARDOUR {
29
30
31 /* Yes, this is a bit of a mess right now.  I'll clean it up when everything
32  * using it works out.. */
33
34
35 /** A buffer of recordable/playable data.
36  *
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).
41  * 
42  * To actually read/write buffer contents, use the appropriate derived class.
43  */
44 class Buffer
45 {
46 public:
47         Buffer(DataType type, size_t capacity)
48         : _type(type), _capacity(capacity), _size(0) 
49         {}
50
51         virtual ~Buffer() {}
52
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; }
56
57         /** Amount of valid data in buffer.  Use this over capacity almost always. */
58         size_t size() const { return _size; }
59
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; }
63
64 protected:
65         DataType _type;
66         size_t   _capacity;
67         size_t   _size;
68 };
69
70
71 /* Inside every class with a type in it's name is a template waiting to get out... */
72
73
74 /** Buffer containing 32-bit floating point (audio) data. */
75 class AudioBuffer : public Buffer
76 {
77 public:
78         AudioBuffer(size_t capacity)
79                 : Buffer(DataType::AUDIO, capacity)
80                 , _data(NULL)
81         {
82                 _size = capacity; // For audio buffers, size = capacity (always)
83 #ifdef NO_POSIX_MEMALIGN
84                 _data =  (Sample *) malloc(sizeof(Sample) * capacity);
85 #else
86                 posix_memalign((void**)_data, 16, sizeof(Sample) * capacity);
87 #endif  
88                 assert(_data);
89                 memset(_data, 0, sizeof(Sample) * capacity);
90         }
91
92         const Sample* data() const { return _data; }
93         Sample*       data()       { return _data; }
94
95 private:
96         // These are undefined (prevent copies)
97         AudioBuffer(const AudioBuffer& copy);            
98         AudioBuffer& operator=(const AudioBuffer& copy);
99
100         Sample* _data; ///< Actual buffer contents
101 };
102
103
104
105 /** Buffer containing 8-bit unsigned char (MIDI) data. */
106 class MidiBuffer : public Buffer
107 {
108 public:
109         MidiBuffer(size_t capacity)
110                 : Buffer(DataType::MIDI, capacity)
111                 , _data(NULL)
112         {
113                 _size = capacity; // For audio buffers, size = capacity (always)
114 #ifdef NO_POSIX_MEMALIGN
115                 _data =  (RawMidi *) malloc(sizeof(RawMidi) * capacity);
116 #else
117                 posix_memalign((void**)_data, 16, sizeof(RawMidi) * capacity);
118 #endif  
119                 assert(_data);
120                 memset(_data, 0, sizeof(RawMidi) * capacity);
121         }
122
123         const RawMidi* data() const { return _data; }
124         RawMidi*       data()       { return _data; }
125
126 private:
127         // These are undefined (prevent copies)
128         MidiBuffer(const MidiBuffer& copy);            
129         MidiBuffer& operator=(const MidiBuffer& copy);
130
131         RawMidi* _data; ///< Actual buffer contents
132 };
133
134
135 } // namespace ARDOUR
136
137 #endif // __ardour_buffer_h__