if a request to reset the speed to zero as the default arrives when stopped, reset...
[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, bool& looped)
98 {
99         looped = false;
100
101         // DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 track notes, looped = %2\n", this, looped));
102
103         for (MidiBuffer::iterator i = from; i != to; ++i) {
104                 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
105                 if (ev.event_type() == LoopEventType) {
106                         looped = true;
107                         continue;
108                 }
109
110                 /* catch AllNotesOff message and turn off all notes
111                  */
112                 
113                 if (ev.type() == MIDI_CTL_ALL_NOTES_OFF) {
114                         cerr << "State tracker sees ALL_NOTES_OFF, silenceing " << sizeof (_active_notes) << endl;
115                         memset (_active_notes, 0, sizeof (_active_notes));
116                         _on = 0;
117                 } else {
118                         track_note_onoffs (ev);
119                 }
120         }
121 }
122
123 void
124 MidiStateTracker::resolve_notes (MidiBuffer &dst, framepos_t time)
125 {
126         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MB-resolve notes @ %2 on = %3\n", this, time, _on));
127
128         if (!_on) {
129                 return;
130         }
131
132         for (int channel = 0; channel < 16; ++channel) {
133                 for (int note = 0; note < 128; ++note) {
134                         while (_active_notes[note + 128 * channel]) {
135                                 uint8_t buffer[3] = { ((uint8_t) (MIDI_CMD_NOTE_OFF | channel)), uint8_t (note), 0 };
136                                 Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
137                                         (MIDI_CMD_NOTE_OFF, time, 3, buffer, false);
138                                 /* note that we do not care about failure from
139                                    push_back() ... should we warn someone ?
140                                 */
141                                 dst.push_back (noteoff);
142                                 _active_notes[note + 128 * channel]--;
143                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MB-resolved note %2/%3 at %4\n", 
144                                                                                        this, (int) note, (int) channel, time));
145                         }
146                 }
147         }
148         _on = 0;
149 }
150
151 void
152 MidiStateTracker::resolve_notes (Evoral::EventSink<framepos_t> &dst, framepos_t time)
153 {
154         uint8_t buf[3];
155
156         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 EVS-resolve notes @ %2 on = %3\n", this, time, _on));
157
158         if (!_on) {
159                 return;
160         }
161
162         for (int channel = 0; channel < 16; ++channel) {
163                 for (int note = 0; note < 128; ++note) {
164                         while (_active_notes[note + 128 * channel]) {
165                                 buf[0] = MIDI_CMD_NOTE_OFF|channel;
166                                 buf[1] = note;
167                                 buf[2] = 0;
168                                 /* note that we do not care about failure from
169                                    write() ... should we warn someone ?
170                                 */
171                                 dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
172                                 _active_notes[note + 128 * channel]--;
173                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: EVS-resolved note %2/%3 at %4\n", 
174                                                                                        this, (int) note, (int) channel, time));
175                         }
176                 }
177         }
178         _on = 0;
179 }
180
181 void
182 MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time)
183 {
184         DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1 MS-resolve notes @ %2 on = %3\n", this, time, _on));
185
186         if (!_on) {
187                 return;
188         }
189
190         /* NOTE: the src must be locked */
191
192         for (int channel = 0; channel < 16; ++channel) {
193                 for (int note = 0; note < 128; ++note) {
194                         while (_active_notes[note + 128 * channel]) {
195                                 Evoral::MIDIEvent<Evoral::MusicalTime> ev ((MIDI_CMD_NOTE_OFF|channel), time, 3, 0, true);
196                                 ev.set_type (MIDI_CMD_NOTE_OFF);
197                                 ev.set_channel (channel);
198                                 ev.set_note (note);
199                                 ev.set_velocity (0);
200                                 src.append_event_unlocked_beats (ev);
201                                 DEBUG_TRACE (PBD::DEBUG::MidiTrackers, string_compose ("%1: MS-resolved note %2/%3 at %4\n", 
202                                                                                        this, (int) note, (int) channel, time));
203                                 _active_notes[note + 128 * channel]--;
204                                 /* don't stack events up at the same time */
205                                 time += 1.0/128.0;
206                         }
207                 }
208         }
209         _on = 0;
210 }
211
212 void
213 MidiStateTracker::dump (ostream& o)
214 {
215         o << "******\n";
216         for (int c = 0; c < 16; ++c) {
217                 for (int x = 0; x < 128; ++x) {
218                         if (_active_notes[c * 128 + x]) {
219                                 o << "Channel " << c+1 << " Note " << x << " is on ("
220                                   << (int) _active_notes[c*128+x] <<  "times)\n";
221                         }
222                 }
223         }
224         o << "+++++\n";
225 }