2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "ardour/midi_port.h"
23 #include "ardour/data_type.h"
25 using namespace ARDOUR;
28 MidiPort::MidiPort (const std::string& name, Flags flags)
29 : Port (name, DataType::MIDI, flags)
30 , _has_been_mixed_down (false)
32 _buffer = new MidiBuffer (raw_buffer_size(0));
41 MidiPort::cycle_start (nframes_t nframes)
44 assert (_buffer->size () == 0);
46 if (sends_output ()) {
47 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
52 MidiPort::get_midi_buffer (nframes_t nframes, nframes_t offset)
54 if (_has_been_mixed_down) {
58 if (receives_input ()) {
60 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
61 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
63 assert (event_count < _buffer->capacity());
65 /* suck all relevant MIDI events from the JACK MIDI port buffer
69 nframes_t off = offset + _port_offset;
71 for (nframes_t i = 0; i < event_count; ++i) {
75 jack_midi_event_get (&ev, jack_buffer, i);
77 if (ev.time > off && ev.time < off+nframes) {
78 _buffer->push_back (ev);
83 _has_been_mixed_down = true;
87 _buffer->silence (nframes);
91 _has_been_mixed_down = true;
99 MidiPort::cycle_end (nframes_t nframes)
101 _has_been_mixed_down = false;
105 MidiPort::cycle_split ()
107 _has_been_mixed_down = false;
111 MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
113 if (sends_output ()) {
115 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
117 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
118 const Evoral::Event<nframes_t>& ev = *i;
120 // event times are in frames, relative to cycle start
122 // XXX split cycle start or cycle start?
124 assert(ev.time() < (nframes+offset+_port_offset));
126 if (ev.time() >= offset + _port_offset) {
127 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
134 MidiPort::raw_buffer_size (nframes_t nframes) const
136 return jack_midi_max_event_size(jack_port_get_buffer(_jack_port, nframes));