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>
24 #include <midi++/jackcompat.h>
26 using namespace ARDOUR;
29 MidiPort::MidiPort(jack_port_t* p)
31 , _buffer(4096) // FIXME FIXME FIXME Jack needs to tell us this
32 , _nframes_this_cycle(0)
35 assert(dt == DataType::MIDI);
48 MidiPort::cycle_start (jack_nframes_t nframes)
51 assert(_buffer.size() == 0);
53 _nframes_this_cycle = nframes;
55 if (_flags & JackPortIsOutput) {
56 _buffer.silence(nframes);
57 assert(_buffer.size() == 0);
61 // We're an input - copy Jack events to internal buffer
63 void* jack_buffer = jack_port_get_buffer(_port, nframes);
65 const jack_nframes_t event_count
66 = jack_midi_get_event_count(jack_buffer);
68 assert(event_count < _buffer.capacity());
72 // FIXME: too slow, event struct is copied twice (here and MidiBuffer::push_back)
73 for (jack_nframes_t i=0; i < event_count; ++i) {
75 // This will fail to compile if we change MidiEvent to our own class
76 jack_midi_event_get(static_cast<jack_midi_event_t*>(&ev), jack_buffer, i);
78 _buffer.push_back(ev);
79 // Convert note ons with velocity 0 to proper note offs
80 // FIXME: Jack MIDI should guarantee this - does it?
81 //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0)
82 // ev->buffer[0] = MIDI_CMD_NOTE_OFF;
85 assert(_buffer.size() == event_count);
87 //if (_buffer.size() > 0)
88 // cerr << "MIDIPort got " << event_count << " events." << endl;
94 if (_flags & JackPortIsInput) {
95 _nframes_this_cycle = 0;
99 // We're an output - copy events from internal buffer to Jack buffer
101 void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
103 const jack_nframes_t event_count = _buffer.size();
105 //if (event_count > 0)
106 // cerr << "MIDIPort writing " << event_count << " events." << endl;
108 jack_midi_clear_buffer(jack_buffer);
109 for (jack_nframes_t i=0; i < event_count; ++i) {
110 const jack_midi_event_t& ev = _buffer[i];
111 assert(ev.time < _nframes_this_cycle);
112 jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size);
115 _nframes_this_cycle = 0;