a grab bag of changes correcting and improving the way MIDI note on/off tracking...
[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
22 #include "pbd/compose.h"
23 #include "pbd/stacktrace.h"
24
25 #include "ardour/debug.h"
26 #include "ardour/event_type_map.h"
27 #include "ardour/midi_ring_buffer.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/midi_state_tracker.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33
34
35 MidiStateTracker::MidiStateTracker ()
36 {
37         reset ();
38 }
39
40 void
41 MidiStateTracker::reset ()
42 {
43         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: reset\n", this));
44         memset (_active_notes, 0, sizeof (_active_notes));
45         _on = 0;
46 }
47
48 void
49 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
50 {
51         if (event.is_note_on()) {
52                 add (event.note(), event.channel());
53         } else if (event.is_note_off()){
54                 remove (event.note(), event.channel());
55         }
56 }
57
58 void
59 MidiStateTracker::add (uint8_t note, uint8_t chn)
60 {
61         if (_active_notes[note+128 * chn] == 0) {
62                 ++_on;
63         }
64         ++_active_notes[note + 128 * chn];
65
66         if (_active_notes[note+128 * chn] > 1) {
67                 cerr << this << " note " << (int) note << '/' << (int) chn << " was already on, now at " << (int) _active_notes[note+128*chn] << endl;
68         }
69
70         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 ON %2/%3 voices %5 total on %4\n",
71                                                                this, (int) note, (int) chn, _on,
72                                                                (int) _active_notes[note+128 * chn]));
73 }
74
75 void
76 MidiStateTracker::remove (uint8_t note, uint8_t chn)
77 {       
78         switch (_active_notes[note + 128 * chn]) {
79         case 0:
80                 break;
81         case 1:
82                 --_on;
83                 _active_notes [note + 128 * chn] = 0;
84                 break;
85         default:
86                 --_active_notes [note + 128 * chn];
87                 break;
88
89         }
90         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 OFF %2/%3 current voices = %5 total on %4\n",
91                                                                this, (int) note, (int) chn, _on, 
92                                                                (int) _active_notes[note+128 * chn]));
93 }
94
95 void
96 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to, bool& looped)
97 {
98         looped = false;
99
100         // DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 track notes, looped = %2\n", this, looped));
101
102         for (MidiBuffer::iterator i = from; i != to; ++i) {
103                 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
104                 if (ev.event_type() == LoopEventType) {
105                         looped = true;
106                         continue;
107                 }
108
109                 /* catch AllNotesOff message and turn off all notes
110                  */
111                 
112                 if (ev.type() == MIDI_CTL_ALL_NOTES_OFF) {
113                         cerr << "State tracker sees ALL_NOTES_OFF, silenceing " << sizeof (_active_notes) << endl;
114                         memset (_active_notes, 0, sizeof (_active_notes));
115                         _on = 0;
116                 } else {
117                         track_note_onoffs (ev);
118                 }
119         }
120 }
121
122 void
123 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
124 {
125         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
126
127         if (!_on) {
128                 return;
129         }
130
131         for (int channel = 0; channel < 16; ++channel) {
132                 for (int note = 0; note < 128; ++note) {
133                         while (_active_notes[note + 128 * channel]) {
134                                 uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
135                                 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
136                                         (MIDI_CMD_NOTE_OFF, time, 3, buffer, false);
137                                 /* note that we do not care about failure from
138                                    push_back() ... should we warn someone ?
139                                 */
140                                 dst.push_back (noteoff);
141                                 _active_notes[note + 128 * channel]--;
142                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n", 
143                                                                                        this, (int) note, (int) channel, time));
144                         }
145                 }
146         }
147         _on = 0;
148 }
149
150 void
151 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
152 {
153         uint8_t buf[3];
154
155         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
156
157         if (!_on) {
158                 return;
159         }
160
161         for (int channel = 0; channel < 16; ++channel) {
162                 for (int note = 0; note < 128; ++note) {
163                         while (_active_notes[note + 128 * channel]) {
164                                 buf[0] = MIDI_CMD_NOTE_OFF|channel;
165                                 buf[1] = note;
166                                 buf[2] = 0;
167                                 /* note that we do not care about failure from
168                                    write() ... should we warn someone ?
169                                 */
170                                 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
171                                 _active_notes[note + 128 * channel]--;
172                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n", 
173                                                                                        this, (int) note, (int) channel, time));
174                         }
175                 }
176         }
177         _on = 0;
178 }
179
180 void
181 MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time)
182 {
183         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
184
185         if (!_on) {
186                 return;
187         }
188
189         /* NOTE: the src must be locked */
190
191         for (int channel = 0; channel < 16; ++channel) {
192                 for (int note = 0; note < 128; ++note) {
193                         while (_active_notes[note + 128 * channel]) {
194                                 Evoral::MIDIEvent<Evoral::MusicalTime> ev ((MIDI_CMD_NOTE_OFF|channel), time, 3, 0, true);
195                                 ev.set_type (MIDI_CMD_NOTE_OFF);
196                                 ev.set_channel (channel);
197                                 ev.set_note (note);
198                                 ev.set_velocity (0);
199                                 src.append_event_unlocked_beats (ev);
200                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n", 
201                                                                                        this, (int) note, (int) channel, time));
202                                 _active_notes[note + 128 * channel]--;
203                                 /* don't stack events up at the same time */
204                                 time += 1.0/128.0;
205                         }
206                 }
207         }
208         _on = 0;
209 }
210
211 void
212 MidiStateTracker::dump (ostream& o)
213 {
214         o << "******\n";
215         for (int c = 0; c < 16; ++c) {
216                 for (int x = 0; x < 128; ++x) {
217                         if (_active_notes[c * 128 + x]) {
218                                 o << "Channel " << c+1 << " Note " << x << " is on ("
219                                   << (int) _active_notes[c*128+x] <<  "times)\n";
220                         }
221                 }
222         }
223         o << "+++++\n";
224 }