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.
20 #include <ardour/midi_port.h>
21 #include <ardour/data_type.h>
24 using namespace ARDOUR;
27 MidiPort::MidiPort(jack_port_t* p)
29 , _buffer(4096) // FIXME FIXME FIXME Jack needs to tell us this
30 , _nframes_this_cycle(0)
33 assert(dt == DataType::MIDI);
46 MidiPort::cycle_start (jack_nframes_t nframes)
49 assert(_buffer.size() == 0);
51 _nframes_this_cycle = nframes;
53 if (_flags & JackPortIsOutput) {
54 _buffer.silence(nframes);
55 assert(_buffer.size() == 0);
59 // We're an input - copy Jack events to internal buffer
61 void* jack_buffer = jack_port_get_buffer(_port, nframes);
63 const jack_nframes_t event_count
64 = jack_midi_port_get_info(jack_buffer, nframes)->event_count;
66 assert(event_count < _buffer.capacity());
70 // FIXME: too slow, event struct is copied twice (here and MidiBuffer::push_back)
71 for (jack_nframes_t i=0; i < event_count; ++i) {
73 // This will fail to compile if we change MidiEvent to our own class
74 jack_midi_event_get(static_cast<jack_midi_event_t*>(&ev), jack_buffer, i, nframes);
76 _buffer.push_back(ev);
77 // Convert note ons with velocity 0 to proper note offs
78 // FIXME: Jack MIDI should guarantee this - does it?
79 //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0)
80 // ev->buffer[0] = MIDI_CMD_NOTE_OFF;
83 assert(_buffer.size() == event_count);
85 //if (_buffer.size() > 0)
86 // cerr << "MIDIPort got " << event_count << " events." << endl;
92 if (_flags & JackPortIsInput) {
93 _nframes_this_cycle = 0;
97 // We're an output - copy events from internal buffer to Jack buffer
99 void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
101 const jack_nframes_t event_count = _buffer.size();
103 //if (event_count > 0)
104 // cerr << "MIDIPort writing " << event_count << " events." << endl;
106 jack_midi_clear_buffer(jack_buffer, _nframes_this_cycle);
107 for (jack_nframes_t i=0; i < event_count; ++i) {
108 const jack_midi_event_t& ev = _buffer[i];
109 assert(ev.time < _nframes_this_cycle);
110 jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size, _nframes_this_cycle);
113 _nframes_this_cycle = 0;