Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / midi_port.cc
index 77f9a6cb439cf884c1e33cdbc30f6b32e03cacee..f8259c891700bf384a660e066098e18414804abd 100644 (file)
@@ -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
 #include <cassert>
 #include <iostream>
 
-#include "ardour/midi_port.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"
+#include "ardour/session.h"
 
-using namespace ARDOUR;
 using namespace std;
+using namespace ARDOUR;
+using namespace PBD;
+
+#define port_engine AudioEngine::instance()->port_engine()
 
-MidiPort::MidiPort (const std::string& name, Flags flags)
-        : Port (name, DataType::MIDI, flags)
-       , _has_been_mixed_down (false)
+MidiPort::MidiPort (const std::string& name, PortFlags flags)
+       : Port (name, DataType::MIDI, flags)
+       , _resolve_required (false)
+       , _input_active (true)
+       , _trace_parser (0)
+       , _data_fetched_for_cycle (false)
 {
-       // FIXME: size kludge (see BufferSet::ensure_buffers)
-       // Jack needs to tell us this
-       _buffer = new MidiBuffer (1024 * 32);
+       _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
 }
 
 MidiPort::~MidiPort()
 {
+       if (_shadow_port) {
+               AudioEngine::instance()->unregister_port (_shadow_port);
+               _shadow_port.reset ();
+       }
+
        delete _buffer;
 }
 
+void
+MidiPort::parse_input (pframes_t nframes, MIDI::Parser& parser)
+{
+}
 
 void
-MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
+MidiPort::cycle_start (pframes_t nframes)
 {
+       Port::cycle_start (nframes);
+
        _buffer->clear ();
-       assert (_buffer->size () == 0);
-       
-       if (sends_output ()) {
-               jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
+
+       if (sends_output () && _port_handle) {
+               port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
+       }
+
+       if (receives_input() && _trace_parser) {
+               read_and_parse_entire_midi_buffer_with_no_speed_adjustment (nframes, *_trace_parser, AudioEngine::instance()->sample_time_at_cycle_start());
        }
+
+       if (inbound_midi_filter) {
+               MidiBuffer& mb (get_midi_buffer (nframes));
+               inbound_midi_filter (mb, mb);
+       }
+
+       if (_shadow_port) {
+               MidiBuffer& mb (get_midi_buffer (nframes));
+               if (shadow_midi_filter (mb, _shadow_port->get_midi_buffer (nframes))) {
+                       _shadow_port->flush_buffers (nframes);
+               }
+       }
+
 }
 
 MidiBuffer &
-MidiPort::get_midi_buffer (nframes_t nframes, nframes_t offset)
+MidiPort::get_midi_buffer (pframes_t nframes)
 {
-       if (_has_been_mixed_down) {
-           return *_buffer;
+       if (_data_fetched_for_cycle) {
+               return *_buffer;
        }
 
-       if (receives_input ()) {
-                       
-               void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
-               const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
-               
-               assert (event_count < _buffer->capacity());
-               
-               jack_midi_event_t ev;
-               
-               for (nframes_t i = 0; i < event_count; ++i) {
-                       
-                       jack_midi_event_get (&ev, jack_buffer, i);
-                       
-                       // i guess this should do but i leave it off to test the rest first.
-                       //if (ev.time > offset && ev.time < offset+nframes)
-                       _buffer->push_back (ev);
-               }
-               
-               if (nframes) {
-                       _has_been_mixed_down = true;
+       if (receives_input () && _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 MIDI events for this cycle of nframes 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 const* buf;
+
+                       port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
+
+                       if (buf[0] == 0xfe) {
+                               /* throw away active sensing */
+                               continue;
+                       }
+
+                       timestamp = floor (timestamp * _speed_ratio);
+
+                       /* check that the event is in the acceptable time range */
+                       if ((timestamp <  (_global_port_buffer_offset)) ||
+                           (timestamp >= (_global_port_buffer_offset + nframes))) {
+                               // XXX this is normal after a split cycles:
+                               // The engine buffer contains the data for the complete cycle, but
+                               // only the part after _global_port_buffer_offset is needed.
+#ifndef NDEBUG
+                               cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
+                                    << _global_port_buffer_offset << " limit="
+                                    << (_global_port_buffer_offset + nframes)
+                                    << " = (" << _global_port_buffer_offset
+                                    << " + " << nframes
+                                    << ")\n";
+#endif
+                               continue;
+                       }
+
+                       if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
+                               /* normalize note on with velocity 0 to proper note off */
+                               uint8_t ev[3];
+                               ev[0] = 0x80 | (buf[0] & 0x0F);  /* note off */
+                               ev[1] = buf[1];
+                               ev[2] = 0x40;  /* default velocity */
+                               _buffer->push_back (timestamp, size, ev);
+                       } else {
+                               _buffer->push_back (timestamp, size, buf);
+                       }
                }
-               
+
        } else {
-               _buffer->silence (nframes, offset);
+               _buffer->silence (nframes);
        }
-       
+
        if (nframes) {
-               _has_been_mixed_down = true;
+               _data_fetched_for_cycle = true;
        }
 
        return *_buffer;
 }
 
-       
 void
-MidiPort::cycle_end (nframes_t nframes, nframes_t offset)
+MidiPort::read_and_parse_entire_midi_buffer_with_no_speed_adjustment (pframes_t nframes, MIDI::Parser& parser, samplepos_t now)
 {
-#if 0
+       void* buffer = port_engine.get_buffer (_port_handle, nframes);
+       const pframes_t event_count = port_engine.get_midi_event_count (buffer);
 
-       if (sends_output ()) {
-               /* FIXME: offset */
-
-               // We're an output - copy events from source buffer to Jack buffer
-               
-               void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
-               
-               jack_midi_clear_buffer (jack_buffer);
-               
-               for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
-                       const Evoral::Event& ev = *i;
+       for (pframes_t i = 0; i < event_count; ++i) {
 
-                       // event times should be frames, relative to cycle start
-                       assert(ev.time() >= 0);
-                       assert(ev.time() < nframes);
-                       jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
+               pframes_t timestamp;
+               size_t size;
+               uint8_t const* buf;
+
+               port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
+
+               if (buf[0] == 0xfe) {
+                       /* throw away active sensing */
+                       continue;
+               }
+
+               parser.set_timestamp (now + timestamp);
+
+               /* During this parsing stage, signals will be emitted from the
+                * Parser, which will update anything connected to it.
+                *
+                * As of July 2018, this is only used by TransportMasters which
+                * read MIDI before the process() cycle really gets started.
+                */
+
+               if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
+                       /* normalize note on with velocity 0 to proper note off */
+                       parser.scanner (0x80 | (buf[0] & 0x0F));  /* note off */
+                       parser.scanner (buf[1]);
+                       parser.scanner (0x40);  /* default (off) velocity */
+               } else {
+                       for (size_t n = 0; n < size; ++n) {
+                               parser.scanner (buf[n]);
+                       }
                }
        }
-#endif
+}
+
+void
+MidiPort::cycle_end (pframes_t /*nframes*/)
+{
+       _data_fetched_for_cycle = false;
+}
+
+void
+MidiPort::cycle_split ()
+{
+       _data_fetched_for_cycle = false;
+}
+
+void
+MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
+{
+       for (uint8_t channel = 0; channel <= 0xF; channel++) {
+
+               uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
+               pframes_t tme = floor (when / _speed_ratio);
+
+               /* 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, tme, ev, 3) != 0) {
+                       cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
+               }
 
-       _has_been_mixed_down = false;
+               ev[1] = MIDI_CTL_ALL_NOTES_OFF;
+
+               if (port_engine.midi_event_put (port_buffer, tme, ev, 3) != 0) {
+                       cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
+               }
+       }
 }
 
 void
-MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
+MidiPort::flush_buffers (pframes_t nframes)
 {
-       /* FIXME: offset */
-       
        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 (port_buffer, _global_port_buffer_offset);
+                       _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::Event<nframes_t>& ev = *i;
-                       // event times should be frames, relative to cycle start
-                       assert(ev.time() >= 0);
-                       assert(ev.time() < (nframes+offset));
-                       if (ev.time() >= offset) {
-                               jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
+
+                       const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
+
+
+                       if (sends_output() && _trace_parser) {
+                               uint8_t const * const buf = ev.buffer();
+                               const samplepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
+
+                               _trace_parser->set_timestamp (now + ev.time());
+
+                               uint32_t limit = ev.size();
+
+                               for (size_t n = 0; n < limit; ++n) {
+                                       _trace_parser->scanner (buf[n]);
+                               }
+                       }
+
+
+                       // event times are in samples, relative to cycle start
+
+#ifndef NDEBUG
+                       if (DEBUG_ENABLED (DEBUG::MidiIO)) {
+                               const Session* s = AudioEngine::instance()->session();
+                               const samplepos_t now = (s ? s->transport_sample() : 0);
+                               DEBUG_STR_DECL(a);
+                               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(),
+                                                                   now + ev.time(), nframes, _global_port_buffer_offset, name()));
+                               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));
+
+                       if (ev.time() >= _global_port_buffer_offset) {
+                               pframes_t tme = floor (ev.time() / _speed_ratio);
+                               if (port_engine.midi_event_put (port_buffer, tme, ev.buffer(), ev.size()) != 0) {
+                                       cerr << "write failed, dropped event, time "
+                                            << ev.time()
+                                                        << " > " << _global_port_buffer_offset << endl;
+                               }
+                       } else {
+                               cerr << "drop flushed event on the floor, time " << ev.time()
+                                    << " too early for " << _global_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 ()
+{
+       _resolve_required = true;
+}
+
+void
+MidiPort::realtime_locate ()
+{
+       _resolve_required = true;
+}
+
+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));
+}
+
+void
+MidiPort::set_input_active (bool yn)
+{
+       _input_active = yn;
+}
+
+void
+MidiPort::set_trace (MIDI::Parser * p)
+{
+       _trace_parser = p;
+}
+
+int
+MidiPort::add_shadow_port (string const & name, MidiFilter mf)
+{
+       if (!ARDOUR::Port::receives_input()) {
+               return -1;
+       }
+
+       if (_shadow_port) {
+               return -2;
+       }
+
+       shadow_midi_filter = mf;
+
+       if (!(_shadow_port = boost::dynamic_pointer_cast<MidiPort> (AudioEngine::instance()->register_output_port (DataType::MIDI, name, false, PortFlags (Shadow|IsTerminal))))) {
+               return -3;
+       }
+
+       /* forward on our port latency to the shadow port.
+
+          XXX: need to capture latency changes and forward them too.
+       */
+
+       LatencyRange latency = private_latency_range (false);
+       _shadow_port->set_private_latency_range (latency, false);
+
+       return 0;
+}