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 "ardour/audioengine.h"
26 using namespace ARDOUR;
29 MidiPort::MidiPort (const std::string& name, Flags flags)
30 : Port (name, DataType::MIDI, flags)
31 , _has_been_mixed_down (false)
32 , _resolve_required (false)
34 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
43 MidiPort::cycle_start (pframes_t nframes)
45 Port::cycle_start (nframes);
49 assert (_buffer->size () == 0);
51 if (sends_output ()) {
52 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
57 MidiPort::get_midi_buffer (pframes_t nframes)
59 if (_has_been_mixed_down) {
63 if (receives_input ()) {
65 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
66 const pframes_t event_count = jack_midi_get_event_count (jack_buffer);
68 assert (event_count < _buffer->capacity());
70 /* suck all relevant MIDI events from the JACK MIDI port buffer
74 for (pframes_t i = 0; i < event_count; ++i) {
78 jack_midi_event_get (&ev, jack_buffer, i);
80 if (ev.buffer[0] == 0xfe) {
81 /* throw away active sensing */
85 /* check that the event is in the acceptable time range */
87 if ((ev.time >= (_global_port_buffer_offset + _port_buffer_offset)) &&
88 (ev.time < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
89 _buffer->push_back (ev);
91 cerr << "Dropping incoming MIDI at time " << ev.time << "; offset="
92 << _global_port_buffer_offset << " limit="
93 << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
98 _buffer->silence (nframes);
102 _has_been_mixed_down = true;
110 MidiPort::cycle_end (pframes_t /*nframes*/)
112 _has_been_mixed_down = false;
116 MidiPort::cycle_split ()
118 _has_been_mixed_down = false;
122 MidiPort::resolve_notes (void* jack_buffer, MidiBuffer::TimeType when)
128 for (uint8_t channel = 0; channel <= 0xF; channel++) {
129 ev[0] = (MIDI_CMD_CONTROL | channel);
131 /* we need to send all notes off AND turn the
132 * sustain/damper pedal off to handle synths
133 * that prioritize sustain over AllNotesOff
136 ev[1] = MIDI_CTL_SUSTAIN;
138 if (jack_midi_event_write (jack_buffer, when, ev, 3) != 0) {
139 cerr << "failed to deliver sustain-zero on channel " << channel << " on port " << name() << endl;
142 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
144 if (jack_midi_event_write (jack_buffer, 0, ev, 3) != 0) {
145 cerr << "failed to deliver ALL NOTES OFF on channel " << channel << " on port " << name() << endl;
151 MidiPort::flush_buffers (pframes_t nframes, framepos_t time)
153 if (sends_output ()) {
155 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
157 if (_resolve_required) {
158 /* resolve all notes at the start of the buffer */
159 resolve_notes (jack_buffer, 0);
160 _resolve_required= false;
163 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
165 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
167 // event times are in frames, relative to cycle start
169 assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
171 if (ev.event_type() == LoopEventType) {
172 resolve_notes (jack_buffer, ev.time());
176 if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
177 if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
178 cerr << "write failed, drop flushed note off on the floor, time "
179 << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
182 cerr << "drop flushed event on the floor, time " << ev.time()
183 << " < " << _global_port_buffer_offset + _port_buffer_offset << endl;
190 MidiPort::transport_stopped ()
192 _resolve_required = true;
196 MidiPort::realtime_locate ()
198 _resolve_required = true;
206 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));