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 "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"
32 using namespace ARDOUR;
35 MidiStateTracker::MidiStateTracker ()
41 MidiStateTracker::reset ()
43 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: reset\n", this));
44 memset (_active_notes, 0, sizeof (_active_notes));
49 MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
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());
59 MidiStateTracker::add (uint8_t note, uint8_t chn)
61 if (_active_notes[note+128 * chn] == 0) {
64 ++_active_notes[note + 128 * chn];
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;
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]));
76 MidiStateTracker::remove (uint8_t note, uint8_t chn)
78 switch (_active_notes[note + 128 * chn]) {
83 _active_notes [note + 128 * chn] = 0;
86 --_active_notes [note + 128 * chn];
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]));
96 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to, bool& looped)
100 // DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 track notes, looped = %2\n", this, looped));
102 for (MidiBuffer::iterator i = from; i != to; ++i) {
103 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
104 if (ev.event_type() == LoopEventType) {
109 /* catch AllNotesOff message and turn off all notes
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));
117 track_note_onoffs (ev);
123 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
125 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
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 ?
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));
151 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
155 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
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;
167 /* note that we do not care about failure from
168 write() ... should we warn someone ?
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));
181 MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time)
183 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
189 /* NOTE: the src must be locked */
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);
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 */
212 MidiStateTracker::dump (ostream& o)
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";