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)
30 , _nframes_this_cycle(0)
33 assert(dt == DataType::MIDI);
37 _buffer = new MidiBuffer(4096); // FIXME FIXME FIXME
47 MidiPort::cycle_start (jack_nframes_t nframes)
49 _nframes_this_cycle = nframes;
51 if (_flags & JackPortIsOutput) {
56 // We're an input - copy Jack events to internal buffer
58 void* jack_buffer = jack_port_get_buffer(_port, nframes);
60 const jack_nframes_t event_count
61 = jack_midi_port_get_info(jack_buffer, nframes)->event_count;
63 assert(event_count < _buffer->capacity());
65 for (jack_nframes_t i=0; i < event_count; ++i) {
66 jack_midi_event_t* const ev = &_buffer->data()[i];
67 jack_midi_event_get(ev, jack_buffer, i, nframes);
69 // Convert note ons with velocity 0 to proper note offs
70 // FIXME: Jack MIDI should guarantee this - does it?
71 //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0)
72 // ev->buffer[0] = MIDI_CMD_NOTE_OFF;
75 _buffer->set_size(event_count);
77 //if (_buffer->size() > 0)
78 // cerr << "MIDIPort got " << event_count << " events." << endl;
84 if (_flags & JackPortIsInput) {
85 _nframes_this_cycle = 0; // catch any oopses
89 // We're an output - copy events from internal buffer to Jack buffer
91 void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
93 const jack_nframes_t event_count = _buffer->size();
95 jack_midi_clear_buffer(jack_buffer, _nframes_this_cycle);
96 for (jack_nframes_t i=0; i < event_count; ++i) {
97 const jack_midi_event_t& ev = _buffer->data()[i];
98 jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size, _nframes_this_cycle);
101 _nframes_this_cycle = 0; // catch oopses