new port design, probably about 90% done (i.e it mostly works and this commit is...
[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 = (MidiEvent *) malloc(sizeof(MidiEvent) * _capacity);
78         _data = (Byte *) malloc(sizeof(Byte) * _capacity * MAX_EVENT_SIZE);
79 #else
80         posix_memalign((void**)&_events, CPU_CACHE_ALIGN, sizeof(MidiEvent) * _capacity);
81         posix_memalign((void**)&_data, CPU_CACHE_ALIGN, sizeof(Byte) * _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         const MidiBuffer& msrc = (MidiBuffer&)src;
108
109         assert(_capacity >= src.size());
110
111         clear();
112         assert(_size == 0);
113
114         // FIXME: slow
115         for (size_t i=0; i < src.size(); ++i) {
116                 const MidiEvent& ev = msrc[i];
117                 if (ev.time() >= offset && ev.time() < offset+nframes) {
118                         //cerr << "MidiBuffer::read_from got event, " << ev.time() << endl;
119                         push_back(ev);
120                 } else {
121                         //cerr << "MidiBuffer event out of range, " << ev.time() << endl;
122                 }
123         }
124
125         _silent = src.silent();
126 }
127
128
129 /** Push an event into the buffer.
130  *
131  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
132  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
133  * Realtime safe.
134  * @return false if operation failed (not enough room)
135  */
136 bool
137 MidiBuffer::push_back(const MidiEvent& ev)
138 {
139         if (_size == _capacity)
140                 return false;
141
142         Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
143
144         memcpy(write_loc, ev.buffer(), ev.size());
145         _events[_size] = ev;
146         _events[_size].set_buffer(write_loc, false);
147         ++_size;
148
149         //cerr << "MidiBuffer: pushed, size = " << _size << endl;
150
151         _silent = false;
152
153         return true;
154 }
155
156
157 /** Push an event into the buffer.
158  *
159  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
160  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
161  * Realtime safe.
162  * @return false if operation failed (not enough room)
163  */
164 bool
165 MidiBuffer::push_back(const jack_midi_event_t& ev)
166 {
167         if (_size == _capacity)
168                 return false;
169
170         Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
171
172         memcpy(write_loc, ev.buffer, ev.size);
173         _events[_size].time() = (double)ev.time;
174         _events[_size].size() = ev.size;
175         _events[_size].set_buffer(write_loc, false);
176         ++_size;
177
178         //cerr << "MidiBuffer: pushed, size = " << _size << endl;
179
180         _silent = false;
181
182         return true;
183 }
184
185
186 /** Reserve space for a new event in the buffer.
187  *
188  * This call is for copying MIDI directly into the buffer, the data location
189  * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
190  * This call MUST be immediately followed by a write to the returned data
191  * location, or the buffer will be corrupted and very nasty things will happen.
192  */
193 Byte*
194 MidiBuffer::reserve(double time, size_t size)
195 {
196         assert(size <= MAX_EVENT_SIZE);
197
198         if (_size == _capacity)
199                 return 0;
200
201         Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
202
203         _events[_size].time() = time;
204         _events[_size].size() = size;
205         _events[_size].set_buffer(write_loc, false);
206         ++_size;
207
208         //cerr << "MidiBuffer: reserved, size = " << _size << endl;
209
210         _silent = false;
211
212         return write_loc;
213 }
214
215
216 void
217 MidiBuffer::silence(nframes_t dur, nframes_t offset)
218 {
219         // FIXME use parameters
220         if (offset != 0)
221                 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
222
223         memset(_events, 0, sizeof(MidiEvent) * _capacity);
224         memset(_data, 0, sizeof(Byte) * _capacity * MAX_EVENT_SIZE);
225         _size = 0;
226         _silent = true;
227 }
228
229
230 /** Clear, and merge \a a and \a b into this buffer.
231  *
232  * FIXME: This is slow.
233  *
234  * \return true if complete merge was successful
235  */
236 bool
237 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
238 {
239         _size = 0;
240
241         // Die if a merge isn't necessary as it's expensive
242         assert(a.size() > 0 && b.size() > 0);
243
244         size_t a_index = 0;
245         size_t b_index = 0;
246         size_t count = a.size() + b.size();
247
248         while (count > 0 && a_index < a.size() && b_index < b.size()) {
249                 
250                 if (size() == capacity()) {
251                         cerr << "WARNING: MIDI buffer overrun, events lost!" << endl;
252                         return false;
253                 }
254                 
255                 if (a_index == a.size()) {
256                         push_back(a[a_index]);
257                         ++a_index;
258                 } else if (b_index == b.size()) {
259                         push_back(b[b_index]);
260                         ++b_index;
261                 } else {
262                         const MidiEvent& a_ev = a[a_index];
263                         const MidiEvent& b_ev = b[b_index];
264
265                         if (a_ev.time() <= b_ev.time()) {
266                                 push_back(a_ev);
267                                 ++a_index;
268                         } else {
269                                 push_back(b_ev);
270                                 ++b_index;
271                         }
272                 }
273
274                 --count;
275         }
276
277         return true;
278 }
279