fix possible crash when setting delivery name w/o panshell
[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/event_type_map.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_state_tracker.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::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
51 {
52         if (event.is_note_on()) {
53                 add (event.note(), event.channel());
54         } else if (event.is_note_off()){
55                 remove (event.note(), event.channel());
56         }
57 }
58
59 void
60 MidiStateTracker::add (uint8_t note, uint8_t chn)
61 {
62         if (_active_notes[note+128 * chn] == 0) {
63                 ++_on;
64         }
65         ++_active_notes[note + 128 * chn];
66
67         if (_active_notes[note+128 * chn] > 1) {
68                 cerr << this << " note " << (int) note << '/' << (int) chn << " was already on, now at " << (int) _active_notes[note+128*chn] << endl;
69         }
70
71         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 ON %2/%3 voices %5 total on %4\n",
72                                                                this, (int) note, (int) chn, _on,
73                                                                (int) _active_notes[note+128 * chn]));
74 }
75
76 void
77 MidiStateTracker::remove (uint8_t note, uint8_t chn)
78 {       
79         switch (_active_notes[note + 128 * chn]) {
80         case 0:
81                 break;
82         case 1:
83                 --_on;
84                 _active_notes [note + 128 * chn] = 0;
85                 break;
86         default:
87                 --_active_notes [note + 128 * chn];
88                 break;
89
90         }
91         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 OFF %2/%3 current voices = %5 total on %4\n",
92                                                                this, (int) note, (int) chn, _on, 
93                                                                (int) _active_notes[note+128 * chn]));
94 }
95
96 void
97 MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::iterator &to)
98 {
99         // DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 track notes\n", this));
100
101         for (MidiBuffer::iterator i = from; i != to; ++i) {
102                 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
103
104                 /* catch AllNotesOff message and turn off all notes
105                  */
106                 
107                 if (ev.type() == MIDI_CTL_ALL_NOTES_OFF) {
108                         cerr << "State tracker sees ALL_NOTES_OFF, silenceing " << sizeof (_active_notes) << endl;
109                         memset (_active_notes, 0, sizeof (_active_notes));
110                         _on = 0;
111                 } else {
112                         track_note_onoffs (ev);
113                 }
114         }
115 }
116
117 void
118 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
119 {
120         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
121
122         if (!_on) {
123                 return;
124         }
125
126         for (int channel = 0; channel < 16; ++channel) {
127                 for (int note = 0; note < 128; ++note) {
128                         while (_active_notes[note + 128 * channel]) {
129                                 uint8_t buffer[3] = { ((uint8_t) (MIDI_CMD_NOTE_OFF | channel)), uint8_t (note), 0 };
130                                 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
131                                         (MIDI_CMD_NOTE_OFF, time, 3, buffer, false);
132                                 /* note that we do not care about failure from
133                                    push_back() ... should we warn someone ?
134                                 */
135                                 dst.push_back (noteoff);
136                                 _active_notes[note + 128 * channel]--;
137                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n", 
138                                                                                        this, (int) note, (int) channel, time));
139                         }
140                 }
141         }
142         _on = 0;
143 }
144
145 void
146 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
147 {
148         uint8_t buf[3];
149
150         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
151
152         if (!_on) {
153                 return;
154         }
155
156         for (int channel = 0; channel < 16; ++channel) {
157                 for (int note = 0; note < 128; ++note) {
158                         while (_active_notes[note + 128 * channel]) {
159                                 buf[0] = MIDI_CMD_NOTE_OFF|channel;
160                                 buf[1] = note;
161                                 buf[2] = 0;
162                                 /* note that we do not care about failure from
163                                    write() ... should we warn someone ?
164                                 */
165                                 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
166                                 _active_notes[note + 128 * channel]--;
167                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n", 
168                                                                                        this, (int) note, (int) channel, time));
169                         }
170                 }
171         }
172         _on = 0;
173 }
174
175 void
176 MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time)
177 {
178         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
179
180         if (!_on) {
181                 return;
182         }
183
184         /* NOTE: the src must be locked */
185
186         for (int channel = 0; channel < 16; ++channel) {
187                 for (int note = 0; note < 128; ++note) {
188                         while (_active_notes[note + 128 * channel]) {
189                                 Evoral::MIDIEvent<Evoral::MusicalTime> ev ((MIDI_CMD_NOTE_OFF|channel), time, 3, 0, true);
190                                 ev.set_type (MIDI_CMD_NOTE_OFF);
191                                 ev.set_channel (channel);
192                                 ev.set_note (note);
193                                 ev.set_velocity (0);
194                                 src.append_event_unlocked_beats (ev);
195                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n", 
196                                                                                        this, (int) note, (int) channel, time));
197                                 _active_notes[note + 128 * channel]--;
198                                 /* don't stack events up at the same time */
199                                 time += 1.0/128.0;
200                         }
201                 }
202         }
203         _on = 0;
204 }
205
206 void
207 MidiStateTracker::dump (ostream& o)
208 {
209         o << "******\n";
210         for (int c = 0; c < 16; ++c) {
211                 for (int x = 0; x < 128; ++x) {
212                         if (_active_notes[c * 128 + x]) {
213                                 o << "Channel " << c+1 << " Note " << x << " is on ("
214                                   << (int) _active_notes[c*128+x] <<  "times)\n";
215                         }
216                 }
217         }
218         o << "+++++\n";
219 }