the mega-properties/SequenceProperty patch. split is broken at present (right hand...
[ardour.git] / libs / ardour / midi_ring_buffer.cc
1 /*
2     Copyright (C) 2006-2008 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "pbd/compose.h"
20
21 #include "ardour/debug.h"
22 #include "ardour/midi_ring_buffer.h"
23 #include "ardour/midi_buffer.h"
24 #include "ardour/event_type_map.h"
25
26 using namespace std;
27 using namespace ARDOUR;
28 using namespace PBD;
29
30 /** Read a block of MIDI events from buffer into a MidiBuffer.
31  *
32  * Timestamps of events returned are relative to start (i.e. event with stamp 0
33  * occurred at start), with offset added.
34  */
35 template<typename T>
36 size_t
37 MidiRingBuffer<T>::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
38 {
39         if (this->read_space() == 0) {
40                 return 0;
41         }
42
43         T                 ev_time;
44         Evoral::EventType ev_type;
45         uint32_t          ev_size;
46
47         size_t count = 0;
48
49         while (this->read_space() >= sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
50
51                 this->full_peek(sizeof(T), (uint8_t*)&ev_time);
52
53                 if (ev_time > end) {
54                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
55                         break;
56                 } else if (ev_time < start) {
57                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
58                         break;
59                 } else {
60                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
61                 }
62
63                 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
64                 if (!success) {
65                         cerr << "WARNING: error reading event prefix from MIDI ring" << endl;
66                         continue;
67                 }
68
69                 // This event marks a loop end (i.e. the next event's timestamp will be non-monotonic)
70                 if (ev_type == LoopEventType) {
71                         /*ev_time -= start;
72                           ev_time += offset;*/
73                         // cerr << "MRB loop boundary @ " << ev_time << endl;
74
75                         // Return without reading data or writing to buffer (loop events have no data)
76                         // FIXME: This is not correct, loses events after the loop this cycle
77                         return count + 1;
78                 }
79
80                 uint8_t status;
81                 success = this->full_peek(sizeof(uint8_t), &status);
82                 assert(success); // If this failed, buffer is corrupt, all hope is lost
83
84                 // Ignore event if it doesn't match channel filter
85                 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
86                         const uint8_t channel = status & 0x0F;
87                         if (!(get_channel_mask() & (1L << channel))) {
88                                 // cerr << "MRB skipping event due to channel mask" << endl;
89                                 this->skip(ev_size); // Advance read pointer to next event
90                                 continue;
91                         }
92                 }
93
94                 assert(ev_time >= start);
95                 ev_time -= start;
96                 ev_time += offset;
97
98                 // write the timestamp to address (write_loc - 1)
99                 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
100                 if (write_loc == NULL) {
101                         // cerr << "MRB: Unable to reserve space in buffer, event skipped";
102                         continue;
103                 }
104
105                 // write MIDI buffer contents
106                 success = Evoral::EventRingBuffer<T>::full_read(ev_size, write_loc);
107
108 #ifndef NDEBUG
109                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, "wrote MidiEvent to Buffer: ");
110                 for (size_t i=0; i < ev_size; ++i) {
111                         DEBUG_STR_DECL(a);
112                         DEBUG_STR_APPEND(a,hex);
113                         DEBUG_STR_APPEND(a,"0x");
114                         DEBUG_STR_APPEND(a,(int)write_loc[i]);
115                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
116                 }
117                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, "\n");
118 #endif
119
120                 if (success) {
121                         if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
122                                 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
123                         }
124                         ++count;
125                 } else {
126                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
127                 }
128         }
129
130         return count;
131 }
132 template<typename T>
133 void
134 MidiRingBuffer<T>::dump(ostream& str)
135 {
136         size_t rspace;
137
138         if ((rspace = this->read_space()) == 0) {
139                 str << "MRB::dump: empty\n";
140                 return;
141         }
142
143         T                 ev_time;
144         Evoral::EventType ev_type;
145         uint32_t          ev_size;
146         size_t read_ptr = g_atomic_int_get (&this->_read_ptr);
147
148         str << "Dump @ " << read_ptr << endl;
149
150         while (1) {
151                 uint8_t* wp;
152                 uint8_t* data;
153                 size_t write_ptr;
154
155 #define space(r,w) ((w > r) ? (w - r) : ((w - r + this->_size) % this->_size))
156
157                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
158                 if (space (read_ptr, write_ptr) < sizeof (T)) {
159                         break;
160                 }
161
162                 wp = &this->_buf[read_ptr];
163                 memcpy (&ev_time, wp, sizeof (T));
164                 read_ptr = (read_ptr + sizeof (T)) % this->_size;
165                 str << "time " << ev_time;
166
167                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
168                 if (space (read_ptr, write_ptr) < sizeof (ev_type)) {
169                         break;
170                 }
171
172                 wp = &this->_buf[read_ptr];
173                 memcpy (&ev_type, wp, sizeof (ev_type));
174                 read_ptr = (read_ptr + sizeof (ev_type)) % this->_size;
175                 str << " type " << ev_type;
176
177                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
178                 if (space (read_ptr, write_ptr) < sizeof (ev_size)) {
179                         str << "!OUT!\n";
180                         break;
181                 }
182
183                 wp = &this->_buf[read_ptr];
184                 memcpy (&ev_size, wp, sizeof (ev_size));
185                 read_ptr = (read_ptr + sizeof (ev_size)) % this->_size;
186                 str << " size " << ev_size;
187
188                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
189                 if (space (read_ptr, write_ptr) < ev_size) {
190                         str << "!OUT!\n";
191                         break;
192                 }
193
194                 data = new uint8_t[ev_size];
195
196                 wp = &this->_buf[read_ptr];
197                 memcpy (data, wp, ev_size);
198                 read_ptr = (read_ptr + ev_size) % this->_size;
199
200                 for (uint32_t i = 0; i != ev_size; ++i) {
201                         str << ' ' << hex << (int) data[i] << dec;
202                 }
203
204                 str << endl;
205
206                 delete [] data;
207         }
208 }
209
210
211 template class MidiRingBuffer<nframes_t>;
212