Fix a couple of valgrind-spotted out-of-bounds accesses that may have been causing...
[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 " << " size: " << _size 
115             << " event size: " << ev.size() 
116             << " capacity: " << _capacity 
117             << " stamp size: " << stamp_size << " \n";*/
118         
119         if (_size + stamp_size + ev.size() >= _capacity) {
120                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
121                 return false;
122         }
123
124         if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
125                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
126                 return false;
127         }
128
129         uint8_t* const write_loc = _data + _size;
130         *((TimeType*)write_loc) = ev.time();
131         memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
132
133         _size += stamp_size + ev.size();
134         _silent = false;
135         
136         return true;
137 }
138
139
140 /** Push an event into the buffer.
141  *
142  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
143  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
144  * Realtime safe.
145  * @return false if operation failed (not enough room)
146  */
147 bool
148 MidiBuffer::push_back(const jack_midi_event_t& ev)
149 {
150         const size_t stamp_size = sizeof(TimeType);
151         if (_size + stamp_size + ev.size >= _capacity) {
152                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
153                 return false;
154         }
155         
156         if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
157                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
158                 return false;
159         }
160
161         uint8_t* const write_loc = _data + _size;
162         *((TimeType*)write_loc) = ev.time;
163         memcpy(write_loc + stamp_size, ev.buffer, ev.size);
164
165         _size += stamp_size + ev.size;
166         _silent = false;
167         
168         return true;
169 }
170
171
172 /** Reserve space for a new event in the buffer.
173  *
174  * This call is for copying MIDI directly into the buffer, the data location
175  * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
176  * This call MUST be immediately followed by a write to the returned data
177  * location, or the buffer will be corrupted and very nasty things will happen.
178  */
179 uint8_t*
180 MidiBuffer::reserve(TimeType time, size_t size)
181 {
182         const size_t stamp_size = sizeof(TimeType);
183         if (_size + stamp_size + size >= _capacity) {
184                 return 0;
185         }
186
187         // write timestamp
188         uint8_t* write_loc = _data + _size;
189         *((TimeType*)write_loc) = time;
190         
191         // move write_loc to begin of MIDI buffer data to write to
192         write_loc += stamp_size;
193
194         _size += stamp_size + size;
195         _silent = false;
196         
197         return write_loc;
198 }
199
200
201 void
202 MidiBuffer::silence (nframes_t nframes, nframes_t offset)
203 {
204         /* XXX iterate over existing events, find all in range given by offset & nframes,
205            and delete them.
206         */
207
208         _size = 0;
209         _silent = true;
210 }
211
212 /** Merge \a other into this buffer.  Realtime safe. */
213 bool
214 MidiBuffer::merge_in_place(const MidiBuffer &other)
215 {
216         if (other.size() == 0) {
217                 return true;
218         }
219
220         if (_size == 0) {
221                 copy(other);
222                 return true;
223         }
224
225         if (_size + other.size() > _capacity) {
226                 cerr << "MidiBuffer::merge failed (no space)" << endl;
227                 return false;
228         }
229         
230         cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
231         return true;
232 }
233
234 /** Clear, and merge \a a and \a b into this buffer.
235  *
236  * \return true if complete merge was successful
237  */
238 bool
239 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
240 {
241         _size = 0;
242         
243         if (this == &a) {
244             merge_in_place(b);
245         }
246
247         if (this == &b) {
248             merge_in_place(a);
249         }
250         
251         cerr << "FIXME: MIDI BUFFER MERGE" << endl;
252         return true;
253 }
254