Disable excessive debug printing.
[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_ring_buffer.h"
23 #include "ardour/midi_state_tracker.h"
24
25 using namespace std;
26 using namespace ARDOUR;
27
28
29 MidiStateTracker::MidiStateTracker ()
30 {
31         reset ();
32 }
33
34 void
35 MidiStateTracker::reset ()
36 {
37         memset (_active_notes, 0, sizeof (_active_notes));
38 }
39
40 void
41 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
42 {
43         if (event.is_note_on()) {
44                 _active_notes [event.note() + 128 * event.channel()]++;
45         } else if (event.is_note_off()){
46                 if (_active_notes[event.note() + 128 * event.channel()]) {
47                         _active_notes [event.note() + 128 * event.channel()]--;
48                 }
49         }
50 }
51
52 void
53 MidiStateTracker::add (uint8_t note, uint8_t chn)
54 {
55         ++_active_notes[note + 128 * chn];
56 }
57
58 void
59 MidiStateTracker::remove (uint8_t note, uint8_t chn)
60 {
61         if (_active_notes[note + 128 * chn]) {
62                 --_active_notes[note + 128 * chn];
63         }
64 }
65
66 void
67 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to, bool& looped)
68 {
69         looped = false;
70
71         for (MidiBuffer::iterator i = from; i != to; ++i) {
72                 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
73                 if (ev.event_type() == LoopEventType) {
74                         looped = true;
75                         continue;
76                 }
77
78                 track_note_onoffs (ev);
79         }
80 }
81
82 void
83 MidiStateTracker::resolve_notes (MidiBuffer &dst, nframes64_t time)
84 {
85         for (int channel = 0; channel < 16; ++channel) {
86                 for (int note = 0; note < 128; ++note) {
87                         while (_active_notes[channel * 128 + note]) {
88                                 uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
89                                 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
90                                         (time, MIDI_CMD_NOTE_OFF, 3, buffer, false);
91                                 dst.push_back (noteoff);
92
93                                 _active_notes[channel * 128 + note]--;
94                         }
95                 }
96         }
97 }
98
99 void
100 MidiStateTracker::resolve_notes (MidiRingBuffer<nframes_t> &dst, nframes64_t time)
101 {
102         uint8_t buf[3];
103         for (int channel = 0; channel < 16; ++channel) {
104                 for (int note = 0; note < 128; ++note) {
105                         while (_active_notes[channel * 128 + note]) {
106                                 buf[0] = MIDI_CMD_NOTE_OFF|channel;
107                                 buf[1] = note;
108                                 buf[2] = 0;
109                                 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
110                                 _active_notes[channel * 128 + note]--;
111                         }
112                 }
113         }
114 }
115
116 void
117 MidiStateTracker::dump (ostream& o)
118 {
119         o << "******\n";
120         for (int c = 0; c < 16; ++c) {
121                 for (int x = 0; x < 128; ++x) {
122                         if (_active_notes[c * 128 + x]) {
123                                 o << "Channel " << c+1 << " Note " << x << " is on ("
124                                   << (int) _active_notes[c*128+x] <<  "times)\n";
125                         }
126                 }
127         }
128         o << "+++++\n";
129 }