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