Progress on the disk side of things:
[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 silence(jack_nframes_t len, jack_nframes_t offset=0) = 0;
61         
62         /** Clear the entire buffer */
63         virtual void clear() { silence(_capacity, 0); }
64
65         virtual void read_from(const Buffer& src, jack_nframes_t offset, jack_nframes_t len) = 0;
66
67 protected:
68         Buffer(DataType type, size_t capacity)
69         : _type(type), _capacity(capacity), _size(0) 
70         {}
71
72         DataType _type;
73         size_t   _capacity;
74         size_t   _size;
75
76 private:
77         // Prevent copies (undefined)
78         Buffer(const Buffer& copy);
79         void operator=(const Buffer& other);
80 };
81
82
83 /* Inside every class with a type in it's name is a template waiting to get out... */
84
85
86 /** Buffer containing 32-bit floating point (audio) data. */
87 class AudioBuffer : public Buffer
88 {
89 public:
90         AudioBuffer(size_t capacity);
91         
92         ~AudioBuffer();
93
94         void silence(jack_nframes_t len, jack_nframes_t offset=0)
95         {
96                 assert(_capacity > 0);
97                 assert(offset + len <= _capacity);
98                 memset(_data + offset, 0, sizeof (Sample) * len);
99         }
100         
101         /** Read @a len frames FROM THE START OF @a src into self at @a offset */
102         void read_from(const Buffer& src, jack_nframes_t len, jack_nframes_t offset)
103         {
104                 assert(_capacity > 0);
105                 assert(src.type() == _type == DataType::AUDIO);
106                 assert(offset + len <= _capacity);
107                 memcpy(_data + offset, ((AudioBuffer&)src).data(len), sizeof(Sample) * len);
108         }
109         
110         /** Accumulate (add)@a len frames FROM THE START OF @a src into self at @a offset */
111         void accumulate_from(const AudioBuffer& src, jack_nframes_t len, jack_nframes_t offset)
112         {
113                 assert(_capacity > 0);
114                 assert(offset + len <= _capacity);
115
116                 Sample*       const dst_raw = _data + offset;
117                 const Sample* const src_raw = src.data(len);
118
119                 for (jack_nframes_t n = 0; n < len; ++n) {
120                         dst_raw[n] += src_raw[n];
121                 }
122         }
123         
124         /** Accumulate (add) @a len frames FROM THE START OF @a src into self at @a offset
125          * scaling by @a gain_coeff */
126         void accumulate_with_gain_from(const AudioBuffer& src, jack_nframes_t len, jack_nframes_t offset, gain_t gain_coeff)
127         {
128                 assert(_capacity > 0);
129                 assert(offset + len <= _capacity);
130
131                 Sample*       const dst_raw = _data + offset;
132                 const Sample* const src_raw = src.data(len);
133
134                 for (jack_nframes_t n = 0; n < len; ++n) {
135                         dst_raw[n] += src_raw[n] * gain_coeff;
136                 }
137         }
138         
139         void apply_gain(gain_t gain, jack_nframes_t len, jack_nframes_t offset=0) {
140                 Sample* const buf = _data + offset;
141
142                 for (jack_nframes_t n = 0; n < len; ++n) {
143                         buf[n] *= gain;
144                 }
145         }
146
147         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
148          * 
149          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
150          */
151         void set_data(Sample* data, size_t size)
152         {
153                 assert(!_owns_data); // prevent leaks
154                 _capacity = size;
155                 _size = size;
156                 _data = data;
157         }
158
159         const Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0) const
160                 { assert(offset + nframes <= _capacity); return _data + offset; }
161
162         Sample* data(jack_nframes_t nframes, jack_nframes_t offset=0)
163                 { assert(offset + nframes <= _capacity); return _data + offset; }
164
165 private:
166         // These are undefined (prevent copies)
167         AudioBuffer(const AudioBuffer& copy);            
168         AudioBuffer& operator=(const AudioBuffer& copy);
169
170         bool    _owns_data;
171         Sample* _data; ///< Actual buffer contents
172 };
173
174
175
176 /** Buffer containing 8-bit unsigned char (MIDI) data. */
177 class MidiBuffer : public Buffer
178 {
179 public:
180         MidiBuffer(size_t capacity);
181         
182         ~MidiBuffer();
183
184         void silence(jack_nframes_t dur, jack_nframes_t offset=0);
185         
186         void read_from(const Buffer& src, jack_nframes_t nframes, jack_nframes_t offset);
187
188         bool push_back(const MidiEvent& event);
189         
190         const MidiEvent& operator[](size_t i) const { assert(i < _size); return _events[i]; }
191         MidiEvent& operator[](size_t i) { assert(i < _size); return _events[i]; }
192
193         static size_t max_event_size() { return MAX_EVENT_SIZE; }
194
195         //void set_size(size_t size) { _size = size; }
196
197         //const RawMidi* data() const { return _data; }
198         //RawMidi*       data()       { return _data; }
199
200 private:
201         // These are undefined (prevent copies)
202         MidiBuffer(const MidiBuffer& copy);            
203         MidiBuffer& operator=(const MidiBuffer& copy);
204
205         // FIXME: Jack needs to tell us this
206         static const size_t MAX_EVENT_SIZE = 4; // bytes
207         
208         /* We use _size as "number of events", so the size of _data is
209          * (_size * MAX_EVENT_SIZE)
210          */
211
212         MidiEvent* _events;     ///< Event structs that point to offsets in _data
213         RawMidi*   _data;       ///< MIDI, straight up.  No time stamps.
214 };
215
216 } // namespace ARDOUR
217
218 #endif // __ardour_buffer_h__