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