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>
31 #include "midi++/mmc.h"
32 #include "midi++/port.h"
33 #include "midi++/manager.h"
34 #include "pbd/error.h"
35 #include <glibmm/thread.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))
894 DEBUG_STR_SET(foo,"sending ");
895 DEBUG_STR(foo) << transmitting_timecode_time;
896 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
899 // Increment quarter frame counter
900 next_quarter_frame_to_send++;
902 if (next_quarter_frame_to_send >= 8) {
903 // Wrap quarter frame counter
904 next_quarter_frame_to_send = 0;
905 // Increment timecode time twice
906 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
907 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
908 // Re-calculate timing of first quarter frame
909 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
910 outbound_mtc_timecode_frame += 8 * quarter_frame_duration;
911 // Compensate for audio latency
912 outbound_mtc_timecode_frame += _worst_output_latency;
919 /***********************************************************************
921 **********************************************************************/
924 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
926 using namespace MIDI;
928 Timecode::Time timecode;
930 if (_mmc_port == 0 || !session_send_mmc) {
931 // cerr << "Not delivering MMC " << _mmc_port << " - " << session_send_mmc << endl;
935 mmc_buffer[nbytes++] = cmd;
937 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
940 case MachineControl::cmdLocate:
941 timecode_time_subframes (where, timecode);
943 mmc_buffer[nbytes++] = 0x6; // byte count
944 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
945 mmc_buffer[nbytes++] = timecode.hours;
946 mmc_buffer[nbytes++] = timecode.minutes;
947 mmc_buffer[nbytes++] = timecode.seconds;
948 mmc_buffer[nbytes++] = timecode.frames;
949 mmc_buffer[nbytes++] = timecode.subframes;
952 case MachineControl::cmdStop:
955 case MachineControl::cmdPlay:
956 /* always convert Play into Deferred Play */
958 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
961 case MachineControl::cmdDeferredPlay:
964 case MachineControl::cmdRecordStrobe:
967 case MachineControl::cmdRecordExit:
970 case MachineControl::cmdRecordPause:
979 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
981 if (_mmc_port->midimsg (mmc_buffer, nbytes, 0)) {
982 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
988 Session::mmc_step_timeout ()
993 gettimeofday (&now, 0);
995 timersub (&now, &last_mmc_step, &diff);
996 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
998 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
999 /* too long or too slow, stop transport */
1000 request_transport_speed (0.0);
1001 step_queued = false;
1005 if (diff_usecs < 250000.0) {
1006 /* too short, just keep going */
1012 request_transport_speed (_transport_speed * 0.75);
1016 /*---------------------------------------------------------------------------
1018 ---------------------------------------------------------------------------*/
1021 Session::start_midi_thread ()
1023 if (pipe (midi_request_pipe)) {
1024 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1028 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1029 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1033 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1034 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1038 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1039 error << _("Session: could not create transport thread") << endmsg;
1047 Session::terminate_midi_thread ()
1051 MIDIRequest* request = new MIDIRequest;
1054 request->type = MIDIRequest::Quit;
1056 midi_requests.write (&request, 1);
1057 poke_midi_thread ();
1059 pthread_join (midi_thread, &status);
1064 Session::poke_midi_thread ()
1068 if (write (midi_request_pipe[1], &c, 1) != 1) {
1069 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1074 Session::_midi_thread_work (void* arg)
1076 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1077 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1079 ((Session *) arg)->midi_thread_work ();
1084 Session::midi_thread_work ()
1086 MIDIRequest* request;
1087 struct pollfd pfd[4];
1091 struct sched_param rtparam;
1094 vector<MIDI::Port*> ports;
1096 PBD::notify_gui_about_thread_creation (pthread_self(), X_("MIDI"), 2048);
1098 memset (&rtparam, 0, sizeof (rtparam));
1099 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1101 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1102 // do we care? not particularly.
1105 /* set up the port vector; 5 is the largest possible size for now */
1107 ports.assign (5, (MIDI::Port*) 0);
1113 pfd[nfds].fd = midi_request_pipe[0];
1114 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1117 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1118 pfd[nfds].fd = _mmc_port->selectable();
1119 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1120 ports[nfds] = _mmc_port;
1121 //cerr << "MIDI port " << nfds << " = MMC @ " << _mmc_port << endl;
1125 /* if MTC is being handled on a different port from MMC
1126 or we are not handling MMC at all, poll
1130 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1131 pfd[nfds].fd = _mtc_port->selectable();
1132 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1133 ports[nfds] = _mtc_port;
1134 //cerr << "MIDI port " << nfds << " = MTC @ " << _mtc_port << endl;
1138 if (_midi_clock_port && (_midi_clock_port != _mmc_port || !Config->get_mmc_control()) && _midi_clock_port->selectable() >= 0) {
1139 pfd[nfds].fd = _midi_clock_port->selectable();
1140 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1141 ports[nfds] = _midi_clock_port;
1145 /* if we are using MMC control, we obviously have to listen
1149 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1150 pfd[nfds].fd = _midi_port->selectable();
1151 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1152 ports[nfds] = _midi_port;
1153 // cerr << "MIDI port " << nfds << " = MIDI @ " << _midi_port << endl;
1157 if (!midi_timeouts.empty()) {
1158 timeout = 100; /* 10msecs */
1160 timeout = -1; /* if there is no data, we don't care */
1164 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1165 if (poll (pfd, nfds, timeout) < 0) {
1166 if (errno == EINTR) {
1167 /* gdb at work, perhaps */
1171 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1175 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1179 /* check the transport request pipe */
1181 if (pfd[0].revents & ~POLLIN) {
1182 error << _("Error on transport thread request pipe") << endmsg;
1186 if (pfd[0].revents & POLLIN) {
1190 // cerr << "MIDI request FIFO ready\n";
1193 /* empty the pipe of all current requests */
1196 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1199 if ((size_t) nread < sizeof (foo)) {
1204 } else if (nread == 0) {
1206 } else if (errno == EAGAIN) {
1209 fatal << _("Error reading from transport request pipe") << endmsg;
1214 while (midi_requests.read (&request, 1) == 1) {
1216 switch (request->type) {
1217 case MIDIRequest::PortChange:
1218 /* restart poll with new ports */
1219 // cerr << "rebind\n";
1223 case MIDIRequest::Quit:
1225 pthread_exit_pbd (0);
1243 /* now read the rest of the ports */
1245 for (int p = 1; p < nfds; ++p) {
1246 if ((pfd[p].revents & ~POLLIN)) {
1247 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1251 if (pfd[p].revents & POLLIN) {
1257 /* timeout driven */
1259 if (fds_ready < 2 && timeout != -1) {
1261 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1263 MidiTimeoutList::iterator tmp;
1268 midi_timeouts.erase (i);