2 Copyright (C) 2006-2008 Paul Davis
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)
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
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.
21 #include "ardour/event_type_map.h"
22 #include "ardour/midi_state_tracker.h"
25 using namespace ARDOUR;
28 MidiStateTracker::MidiStateTracker ()
30 _active_notes.reset();
34 MidiStateTracker::reset ()
36 _active_notes.reset ();
40 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
42 if (event.is_note_on()) {
43 _active_notes [event.note() + 128 * event.channel()] = true;
44 } else if (event.is_note_off()){
45 _active_notes [event.note() + 128 * event.channel()] = false;
50 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to)
54 for (MidiBuffer::iterator i = from; i != to; ++i) {
55 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
56 if (ev.event_type() == LoopEventType) {
61 track_note_onoffs (ev);
67 MidiStateTracker::resolve_notes (MidiBuffer &dst, nframes_t time)
69 // Dunno if this is actually faster but at least it fills our cacheline.
70 if (_active_notes.none ())
73 for (int channel = 0; channel < 16; ++channel) {
74 for (int note = 0; note < 128; ++note) {
75 if (_active_notes[channel * 128 + note]) {
76 uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
77 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
78 (time, MIDI_CMD_NOTE_OFF, 3, buffer, false);
80 dst.push_back (noteoff);
82 _active_notes [channel * 128 + note] = false;
89 MidiStateTracker::dump (ostream& o)
92 for (int c = 0; c < 16; ++c) {
93 for (int x = 0; x < 128; ++x) {
94 if (_active_notes[c * 128 + x]) {
95 o << "Channel " << c+1 << " Note " << x << " is on\n";