NO-OP: whitespace and comments
[ardour.git] / libs / ardour / midi_state_tracker.cc
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <iostream>
22
23 #include "pbd/compose.h"
24 #include "pbd/stacktrace.h"
25
26 #include "evoral/EventSink.hpp"
27
28 #include "ardour/debug.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_state_tracker.h"
31 #include "ardour/parameter_types.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36
37 MidiStateTracker::MidiStateTracker ()
38 {
39         reset ();
40 }
41
42 void
43 MidiStateTracker::reset ()
44 {
45         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: reset\n", this));
46         memset (_active_notes, 0, sizeof (_active_notes));
47         _on = 0;
48 }
49
50 void
51 MidiStateTracker::add (uint8_t note, uint8_t chn)
52 {
53         if (_active_notes[note+128 * chn] == 0) {
54                 ++_on;
55         }
56         ++_active_notes[note + 128 * chn];
57
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;
60         }
61
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]));
65 }
66
67 void
68 MidiStateTracker::remove (uint8_t note, uint8_t chn)
69 {
70         switch (_active_notes[note + 128 * chn]) {
71         case 0:
72                 break;
73         case 1:
74                 --_on;
75                 _active_notes [note + 128 * chn] = 0;
76                 break;
77         default:
78                 --_active_notes [note + 128 * chn];
79                 break;
80
81         }
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]));
85 }
86
87 void
88 MidiStateTracker::track (const MidiBuffer::const_iterator &from, const MidiBuffer::const_iterator &to)
89 {
90         for (MidiBuffer::const_iterator i = from; i != to; ++i) {
91                 track(*i);
92         }
93 }
94
95 void
96 MidiStateTracker::track (const uint8_t* evbuf)
97 {
98         const uint8_t type = evbuf[0] & 0xF0;
99         const uint8_t chan = evbuf[0] & 0x0F;
100         switch (type) {
101         case MIDI_CTL_ALL_NOTES_OFF:
102                 reset();
103                 break;
104         case MIDI_CMD_NOTE_ON:
105                 add(evbuf[1], chan);
106                 break;
107         case MIDI_CMD_NOTE_OFF:
108                 remove(evbuf[1], chan);
109                 break;
110         }
111 }
112
113 void
114 MidiStateTracker::resolve_notes (MidiBuffer &dst, samplepos_t time)
115 {
116         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
117
118         if (!_on) {
119                 return;
120         }
121
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 ?
130                                 */
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));
135                         }
136                 }
137         }
138         _on = 0;
139 }
140
141 void
142 MidiStateTracker::resolve_notes (Evoral::EventSink<samplepos_t> &dst, samplepos_t time)
143 {
144         uint8_t buf[3];
145
146         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
147
148         if (!_on) {
149                 return;
150         }
151
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;
156                                 buf[1] = note;
157                                 buf[2] = 0;
158                                 /* note that we do not care about failure from
159                                    write() ... should we warn someone ?
160                                 */
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));
165                         }
166                 }
167         }
168         _on = 0;
169 }
170
171 void
172 MidiStateTracker::resolve_notes (MidiSource& src, const MidiSource::Lock& lock, Temporal::Beats time)
173 {
174         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
175
176         if (!_on) {
177                 return;
178         }
179
180         /* NOTE: the src must be locked */
181
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);
188                                 ev.set_note (note);
189                                 ev.set_velocity (0);
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();
196                         }
197                 }
198         }
199         _on = 0;
200 }
201
202 void
203 MidiStateTracker::dump (ostream& o)
204 {
205         o << "******\n";
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";
211                         }
212                 }
213         }
214         o << "+++++\n";
215 }