X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_port.cc;h=615abede1c58e904bb2de4b991de779c4ba8191d;hb=b68a4e5cdc59ffb81c1952ac9cad63ddf0c76d06;hp=c27332fce12486b968e30664ffd0016c4ee721a5;hpb=84be4eafc5228eacfed02f78489fa82d401c2947;p=ardour.git diff --git a/libs/ardour/midi_port.cc b/libs/ardour/midi_port.cc index c27332fce1..615abede1c 100644 --- a/libs/ardour/midi_port.cc +++ b/libs/ardour/midi_port.cc @@ -19,18 +19,28 @@ #include #include -#include "ardour/midi_port.h" -#include "ardour/data_type.h" +#include "pbd/compose.h" +#include "pbd/debug.h" + #include "ardour/audioengine.h" +#include "ardour/data_type.h" +#include "ardour/debug.h" +#include "ardour/midi_buffer.h" +#include "ardour/midi_port.h" -using namespace ARDOUR; using namespace std; +using namespace ARDOUR; +using namespace PBD; -MidiPort::MidiPort (const std::string& name, Flags flags) +#define port_engine AudioEngine::instance()->port_engine() + +MidiPort::MidiPort (const std::string& name, PortFlags flags) : Port (name, DataType::MIDI, flags) , _has_been_mixed_down (false) , _resolve_required (false) , _input_active (true) + , _always_parse (false) + , _trace_on (false) { _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)); } @@ -43,15 +53,39 @@ MidiPort::~MidiPort() void MidiPort::cycle_start (pframes_t nframes) { + framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start(); + Port::cycle_start (nframes); _buffer->clear (); - assert (_buffer->size () == 0); - if (sends_output ()) { - jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes)); + port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes)); } + + if (_always_parse || (receives_input() && _trace_on)) { + MidiBuffer& mb (get_midi_buffer (nframes)); + + /* dump incoming MIDI to parser */ + + for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) { + uint8_t* buf = (*b).buffer(); + + _self_parser.set_timestamp (now + (*b).time()); + + uint32_t limit = (*b).size(); + + for (size_t n = 0; n < limit; ++n) { + _self_parser.scanner (buf[n]); + } + } + } +} + +Buffer& +MidiPort::get_buffer (pframes_t nframes) +{ + return get_midi_buffer (nframes); } MidiBuffer & @@ -65,33 +99,37 @@ MidiPort::get_midi_buffer (pframes_t nframes) if (_input_active) { - void* jack_buffer = jack_port_get_buffer (_jack_port, nframes); - const pframes_t event_count = jack_midi_get_event_count (jack_buffer); - - assert (event_count < _buffer->capacity()); - - /* suck all relevant MIDI events from the JACK MIDI port buffer + void* buffer = port_engine.get_buffer (_port_handle, nframes); + const pframes_t event_count = port_engine.get_midi_event_count (buffer); + + /* suck all relevant MIDI events from the MIDI port buffer into our MidiBuffer */ - + for (pframes_t i = 0; i < event_count; ++i) { - jack_midi_event_t ev; + pframes_t timestamp; + size_t size; + uint8_t* buf; - jack_midi_event_get (&ev, jack_buffer, i); + port_engine.midi_event_get (timestamp, size, &buf, buffer, i); - if (ev.buffer[0] == 0xfe) { + if (buf[0] == 0xfe) { /* throw away active sensing */ continue; + } else if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) { + /* normalize note on with velocity 0 to proper note off */ + buf[0] = 0x80 | (buf[0] & 0x0F); /* note off */ + buf[2] = 0x40; /* default velocity */ } /* check that the event is in the acceptable time range */ - if ((ev.time >= (_global_port_buffer_offset + _port_buffer_offset)) && - (ev.time < (_global_port_buffer_offset + _port_buffer_offset + nframes))) { - _buffer->push_back (ev); + if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) && + (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) { + _buffer->push_back (timestamp, size, buf); } else { - cerr << "Dropping incoming MIDI at time " << ev.time << "; offset=" + cerr << "Dropping incoming MIDI at time " << timestamp << "; offset=" << _global_port_buffer_offset << " limit=" << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n"; } @@ -112,7 +150,6 @@ MidiPort::get_midi_buffer (pframes_t nframes) return *_buffer; } - void MidiPort::cycle_end (pframes_t /*nframes*/) { @@ -126,73 +163,119 @@ MidiPort::cycle_split () } void -MidiPort::resolve_notes (void* jack_buffer, MidiBuffer::TimeType when) +MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when) { - uint8_t ev[3]; - - ev[2] = 0; - for (uint8_t channel = 0; channel <= 0xF; channel++) { - ev[0] = (MIDI_CMD_CONTROL | channel); + + uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 }; /* we need to send all notes off AND turn the * sustain/damper pedal off to handle synths * that prioritize sustain over AllNotesOff */ - ev[1] = MIDI_CTL_SUSTAIN; - - if (jack_midi_event_write (jack_buffer, when, ev, 3) != 0) { - cerr << "failed to deliver sustain-zero on channel " << channel << " on port " << name() << endl; + if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) { + cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl; } ev[1] = MIDI_CTL_ALL_NOTES_OFF; - if (jack_midi_event_write (jack_buffer, 0, ev, 3) != 0) { - cerr << "failed to deliver ALL NOTES OFF on channel " << channel << " on port " << name() << endl; + if (port_engine.midi_event_put (port_buffer, 0, ev, 3) != 0) { + cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl; } } } void -MidiPort::flush_buffers (pframes_t nframes, framepos_t time) +MidiPort::flush_buffers (pframes_t nframes) { if (sends_output ()) { - void* jack_buffer = jack_port_get_buffer (_jack_port, nframes); - + void* port_buffer = 0; + if (_resolve_required) { + port_buffer = port_engine.get_buffer (_port_handle, nframes); /* resolve all notes at the start of the buffer */ - resolve_notes (jack_buffer, 0); - _resolve_required= false; + resolve_notes (port_buffer, 0); + _resolve_required = false; + } + + if (_buffer->empty()) { + return; } + if (!port_buffer) { + port_buffer = port_engine.get_buffer (_port_handle, nframes); + } + + for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) { const Evoral::MIDIEvent ev (*i, false); - // event times are in frames, relative to cycle start - assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset)); + if (sends_output() && _trace_on) { + uint8_t const * const buf = ev.buffer(); + const framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start(); - if (ev.event_type() == LoopEventType) { - resolve_notes (jack_buffer, ev.time()); - continue; + _self_parser.set_timestamp (now + ev.time()); + + uint32_t limit = ev.size(); + + for (size_t n = 0; n < limit; ++n) { + _self_parser.scanner (buf[n]); + } } + + // event times are in frames, relative to cycle start + +#ifndef NDEBUG + if (DEBUG::MidiIO & PBD::debug_bits) { + DEBUG_STR_DECL(a); + DEBUG_STR_APPEND(a, string_compose ("MidiPort %1 pop event @ %2 sz %3 ", _buffer, ev.time(), ev.size())); + for (size_t i=0; i < ev.size(); ++i) { + DEBUG_STR_APPEND(a,hex); + DEBUG_STR_APPEND(a,"0x"); + DEBUG_STR_APPEND(a,(int)(ev.buffer()[i])); + DEBUG_STR_APPEND(a,' '); + } + DEBUG_STR_APPEND(a,'\n'); + DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str()); + } +#endif + + assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset)); + if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) { - if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) { + if (port_engine.midi_event_put (port_buffer, (pframes_t) ev.time(), ev.buffer(), ev.size()) != 0) { cerr << "write failed, drop flushed note off on the floor, time " << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl; } } else { cerr << "drop flushed event on the floor, time " << ev.time() - << " < " << _global_port_buffer_offset + _port_buffer_offset << endl; + << " too early for " << _global_port_buffer_offset + << " + " << _port_buffer_offset; + for (size_t xx = 0; xx < ev.size(); ++xx) { + cerr << ' ' << hex << (int) ev.buffer()[xx]; + } + cerr << dec << endl; } } + + /* done.. the data has moved to the port buffer, mark it so + */ + + _buffer->clear (); } } +void +MidiPort::require_resolve () +{ + _resolve_required = true; +} + void MidiPort::transport_stopped () { @@ -210,6 +293,7 @@ MidiPort::reset () { Port::reset (); delete _buffer; + cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl; _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)); } @@ -218,3 +302,15 @@ MidiPort::set_input_active (bool yn) { _input_active = yn; } + +void +MidiPort::set_always_parse (bool yn) +{ + _always_parse = yn; +} + +void +MidiPort::set_trace_on (bool yn) +{ + _trace_on = yn; +}