3 Copyright (C) 1999-2002 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <boost/shared_ptr.hpp>
32 #include "midi++/mmc.h"
33 #include "midi++/port.h"
34 #include "midi++/manager.h"
35 #include "pbd/error.h"
36 #include "pbd/pthread_utils.h"
38 #include "ardour/configuration.h"
39 #include "ardour/debug.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/session.h"
42 #include "ardour/audio_track.h"
43 #include "ardour/midi_track.h"
44 #include "ardour/audio_diskstream.h"
45 #include "ardour/slave.h"
46 #include "ardour/cycles.h"
47 #include "ardour/timecode.h"
52 using namespace ARDOUR;
56 MachineControl::CommandSignature MMC_CommandSignature;
57 MachineControl::ResponseSignature MMC_ResponseSignature;
64 boost::shared_ptr<RouteList> r = routes.reader ();
66 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
67 MidiTrack *track = dynamic_cast<MidiTrack*>((*i).get());
76 Session::use_config_midi_ports ()
80 if (default_mmc_port) {
81 set_mmc_port (default_mmc_port->name());
86 if (default_mtc_port) {
87 set_mtc_port (default_mtc_port->name());
92 if (default_midi_port) {
93 set_midi_port (default_midi_port->name());
98 if (default_midi_clock_port) {
99 set_midi_clock_port (default_midi_clock_port->name());
101 set_midi_clock_port ("");
108 /***********************************************************************
110 **********************************************************************/
113 Session::set_mtc_port (string port_tag)
117 if (port_tag.length() == 0) {
119 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
120 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
124 if (_mtc_port == 0) {
134 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
135 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
141 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
145 Config->set_mtc_port_name (port_tag);
148 MTC_PortChanged(); /* EMIT SIGNAL */
149 change_midi_ports ();
155 Session::set_mmc_receive_device_id (uint32_t device_id)
158 mmc->set_receive_device_id (device_id);
163 Session::set_mmc_send_device_id (uint32_t device_id)
166 mmc->set_send_device_id (device_id);
171 Session::set_mmc_port (string port_tag)
173 MIDI::byte old_recv_device_id = 0;
174 MIDI::byte old_send_device_id = 0;
175 bool reset_id = false;
177 if (port_tag.length() == 0) {
178 if (_mmc_port == 0) {
187 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
194 old_recv_device_id = mmc->receive_device_id();
195 old_recv_device_id = mmc->send_device_id();
200 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
201 MMC_CommandSignature,
202 MMC_ResponseSignature);
205 mmc->set_receive_device_id (old_recv_device_id);
206 mmc->set_send_device_id (old_send_device_id);
210 (mem_fun (*this, &Session::mmc_deferred_play));
211 mmc->DeferredPlay.connect
212 (mem_fun (*this, &Session::mmc_deferred_play));
214 (mem_fun (*this, &Session::mmc_stop));
215 mmc->FastForward.connect
216 (mem_fun (*this, &Session::mmc_fast_forward));
218 (mem_fun (*this, &Session::mmc_rewind));
220 (mem_fun (*this, &Session::mmc_pause));
221 mmc->RecordPause.connect
222 (mem_fun (*this, &Session::mmc_record_pause));
223 mmc->RecordStrobe.connect
224 (mem_fun (*this, &Session::mmc_record_strobe));
225 mmc->RecordExit.connect
226 (mem_fun (*this, &Session::mmc_record_exit));
228 (mem_fun (*this, &Session::mmc_locate));
230 (mem_fun (*this, &Session::mmc_step));
232 (mem_fun (*this, &Session::mmc_shuttle));
233 mmc->TrackRecordStatusChange.connect
234 (mem_fun (*this, &Session::mmc_record_enable));
237 /* also handle MIDI SPP because its so common */
239 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
240 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
241 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
243 Config->set_mmc_port_name (port_tag);
246 MMC_PortChanged(); /* EMIT SIGNAL */
247 change_midi_ports ();
253 Session::set_midi_port (string /*port_tag*/)
256 if (port_tag.length() == 0) {
257 if (_midi_port == 0) {
266 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
272 /* XXX need something to forward this to control protocols ? or just
276 Config->set_midi_port_name (port_tag);
280 MIDI_PortChanged(); /* EMIT SIGNAL */
281 change_midi_ports ();
287 Session::set_midi_clock_port (string port_tag)
291 if (port_tag.length() == 0) {
293 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
294 error << _("Ardour is slaved to MIDI Clock - port cannot be reset") << endmsg;
298 if (_midi_clock_port == 0) {
302 _midi_clock_port = 0;
308 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
309 error << string_compose (_("unknown port %1 requested for MIDI Clock"), port_tag) << endl;
313 _midi_clock_port = port;
315 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
319 Config->set_midi_clock_port_name (port_tag);
322 MIDIClock_PortChanged(); /* EMIT SIGNAL */
323 change_midi_ports ();
329 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
331 MIDI::Parser* input_parser;
333 cerr << "enabling tracing: " << yn << " for input port " << port->name() << endl;
336 if ((input_parser = port->input()) != 0) {
337 input_parser->trace (yn, &cout, "input: ");
342 if ((input_parser = _mmc_port->input()) != 0) {
343 input_parser->trace (yn, &cout, "input: ");
347 if (_mtc_port && _mtc_port != _mmc_port) {
348 if ((input_parser = _mtc_port->input()) != 0) {
349 input_parser->trace (yn, &cout, "input: ");
353 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
354 if ((input_parser = _midi_port->input()) != 0) {
355 input_parser->trace (yn, &cout, "input: ");
360 && _midi_clock_port != _mmc_port
361 && _midi_clock_port != _mtc_port
362 && _midi_clock_port != _midi_port) {
363 if ((input_parser = _midi_clock_port->input()) != 0) {
364 input_parser->trace (yn, &cout, "input: ");
369 Config->set_trace_midi_input (yn);
373 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
375 MIDI::Parser* output_parser;
378 if ((output_parser = port->output()) != 0) {
379 output_parser->trace (yn, &cout, "output: ");
383 if ((output_parser = _mmc_port->output()) != 0) {
384 output_parser->trace (yn, &cout, "output: ");
388 if (_mtc_port && _mtc_port != _mmc_port) {
389 if ((output_parser = _mtc_port->output()) != 0) {
390 output_parser->trace (yn, &cout, "output: ");
394 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
395 if ((output_parser = _midi_port->output()) != 0) {
396 output_parser->trace (yn, &cout, "output: ");
402 Config->set_trace_midi_output (yn);
406 Session::get_trace_midi_input(MIDI::Port *port)
408 MIDI::Parser* input_parser;
410 if ((input_parser = port->input()) != 0) {
411 return input_parser->tracing();
416 if ((input_parser = _mmc_port->input()) != 0) {
417 return input_parser->tracing();
422 if ((input_parser = _mtc_port->input()) != 0) {
423 return input_parser->tracing();
428 if ((input_parser = _midi_port->input()) != 0) {
429 return input_parser->tracing();
438 Session::get_trace_midi_output(MIDI::Port *port)
440 MIDI::Parser* output_parser;
442 if ((output_parser = port->output()) != 0) {
443 return output_parser->tracing();
448 if ((output_parser = _mmc_port->output()) != 0) {
449 return output_parser->tracing();
454 if ((output_parser = _mtc_port->output()) != 0) {
455 return output_parser->tracing();
460 if ((output_parser = _midi_port->output()) != 0) {
461 return output_parser->tracing();
471 Session::setup_midi_control ()
473 outbound_mtc_timecode_frame = 0;
474 next_quarter_frame_to_send = 0;
476 /* setup the MMC buffer */
478 mmc_buffer[0] = 0xf0; // SysEx
479 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
480 mmc_buffer[2] = (mmc ? mmc->send_device_id() : 0x7f);
481 mmc_buffer[3] = 0x6; // MCC
483 /* Set up the qtr frame message */
496 Session::spp_start (Parser &, nframes_t /*timestamp*/)
498 if (Config->get_mmc_control() && (!config.get_external_sync() || config.get_sync_source() != JACK)) {
499 request_transport_speed (1.0);
504 Session::spp_continue (Parser& ignored, nframes_t timestamp)
506 spp_start (ignored, timestamp);
510 Session::spp_stop (Parser&, nframes_t /*timestamp*/)
512 if (Config->get_mmc_control()) {
518 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
520 if (Config->get_mmc_control() && (!config.get_external_sync() || (config.get_sync_source() != JACK))) {
521 request_transport_speed (1.0);
526 Session::mmc_record_pause (MIDI::MachineControl &/*mmc*/)
528 if (Config->get_mmc_control()) {
529 maybe_enable_record();
534 Session::mmc_record_strobe (MIDI::MachineControl &/*mmc*/)
536 if (!Config->get_mmc_control())
539 /* record strobe does an implicit "Play" command */
541 if (_transport_speed != 1.0) {
543 /* start_transport() will move from Enabled->Recording, so we
544 don't need to do anything here except enable recording.
545 its not the same as maybe_enable_record() though, because
546 that *can* switch to Recording, which we do not want.
549 save_state ("", true);
550 g_atomic_int_set (&_record_status, Enabled);
551 RecordStateChanged (); /* EMIT SIGNAL */
553 request_transport_speed (1.0);
562 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
564 if (Config->get_mmc_control()) {
565 disable_record (false);
570 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
572 if (Config->get_mmc_control()) {
578 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
580 if (Config->get_mmc_control()) {
582 /* We support RECORD_PAUSE, so the spec says that
583 we must interpret PAUSE like RECORD_PAUSE if
587 if (actively_recording()) {
588 maybe_enable_record ();
595 static bool step_queued = false;
598 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
600 if (!Config->get_mmc_control()) {
605 struct timeval diff = { 0, 0 };
607 gettimeofday (&now, 0);
609 timersub (&now, &last_mmc_step, &diff);
611 gettimeofday (&now, 0);
612 timersub (&now, &last_mmc_step, &diff);
614 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
618 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
619 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
621 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
622 /* change direction */
623 step_speed = cur_speed;
625 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
631 cerr << "delta = " << diff_secs
632 << " ct = " << _transport_speed
633 << " steps = " << steps
634 << " new speed = " << cur_speed
635 << " speed = " << step_speed
639 request_transport_speed (step_speed);
643 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
649 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
651 if (Config->get_mmc_control()) {
652 request_transport_speed(-8.0f);
657 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
659 if (Config->get_mmc_control()) {
660 request_transport_speed(8.0f);
665 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
667 if (!Config->get_mmc_control()) {
671 nframes_t target_frame;
672 Timecode::Time timecode;
674 timecode.hours = mmc_tc[0] & 0xf;
675 timecode.minutes = mmc_tc[1];
676 timecode.seconds = mmc_tc[2];
677 timecode.frames = mmc_tc[3];
678 timecode.rate = timecode_frames_per_second();
679 timecode.drop = timecode_drop_frames();
681 // Also takes timecode offset into account:
682 timecode_to_sample( timecode, target_frame, true /* use_offset */, false /* use_subframes */ );
684 if (target_frame > max_frames) {
685 target_frame = max_frames;
688 /* Some (all?) MTC/MMC devices do not send a full MTC frame
689 at the end of a locate, instead sending only an MMC
690 locate command. This causes the current position
691 of an MTC slave to become out of date. Catch this.
694 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
697 // cerr << "Locate *with* MTC slave\n";
698 mtcs->handle_locate (mmc_tc);
700 // cerr << "Locate without MTC slave\n";
701 request_locate (target_frame, false);
706 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
708 if (!Config->get_mmc_control()) {
712 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
713 speed *= Config->get_shuttle_speed_factor();
717 request_transport_speed (speed);
719 request_transport_speed (-speed);
724 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
726 if (Config->get_mmc_control()) {
728 RouteList::iterator i;
729 boost::shared_ptr<RouteList> r = routes.reader();
731 for (i = r->begin(); i != r->end(); ++i) {
734 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
735 if (trk == at->remote_control_id()) {
736 at->set_record_enable (enabled, &mmc);
745 Session::change_midi_ports ()
747 MIDIRequest* request = new MIDIRequest;
749 request->type = MIDIRequest::PortChange;
750 midi_requests.write (&request, 1);
754 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
755 * This resets the MTC code, the next quarter frame message that is sent will be
756 * the first one with the beginning of this cycle as the new start point.
759 Session::send_full_time_code(nframes_t /*nframes*/)
761 /* This function could easily send at a given frame offset, but would
762 * that be useful? Does ardour do sub-block accurate locating? [DR] */
765 Timecode::Time timecode;
767 _send_timecode_update = false;
769 if (_mtc_port == 0 || !session_send_mtc) {
773 // Get timecode time for this transport frame
774 sample_to_timecode(_transport_frame, timecode, true /* use_offset */, false /* no subframes */);
776 transmitting_timecode_time = timecode;
777 outbound_mtc_timecode_frame = _transport_frame;
779 // I don't understand this bit yet.. [DR]
780 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
781 // start MTC quarter frame transmission on an even frame
782 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
783 outbound_mtc_timecode_frame += (nframes_t) _frames_per_timecode_frame;
786 // Compensate for audio latency
787 outbound_mtc_timecode_frame += _worst_output_latency;
789 next_quarter_frame_to_send = 0;
791 // Sync slave to the same Timecode time as we are on
799 msg[5] = mtc_timecode_bits | timecode.hours;
800 msg[6] = timecode.minutes;
801 msg[7] = timecode.seconds;
802 msg[8] = timecode.frames;
804 cerr << "MTC: Sending full time code at " << outbound_mtc_timecode_frame << endl;
806 // Send message at offset 0, sent time is for the start of this cycle
807 if (_mtc_port->midimsg (msg, sizeof (msg), 0)) {
808 error << _("Session: could not send full MIDI time code") << endmsg;
815 /** Send MTC (quarter-frame) messages for this cycle.
816 * Must be called exactly once per cycle from the audio thread. Realtime safe.
817 * This function assumes the state of full Timecode is sane, eg. the slave is
818 * expecting quarter frame messages and has the right frame of reference (any
819 * full MTC Timecode time messages that needed to be sent should have been sent
820 * earlier already this cycle by send_full_time_code)
823 Session::send_midi_time_code_for_cycle(nframes_t nframes)
825 assert (next_quarter_frame_to_send >= 0);
826 assert (next_quarter_frame_to_send <= 7);
828 if (_mtc_port == 0 || !session_send_mtc || transmitting_timecode_time.negative
829 /*|| (next_quarter_frame_to_send < 0)*/ ) {
830 // cerr << "(MTC) Not sending MTC\n";
834 /* Duration of one quarter frame */
835 nframes_t quarter_frame_duration = ((long) _frames_per_timecode_frame) >> 2;
837 DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 NQ %3 FD %\4n", _transport_frame, outbound_mtc_timecode_frame,
838 next_quarter_frame_to_send, quarter_frame_duration));
840 // FIXME: this should always be true
841 //assert((outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))
842 // > _transport_frame);
845 // Send quarter frames for this cycle
846 while (_transport_frame + nframes > (outbound_mtc_timecode_frame +
847 (next_quarter_frame_to_send * quarter_frame_duration))) {
849 DEBUG_TRACE (DEBUG::MTC, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send));
851 switch (next_quarter_frame_to_send) {
853 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
856 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
859 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
862 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
865 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
868 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
871 mtc_msg[1] = 0x60 | ((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf);
874 mtc_msg[1] = 0x70 | (((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf0) >> 4);
878 const nframes_t msg_time = (outbound_mtc_timecode_frame
879 + (quarter_frame_duration * next_quarter_frame_to_send));
881 // This message must fall within this block or something is broken
882 assert(msg_time >= _transport_frame);
883 assert(msg_time < _transport_frame + nframes);
885 nframes_t out_stamp = msg_time - _transport_frame;
886 assert(out_stamp < nframes);
888 if (_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
889 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
895 DEBUG_STR_SET(foo,"sending ");
896 DEBUG_STR(foo) << transmitting_timecode_time;
897 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
901 // Increment quarter frame counter
902 next_quarter_frame_to_send++;
904 if (next_quarter_frame_to_send >= 8) {
905 // Wrap quarter frame counter
906 next_quarter_frame_to_send = 0;
907 // Increment timecode time twice
908 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
909 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
910 // Re-calculate timing of first quarter frame
911 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
912 outbound_mtc_timecode_frame += 8 * quarter_frame_duration;
913 // Compensate for audio latency
914 outbound_mtc_timecode_frame += _worst_output_latency;
921 /***********************************************************************
923 **********************************************************************/
926 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
928 using namespace MIDI;
930 Timecode::Time timecode;
932 if (_mmc_port == 0 || !session_send_mmc) {
933 // cerr << "Not delivering MMC " << _mmc_port << " - " << session_send_mmc << endl;
937 mmc_buffer[nbytes++] = cmd;
939 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
942 case MachineControl::cmdLocate:
943 timecode_time_subframes (where, timecode);
945 mmc_buffer[nbytes++] = 0x6; // byte count
946 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
947 mmc_buffer[nbytes++] = timecode.hours;
948 mmc_buffer[nbytes++] = timecode.minutes;
949 mmc_buffer[nbytes++] = timecode.seconds;
950 mmc_buffer[nbytes++] = timecode.frames;
951 mmc_buffer[nbytes++] = timecode.subframes;
954 case MachineControl::cmdStop:
957 case MachineControl::cmdPlay:
958 /* always convert Play into Deferred Play */
960 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
963 case MachineControl::cmdDeferredPlay:
966 case MachineControl::cmdRecordStrobe:
969 case MachineControl::cmdRecordExit:
972 case MachineControl::cmdRecordPause:
981 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
983 if (_mmc_port->midimsg (mmc_buffer, nbytes, 0)) {
984 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
990 Session::mmc_step_timeout ()
995 gettimeofday (&now, 0);
997 timersub (&now, &last_mmc_step, &diff);
998 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1000 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1001 /* too long or too slow, stop transport */
1002 request_transport_speed (0.0);
1003 step_queued = false;
1007 if (diff_usecs < 250000.0) {
1008 /* too short, just keep going */
1014 request_transport_speed (_transport_speed * 0.75);
1018 /*---------------------------------------------------------------------------
1020 ---------------------------------------------------------------------------*/
1023 Session::start_midi_thread ()
1025 if (pipe (midi_request_pipe)) {
1026 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1030 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1031 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1035 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1036 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1040 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1041 error << _("Session: could not create transport thread") << endmsg;
1049 Session::terminate_midi_thread ()
1053 MIDIRequest* request = new MIDIRequest;
1056 request->type = MIDIRequest::Quit;
1058 midi_requests.write (&request, 1);
1059 poke_midi_thread ();
1061 pthread_join (midi_thread, &status);
1066 Session::poke_midi_thread ()
1070 if (write (midi_request_pipe[1], &c, 1) != 1) {
1071 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1076 Session::_midi_thread_work (void* arg)
1078 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1079 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1081 ((Session *) arg)->midi_thread_work ();
1087 Session::midi_thread_work ()
1089 MIDIRequest* request;
1094 struct sched_param rtparam;
1097 vector<MIDI::Port*> ports;
1099 PBD::notify_gui_about_thread_creation (pthread_self(), X_("MIDI"), 2048);
1100 SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
1102 memset (&rtparam, 0, sizeof (rtparam));
1103 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1105 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1106 // do we care? not particularly.
1109 /* set up the port vector; 5 is the largest possible size for now */
1111 ports.assign (5, (MIDI::Port*) 0);
1113 GMainContext* main_context = g_main_context_new ();
1119 gpfd[nfds].fd = midi_request_pipe[0];
1120 gpfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1123 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1124 gpfd[nfds].fd = _mmc_port->selectable();
1125 gpfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1126 ports[nfds] = _mmc_port;
1127 g_main_context_add_poll (&gpfd[nfds]);
1128 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for mmc @ %2\n", nfds, _mmc_port));
1132 /* if MTC is being handled on a different port from MMC
1133 or we are not handling MMC at all, poll
1137 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1138 gpfd[nfds].fd = _mtc_port->selectable();
1139 gpfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1140 ports[nfds] = _mtc_port;
1141 g_main_context_add_poll (&gpfd[nfds]);
1142 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for mtc @ %2\n", nfds, _mtc_port));
1146 if (_midi_clock_port && (_midi_clock_port != _mmc_port || !Config->get_mmc_control()) && _midi_clock_port->selectable() >= 0) {
1147 gpfd[nfds].fd = _midi_clock_port->selectable();
1148 gpfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1149 ports[nfds] = _midi_clock_port;
1150 g_main_context_add_poll (&gpfd[nfds]);
1151 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for midi clock @ %2\n", nfds, _midi_clock_port));
1155 /* if we are using MMC control, we obviously have to listen
1159 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1160 gpfd[nfds].fd = _midi_port->selectable();
1161 gpfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1162 ports[nfds] = _midi_port;
1163 g_main_context_add_poll (&gpfd[nfds]);
1164 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for midi @ %2\n", nfds, _midi_port));
1168 if (!midi_timeouts.empty()) {
1169 timeout = 100; /* 10msecs */
1171 timeout = -1; /* if there is no data, we don't care */
1176 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("MIDI poll on %1 fds for %2\n", nfds, timeout));
1177 if (g_poll (gpfd, nfds, timeout) < 0) {
1178 if (errno == EINTR) {
1179 /* gdb at work, perhaps */
1183 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1188 nframes64_t now = engine().frame_time();
1190 DEBUG_TRACE (DEBUG::MidiIO, "MIDI thread awake\n");
1194 /* check the transport request pipe */
1196 if (gpfd[0].revents & ~POLLIN) {
1197 error << _("Error on transport thread request pipe") << endmsg;
1201 if (gpfd[0].revents & POLLIN) {
1205 DEBUG_TRACE (DEBUG::MidiIO, "MIDI request FIFO ready\n");
1208 /* empty the pipe of all current requests */
1211 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1214 if ((size_t) nread < sizeof (foo)) {
1219 } else if (nread == 0) {
1221 } else if (errno == EAGAIN) {
1224 fatal << _("Error reading from transport request pipe") << endmsg;
1229 while (midi_requests.read (&request, 1) == 1) {
1231 switch (request->type) {
1232 case MIDIRequest::PortChange:
1233 /* restart poll with new ports */
1234 DEBUG_TRACE (DEBUG::MidiIO, "rebind\n");
1238 case MIDIRequest::Quit:
1240 DEBUG_TRACE (DEBUG::MidiIO, "thread quit\n");
1241 pthread_exit_pbd (0);
1256 DEBUG_TRACE (DEBUG::MidiIO, "ports changed, restart poll\n");
1261 /* now read the rest of the ports */
1263 for (int p = 1; p < nfds; ++p) {
1265 DEBUG_STR_SET(foo, "port #%1 revents = ");
1266 DEBUG_STR(foo) << hex << pfd[p].revents << dec << endl;
1267 DEBUG_TRACE (DEBUG::MidiIO, string_compose (DEBUG_STR(foo).str(), p));
1269 if ((pfd[p].revents & ~POLLIN)) {
1270 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1274 if (pfd[p].revents & POLLIN) {
1275 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("MIDI fd # %1 has data ready @ %2\n", p, now));
1277 ports[p]->parse (now);
1280 g_main_context_remove_poll (&gpfd[p]);
1283 /* timeout driven */
1285 if (fds_ready < 2 && timeout != -1) {
1287 DEBUG_TRACE (DEBUG::MidiIO, "Check timeouts\n");
1288 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1290 MidiTimeoutList::iterator tmp;
1295 midi_timeouts.erase (i);
1307 Session::midi_thread_work ()
1309 MIDIRequest* request;
1310 struct pollfd pfd[4];
1314 struct sched_param rtparam;
1317 vector<MIDI::Port*> ports;
1319 PBD::notify_gui_about_thread_creation (pthread_self(), X_("MIDI"), 2048);
1320 SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
1322 memset (&rtparam, 0, sizeof (rtparam));
1323 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1325 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1326 // do we care? not particularly.
1329 /* set up the port vector; 5 is the largest possible size for now */
1331 ports.assign (5, (MIDI::Port*) 0);
1337 pfd[nfds].fd = midi_request_pipe[0];
1338 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1341 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1342 pfd[nfds].fd = _mmc_port->selectable();
1343 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1344 ports[nfds] = _mmc_port;
1345 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for mmc @ %2\n", nfds, _mmc_port));
1349 /* if MTC is being handled on a different port from MMC
1350 or we are not handling MMC at all, poll
1354 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1355 pfd[nfds].fd = _mtc_port->selectable();
1356 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1357 ports[nfds] = _mtc_port;
1358 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for mtc @ %2\n", nfds, _mtc_port));
1362 if (_midi_clock_port && (_midi_clock_port != _mmc_port || !Config->get_mmc_control()) && _midi_clock_port->selectable() >= 0) {
1363 pfd[nfds].fd = _midi_clock_port->selectable();
1364 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1365 ports[nfds] = _midi_clock_port;
1366 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for midi clock @ %2\n", nfds, _midi_clock_port));
1370 /* if we are using MMC control, we obviously have to listen
1374 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1375 pfd[nfds].fd = _midi_port->selectable();
1376 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1377 ports[nfds] = _midi_port;
1378 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("set up port #%1 for midi @ %2\n", nfds, _midi_port));
1382 if (!midi_timeouts.empty()) {
1383 timeout = 100; /* 10msecs */
1385 timeout = -1; /* if there is no data, we don't care */
1389 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("MIDI poll on %1 fds for %2\n", nfds, timeout));
1390 if (poll (pfd, nfds, timeout) < 0) {
1391 if (errno == EINTR) {
1392 /* gdb at work, perhaps */
1396 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1401 nframes64_t now = engine().frame_time();
1403 DEBUG_TRACE (DEBUG::MidiIO, "MIDI thread awake\n");
1407 /* check the transport request pipe */
1409 if (pfd[0].revents & ~POLLIN) {
1410 error << _("Error on transport thread request pipe") << endmsg;
1414 if (pfd[0].revents & POLLIN) {
1418 DEBUG_TRACE (DEBUG::MidiIO, "MIDI request FIFO ready\n");
1421 /* empty the pipe of all current requests */
1424 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1427 if ((size_t) nread < sizeof (foo)) {
1432 } else if (nread == 0) {
1434 } else if (errno == EAGAIN) {
1437 fatal << _("Error reading from transport request pipe") << endmsg;
1442 while (midi_requests.read (&request, 1) == 1) {
1444 switch (request->type) {
1445 case MIDIRequest::PortChange:
1446 /* restart poll with new ports */
1447 DEBUG_TRACE (DEBUG::MidiIO, "rebind\n");
1451 case MIDIRequest::Quit:
1453 DEBUG_TRACE (DEBUG::MidiIO, "thread quit\n");
1454 pthread_exit_pbd (0);
1469 DEBUG_TRACE (DEBUG::MidiIO, "ports changed, restart poll\n");
1474 /* now read the rest of the ports */
1476 for (int p = 1; p < nfds; ++p) {
1479 DEBUG_STR_SET(foo, "port #%1 revents = ");
1480 DEBUG_STR(foo) << hex << pfd[p].revents << dec << endl;
1481 DEBUG_TRACE (DEBUG::MidiIO, string_compose (DEBUG_STR(foo).str(), p));
1484 if ((pfd[p].revents & ~POLLIN)) {
1485 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1489 if (pfd[p].revents & POLLIN) {
1490 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("MIDI fd # %1 has data ready @ %2\n", p, now));
1492 ports[p]->parse (now);
1496 /* timeout driven */
1498 if (fds_ready < 2 && timeout != -1) {
1500 DEBUG_TRACE (DEBUG::MidiIO, "Check timeouts\n");
1501 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1503 MidiTimeoutList::iterator tmp;
1508 midi_timeouts.erase (i);