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"
30 #include "ardour/session.h"
33 using namespace ARDOUR;
36 #define port_engine AudioEngine::instance()->port_engine()
38 MidiPort::MidiPort (const std::string& name, PortFlags flags)
39 : Port (name, DataType::MIDI, flags)
40 , _has_been_mixed_down (false)
41 , _resolve_required (false)
42 , _input_active (true)
43 , _always_parse (false)
46 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
55 MidiPort::cycle_start (pframes_t nframes)
57 framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
59 Port::cycle_start (nframes);
63 if (sends_output ()) {
64 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
67 if (_always_parse || (receives_input() && _trace_on)) {
68 MidiBuffer& mb (get_midi_buffer (nframes));
70 /* dump incoming MIDI to parser */
72 for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
73 uint8_t* buf = (*b).buffer();
75 _self_parser.set_timestamp (now + (*b).time());
77 uint32_t limit = (*b).size();
79 for (size_t n = 0; n < limit; ++n) {
80 _self_parser.scanner (buf[n]);
85 if (inbound_midi_filter) {
86 MidiBuffer& mb (get_midi_buffer (nframes));
87 inbound_midi_filter (mb, mb);
91 MidiBuffer& mb (get_midi_buffer (nframes));
92 if (shadow_midi_filter (mb, _shadow_port->get_midi_buffer (nframes))) {
93 _shadow_port->flush_buffers (nframes);
100 MidiPort::get_buffer (pframes_t nframes)
102 return get_midi_buffer (nframes);
106 MidiPort::get_midi_buffer (pframes_t nframes)
108 if (_has_been_mixed_down) {
112 if (receives_input ()) {
116 void* buffer = port_engine.get_buffer (_port_handle, nframes);
117 const pframes_t event_count = port_engine.get_midi_event_count (buffer);
119 /* suck all relevant MIDI events from the MIDI port buffer
123 for (pframes_t i = 0; i < event_count; ++i) {
129 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
131 if (buf[0] == 0xfe) {
132 /* throw away active sensing */
134 } else if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
135 /* normalize note on with velocity 0 to proper note off */
136 buf[0] = 0x80 | (buf[0] & 0x0F); /* note off */
137 buf[2] = 0x40; /* default velocity */
140 /* check that the event is in the acceptable time range */
142 if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) &&
143 (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
144 _buffer->push_back (timestamp, size, buf);
146 cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
147 << _global_port_buffer_offset << " limit="
148 << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
153 _buffer->silence (nframes);
157 _buffer->silence (nframes);
161 _has_been_mixed_down = true;
168 MidiPort::cycle_end (pframes_t /*nframes*/)
170 _has_been_mixed_down = false;
174 MidiPort::cycle_split ()
176 _has_been_mixed_down = false;
180 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
182 for (uint8_t channel = 0; channel <= 0xF; channel++) {
184 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
186 /* we need to send all notes off AND turn the
187 * sustain/damper pedal off to handle synths
188 * that prioritize sustain over AllNotesOff
191 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
192 cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
195 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
197 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
198 cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
204 MidiPort::flush_buffers (pframes_t nframes)
206 if (sends_output ()) {
208 void* port_buffer = 0;
210 if (_resolve_required) {
211 port_buffer = port_engine.get_buffer (_port_handle, nframes);
212 /* resolve all notes at the start of the buffer */
213 resolve_notes (port_buffer, _global_port_buffer_offset);
214 _resolve_required = false;
217 if (_buffer->empty()) {
222 port_buffer = port_engine.get_buffer (_port_handle, nframes);
226 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
228 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
231 if (sends_output() && _trace_on) {
232 uint8_t const * const buf = ev.buffer();
233 const framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
235 _self_parser.set_timestamp (now + ev.time());
237 uint32_t limit = ev.size();
239 for (size_t n = 0; n < limit; ++n) {
240 _self_parser.scanner (buf[n]);
245 // event times are in frames, relative to cycle start
248 if (DEBUG_ENABLED (DEBUG::MidiIO)) {
249 const Session* s = AudioEngine::instance()->session();
250 const framepos_t now = (s ? s->transport_frame() : 0);
252 DEBUG_STR_APPEND(a, string_compose ("MidiPort %8 %1 pop event @ %2 (global %4, within %5 gpbo %6 pbo %7 sz %3 ", _buffer, ev.time(), ev.size(),
253 now + ev.time(), nframes, _global_port_buffer_offset, _port_buffer_offset, name()));
254 for (size_t i=0; i < ev.size(); ++i) {
255 DEBUG_STR_APPEND(a,hex);
256 DEBUG_STR_APPEND(a,"0x");
257 DEBUG_STR_APPEND(a,(int)(ev.buffer()[i]));
258 DEBUG_STR_APPEND(a,' ');
260 DEBUG_STR_APPEND(a,'\n');
261 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
265 assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
267 if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
268 if (port_engine.midi_event_put (port_buffer, (pframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
269 cerr << "write failed, drop flushed note off on the floor, time "
270 << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
273 cerr << "drop flushed event on the floor, time " << ev.time()
274 << " too early for " << _global_port_buffer_offset
275 << " + " << _port_buffer_offset;
276 for (size_t xx = 0; xx < ev.size(); ++xx) {
277 cerr << ' ' << hex << (int) ev.buffer()[xx];
283 /* done.. the data has moved to the port buffer, mark it so
291 MidiPort::require_resolve ()
293 _resolve_required = true;
297 MidiPort::transport_stopped ()
299 _resolve_required = true;
303 MidiPort::realtime_locate ()
305 _resolve_required = true;
313 cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
314 _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
318 MidiPort::set_input_active (bool yn)
324 MidiPort::set_always_parse (bool yn)
330 MidiPort::set_trace_on (bool yn)
336 MidiPort::add_shadow_port (string const & name, MidiFilter mf)
338 if (!ARDOUR::Port::receives_input()) {
346 shadow_midi_filter = mf;
348 if (!(_shadow_port = boost::dynamic_pointer_cast<MidiPort> (AudioEngine::instance()->register_output_port (DataType::MIDI, name, false, PortFlags (Shadow|IsTerminal))))) {
352 /* forward on our port latency to the shadow port.
354 XXX: need to capture latency changes and forward them too.
357 LatencyRange latency = private_latency_range (false);
358 _shadow_port->set_private_latency_range (latency, false);