2008-06-02 17:41:35 -04:00
|
|
|
/*
|
2009-10-14 12:10:01 -04:00
|
|
|
Copyright (C) 2006 Paul Davis
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "ardour/midi_port.h"
|
|
|
|
#include "ardour/data_type.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace ARDOUR;
|
|
|
|
using namespace std;
|
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
MidiPort::MidiPort (const std::string& name, Flags flags)
|
2009-05-04 11:50:51 -04:00
|
|
|
: Port (name, DataType::MIDI, flags)
|
2008-10-08 16:14:22 -04:00
|
|
|
, _has_been_mixed_down (false)
|
2009-10-14 13:35:33 -04:00
|
|
|
, _resolve_in_process (false)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2009-05-04 11:50:51 -04:00
|
|
|
_buffer = new MidiBuffer (raw_buffer_size(0));
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MidiPort::~MidiPort()
|
|
|
|
{
|
2009-01-20 21:27:21 -05:00
|
|
|
delete _buffer;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-03 17:26:29 -05:00
|
|
|
MidiPort::cycle_start (pframes_t nframes)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2011-03-14 21:16:24 -04:00
|
|
|
Port::cycle_start (nframes);
|
2011-03-10 21:55:52 -05:00
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
_buffer->clear ();
|
2011-03-10 21:55:52 -05:00
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
assert (_buffer->size () == 0);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
if (sends_output ()) {
|
|
|
|
jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2008-10-08 16:14:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MidiBuffer &
|
2011-03-10 21:55:52 -05:00
|
|
|
MidiPort::get_midi_buffer (pframes_t nframes)
|
2009-02-01 16:04:12 -05:00
|
|
|
{
|
2009-01-20 21:27:21 -05:00
|
|
|
if (_has_been_mixed_down) {
|
2009-04-23 13:48:37 -04:00
|
|
|
return *_buffer;
|
2009-01-20 21:27:21 -05:00
|
|
|
}
|
2008-10-08 16:14:22 -04:00
|
|
|
|
2009-01-20 21:27:21 -05:00
|
|
|
if (receives_input ()) {
|
2009-04-23 13:48:37 -04:00
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
|
2011-03-10 21:55:52 -05:00
|
|
|
const pframes_t event_count = jack_midi_get_event_count (jack_buffer);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
assert (event_count < _buffer->capacity());
|
2009-04-23 13:48:37 -04:00
|
|
|
|
|
|
|
/* suck all relevant MIDI events from the JACK MIDI port buffer
|
|
|
|
into our MidiBuffer
|
|
|
|
*/
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2010-12-03 17:26:29 -05:00
|
|
|
for (pframes_t i = 0; i < event_count; ++i) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-04-23 13:48:37 -04:00
|
|
|
jack_midi_event_t ev;
|
|
|
|
|
2009-02-10 19:58:24 -05:00
|
|
|
jack_midi_event_get (&ev, jack_buffer, i);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2011-03-14 21:16:24 -04:00
|
|
|
if (ev.buffer[0] == 0xfe) {
|
|
|
|
/* throw away active sensing */
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-27 10:09:16 -04:00
|
|
|
|
2011-03-14 21:16:24 -04:00
|
|
|
/* check that the event is in the acceptable time range */
|
2011-03-10 21:55:52 -05:00
|
|
|
|
|
|
|
if ((ev.time >= (_global_port_buffer_offset + _port_buffer_offset)) &&
|
2011-03-14 21:16:24 -04:00
|
|
|
(ev.time < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
|
2009-04-23 13:48:37 -04:00
|
|
|
_buffer->push_back (ev);
|
2010-08-09 20:09:42 -04:00
|
|
|
} else {
|
2011-03-10 21:55:52 -05:00
|
|
|
cerr << "Dropping incoming MIDI at time " << ev.time << "; offset="
|
2011-03-14 21:16:24 -04:00
|
|
|
<< _global_port_buffer_offset << " limit="
|
|
|
|
<< (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
|
2009-04-23 13:48:37 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2009-04-23 13:48:37 -04:00
|
|
|
_buffer->silence (nframes);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-01-20 21:27:21 -05:00
|
|
|
if (nframes) {
|
2008-10-08 16:14:22 -04:00
|
|
|
_has_been_mixed_down = true;
|
2009-01-20 21:27:21 -05:00
|
|
|
}
|
2008-10-08 16:14:22 -04:00
|
|
|
|
|
|
|
return *_buffer;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
void
|
2010-12-03 17:26:29 -05:00
|
|
|
MidiPort::cycle_end (pframes_t /*nframes*/)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2009-04-23 13:48:37 -04:00
|
|
|
_has_been_mixed_down = false;
|
|
|
|
}
|
2009-01-20 21:27:21 -05:00
|
|
|
|
2009-04-23 13:48:37 -04:00
|
|
|
void
|
|
|
|
MidiPort::cycle_split ()
|
|
|
|
{
|
2008-10-08 16:14:22 -04:00
|
|
|
_has_been_mixed_down = false;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2008-10-08 16:14:22 -04:00
|
|
|
void
|
2011-03-07 14:06:42 -05:00
|
|
|
MidiPort::flush_buffers (pframes_t nframes, framepos_t time)
|
2008-10-08 16:14:22 -04:00
|
|
|
{
|
2009-02-10 19:58:24 -05:00
|
|
|
if (sends_output ()) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-01-20 21:27:21 -05:00
|
|
|
void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
|
|
|
|
|
2009-10-14 13:35:33 -04:00
|
|
|
// Feed the data through the MidiStateTracker
|
|
|
|
bool did_loop;
|
|
|
|
|
|
|
|
_midi_state_tracker.track (_buffer->begin(), _buffer->end(), did_loop);
|
|
|
|
|
|
|
|
if (did_loop || _resolve_in_process) {
|
|
|
|
/* add necessary note offs */
|
|
|
|
_midi_state_tracker.resolve_notes (*_buffer, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
_resolve_in_process = false;
|
|
|
|
|
2009-01-20 21:27:21 -05:00
|
|
|
for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
|
2010-12-03 17:26:29 -05:00
|
|
|
const Evoral::Event<framepos_t>& ev = *i;
|
2009-04-23 13:48:37 -04:00
|
|
|
|
2009-02-25 19:58:35 -05:00
|
|
|
// event times are in frames, relative to cycle start
|
2009-04-23 13:48:37 -04:00
|
|
|
|
2011-03-10 21:55:52 -05:00
|
|
|
assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
|
2009-04-23 13:48:37 -04:00
|
|
|
|
2011-03-10 21:55:52 -05:00
|
|
|
if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
|
2010-06-09 09:00:54 -04:00
|
|
|
if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
|
2011-03-14 21:16:24 -04:00
|
|
|
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;
|
|
|
|
}
|
2009-01-20 21:27:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-14 13:35:33 -04:00
|
|
|
void
|
|
|
|
MidiPort::transport_stopped ()
|
|
|
|
{
|
|
|
|
_resolve_in_process = true;
|
|
|
|
}
|
|
|
|
|
2009-05-04 11:50:51 -04:00
|
|
|
size_t
|
2010-12-03 17:26:29 -05:00
|
|
|
MidiPort::raw_buffer_size (pframes_t nframes) const
|
2009-05-04 11:50:51 -04:00
|
|
|
{
|
2010-12-03 17:26:29 -05:00
|
|
|
return jack_midi_max_event_size (jack_port_get_buffer (_jack_port, nframes));
|
2009-05-04 11:50:51 -04:00
|
|
|
}
|
|
|
|
|