Actually added the code mentioned in my last commit. Whoops.
[ardour.git] / libs / ardour / ardour / buffer.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3     Written by Dave Robillard, 2006
4     
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9     
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14     
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_buffer_h__
21 #define __ardour_buffer_h__
22
23 #define _XOPEN_SOURCE 600
24 #include <cstdlib> // for posix_memalign
25 #include <cassert>
26 #include <ardour/types.h>
27 #include <jack/jack.h>
28
29 namespace ARDOUR {
30
31
32 /** A buffer of recordable/playable data.
33  *
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).
38  * 
39  * To actually read/write buffer contents, use the appropriate derived class.
40  */
41 class Buffer
42 {
43 public:
44         /** Unfortunately using RTTI and dynamic_cast to find the type of the
45          * buffer is just too slow, this is done in very performance critical
46          * bits of the code. */
47         enum Type { NIL = 0, AUDIO, MIDI };
48
49         Buffer(Type type, size_t capacity)
50         : _type(type), _capacity(capacity), _size(0) 
51         {}
52
53         virtual ~Buffer() {}
54
55         /** Maximum capacity of buffer.
56          * Note in some cases the entire buffer may not contain valid data, use size. */
57         size_t capacity() const { return _capacity; }
58
59         /** Amount of valid data in buffer.  Use this over capacity almost always. */
60         size_t size() const { return _size; }
61
62         /** Type of this buffer.
63          * Based on this you can cast a Buffer* to the desired type. */
64         virtual Type type() const { return _type; }
65
66         /** Jack type (eg JACK_DEFAULT_AUDIO_TYPE) */
67         const char* jack_type() const { return type_to_jack_type(type()); }
68         
69         /** Separate for creating ports (before a buffer exists to call jack_type on) */
70         static const char* type_to_jack_type(Type t) {
71                 switch (t) {
72                         case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
73                         case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
74                         default:    return "";
75                 }
76         }
77
78 protected:
79         Type   _type;
80         size_t _capacity;
81         size_t _size;
82 };
83
84
85 /* Since we only have two types, templates aren't worth it, yet.. */
86
87
88 /** Buffer containing 32-bit floating point (audio) data. */
89 class AudioBuffer : public Buffer
90 {
91 public:
92         AudioBuffer(size_t capacity)
93                 : Buffer(AUDIO, capacity)
94                 , _data(NULL)
95         {
96                 _size = capacity; // For audio buffers, size = capacity always
97                 posix_memalign((void**)_data, 16, sizeof(Sample) * capacity);
98                 assert(_data);
99                 memset(_data, 0, sizeof(Sample) * capacity);
100         }
101
102         const Sample* data() const { return _data; }
103         Sample*       data()       { return _data; }
104
105 private:
106         // These are undefined (prevent copies)
107         AudioBuffer(const AudioBuffer& copy);            
108         AudioBuffer& operator=(const AudioBuffer& copy);
109
110         Sample* const _data; ///< Actual buffer contents
111 };
112
113
114
115 /** Buffer containing 8-bit unsigned char (MIDI) data. */
116 class MidiBuffer : public Buffer
117 {
118 public:
119         MidiBuffer(size_t capacity)
120                 : Buffer(MIDI, capacity)
121                 , _data(NULL)
122         {
123                 posix_memalign((void**)_data, 16, sizeof(RawMidi) * capacity);
124                 assert(_data);
125                 assert(_size == 0);
126                 memset(_data, 0, sizeof(Sample) * capacity);
127         }
128
129         const RawMidi* data() const { return _data; }
130         RawMidi*       data()       { return _data; }
131
132 private:
133         // These are undefined (prevent copies)
134         MidiBuffer(const MidiBuffer& copy);            
135         MidiBuffer& operator=(const MidiBuffer& copy);
136
137         RawMidi* const _data; ///< Actual buffer contents
138 };
139
140
141 } // namespace ARDOUR
142
143 #endif // __ardour_buffer_h__