- Changed IO's vector<Port*>'s to PortList
[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 #include <cstdlib>
23 #include <cassert>
24 #include <iostream>
25 #include <ardour/types.h>
26 #include <ardour/data_type.h>
27
28 namespace ARDOUR {
29
30
31 /** A buffer of recordable/playable data.
32  *
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).
37  * 
38  * To actually read/write buffer contents, use the appropriate derived class.
39  */
40 class Buffer
41 {
42 public:
43         virtual ~Buffer() {}
44
45         /** Factory function */
46         static Buffer* create(DataType type, size_t capacity);
47
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; }
51
52         /** Amount of valid data in buffer.  Use this over capacity almost always. */
53         size_t size() const { return _size; }
54
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; }
58
59         /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
60         virtual void clear(jack_nframes_t offset = 0) = 0;
61         
62         virtual void write(const Buffer& src, jack_nframes_t offset, jack_nframes_t len) = 0;
63
64 protected:
65         Buffer(DataType type, size_t capacity)
66         : _type(type), _capacity(capacity), _size(0) 
67         {}
68
69         DataType _type;
70         size_t   _capacity;
71         size_t   _size;
72
73 private:
74         // Prevent copies (undefined)
75         Buffer(const Buffer& copy);
76         void operator=(const Buffer& other);
77 };
78
79
80 /* Inside every class with a type in it's name is a template waiting to get out... */
81
82
83 /** Buffer containing 32-bit floating point (audio) data. */
84 class AudioBuffer : public Buffer
85 {
86 public:
87         AudioBuffer(size_t capacity);
88         
89         ~AudioBuffer();
90
91         void clear(jack_nframes_t offset=0) { memset(_data + offset, 0, sizeof (Sample) * _capacity); }
92
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)
95         {
96                 assert(src.type() == _type == DataType::AUDIO);
97                 assert(offset + len <= _capacity);
98                 memcpy(_data + offset, ((AudioBuffer&)src).data(len, offset), sizeof(Sample) * len);
99         }
100         
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)
103         {
104                 assert(offset + len <= _capacity);
105                 memcpy(_data + offset, src, sizeof(Sample) * len);
106         }
107
108         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
109          * 
110          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
111          */
112         void set_data(Sample* data, size_t size)
113         {
114                 assert(!_owns_data); // prevent leaks
115                 _capacity = size;
116                 _size = size;
117                 _data = data;
118         }
119
120         const Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0) const
121                 { assert(offset + nframes <= _capacity); return _data + offset; }
122
123         Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0)
124                 { assert(offset + nframes <= _capacity); return _data + offset; }
125
126 private:
127         // These are undefined (prevent copies)
128         AudioBuffer(const AudioBuffer& copy);            
129         AudioBuffer& operator=(const AudioBuffer& copy);
130
131         bool    _owns_data;
132         Sample* _data; ///< Actual buffer contents
133 };
134
135
136
137 /** Buffer containing 8-bit unsigned char (MIDI) data. */
138 class MidiBuffer : public Buffer
139 {
140 public:
141         MidiBuffer(size_t capacity);
142         
143         ~MidiBuffer();
144
145         // FIXME: clear events starting at offset
146         void clear(jack_nframes_t offset=0) { assert(offset == 0); _size = 0; }
147         
148         void write(const Buffer& src, jack_nframes_t offset, jack_nframes_t nframes);
149
150         void set_size(size_t size) { _size = size; }
151
152         const RawMidi* data() const { return _data; }
153         RawMidi*       data()       { return _data; }
154
155 private:
156         // These are undefined (prevent copies)
157         MidiBuffer(const MidiBuffer& copy);            
158         MidiBuffer& operator=(const MidiBuffer& copy);
159
160         bool     _owns_data;
161         RawMidi* _data; ///< Actual buffer contents
162 };
163
164 } // namespace ARDOUR
165
166 #endif // __ardour_buffer_h__