Removed fixed/maximum event size assumption/limitation from MIDI buffer.
[ardour.git] / libs / ardour / midi_state_tracker.cc
1 /*
2     Copyright (C) 2006-2008 Paul Davis 
3     Author: Torben Hohn
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 <ardour/event_type_map.h>
22 #include <ardour/midi_state_tracker.h>
23
24 using namespace std;
25 using namespace ARDOUR;
26
27
28 MidiStateTracker::MidiStateTracker ()
29 {
30         _active_notes.reset();
31 }
32
33 void
34 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent &event)
35 {
36         if (event.is_note_on()) {
37                 _active_notes [event.note() + 128 * event.channel()] = true;
38         } else if (event.is_note_off()){
39                 _active_notes [event.note() + 128 * event.channel()] = false;
40         }
41 }
42
43 bool
44 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to)
45 {
46         bool ret = false;
47
48         for (MidiBuffer::iterator i = from; i != to; ++i) {
49                 const Evoral::MIDIEvent ev(*i, false);
50                 if (ev.event_type() == LoopEventType) {
51                         ret = true;
52                         continue;
53                 }
54
55                 track_note_onoffs (ev);
56         }
57         return ret;
58 }
59
60 void
61 MidiStateTracker::resolve_notes (MidiBuffer &dst, nframes_t time)
62 {
63         // Dunno if this is actually faster but at least it fills our cacheline.
64         if (_active_notes.none ())
65                 return;
66
67         for (int channel = 0; channel < 16; ++channel) {
68                 for (int note = 0; note < 128; ++note) {
69                         if (_active_notes[channel * 128 + note]) {
70                                 uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
71                                 Evoral::MIDIEvent noteoff (time, MIDI_CMD_NOTE_OFF, 3, buffer, false);
72
73                                 dst.push_back (noteoff);        
74
75                                 _active_notes [channel * 128 + note] = false;
76                         }
77                 }
78         }
79 }
80