switch MIDI Clock slave code to use DEBUG_TRACE; don't make it require start/stop...
[ardour2.git] / libs / ardour / midi_state_tracker.cc
blob03ac16ca8722af7ae7d8f2d54f99ecb8e3f25878
1 /*
2 Copyright (C) 2006-2008 Paul Davis
3 Author: Torben Hohn
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.
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.
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.
20 #include <iostream>
21 #include "ardour/event_type_map.h"
22 #include "ardour/midi_ring_buffer.h"
23 #include "ardour/midi_state_tracker.h"
25 using namespace std;
26 using namespace ARDOUR;
29 MidiStateTracker::MidiStateTracker ()
31 reset ();
34 void
35 MidiStateTracker::reset ()
37 memset (_active_notes, 0, sizeof (_active_notes));
38 _on = 0;
41 void
42 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
44 if (event.is_note_on()) {
45 add (event.note(), event.channel());
46 } else if (event.is_note_off()){
47 remove (event.note(), event.channel());
51 void
52 MidiStateTracker::add (uint8_t note, uint8_t chn)
54 ++_active_notes[note + 128 * chn];
55 ++_on;
58 void
59 MidiStateTracker::remove (uint8_t note, uint8_t chn)
61 switch (_active_notes[note + 128 * chn]) {
62 case 0:
63 break;
64 case 1:
65 --_on;
66 _active_notes [note + 128 * chn] = 0;
67 break;
68 default:
69 --_active_notes [note + 128 * chn];
70 break;
75 void
76 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to, bool& looped)
78 looped = false;
80 for (MidiBuffer::iterator i = from; i != to; ++i) {
81 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
82 if (ev.event_type() == LoopEventType) {
83 looped = true;
84 continue;
87 track_note_onoffs (ev);
91 void
92 MidiStateTracker::resolve_notes (MidiBuffer &dst, nframes64_t time)
94 if (!_on) {
95 return;
98 for (int channel = 0; channel < 16; ++channel) {
99 for (int note = 0; note < 128; ++note) {
100 while (_active_notes[note + 128 * channel]) {
101 uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
102 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
103 (time, MIDI_CMD_NOTE_OFF, 3, buffer, false);
104 dst.push_back (noteoff);
105 _active_notes[note + 128 * channel]--;
109 _on = 0;
112 void
113 MidiStateTracker::resolve_notes (Evoral::EventSink<nframes_t> &dst, nframes64_t time)
115 uint8_t buf[3];
117 if (!_on) {
118 return;
121 for (int channel = 0; channel < 16; ++channel) {
122 for (int note = 0; note < 128; ++note) {
123 while (_active_notes[note + 128 * channel]) {
124 buf[0] = MIDI_CMD_NOTE_OFF|channel;
125 buf[1] = note;
126 buf[2] = 0;
127 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
128 _active_notes[note + 128 * channel]--;
132 _on = 0;
135 void
136 MidiStateTracker::dump (ostream& o)
138 o << "******\n";
139 for (int c = 0; c < 16; ++c) {
140 for (int x = 0; x < 128; ++x) {
141 if (_active_notes[c * 128 + x]) {
142 o << "Channel " << c+1 << " Note " << x << " is on ("
143 << (int) _active_notes[c*128+x] << "times)\n";
147 o << "+++++\n";