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