assorted extra debug output for MTC
[ardour.git] / libs / ardour / midi_port.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cassert>
20 #include <iostream>
21
22 #include "ardour/midi_port.h"
23 #include "ardour/data_type.h"
24 #include "ardour/audioengine.h"
25
26 using namespace ARDOUR;
27 using namespace std;
28
29 #define port_engine AudioEngine::instance()->port_engine()
30
31 MidiPort::MidiPort (const std::string& name, PortFlags flags)
32         : Port (name, DataType::MIDI, flags)
33         , _has_been_mixed_down (false)
34         , _resolve_required (false)
35         , _input_active (true)
36 {
37         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
38 }
39
40 MidiPort::~MidiPort()
41 {
42         delete _buffer;
43 }
44
45 void
46 MidiPort::cycle_start (pframes_t nframes)
47 {
48         Port::cycle_start (nframes);
49
50         _buffer->clear ();
51
52         if (sends_output ()) {
53                 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
54         }
55 }
56
57 MidiBuffer &
58 MidiPort::get_midi_buffer (pframes_t nframes)
59 {
60         if (_has_been_mixed_down) {
61                 return *_buffer;
62         }
63
64         if (receives_input ()) {
65
66                 if (_input_active) {
67
68                         void* buffer = port_engine.get_buffer (_port_handle, nframes);
69                         const pframes_t event_count = port_engine.get_midi_event_count (buffer);
70                         
71                         /* suck all relevant MIDI events from the MIDI port buffer
72                            into our MidiBuffer
73                         */
74
75                         cerr << "grabbing " << event_count << " events\n";
76
77                         for (pframes_t i = 0; i < event_count; ++i) {
78                                 
79                                 pframes_t timestamp;
80                                 size_t size;
81                                 uint8_t* buf;
82                                 
83                                 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
84                                 
85                                 if (buf[0] == 0xfe) {
86                                         /* throw away active sensing */
87                                         continue;
88                                 }
89                                 
90                                 /* check that the event is in the acceptable time range */
91                                 
92                                 if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) &&
93                                     (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
94                                         _buffer->push_back (timestamp, size, buf);
95                                 } else {
96                                         cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
97                                              << _global_port_buffer_offset << " limit="
98                                              << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
99                                 }
100                         } 
101
102                 } else {
103                         _buffer->silence (nframes);
104                 }
105
106         } else {
107                 _buffer->silence (nframes);
108         }
109
110         if (nframes) {
111                 _has_been_mixed_down = true;
112         }
113
114         return *_buffer;
115 }
116
117 void
118 MidiPort::cycle_end (pframes_t /*nframes*/)
119 {
120         _has_been_mixed_down = false;
121 }
122
123 void
124 MidiPort::cycle_split ()
125 {
126         _has_been_mixed_down = false;
127 }
128
129 void
130 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
131 {
132         for (uint8_t channel = 0; channel <= 0xF; channel++) {
133
134                 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
135
136                 /* we need to send all notes off AND turn the
137                  * sustain/damper pedal off to handle synths
138                  * that prioritize sustain over AllNotesOff
139                  */
140
141                 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
142                         cerr << "failed to deliver sustain-zero on channel " << channel << " on port " << name() << endl;
143                 }
144
145                 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
146
147                 if (port_engine.midi_event_put (port_buffer, 0, ev, 3) != 0) {
148                         cerr << "failed to deliver ALL NOTES OFF on channel " << channel << " on port " << name() << endl;
149                 }
150         }
151 }
152
153 void
154 MidiPort::flush_buffers (pframes_t nframes)
155 {
156         if (sends_output ()) {
157
158                 void* port_buffer = port_engine.get_buffer (_port_handle, nframes);
159                 
160                 if (_resolve_required) {
161                         /* resolve all notes at the start of the buffer */
162                         resolve_notes (port_buffer, 0);
163                         _resolve_required = false;
164                 }
165
166                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
167
168                         const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
169
170                         // event times are in frames, relative to cycle start
171
172                         assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
173
174                         if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
175                                 if (port_engine.midi_event_put (port_buffer, (pframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
176                                         cerr << "write failed, drop flushed note off on the floor, time "
177                                              << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
178                                 }
179                         } else {
180                                 cerr << "drop flushed event on the floor, time " << ev
181                                      << " to early for " << _global_port_buffer_offset 
182                                      << " + " << _port_buffer_offset << endl;
183                         }
184                 }
185         }
186 }
187
188 void
189 MidiPort::require_resolve ()
190 {
191         _resolve_required = true;
192 }
193
194 void
195 MidiPort::transport_stopped ()
196 {
197         _resolve_required = true;
198 }
199
200 void
201 MidiPort::realtime_locate ()
202 {
203         _resolve_required = true;
204 }
205
206 void
207 MidiPort::reset ()
208 {
209         Port::reset ();
210         delete _buffer;
211         cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
212         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
213 }
214
215 void
216 MidiPort::set_input_active (bool yn)
217 {
218         _input_active = yn;
219 }