X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_port.cc;h=55280eb9428e82aed7fc611a53d866991f0b0b82;hb=944c32346b3c6bb87fccb22f159bdd9297bae50d;hp=d7ce12d411f52bd1656cd5b614ae985f06f11927;hpb=532f6aad4ac79ca15d69deccd18fca90e444c437;p=ardour.git diff --git a/libs/ardour/midi_port.cc b/libs/ardour/midi_port.cc index d7ce12d411..55280eb942 100644 --- a/libs/ardour/midi_port.cc +++ b/libs/ardour/midi_port.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2006 Paul Davis + Copyright (C) 2006 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,98 +17,242 @@ */ #include -#include -#include #include +#include "ardour/midi_port.h" +#include "ardour/data_type.h" +#include "ardour/audioengine.h" + using namespace ARDOUR; using namespace std; -MidiPort::MidiPort(jack_port_t* p) - : Port(p) - , _buffer(4096) // FIXME FIXME FIXME Jack needs to tell us this - , _nframes_this_cycle(0) +#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) { - DataType dt(_type); - assert(dt == DataType::MIDI); + _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)); +} - reset(); +MidiPort::~MidiPort() +{ + delete _buffer; +} +void +MidiPort::cycle_start (pframes_t nframes) +{ + framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start(); + Port::cycle_start (nframes); + + _buffer->clear (); + + if (sends_output ()) { + port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes)); + } + + if (_always_parse) { + 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]); + } + } + } } +MidiBuffer & +MidiPort::get_midi_buffer (pframes_t nframes) +{ + if (_has_been_mixed_down) { + return *_buffer; + } + + if (receives_input ()) { -MidiPort::~MidiPort() + if (_input_active) { + + 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) { + + pframes_t timestamp; + size_t size; + uint8_t* buf; + + port_engine.midi_event_get (timestamp, size, &buf, buffer, i); + + if (buf[0] == 0xfe) { + /* throw away active sensing */ + continue; + } + + /* check that the event is in the acceptable time range */ + + 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 " << timestamp << "; offset=" + << _global_port_buffer_offset << " limit=" + << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n"; + } + } + + } else { + _buffer->silence (nframes); + } + + } else { + _buffer->silence (nframes); + } + + if (nframes) { + _has_been_mixed_down = true; + } + + return *_buffer; +} + +void +MidiPort::cycle_end (pframes_t /*nframes*/) { + _has_been_mixed_down = false; } void -MidiPort::cycle_start (jack_nframes_t nframes) +MidiPort::cycle_split () { - _buffer.clear(); - assert(_buffer.size() == 0); + _has_been_mixed_down = false; +} + +void +MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when) +{ + for (uint8_t channel = 0; channel <= 0xF; channel++) { - _nframes_this_cycle = nframes; + uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 }; - if (_flags & JackPortIsOutput) { - _buffer.silence(nframes); - assert(_buffer.size() == 0); - return; + /* we need to send all notes off AND turn the + * sustain/damper pedal off to handle synths + * that prioritize sustain over AllNotesOff + */ + + 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 (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; + } } +} - // We're an input - copy Jack events to internal buffer - - void* jack_buffer = jack_port_get_buffer(_port, nframes); +void +MidiPort::flush_buffers (pframes_t nframes) +{ + if (sends_output ()) { + + 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 (port_buffer, 0); + _resolve_required = false; + } + + if (_buffer->empty()) { + return; + } + + if (!port_buffer) { + port_buffer = port_engine.get_buffer (_port_handle, nframes); + } - const jack_nframes_t event_count - = jack_midi_get_event_count(jack_buffer, nframes); + for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) { - assert(event_count < _buffer.capacity()); + const Evoral::MIDIEvent ev (*i, false); - MidiEvent ev; + // event times are in frames, relative to cycle start - // FIXME: too slow, event struct is copied twice (here and MidiBuffer::push_back) - for (jack_nframes_t i=0; i < event_count; ++i) { + assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset)); - // This will fail to compile if we change MidiEvent to our own class - jack_midi_event_get(static_cast(&ev), jack_buffer, i, nframes); + if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) { + 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() + << " to early for " << _global_port_buffer_offset + << " + " << _port_buffer_offset << endl; + } + } - _buffer.push_back(ev); - // Convert note ons with velocity 0 to proper note offs - // FIXME: Jack MIDI should guarantee this - does it? - //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0) - // ev->buffer[0] = MIDI_CMD_NOTE_OFF; + /* done.. the data has moved to the port buffer, mark it so + */ + + _buffer->clear (); } +} - assert(_buffer.size() == event_count); +void +MidiPort::require_resolve () +{ + _resolve_required = true; +} - //if (_buffer.size() > 0) - // cerr << "MIDIPort got " << event_count << " events." << endl; +void +MidiPort::transport_stopped () +{ + _resolve_required = true; } void -MidiPort::cycle_end() +MidiPort::realtime_locate () { - if (_flags & JackPortIsInput) { - _nframes_this_cycle = 0; - return; - } + _resolve_required = true; +} - // We're an output - copy events from internal buffer to Jack buffer - - void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle); +void +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)); +} - const jack_nframes_t event_count = _buffer.size(); - - //if (event_count > 0) - // cerr << "MIDIPort writing " << event_count << " events." << endl; +void +MidiPort::set_input_active (bool yn) +{ + _input_active = yn; +} - jack_midi_clear_buffer(jack_buffer, _nframes_this_cycle); - for (jack_nframes_t i=0; i < event_count; ++i) { - const jack_midi_event_t& ev = _buffer[i]; - assert(ev.time < _nframes_this_cycle); - jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size, _nframes_this_cycle); - } - - _nframes_this_cycle = 0; +void +MidiPort::set_always_parse (bool yn) +{ + _always_parse = yn; }