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.
22 #include "pbd/compose.h"
23 #include "pbd/stacktrace.h"
25 #include "evoral/EventSink.hpp"
27 #include "ardour/debug.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_state_tracker.h"
33 using namespace ARDOUR;
36 MidiStateTracker::MidiStateTracker ()
42 MidiStateTracker::reset ()
44 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: reset\n", this));
45 memset (_active_notes, 0, sizeof (_active_notes));
50 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
52 if (event.is_note_on()) {
53 add (event.note(), event.channel());
54 } else if (event.is_note_off()){
55 remove (event.note(), event.channel());
60 MidiStateTracker::add (uint8_t note, uint8_t chn)
62 if (_active_notes[note+128 * chn] == 0) {
65 ++_active_notes[note + 128 * chn];
67 if (_active_notes[note+128 * chn] > 1) {
68 cerr << this << " note " << (int) note << '/' << (int) chn << " was already on, now at " << (int) _active_notes[note+128*chn] << endl;
71 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 ON %2/%3 voices %5 total on %4\n",
72 this, (int) note, (int) chn, _on,
73 (int) _active_notes[note+128 * chn]));
77 MidiStateTracker::remove (uint8_t note, uint8_t chn)
79 switch (_active_notes[note + 128 * chn]) {
84 _active_notes [note + 128 * chn] = 0;
87 --_active_notes [note + 128 * chn];
91 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 OFF %2/%3 current voices = %5 total on %4\n",
92 this, (int) note, (int) chn, _on,
93 (int) _active_notes[note+128 * chn]));
97 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to)
99 // DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 track notes\n", this));
101 for (MidiBuffer::iterator i = from; i != to; ++i) {
102 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
104 /* catch AllNotesOff message and turn off all notes
107 if (ev.type() == MIDI_CTL_ALL_NOTES_OFF) {
108 cerr << "State tracker sees ALL_NOTES_OFF, silenceing " << sizeof (_active_notes) << endl;
109 memset (_active_notes, 0, sizeof (_active_notes));
112 track_note_onoffs (ev);
118 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
120 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
126 for (int channel = 0; channel < 16; ++channel) {
127 for (int note = 0; note < 128; ++note) {
128 while (_active_notes[note + 128 * channel]) {
129 uint8_t buffer[3] = { ((uint8_t) (MIDI_CMD_NOTE_OFF | channel)), uint8_t (note), 0 };
130 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
131 (MIDI_CMD_NOTE_OFF, time, 3, buffer, false);
132 /* note that we do not care about failure from
133 push_back() ... should we warn someone ?
135 dst.push_back (noteoff);
136 _active_notes[note + 128 * channel]--;
137 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n",
138 this, (int) note, (int) channel, time));
146 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
150 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
156 for (int channel = 0; channel < 16; ++channel) {
157 for (int note = 0; note < 128; ++note) {
158 while (_active_notes[note + 128 * channel]) {
159 buf[0] = MIDI_CMD_NOTE_OFF|channel;
162 /* note that we do not care about failure from
163 write() ... should we warn someone ?
165 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
166 _active_notes[note + 128 * channel]--;
167 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n",
168 this, (int) note, (int) channel, time));
176 MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time)
178 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
184 /* NOTE: the src must be locked */
186 for (int channel = 0; channel < 16; ++channel) {
187 for (int note = 0; note < 128; ++note) {
188 while (_active_notes[note + 128 * channel]) {
189 Evoral::MIDIEvent<Evoral::MusicalTime> ev ((MIDI_CMD_NOTE_OFF|channel), time, 3, 0, true);
190 ev.set_type (MIDI_CMD_NOTE_OFF);
191 ev.set_channel (channel);
194 src.append_event_unlocked_beats (ev);
195 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n",
196 this, (int) note, (int) channel, time));
197 _active_notes[note + 128 * channel]--;
198 /* don't stack events up at the same time */
207 MidiStateTracker::dump (ostream& o)
210 for (int c = 0; c < 16; ++c) {
211 for (int x = 0; x < 128; ++x) {
212 if (_active_notes[c * 128 + x]) {
213 o << "Channel " << c+1 << " Note " << x << " is on ("
214 << (int) _active_notes[c*128+x] << "times)\n";