rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / ardour / midi_buffer.cc
1 /*
2     Copyright (C) 2006-2007 Paul Davis 
3         Author: Dave Robillard
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 #include <iostream>
21 #include <ardour/midi_buffer.h>
22
23 #ifdef __x86_64__
24 static const int CPU_CACHE_ALIGN = 64;
25 #else
26 static const int CPU_CACHE_ALIGN = 16; /* arguably 32 on most arches, but it matters less */
27 #endif
28
29 using namespace std;
30 using namespace ARDOUR;
31
32
33 // FIXME: mirroring for MIDI buffers?
34 MidiBuffer::MidiBuffer(size_t capacity)
35         : Buffer(DataType::MIDI, capacity)
36         , _events(0)
37         , _data(0)
38 //      , _owns_data(false)
39 {
40         if (capacity) {
41                 resize (_capacity);
42                 silence(_capacity);
43         }
44 }
45         
46 MidiBuffer::~MidiBuffer()
47 {
48         if (_events) {
49                 free(_events);
50         }
51         if (_data) {
52                 free(_data);
53         }
54 }
55
56 void
57 MidiBuffer::resize (size_t size)
58 {
59         assert(size > 0);
60
61         if (size < _capacity) {
62                 return;
63         }
64
65         if (_data) {
66                 free (_data);
67         }
68
69         if (_events) {
70                 free (_events);
71         }
72
73         _size = 0;
74         _capacity = size;
75
76 #ifdef NO_POSIX_MEMALIGN
77         _events = (MIDI::Event *) malloc(sizeof(MIDI::Event) * _capacity);
78         _data = (uint8_t *) malloc(sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
79 #else
80         posix_memalign((void**)&_events, CPU_CACHE_ALIGN, sizeof(MIDI::Event) * _capacity);
81         posix_memalign((void**)&_data, CPU_CACHE_ALIGN, sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
82 #endif  
83         assert(_data);
84         assert(_events);
85 }
86
87 void
88 MidiBuffer::copy(const MidiBuffer& copy)
89 {
90         assert(_capacity >= copy._capacity);
91         _size = 0;
92
93         for (size_t i = 0; i < copy.size(); ++i)
94                 push_back(copy[i]);
95 }
96
97
98 /** Read events from @a src starting at time @a offset into the START of this buffer, for
99  * time direction @a nframes.  Relative time, where 0 = start of buffer.
100  *
101  * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
102  */
103 void
104 MidiBuffer::read_from(const Buffer& src, nframes_t nframes, nframes_t offset)
105 {
106         assert(src.type() == DataType::MIDI);
107         assert(&src != this);
108
109         const MidiBuffer& msrc = (MidiBuffer&)src;
110         
111         assert(_capacity >= msrc.size());
112
113         clear();
114         assert(_size == 0);
115         
116         // FIXME: slow
117         for (size_t i=0; i < msrc.size(); ++i) {
118                 const MIDI::Event& ev = msrc[i];
119                 if (ev.time() >= offset && ev.time() < offset+nframes) {
120                         //cout << "MidiBuffer::read_from got event, " << int(ev.type()) << " time: " << ev.time() << " buffer size: " << _size << endl;
121                         push_back(ev);
122                 } else {
123                         cerr << "MidiBuffer event out of range, " << ev.time() << endl;
124                 }
125         }
126
127         _silent = src.silent();
128 }
129
130
131 /** Push an event into the buffer.
132  *
133  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
134  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
135  * Realtime safe.
136  * @return false if operation failed (not enough room)
137  */
138 bool
139 MidiBuffer::push_back(const MIDI::Event& ev)
140 {
141         if (_size == _capacity)
142                 return false;
143
144         uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
145
146         memcpy(write_loc, ev.buffer(), ev.size());
147         _events[_size] = ev;
148         _events[_size].set_buffer(ev.size(), write_loc, false);
149         ++_size;
150
151         //cerr << "MidiBuffer: pushed, size = " << _size << endl;
152
153         _silent = false;
154
155         return true;
156 }
157
158
159 /** Push an event into the buffer.
160  *
161  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
162  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
163  * Realtime safe.
164  * @return false if operation failed (not enough room)
165  */
166 bool
167 MidiBuffer::push_back(const jack_midi_event_t& ev)
168 {
169         if (_size == _capacity)
170                 return false;
171
172         uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
173
174         memcpy(write_loc, ev.buffer, ev.size);
175         _events[_size].time() = (double)ev.time;
176         _events[_size].set_buffer(ev.size, write_loc, false);
177         ++_size;
178
179         //cerr << "MidiBuffer: pushed, size = " << _size << endl;
180
181         _silent = false;
182
183         return true;
184 }
185
186
187 /** Reserve space for a new event in the buffer.
188  *
189  * This call is for copying MIDI directly into the buffer, the data location
190  * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
191  * This call MUST be immediately followed by a write to the returned data
192  * location, or the buffer will be corrupted and very nasty things will happen.
193  */
194 uint8_t*
195 MidiBuffer::reserve(double time, size_t size)
196 {
197         if (size > MAX_EVENT_SIZE) {
198                 cerr << "WARNING: Failed to reserve " << size << " bytes for event";
199                 return 0;
200         }
201
202         if (_size == _capacity)
203                 return 0;
204
205         uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
206
207         _events[_size].time() = time;
208         _events[_size].set_buffer(size, write_loc, false);
209         ++_size;
210
211         //cerr << "MidiBuffer: reserved, size = " << _size << endl;
212
213         _silent = false;
214
215         return write_loc;
216 }
217
218
219 void
220 MidiBuffer::silence(nframes_t dur, nframes_t offset)
221 {
222         // FIXME use parameters
223         if (offset != 0)
224                 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
225
226         memset(_events, 0, sizeof(MIDI::Event) * _capacity);
227         memset(_data, 0, sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
228         _size = 0;
229         _silent = true;
230 }
231
232
233 /** Clear, and merge \a a and \a b into this buffer.
234  *
235  * FIXME: This is slow.
236  *
237  * \return true if complete merge was successful
238  */
239 bool
240 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
241 {
242         _size = 0;
243
244         // Die if a merge isn't necessary as it's expensive
245         assert(a.size() > 0 && b.size() > 0);
246
247         size_t a_index = 0;
248         size_t b_index = 0;
249         size_t count = a.size() + b.size();
250
251         while (count > 0 && a_index < a.size() && b_index < b.size()) {
252                 
253                 if (size() == capacity()) {
254                         cerr << "WARNING: MIDI buffer overrun, events lost!" << endl;
255                         return false;
256                 }
257                 
258                 if (a_index == a.size()) {
259                         push_back(a[a_index]);
260                         ++a_index;
261                 } else if (b_index == b.size()) {
262                         push_back(b[b_index]);
263                         ++b_index;
264                 } else {
265                         const MIDI::Event& a_ev = a[a_index];
266                         const MIDI::Event& b_ev = b[b_index];
267
268                         if (a_ev.time() <= b_ev.time()) {
269                                 push_back(a_ev);
270                                 ++a_index;
271                         } else {
272                                 push_back(b_ev);
273                                 ++b_index;
274                         }
275                 }
276
277                 --count;
278         }
279
280         return true;
281 }
282