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(jack_port_t* p)
30 , _buffer(4096) // FIXME FIXME FIXME Jack needs to tell us this
31 , _nframes_this_cycle(0)
34 assert(dt == DataType::MIDI);
47 MidiPort::cycle_start (nframes_t nframes)
50 assert(_buffer.size() == 0);
52 _nframes_this_cycle = nframes;
54 if (_flags & JackPortIsOutput) {
55 _buffer.silence(nframes);
56 assert(_buffer.size() == 0);
60 // We're an input - copy Jack events to internal buffer
62 void* jack_buffer = jack_port_get_buffer(_port, nframes);
64 const nframes_t event_count
65 = jack_midi_get_event_count(jack_buffer);
67 assert(event_count < _buffer.capacity());
71 for (nframes_t i=0; i < event_count; ++i) {
73 jack_midi_event_get(&ev, jack_buffer, i);
75 _buffer.push_back(ev);
78 assert(_buffer.size() == event_count);
80 //if (_buffer.size() > 0)
81 // cerr << "MIDIPort got " << event_count << " events." << endl;
87 if (_flags & JackPortIsInput) {
88 _nframes_this_cycle = 0;
92 // We're an output - copy events from internal buffer to Jack buffer
94 void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
96 //const nframes_t event_count = _buffer.size();
97 //if (event_count > 0)
98 // cerr << "MIDIPort writing " << event_count << " events." << endl;
100 jack_midi_clear_buffer(jack_buffer);
102 for (MidiBuffer::iterator i = _buffer.begin(); i != _buffer.end(); ++i) {
103 const MidiEvent& ev = *i;
104 // event times should be frames, relative to cycle start
105 assert(ev.time >= 0);
106 assert(ev.time < _nframes_this_cycle);
107 jack_midi_event_write(jack_buffer, (jack_nframes_t)ev.time, ev.buffer, ev.size);
110 _nframes_this_cycle = 0;