more MIDI editing tweaks ; flip mouse mode buttons around for MIDI so that "object...
[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 "pbd/malign.h"
22 #include "ardour/midi_buffer.h"
23
24 using namespace std;
25 using namespace ARDOUR;
26
27 // FIXME: mirroring for MIDI buffers?
28 MidiBuffer::MidiBuffer(size_t capacity)
29         : Buffer(DataType::MIDI, capacity)
30         , _data(0)
31 {
32         if (capacity) {
33                 resize(_capacity);
34                 silence(_capacity);
35         }
36 }
37         
38 MidiBuffer::~MidiBuffer()
39 {
40         free(_data);
41 }
42
43 void
44 MidiBuffer::resize(size_t size)
45 {
46         assert(size > 0);
47
48         if (size < _capacity) {
49                 return;
50         }
51
52         free(_data);
53
54         _size = 0;
55         _capacity = size;
56         cache_aligned_malloc ((void**) &_data, _capacity);
57
58         assert(_data);
59 }
60
61 void
62 MidiBuffer::copy(const MidiBuffer& copy)
63 {
64         assert(_capacity >= copy._size);
65         _size = copy._size;
66         memcpy(_data, copy._data, copy._size);
67 }
68
69
70 /** Read events from @a src starting at time @a offset into the START of this buffer, for
71  * time duration @a nframes.  Relative time, where 0 = start of buffer.
72  *
73  * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
74  */
75 void
76 MidiBuffer::read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset, nframes_t src_offset)
77 {
78         assert (src.type() == DataType::MIDI);
79         assert (&src != this);
80
81         const MidiBuffer& msrc = (MidiBuffer&) src;
82         
83         assert (_capacity >= msrc.size());
84
85         if (dst_offset == 0) {
86                 clear ();
87                 assert (_size == 0);
88         }
89
90         /* XXX use dst_offset somehow */
91         
92         for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
93                 const Evoral::MIDIEvent<TimeType> ev(*i, false);
94                 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
95                         push_back (ev);
96                         cerr << "got note " << ev << endl;
97                 }
98         }
99
100         _silent = src.silent();
101 }
102
103 void
104 MidiBuffer::merge_from (const Buffer& src, nframes_t /*nframes*/, nframes_t /*dst_offset*/, nframes_t /*src_offset*/)
105 {
106         const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
107         assert (mbuf);
108         assert (mbuf != this);
109
110         /* XXX use nframes, and possible offsets */
111         merge_in_place (*mbuf);
112 }
113
114 /** Push an event into the buffer.
115  *
116  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
117  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
118  * Realtime safe.
119  * @return false if operation failed (not enough room)
120  */
121 bool
122 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
123 {
124         const size_t stamp_size = sizeof(TimeType);
125         /*cerr << "MidiBuffer: pushing event @ " << ev.time()
126                 << " size = " << ev.size() << endl;*/
127         
128         if (_size + stamp_size + ev.size() >= _capacity) {
129                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
130                 return false;
131         }
132
133         if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
134                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
135                 return false;
136         }
137
138         push_back(ev.time(), ev.size(), ev.buffer());
139         
140         return true;
141 }
142
143
144 /** Push an event into the buffer.
145  * @return false if operation failed (not enough room)
146  */
147 bool
148 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
149 {
150         const size_t stamp_size = sizeof(TimeType);
151         /*cerr << "MidiBuffer: pushing event @ " << ev.time()
152                 << " size = " << ev.size() << endl;*/
153         
154         if (_size + stamp_size + size >= _capacity) {
155                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
156                 return false;
157         }
158
159         if (!Evoral::midi_event_is_valid(data, size)) {
160                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
161                 return false;
162         }
163
164         uint8_t* const write_loc = _data + _size;
165         *((TimeType*)write_loc) = time;
166         memcpy(write_loc + stamp_size, data, size);
167
168         _size += stamp_size + size;
169         _silent = false;
170         
171         return true;
172 }
173
174
175 /** Push an event into the buffer.
176  *
177  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
178  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
179  * Realtime safe.
180  * @return false if operation failed (not enough room)
181  */
182 bool
183 MidiBuffer::push_back(const jack_midi_event_t& ev)
184 {
185         const size_t stamp_size = sizeof(TimeType);
186         if (_size + stamp_size + ev.size >= _capacity) {
187                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
188                 return false;
189         }
190         
191         if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
192                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
193                 return false;
194         }
195
196         uint8_t* const write_loc = _data + _size;
197         *((TimeType*)write_loc) = ev.time;
198         memcpy(write_loc + stamp_size, ev.buffer, ev.size);
199
200         _size += stamp_size + ev.size;
201         _silent = false;
202         
203         return true;
204 }
205
206
207 /** Reserve space for a new event in the buffer.
208  *
209  * This call is for copying MIDI directly into the buffer, the data location
210  * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
211  * This call MUST be immediately followed by a write to the returned data
212  * location, or the buffer will be corrupted and very nasty things will happen.
213  */
214 uint8_t*
215 MidiBuffer::reserve(TimeType time, size_t size)
216 {
217         const size_t stamp_size = sizeof(TimeType);
218         if (_size + stamp_size + size >= _capacity) {
219                 return 0;
220         }
221
222         // write timestamp
223         uint8_t* write_loc = _data + _size;
224         *((TimeType*)write_loc) = time;
225         
226         // move write_loc to begin of MIDI buffer data to write to
227         write_loc += stamp_size;
228
229         _size += stamp_size + size;
230         _silent = false;
231         
232         return write_loc;
233 }
234
235
236 void
237 MidiBuffer::silence (nframes_t /*nframes*/, nframes_t /*offset*/)
238 {
239         /* XXX iterate over existing events, find all in range given by offset & nframes,
240            and delete them.
241         */
242
243         _size = 0;
244         _silent = true;
245 }
246
247 /** Merge \a other into this buffer.  Realtime safe. */
248 bool
249 MidiBuffer::merge_in_place(const MidiBuffer &other)
250 {
251         if (other.size() == 0) {
252                 return true;
253         }
254
255         if (_size == 0) {
256                 copy(other);
257                 return true;
258         }
259
260         if (_size + other.size() > _capacity) {
261                 cerr << "MidiBuffer::merge failed (no space)" << endl;
262                 return false;
263         }
264         
265         cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
266         return true;
267 }
268
269 /** Clear, and merge \a a and \a b into this buffer.
270  *
271  * \return true if complete merge was successful
272  */
273 bool
274 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
275 {
276         _size = 0;
277         
278         if (this == &a) {
279             merge_in_place(b);
280         }
281
282         if (this == &b) {
283             merge_in_place(a);
284         }
285         
286         cerr << "FIXME: MIDI BUFFER MERGE" << endl;
287         return true;
288 }
289