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 "pbd/compose.h"
23 #include "pbd/debug.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/data_type.h"
27 #include "ardour/debug.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/midi_port.h"
32 using namespace ARDOUR;
35 #define port_engine AudioEngine::instance()->port_engine()
37 MidiPort::MidiPort (const std::string& name, PortFlags flags)
38 : Port (name, DataType::MIDI, flags)
39 , _has_been_mixed_down (false)
40 , _resolve_required (false)
41 , _input_active (true)
42 , _always_parse (false)
45 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
54 MidiPort::cycle_start (pframes_t nframes)
56 framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
58 Port::cycle_start (nframes);
62 if (sends_output ()) {
63 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
66 if (_always_parse || (receives_input() && _trace_on)) {
67 MidiBuffer& mb (get_midi_buffer (nframes));
69 /* dump incoming MIDI to parser */
71 for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
72 uint8_t* buf = (*b).buffer();
74 _self_parser.set_timestamp (now + (*b).time());
76 uint32_t limit = (*b).size();
78 for (size_t n = 0; n < limit; ++n) {
79 _self_parser.scanner (buf[n]);
86 MidiPort::get_buffer (pframes_t nframes)
88 return get_midi_buffer (nframes);
92 MidiPort::get_midi_buffer (pframes_t nframes)
94 if (_has_been_mixed_down) {
98 if (receives_input ()) {
102 void* buffer = port_engine.get_buffer (_port_handle, nframes);
103 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
105 /* suck all relevant MIDI events from the MIDI port buffer
109 for (pframes_t i = 0; i < event_count; ++i) {
115 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
117 if (buf[0] == 0xfe) {
118 /* throw away active sensing */
120 } else if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
121 /* normalize note on with velocity 0 to proper note off */
122 buf[0] = 0x80 | (buf[0] & 0x0F); /* note off */
123 buf[2] = 0x40; /* default velocity */
126 /* check that the event is in the acceptable time range */
128 if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) &&
129 (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
130 _buffer->push_back (timestamp, size, buf);
132 cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
133 << _global_port_buffer_offset << " limit="
134 << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
139 _buffer->silence (nframes);
143 _buffer->silence (nframes);
147 _has_been_mixed_down = true;
154 MidiPort::cycle_end (pframes_t /*nframes*/)
156 _has_been_mixed_down = false;
160 MidiPort::cycle_split ()
162 _has_been_mixed_down = false;
166 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
168 for (uint8_t channel = 0; channel <= 0xF; channel++) {
170 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
172 /* we need to send all notes off AND turn the
173 * sustain/damper pedal off to handle synths
174 * that prioritize sustain over AllNotesOff
177 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
178 cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
181 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
183 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
184 cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
190 MidiPort::flush_buffers (pframes_t nframes)
192 if (sends_output ()) {
194 void* port_buffer = 0;
196 if (_resolve_required) {
197 port_buffer = port_engine.get_buffer (_port_handle, nframes);
198 /* resolve all notes at the start of the buffer */
199 resolve_notes (port_buffer, _global_port_buffer_offset);
200 _resolve_required = false;
203 if (_buffer->empty()) {
208 port_buffer = port_engine.get_buffer (_port_handle, nframes);
212 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
214 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
217 if (sends_output() && _trace_on) {
218 uint8_t const * const buf = ev.buffer();
219 const framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
221 _self_parser.set_timestamp (now + ev.time());
223 uint32_t limit = ev.size();
225 for (size_t n = 0; n < limit; ++n) {
226 _self_parser.scanner (buf[n]);
231 // event times are in frames, relative to cycle start
234 if (DEBUG_ENABLED (DEBUG::MidiIO)) {
236 DEBUG_STR_APPEND(a, string_compose ("MidiPort %1 pop event @ %2 sz %3 ", _buffer, ev.time(), ev.size()));
237 for (size_t i=0; i < ev.size(); ++i) {
238 DEBUG_STR_APPEND(a,hex);
239 DEBUG_STR_APPEND(a,"0x");
240 DEBUG_STR_APPEND(a,(int)(ev.buffer()[i]));
241 DEBUG_STR_APPEND(a,' ');
243 DEBUG_STR_APPEND(a,'\n');
244 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
248 assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
250 if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
251 if (port_engine.midi_event_put (port_buffer, (pframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
252 cerr << "write failed, drop flushed note off on the floor, time "
253 << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
256 cerr << "drop flushed event on the floor, time " << ev.time()
257 << " too early for " << _global_port_buffer_offset
258 << " + " << _port_buffer_offset;
259 for (size_t xx = 0; xx < ev.size(); ++xx) {
260 cerr << ' ' << hex << (int) ev.buffer()[xx];
266 /* done.. the data has moved to the port buffer, mark it so
274 MidiPort::require_resolve ()
276 _resolve_required = true;
280 MidiPort::transport_stopped ()
282 _resolve_required = true;
286 MidiPort::realtime_locate ()
288 _resolve_required = true;
296 cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
297 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
301 MidiPort::set_input_active (bool yn)
307 MidiPort::set_always_parse (bool yn)
313 MidiPort::set_trace_on (bool yn)