2 Copyright (C) 2006 Paul Davis
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.
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.
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.
22 #include "pbd/compose.h"
23 #include "pbd/debug.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/data_type.h"
27 #include "ardour/debug.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/midi_port.h"
30 #include "ardour/session.h"
33 using namespace ARDOUR;
36 #define port_engine AudioEngine::instance()->port_engine()
38 MidiPort::MidiPort (const std::string& name, PortFlags flags)
39 : Port (name, DataType::MIDI, flags)
40 , _resolve_required (false)
41 , _input_active (true)
43 , _data_fetched_for_cycle (false)
45 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
51 AudioEngine::instance()->unregister_port (_shadow_port);
52 _shadow_port.reset ();
59 MidiPort::parse_input (pframes_t nframes, MIDI::Parser& parser)
64 MidiPort::cycle_start (pframes_t nframes)
66 Port::cycle_start (nframes);
70 if (sends_output () && _port_handle) {
71 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
74 if (receives_input() && _trace_parser) {
75 read_and_parse_entire_midi_buffer_with_no_speed_adjustment (nframes, *_trace_parser, AudioEngine::instance()->sample_time_at_cycle_start());
78 if (inbound_midi_filter) {
79 MidiBuffer& mb (get_midi_buffer (nframes));
80 inbound_midi_filter (mb, mb);
84 MidiBuffer& mb (get_midi_buffer (nframes));
85 if (shadow_midi_filter (mb, _shadow_port->get_midi_buffer (nframes))) {
86 _shadow_port->flush_buffers (nframes);
93 MidiPort::get_midi_buffer (pframes_t nframes)
95 if (_data_fetched_for_cycle) {
99 if (receives_input () && _input_active) {
101 void* buffer = port_engine.get_buffer (_port_handle, nframes);
102 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
104 /* suck all MIDI events for this cycle of nframes from
105 the MIDI port buffer into our MidiBuffer.
108 for (pframes_t i = 0; i < event_count; ++i) {
114 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
116 if (buf[0] == 0xfe) {
117 /* throw away active sensing */
121 timestamp = floor (timestamp * _speed_ratio);
123 /* check that the event is in the acceptable time range */
124 if ((timestamp < (_global_port_buffer_offset)) ||
125 (timestamp >= (_global_port_buffer_offset + nframes))) {
126 // XXX this is normal after a split cycles:
127 // The engine buffer contains the data for the complete cycle, but
128 // only the part after _global_port_buffer_offset is needed.
130 cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
131 << _global_port_buffer_offset << " limit="
132 << (_global_port_buffer_offset + nframes)
133 << " = (" << _global_port_buffer_offset
140 if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
141 /* normalize note on with velocity 0 to proper note off */
143 ev[0] = 0x80 | (buf[0] & 0x0F); /* note off */
145 ev[2] = 0x40; /* default velocity */
146 _buffer->push_back (timestamp, size, ev);
148 _buffer->push_back (timestamp, size, buf);
153 _buffer->silence (nframes);
157 _data_fetched_for_cycle = true;
164 MidiPort::read_and_parse_entire_midi_buffer_with_no_speed_adjustment (pframes_t nframes, MIDI::Parser& parser, samplepos_t now)
166 void* buffer = port_engine.get_buffer (_port_handle, nframes);
167 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
169 for (pframes_t i = 0; i < event_count; ++i) {
175 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
177 if (buf[0] == 0xfe) {
178 /* throw away active sensing */
182 parser.set_timestamp (now + timestamp);
184 /* During this parsing stage, signals will be emitted from the
185 * Parser, which will update anything connected to it.
187 * As of July 2018, this is only used by TransportMasters which
188 * read MIDI before the process() cycle really gets started.
191 if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
192 /* normalize note on with velocity 0 to proper note off */
193 parser.scanner (0x80 | (buf[0] & 0x0F)); /* note off */
194 parser.scanner (buf[1]);
195 parser.scanner (0x40); /* default (off) velocity */
197 for (size_t n = 0; n < size; ++n) {
198 parser.scanner (buf[n]);
205 MidiPort::cycle_end (pframes_t /*nframes*/)
207 _data_fetched_for_cycle = false;
211 MidiPort::cycle_split ()
213 _data_fetched_for_cycle = false;
217 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
219 for (uint8_t channel = 0; channel <= 0xF; channel++) {
221 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
222 pframes_t tme = floor (when / _speed_ratio);
224 /* we need to send all notes off AND turn the
225 * sustain/damper pedal off to handle synths
226 * that prioritize sustain over AllNotesOff
229 if (port_engine.midi_event_put (port_buffer, tme, ev, 3) != 0) {
230 cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
233 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
235 if (port_engine.midi_event_put (port_buffer, tme, ev, 3) != 0) {
236 cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
242 MidiPort::flush_buffers (pframes_t nframes)
244 if (sends_output ()) {
246 void* port_buffer = 0;
248 if (_resolve_required) {
249 port_buffer = port_engine.get_buffer (_port_handle, nframes);
250 /* resolve all notes at the start of the buffer */
251 resolve_notes (port_buffer, _global_port_buffer_offset);
252 _resolve_required = false;
255 if (_buffer->empty()) {
260 port_buffer = port_engine.get_buffer (_port_handle, nframes);
264 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
266 const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
269 if (sends_output() && _trace_parser) {
270 uint8_t const * const buf = ev.buffer();
271 const samplepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
273 _trace_parser->set_timestamp (now + ev.time());
275 uint32_t limit = ev.size();
277 for (size_t n = 0; n < limit; ++n) {
278 _trace_parser->scanner (buf[n]);
283 // event times are in samples, relative to cycle start
286 if (DEBUG_ENABLED (DEBUG::MidiIO)) {
287 const Session* s = AudioEngine::instance()->session();
288 const samplepos_t now = (s ? s->transport_sample() : 0);
290 DEBUG_STR_APPEND(a, string_compose ("MidiPort %7 %1 pop event @ %2 (global %4, within %5 gpbo %6 sz %3 ", _buffer, ev.time(), ev.size(),
291 now + ev.time(), nframes, _global_port_buffer_offset, name()));
292 for (size_t i=0; i < ev.size(); ++i) {
293 DEBUG_STR_APPEND(a,hex);
294 DEBUG_STR_APPEND(a,"0x");
295 DEBUG_STR_APPEND(a,(int)(ev.buffer()[i]));
296 DEBUG_STR_APPEND(a,' ');
298 DEBUG_STR_APPEND(a,'\n');
299 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
303 assert (ev.time() < (nframes + _global_port_buffer_offset));
305 if (ev.time() >= _global_port_buffer_offset) {
306 pframes_t tme = floor (ev.time() / _speed_ratio);
307 if (port_engine.midi_event_put (port_buffer, tme, ev.buffer(), ev.size()) != 0) {
308 cerr << "write failed, dropped event, time "
310 << " > " << _global_port_buffer_offset << endl;
313 cerr << "drop flushed event on the floor, time " << ev.time()
314 << " too early for " << _global_port_buffer_offset;
315 for (size_t xx = 0; xx < ev.size(); ++xx) {
316 cerr << ' ' << hex << (int) ev.buffer()[xx];
322 /* done.. the data has moved to the port buffer, mark it so
330 MidiPort::require_resolve ()
332 _resolve_required = true;
336 MidiPort::transport_stopped ()
338 _resolve_required = true;
342 MidiPort::realtime_locate ()
344 _resolve_required = true;
352 cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
353 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
357 MidiPort::set_input_active (bool yn)
363 MidiPort::set_trace (MIDI::Parser * p)
369 MidiPort::add_shadow_port (string const & name, MidiFilter mf)
371 if (!ARDOUR::Port::receives_input()) {
379 shadow_midi_filter = mf;
381 if (!(_shadow_port = boost::dynamic_pointer_cast<MidiPort> (AudioEngine::instance()->register_output_port (DataType::MIDI, name, false, PortFlags (Shadow|IsTerminal))))) {
385 /* forward on our port latency to the shadow port.
387 XXX: need to capture latency changes and forward them too.
390 LatencyRange latency = private_latency_range (false);
391 _shadow_port->set_private_latency_range (latency, false);