fix up marshall/unmarshall of note data for MidiModel::DiffCommand
[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 "ardour/midi_ring_buffer.h"
20 #include "ardour/midi_buffer.h"
21 #include "ardour/event_type_map.h"
22
23 using namespace std;
24
25 namespace ARDOUR {
26
27 /** Read a block of MIDI events from buffer into a MidiBuffer.
28  *
29  * Timestamps of events returned are relative to start (i.e. event with stamp 0
30  * occurred at start), with offset added.
31  */
32 template<typename T>
33 size_t
34 MidiRingBuffer<T>::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
35 {
36         if (this->read_space() == 0) {
37                 return 0;
38         }
39
40         T                 ev_time;
41         Evoral::EventType ev_type;
42         uint32_t          ev_size;
43
44         size_t count = 0;
45
46         //cerr << "MRB read " << start << " .. " << end << " + " << offset << endl;
47
48         while (this->read_space() >= sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
49
50                 this->full_peek(sizeof(T), (uint8_t*)&ev_time);
51
52                 if (ev_time > end) {
53                         //cerr << "MRB event @ " << ev_time << " past end @ " << end << endl;
54                         break;
55                 } else if (ev_time < start) {
56                         //cerr << "MRB event @ " << ev_time << " before start @ " << start << endl;
57                         break;
58                 }
59
60                 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
61                 if (!success) {
62                         cerr << "WARNING: error reading event prefix from MIDI ring" << endl;
63                         continue;
64                 }
65
66                 // This event marks a loop end (i.e. the next event's timestamp will be non-monotonic)
67                 if (ev_type == LoopEventType) {
68                         /*ev_time -= start;
69                           ev_time += offset;*/
70                         cerr << "MRB loop boundary @ " << ev_time << endl;
71
72                         // Return without reading data or writing to buffer (loop events have no data)
73                         // FIXME: This is not correct, loses events after the loop this cycle
74                         return count + 1;
75                 }
76
77                 uint8_t status;
78                 success = this->full_peek(sizeof(uint8_t), &status);
79                 assert(success); // If this failed, buffer is corrupt, all hope is lost
80
81                 // Ignore event if it doesn't match channel filter
82                 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
83                         const uint8_t channel = status & 0x0F;
84                         if (!(get_channel_mask() & (1L << channel))) {
85                                 //cerr << "MRB skipping event due to channel mask" << endl;
86                                 this->skip(ev_size); // Advance read pointer to next event
87                                 continue;
88                         }
89                 }
90
91                 /*cerr << "MRB " << this << " - Reading event, time = "
92                         << ev_time << " - " << start << " => " << ev_time - start
93                         << ", size = " << ev_size << endl;*/
94
95                 assert(ev_time >= start);
96                 ev_time -= start;
97                 ev_time += offset;
98
99                 // write the timestamp to address (write_loc - 1)
100                 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
101                 if (write_loc == NULL) {
102                         cerr << "MRB: Unable to reserve space in buffer, event skipped";
103                         continue;
104                 }
105                 
106                 // write MIDI buffer contents
107                 success = Evoral::EventRingBuffer<T>::full_read(ev_size, write_loc);
108                 
109 #if 0
110                 cerr << "wrote MidiEvent to Buffer: " << hex;
111                 for (size_t i=0; i < ev_size; ++i) {
112                         cerr << (int) write_loc[i] << ' ';
113                 }
114                 cerr << dec << endl;
115 #endif
116
117                 if (success) {
118                         if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
119                                 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
120                         }
121                         ++count;
122                 } else {
123                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
124                 }
125         }
126         
127         return count;
128 }
129
130 template class MidiRingBuffer<nframes_t>;
131
132 } // namespace ARDOUR
133