2 * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
6 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "pbd/compose.h"
27 #include "pbd/debug.h"
29 #include "ardour/audioengine.h"
30 #include "ardour/data_type.h"
31 #include "ardour/debug.h"
32 #include "ardour/midi_buffer.h"
33 #include "ardour/midi_port.h"
34 #include "ardour/session.h"
37 using namespace ARDOUR;
40 #define port_engine AudioEngine::instance()->port_engine()
42 MidiPort::MidiPort (const std::string& name, PortFlags flags)
43 : Port (name, DataType::MIDI, flags)
44 , _resolve_required (false)
45 , _input_active (true)
47 , _data_fetched_for_cycle (false)
49 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
55 AudioEngine::instance()->unregister_port (_shadow_port);
56 _shadow_port.reset ();
63 MidiPort::parse_input (pframes_t nframes, MIDI::Parser& parser)
68 MidiPort::cycle_start (pframes_t nframes)
70 Port::cycle_start (nframes);
74 if (sends_output () && _port_handle) {
75 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
78 if (receives_input() && _trace_parser) {
79 read_and_parse_entire_midi_buffer_with_no_speed_adjustment (nframes, *_trace_parser, AudioEngine::instance()->sample_time_at_cycle_start());
82 if (_inbound_midi_filter) {
83 MidiBuffer& mb (get_midi_buffer (nframes));
84 _inbound_midi_filter (mb, mb);
88 MidiBuffer& mb (get_midi_buffer (nframes));
89 if (_shadow_midi_filter (mb, _shadow_port->get_midi_buffer (nframes))) {
90 _shadow_port->flush_buffers (nframes);
97 MidiPort::get_midi_buffer (pframes_t nframes)
99 if (_data_fetched_for_cycle) {
103 if (receives_input () && _input_active) {
105 void* buffer = port_engine.get_buffer (_port_handle, nframes);
106 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
108 /* suck all MIDI events for this cycle of nframes from
109 the MIDI port buffer into our MidiBuffer.
112 for (pframes_t i = 0; i < event_count; ++i) {
118 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
120 if (buf[0] == 0xfe) {
121 /* throw away active sensing */
125 timestamp = floor (timestamp * _speed_ratio);
127 /* check that the event is in the acceptable time range */
128 if ((timestamp < (_global_port_buffer_offset)) ||
129 (timestamp >= (_global_port_buffer_offset + nframes))) {
130 // XXX this is normal after a split cycles:
131 // The engine buffer contains the data for the complete cycle, but
132 // only the part after _global_port_buffer_offset is needed.
134 cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
135 << _global_port_buffer_offset << " limit="
136 << (_global_port_buffer_offset + nframes)
137 << " = (" << _global_port_buffer_offset
144 if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
145 /* normalize note on with velocity 0 to proper note off */
147 ev[0] = 0x80 | (buf[0] & 0x0F); /* note off */
149 ev[2] = 0x40; /* default velocity */
150 _buffer->push_back (timestamp, size, ev);
152 _buffer->push_back (timestamp, size, buf);
157 _buffer->silence (nframes);
161 _data_fetched_for_cycle = true;
168 MidiPort::read_and_parse_entire_midi_buffer_with_no_speed_adjustment (pframes_t nframes, MIDI::Parser& parser, samplepos_t now)
170 void* buffer = port_engine.get_buffer (_port_handle, nframes);
171 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
173 for (pframes_t i = 0; i < event_count; ++i) {
179 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
181 if (buf[0] == 0xfe) {
182 /* throw away active sensing */
186 parser.set_timestamp (now + timestamp);
188 /* During this parsing stage, signals will be emitted from the
189 * Parser, which will update anything connected to it.
191 * As of July 2018, this is only used by TransportMasters which
192 * read MIDI before the process() cycle really gets started.
195 if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
196 /* normalize note on with velocity 0 to proper note off */
197 parser.scanner (0x80 | (buf[0] & 0x0F)); /* note off */
198 parser.scanner (buf[1]);
199 parser.scanner (0x40); /* default (off) velocity */
201 for (size_t n = 0; n < size; ++n) {
202 parser.scanner (buf[n]);
209 MidiPort::cycle_end (pframes_t /*nframes*/)
211 _data_fetched_for_cycle = false;
215 MidiPort::cycle_split ()
217 _data_fetched_for_cycle = false;
221 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
223 for (uint8_t channel = 0; channel <= 0xF; channel++) {
225 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
226 pframes_t tme = floor (when / _speed_ratio);
228 /* we need to send all notes off AND turn the
229 * sustain/damper pedal off to handle synths
230 * that prioritize sustain over AllNotesOff
233 if (port_engine.midi_event_put (port_buffer, tme, ev, 3) != 0) {
234 cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
237 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
239 if (port_engine.midi_event_put (port_buffer, tme, ev, 3) != 0) {
240 cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
246 MidiPort::flush_buffers (pframes_t nframes)
248 if (sends_output ()) {
250 void* port_buffer = 0;
252 if (_resolve_required) {
253 port_buffer = port_engine.get_buffer (_port_handle, nframes);
254 /* resolve all notes at the start of the buffer */
255 resolve_notes (port_buffer, _global_port_buffer_offset);
256 _resolve_required = false;
259 if (_buffer->empty()) {
264 port_buffer = port_engine.get_buffer (_port_handle, nframes);
268 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
270 const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
273 if (sends_output() && _trace_parser) {
274 uint8_t const * const buf = ev.buffer();
275 const samplepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
277 _trace_parser->set_timestamp (now + ev.time());
279 uint32_t limit = ev.size();
281 for (size_t n = 0; n < limit; ++n) {
282 _trace_parser->scanner (buf[n]);
287 // event times are in samples, relative to cycle start
290 if (DEBUG_ENABLED (DEBUG::MidiIO)) {
291 const Session* s = AudioEngine::instance()->session();
292 const samplepos_t now = (s ? s->transport_sample() : 0);
294 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(),
295 now + ev.time(), nframes, _global_port_buffer_offset, name()));
296 for (size_t i=0; i < ev.size(); ++i) {
297 DEBUG_STR_APPEND(a,hex);
298 DEBUG_STR_APPEND(a,"0x");
299 DEBUG_STR_APPEND(a,(int)(ev.buffer()[i]));
300 DEBUG_STR_APPEND(a,' ');
302 DEBUG_STR_APPEND(a,'\n');
303 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
307 assert (ev.time() < (nframes + _global_port_buffer_offset));
309 if (ev.time() >= _global_port_buffer_offset) {
310 pframes_t tme = floor (ev.time() / _speed_ratio);
311 if (port_engine.midi_event_put (port_buffer, tme, ev.buffer(), ev.size()) != 0) {
312 cerr << "write failed, dropped event, time "
314 << " > " << _global_port_buffer_offset << endl;
317 cerr << "drop flushed event on the floor, time " << ev.time()
318 << " too early for " << _global_port_buffer_offset;
319 for (size_t xx = 0; xx < ev.size(); ++xx) {
320 cerr << ' ' << hex << (int) ev.buffer()[xx];
326 /* done.. the data has moved to the port buffer, mark it so
334 MidiPort::require_resolve ()
336 _resolve_required = true;
340 MidiPort::transport_stopped ()
342 _resolve_required = true;
346 MidiPort::realtime_locate ()
348 _resolve_required = true;
356 cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
357 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
361 MidiPort::set_input_active (bool yn)
367 MidiPort::set_trace (MIDI::Parser * p)
373 MidiPort::add_shadow_port (string const & name, MidiFilter mf)
375 if (!ARDOUR::Port::receives_input()) {
383 _shadow_midi_filter = mf;
385 if (!(_shadow_port = boost::dynamic_pointer_cast<MidiPort> (AudioEngine::instance()->register_output_port (DataType::MIDI, name, false, PortFlags (Shadow|IsTerminal))))) {
389 /* forward on our port latency to the shadow port.
391 XXX: need to capture latency changes and forward them too.
394 LatencyRange latency = private_latency_range (false);
395 _shadow_port->set_private_latency_range (latency, false);