2 * Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pbd/compose.h"
24 #include "pbd/stacktrace.h"
26 #include "evoral/EventSink.hpp"
28 #include "ardour/debug.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_state_tracker.h"
31 #include "ardour/parameter_types.h"
34 using namespace ARDOUR;
37 MidiStateTracker::MidiStateTracker ()
43 MidiStateTracker::reset ()
45 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: reset\n", this));
46 memset (_active_notes, 0, sizeof (_active_notes));
51 MidiStateTracker::add (uint8_t note, uint8_t chn)
53 if (_active_notes[note+128 * chn] == 0) {
56 ++_active_notes[note + 128 * chn];
58 if (_active_notes[note+128 * chn] > 1) {
59 //cerr << this << " note " << (int) note << '/' << (int) chn << " was already on, now at " << (int) _active_notes[note+128*chn] << endl;
62 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 ON %2/%3 voices %5 total on %4\n",
63 this, (int) note, (int) chn, _on,
64 (int) _active_notes[note+128 * chn]));
68 MidiStateTracker::remove (uint8_t note, uint8_t chn)
70 switch (_active_notes[note + 128 * chn]) {
75 _active_notes [note + 128 * chn] = 0;
78 --_active_notes [note + 128 * chn];
82 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 OFF %2/%3 current voices = %5 total on %4\n",
83 this, (int) note, (int) chn, _on,
84 (int) _active_notes[note+128 * chn]));
88 MidiStateTracker::track (const MidiBuffer::const_iterator &from, const MidiBuffer::const_iterator &to)
90 for (MidiBuffer::const_iterator i = from; i != to; ++i) {
96 MidiStateTracker::track (const uint8_t* evbuf)
98 const uint8_t type = evbuf[0] & 0xF0;
99 const uint8_t chan = evbuf[0] & 0x0F;
101 case MIDI_CTL_ALL_NOTES_OFF:
104 case MIDI_CMD_NOTE_ON:
107 case MIDI_CMD_NOTE_OFF:
108 remove(evbuf[1], chan);
114 MidiStateTracker::resolve_notes (MidiBuffer &dst, samplepos_t time)
116 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
122 for (int channel = 0; channel < 16; ++channel) {
123 for (int note = 0; note < 128; ++note) {
124 while (_active_notes[note + 128 * channel]) {
125 uint8_t buffer[3] = { ((uint8_t) (MIDI_CMD_NOTE_OFF | channel)), uint8_t (note), 0 };
126 Evoral::Event<MidiBuffer::TimeType> noteoff
127 (Evoral::MIDI_EVENT, time, 3, buffer, false);
128 /* note that we do not care about failure from
129 push_back() ... should we warn someone ?
131 dst.push_back (noteoff);
132 _active_notes[note + 128 * channel]--;
133 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n",
134 this, (int) note, (int) channel, time));
142 MidiStateTracker::resolve_notes (Evoral::EventSink<samplepos_t> &dst, samplepos_t time)
146 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
152 for (int channel = 0; channel < 16; ++channel) {
153 for (int note = 0; note < 128; ++note) {
154 while (_active_notes[note + 128 * channel]) {
155 buf[0] = MIDI_CMD_NOTE_OFF|channel;
158 /* note that we do not care about failure from
159 write() ... should we warn someone ?
161 dst.write (time, Evoral::MIDI_EVENT, 3, buf);
162 _active_notes[note + 128 * channel]--;
163 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n",
164 this, (int) note, (int) channel, time));
172 MidiStateTracker::resolve_notes (MidiSource& src, const MidiSource::Lock& lock, Temporal::Beats time)
174 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
180 /* NOTE: the src must be locked */
182 for (int channel = 0; channel < 16; ++channel) {
183 for (int note = 0; note < 128; ++note) {
184 while (_active_notes[note + 128 * channel]) {
185 Evoral::Event<Temporal::Beats> ev (Evoral::MIDI_EVENT, time, 3, 0, true);
186 ev.set_type (MIDI_CMD_NOTE_OFF);
187 ev.set_channel (channel);
190 src.append_event_beats (lock, ev);
191 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n",
192 this, (int) note, (int) channel, time));
193 _active_notes[note + 128 * channel]--;
194 /* don't stack events up at the same time */
195 time += Temporal::Beats::tick();
203 MidiStateTracker::dump (ostream& o)
206 for (int c = 0; c < 16; ++c) {
207 for (int x = 0; x < 128; ++x) {
208 if (_active_notes[c * 128 + x]) {
209 o << "Channel " << c+1 << " Note " << x << " is on ("
210 << (int) _active_notes[c*128+x] << " times)\n";