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/midi_source.h"
29 #include "ardour/midi_state_tracker.h"
30 #include "ardour/parameter_types.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::add (uint8_t note, uint8_t chn)
52 if (_active_notes[note+128 * chn] == 0) {
55 ++_active_notes[note + 128 * chn];
57 if (_active_notes[note+128 * chn] > 1) {
58 cerr << this << " note " << (int) note << '/' << (int) chn << " was already on, now at " << (int) _active_notes[note+128*chn] << endl;
61 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 ON %2/%3 voices %5 total on %4\n",
62 this, (int) note, (int) chn, _on,
63 (int) _active_notes[note+128 * chn]));
67 MidiStateTracker::remove (uint8_t note, uint8_t chn)
69 switch (_active_notes[note + 128 * chn]) {
74 _active_notes [note + 128 * chn] = 0;
77 --_active_notes [note + 128 * chn];
81 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 OFF %2/%3 current voices = %5 total on %4\n",
82 this, (int) note, (int) chn, _on,
83 (int) _active_notes[note+128 * chn]));
87 MidiStateTracker::track (const MidiBuffer::const_iterator &from, const MidiBuffer::const_iterator &to)
89 for (MidiBuffer::const_iterator i = from; i != to; ++i) {
95 MidiStateTracker::track (const uint8_t* evbuf)
97 const uint8_t type = evbuf[0] & 0xF0;
98 const uint8_t chan = evbuf[0] & 0x0F;
100 case MIDI_CTL_ALL_NOTES_OFF:
103 case MIDI_CMD_NOTE_ON:
106 case MIDI_CMD_NOTE_OFF:
107 remove(evbuf[1], chan);
113 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
115 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
121 for (int channel = 0; channel < 16; ++channel) {
122 for (int note = 0; note < 128; ++note) {
123 while (_active_notes[note + 128 * channel]) {
124 uint8_t buffer[3] = { ((uint8_t) (MIDI_CMD_NOTE_OFF | channel)), uint8_t (note), 0 };
125 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
126 (MIDI_CMD_NOTE_OFF, time, 3, buffer, false);
127 /* note that we do not care about failure from
128 push_back() ... should we warn someone ?
130 dst.push_back (noteoff);
131 _active_notes[note + 128 * channel]--;
132 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n",
133 this, (int) note, (int) channel, time));
141 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
145 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
151 for (int channel = 0; channel < 16; ++channel) {
152 for (int note = 0; note < 128; ++note) {
153 while (_active_notes[note + 128 * channel]) {
154 buf[0] = MIDI_CMD_NOTE_OFF|channel;
157 /* note that we do not care about failure from
158 write() ... should we warn someone ?
160 dst.write (time, midi_parameter_type (buf[0]), 3, buf);
161 _active_notes[note + 128 * channel]--;
162 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n",
163 this, (int) note, (int) channel, time));
171 MidiStateTracker::resolve_notes (MidiSource& src, const MidiSource::Lock& lock, Evoral::Beats time)
173 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
179 /* NOTE: the src must be locked */
181 for (int channel = 0; channel < 16; ++channel) {
182 for (int note = 0; note < 128; ++note) {
183 while (_active_notes[note + 128 * channel]) {
184 Evoral::MIDIEvent<Evoral::Beats> ev ((MIDI_CMD_NOTE_OFF|channel), time, 3, 0, true);
185 ev.set_type (MIDI_CMD_NOTE_OFF);
186 ev.set_channel (channel);
189 src.append_event_beats (lock, ev);
190 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n",
191 this, (int) note, (int) channel, time));
192 _active_notes[note + 128 * channel]--;
193 /* don't stack events up at the same time */
194 time += Evoral::Beats::tick();
202 MidiStateTracker::dump (ostream& o)
205 for (int c = 0; c < 16; ++c) {
206 for (int x = 0; x < 128; ++x) {
207 if (_active_notes[c * 128 + x]) {
208 o << "Channel " << c+1 << " Note " << x << " is on ("
209 << (int) _active_notes[c*128+x] << "times)\n";