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/audioengine.h>
40 #include <ardour/session.h>
41 #include <ardour/audio_track.h>
42 #include <ardour/midi_track.h>
43 #include <ardour/audio_diskstream.h>
44 #include <ardour/slave.h>
45 #include <ardour/cycles.h>
46 #include <ardour/smpte.h>
51 using namespace ARDOUR;
55 MachineControl::CommandSignature MMC_CommandSignature;
56 MachineControl::ResponseSignature MMC_ResponseSignature;
63 boost::shared_ptr<RouteList> r = routes.reader ();
65 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
66 MidiTrack *track = dynamic_cast<MidiTrack*>((*i).get());
75 Session::use_config_midi_ports ()
79 if (default_mmc_port) {
80 set_mmc_port (default_mmc_port->name());
85 if (default_mtc_port) {
86 set_mtc_port (default_mtc_port->name());
91 if (default_midi_port) {
92 set_midi_port (default_midi_port->name());
97 if (default_midi_clock_port) {
98 set_midi_clock_port (default_midi_clock_port->name());
100 set_midi_clock_port ("");
107 /***********************************************************************
109 **********************************************************************/
112 Session::set_mtc_port (string port_tag)
116 if (port_tag.length() == 0) {
118 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
119 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
123 if (_mtc_port == 0) {
133 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
134 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
140 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
144 Config->set_mtc_port_name (port_tag);
147 MTC_PortChanged(); /* EMIT SIGNAL */
148 change_midi_ports ();
154 Session::set_mmc_receive_device_id (uint32_t device_id)
157 mmc->set_receive_device_id (device_id);
162 Session::set_mmc_send_device_id (uint32_t device_id)
165 mmc->set_send_device_id (device_id);
170 Session::set_mmc_port (string port_tag)
172 MIDI::byte old_recv_device_id = 0;
173 MIDI::byte old_send_device_id = 0;
174 bool reset_id = false;
176 if (port_tag.length() == 0) {
177 if (_mmc_port == 0) {
186 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
193 old_recv_device_id = mmc->receive_device_id();
194 old_recv_device_id = mmc->send_device_id();
199 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
200 MMC_CommandSignature,
201 MMC_ResponseSignature);
204 mmc->set_receive_device_id (old_recv_device_id);
205 mmc->set_send_device_id (old_send_device_id);
209 (mem_fun (*this, &Session::mmc_deferred_play));
210 mmc->DeferredPlay.connect
211 (mem_fun (*this, &Session::mmc_deferred_play));
213 (mem_fun (*this, &Session::mmc_stop));
214 mmc->FastForward.connect
215 (mem_fun (*this, &Session::mmc_fast_forward));
217 (mem_fun (*this, &Session::mmc_rewind));
219 (mem_fun (*this, &Session::mmc_pause));
220 mmc->RecordPause.connect
221 (mem_fun (*this, &Session::mmc_record_pause));
222 mmc->RecordStrobe.connect
223 (mem_fun (*this, &Session::mmc_record_strobe));
224 mmc->RecordExit.connect
225 (mem_fun (*this, &Session::mmc_record_exit));
227 (mem_fun (*this, &Session::mmc_locate));
229 (mem_fun (*this, &Session::mmc_step));
231 (mem_fun (*this, &Session::mmc_shuttle));
232 mmc->TrackRecordStatusChange.connect
233 (mem_fun (*this, &Session::mmc_record_enable));
236 /* also handle MIDI SPP because its so common */
238 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
239 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
240 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
242 Config->set_mmc_port_name (port_tag);
245 MMC_PortChanged(); /* EMIT SIGNAL */
246 change_midi_ports ();
252 Session::set_midi_port (string port_tag)
255 if (port_tag.length() == 0) {
256 if (_midi_port == 0) {
265 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
271 /* XXX need something to forward this to control protocols ? or just
275 Config->set_midi_port_name (port_tag);
279 MIDI_PortChanged(); /* EMIT SIGNAL */
280 change_midi_ports ();
286 Session::set_midi_clock_port (string port_tag)
290 if (port_tag.length() == 0) {
292 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
293 error << _("Ardour is slaved to MIDI Clock - port cannot be reset") << endmsg;
297 if (_midi_clock_port == 0) {
301 _midi_clock_port = 0;
307 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
308 error << string_compose (_("unknown port %1 requested for MIDI Clock"), port_tag) << endl;
312 _midi_clock_port = port;
314 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
318 Config->set_midi_clock_port_name (port_tag);
320 _midi_clock_port->input()->start.connect (mem_fun (*this, &Session::midi_clock_start));
321 _midi_clock_port->input()->contineu.connect (mem_fun (*this, &Session::midi_clock_continue));
322 _midi_clock_port->input()->stop.connect (mem_fun (*this, &Session::midi_clock_stop));
325 MIDIClock_PortChanged(); /* EMIT SIGNAL */
326 change_midi_ports ();
332 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
334 MIDI::Parser* input_parser;
336 cerr << "enabling tracing: " << yn << " for input port " << port->name() << endl;
339 if ((input_parser = port->input()) != 0) {
340 input_parser->trace (yn, &cout, "input: ");
345 if ((input_parser = _mmc_port->input()) != 0) {
346 input_parser->trace (yn, &cout, "input: ");
350 if (_mtc_port && _mtc_port != _mmc_port) {
351 if ((input_parser = _mtc_port->input()) != 0) {
352 input_parser->trace (yn, &cout, "input: ");
356 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
357 if ((input_parser = _midi_port->input()) != 0) {
358 input_parser->trace (yn, &cout, "input: ");
363 && _midi_clock_port != _mmc_port
364 && _midi_clock_port != _mtc_port
365 && _midi_clock_port != _midi_port) {
366 if ((input_parser = _midi_clock_port->input()) != 0) {
367 input_parser->trace (yn, &cout, "input: ");
372 Config->set_trace_midi_input (yn);
376 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
378 MIDI::Parser* output_parser;
381 if ((output_parser = port->output()) != 0) {
382 output_parser->trace (yn, &cout, "output: ");
386 if ((output_parser = _mmc_port->output()) != 0) {
387 output_parser->trace (yn, &cout, "output: ");
391 if (_mtc_port && _mtc_port != _mmc_port) {
392 if ((output_parser = _mtc_port->output()) != 0) {
393 output_parser->trace (yn, &cout, "output: ");
397 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
398 if ((output_parser = _midi_port->output()) != 0) {
399 output_parser->trace (yn, &cout, "output: ");
405 Config->set_trace_midi_output (yn);
409 Session::get_trace_midi_input(MIDI::Port *port)
411 MIDI::Parser* input_parser;
413 if ((input_parser = port->input()) != 0) {
414 return input_parser->tracing();
419 if ((input_parser = _mmc_port->input()) != 0) {
420 return input_parser->tracing();
425 if ((input_parser = _mtc_port->input()) != 0) {
426 return input_parser->tracing();
431 if ((input_parser = _midi_port->input()) != 0) {
432 return input_parser->tracing();
441 Session::get_trace_midi_output(MIDI::Port *port)
443 MIDI::Parser* output_parser;
445 if ((output_parser = port->output()) != 0) {
446 return output_parser->tracing();
451 if ((output_parser = _mmc_port->output()) != 0) {
452 return output_parser->tracing();
457 if ((output_parser = _mtc_port->output()) != 0) {
458 return output_parser->tracing();
463 if ((output_parser = _midi_port->output()) != 0) {
464 return output_parser->tracing();
474 Session::setup_midi_control ()
476 outbound_mtc_smpte_frame = 0;
477 next_quarter_frame_to_send = 0;
479 /* setup the MMC buffer */
481 mmc_buffer[0] = 0xf0; // SysEx
482 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
483 mmc_buffer[2] = (mmc ? mmc->send_device_id() : 0x7f);
484 mmc_buffer[3] = 0x6; // MCC
486 /* Set up the qtr frame message */
499 Session::spp_start (Parser& ignored)
501 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
502 request_transport_speed (1.0);
507 Session::spp_continue (Parser& ignored)
513 Session::spp_stop (Parser& ignored)
515 if (Config->get_mmc_control()) {
521 Session::midi_clock_start (Parser& ignored)
523 if (Config->get_midi_clock_control() && (Config->get_slave_source() == MIDIClock)) {
524 request_transport_speed (1.0);
529 Session::midi_clock_continue (Parser& ignored)
531 midi_clock_start (ignored);
535 Session::midi_clock_stop (Parser& ignored)
537 if (Config->get_midi_clock_control()) {
543 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
545 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
546 request_transport_speed (1.0);
551 Session::mmc_record_pause (MIDI::MachineControl &mmc)
553 if (Config->get_mmc_control()) {
554 maybe_enable_record();
559 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
561 if (!Config->get_mmc_control())
564 /* record strobe does an implicit "Play" command */
566 if (_transport_speed != 1.0) {
568 /* start_transport() will move from Enabled->Recording, so we
569 don't need to do anything here except enable recording.
570 its not the same as maybe_enable_record() though, because
571 that *can* switch to Recording, which we do not want.
574 save_state ("", true);
575 g_atomic_int_set (&_record_status, Enabled);
576 RecordStateChanged (); /* EMIT SIGNAL */
578 request_transport_speed (1.0);
587 Session::mmc_record_exit (MIDI::MachineControl &mmc)
589 if (Config->get_mmc_control()) {
590 disable_record (false);
595 Session::mmc_stop (MIDI::MachineControl &mmc)
597 if (Config->get_mmc_control()) {
603 Session::mmc_pause (MIDI::MachineControl &mmc)
605 if (Config->get_mmc_control()) {
607 /* We support RECORD_PAUSE, so the spec says that
608 we must interpret PAUSE like RECORD_PAUSE if
612 if (actively_recording()) {
613 maybe_enable_record ();
620 static bool step_queued = false;
623 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
625 if (!Config->get_mmc_control()) {
630 struct timeval diff = { 0, 0 };
632 gettimeofday (&now, 0);
634 timersub (&now, &last_mmc_step, &diff);
636 gettimeofday (&now, 0);
637 timersub (&now, &last_mmc_step, &diff);
639 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
643 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
644 double cur_speed = (((steps * 0.5) * smpte_frames_per_second()) / diff_secs) / smpte_frames_per_second();
646 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
647 /* change direction */
648 step_speed = cur_speed;
650 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
656 cerr << "delta = " << diff_secs
657 << " ct = " << _transport_speed
658 << " steps = " << steps
659 << " new speed = " << cur_speed
660 << " speed = " << step_speed
664 request_transport_speed (step_speed);
668 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
674 Session::mmc_rewind (MIDI::MachineControl &mmc)
676 if (Config->get_mmc_control()) {
677 request_transport_speed(-8.0f);
682 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
684 if (Config->get_mmc_control()) {
685 request_transport_speed(8.0f);
690 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
692 if (!Config->get_mmc_control()) {
696 nframes_t target_frame;
699 smpte.hours = mmc_tc[0] & 0xf;
700 smpte.minutes = mmc_tc[1];
701 smpte.seconds = mmc_tc[2];
702 smpte.frames = mmc_tc[3];
703 smpte.rate = smpte_frames_per_second();
704 smpte.drop = smpte_drop_frames();
706 // Also takes smpte offset into account:
707 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
709 if (target_frame > max_frames) {
710 target_frame = max_frames;
713 /* Some (all?) MTC/MMC devices do not send a full MTC frame
714 at the end of a locate, instead sending only an MMC
715 locate command. This causes the current position
716 of an MTC slave to become out of date. Catch this.
719 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
722 // cerr << "Locate *with* MTC slave\n";
723 mtcs->handle_locate (mmc_tc);
725 // cerr << "Locate without MTC slave\n";
726 request_locate (target_frame, false);
731 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
733 if (!Config->get_mmc_control()) {
737 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
738 speed *= Config->get_shuttle_speed_factor();
742 request_transport_speed (speed);
744 request_transport_speed (-speed);
749 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
751 if (Config->get_mmc_control()) {
753 RouteList::iterator i;
754 boost::shared_ptr<RouteList> r = routes.reader();
756 for (i = r->begin(); i != r->end(); ++i) {
759 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
760 if (trk == at->remote_control_id()) {
761 at->set_record_enable (enabled, &mmc);
770 Session::change_midi_ports ()
772 MIDIRequest* request = new MIDIRequest;
774 request->type = MIDIRequest::PortChange;
775 midi_requests.write (&request, 1);
779 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
780 * This resets the MTC code, the next quarter frame message that is sent will be
781 * the first one with the beginning of this cycle as the new start point.
785 Session::send_full_time_code(nframes_t nframes)
787 /* This function could easily send at a given frame offset, but would
788 * that be useful? Does ardour do sub-block accurate locating? [DR] */
793 _send_smpte_update = false;
795 if (_mtc_port == 0 || !session_send_mtc) {
799 // Get smpte time for this transport frame
800 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
802 transmitting_smpte_time = smpte;
803 outbound_mtc_smpte_frame = _transport_frame;
805 // I don't understand this bit yet.. [DR]
806 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
807 // start MTC quarter frame transmission on an even frame
808 SMPTE::increment( transmitting_smpte_time );
809 outbound_mtc_smpte_frame += (nframes_t) _frames_per_smpte_frame;
812 // Compensate for audio latency
813 outbound_mtc_smpte_frame += _worst_output_latency;
815 next_quarter_frame_to_send = 0;
817 // Sync slave to the same SMPTE time as we are on
825 msg[5] = mtc_smpte_bits | smpte.hours;
826 msg[6] = smpte.minutes;
827 msg[7] = smpte.seconds;
828 msg[8] = smpte.frames;
830 cerr << "MTC: Sending full time code at " << outbound_mtc_smpte_frame << endl;
832 // Send message at offset 0, sent time is for the start of this cycle
833 if (_mtc_port->midimsg (msg, sizeof (msg), 0)) {
834 error << _("Session: could not send full MIDI time code") << endmsg;
842 /** Sends MTC (quarter-frame) messages for this cycle.
843 * Must be called exactly once per cycle from the audio thread. Realtime safe.
844 * This function assumes the state of full SMPTE is sane, eg. the slave is
845 * expecting quarter frame messages and has the right frame of reference (any
846 * full MTC SMPTE time messages that needed to be sent should have been sent
847 * earlier already this cycle by send_full_time_code)
850 Session::send_midi_time_code_for_cycle(nframes_t nframes)
852 assert (next_quarter_frame_to_send >= 0);
853 assert (next_quarter_frame_to_send <= 7);
855 if (_mtc_port == 0 || !session_send_mtc || transmitting_smpte_time.negative
856 /*|| (next_quarter_frame_to_send < 0)*/ ) {
857 // cerr << "(MTC) Not sending MTC\n";
861 /* Duration of one quarter frame */
862 nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
864 // cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
865 // << " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
867 // FIXME: this should always be true
868 //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
869 // > _transport_frame);
872 // Send quarter frames for this cycle
873 while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
874 (next_quarter_frame_to_send * quarter_frame_duration))) {
876 // cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
878 switch (next_quarter_frame_to_send) {
880 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
883 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
886 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
889 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
892 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
895 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
898 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
901 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
905 const nframes_t msg_time = (outbound_mtc_smpte_frame
906 + (quarter_frame_duration * next_quarter_frame_to_send));
908 // This message must fall within this block or something is broken
909 assert(msg_time >= _transport_frame);
910 assert(msg_time < _transport_frame + nframes);
912 nframes_t out_stamp = msg_time - _transport_frame;
913 assert(out_stamp < nframes);
915 if (_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
916 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
921 /*cerr << "(MTC) SMPTE: " << transmitting_smpte_time.hours
922 << ":" << transmitting_smpte_time.minutes
923 << ":" << transmitting_smpte_time.seconds
924 << ":" << transmitting_smpte_time.frames
925 << ", qfm = " << next_quarter_frame_to_send
926 << ", stamp = " << out_stamp
927 << ", delta = " << _transport_frame + out_stamp - last_time << endl;*/
929 // Increment quarter frame counter
930 next_quarter_frame_to_send++;
932 if (next_quarter_frame_to_send >= 8) {
933 // Wrap quarter frame counter
934 next_quarter_frame_to_send = 0;
935 // Increment smpte time twice
936 SMPTE::increment( transmitting_smpte_time );
937 SMPTE::increment( transmitting_smpte_time );
938 // Re-calculate timing of first quarter frame
939 //smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
940 outbound_mtc_smpte_frame += 8 * quarter_frame_duration;
941 // Compensate for audio latency
942 outbound_mtc_smpte_frame += _worst_output_latency;
949 /***********************************************************************
951 **********************************************************************/
954 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
956 using namespace MIDI;
960 if (_mmc_port == 0 || !session_send_mmc) {
961 // cerr << "Not delivering MMC " << _mmc_port << " - " << session_send_mmc << endl;
965 mmc_buffer[nbytes++] = cmd;
967 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
970 case MachineControl::cmdLocate:
971 smpte_time_subframes (where, smpte);
973 mmc_buffer[nbytes++] = 0x6; // byte count
974 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
975 mmc_buffer[nbytes++] = smpte.hours;
976 mmc_buffer[nbytes++] = smpte.minutes;
977 mmc_buffer[nbytes++] = smpte.seconds;
978 mmc_buffer[nbytes++] = smpte.frames;
979 mmc_buffer[nbytes++] = smpte.subframes;
982 case MachineControl::cmdStop:
985 case MachineControl::cmdPlay:
986 /* always convert Play into Deferred Play */
988 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
991 case MachineControl::cmdDeferredPlay:
994 case MachineControl::cmdRecordStrobe:
997 case MachineControl::cmdRecordExit:
1000 case MachineControl::cmdRecordPause:
1009 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
1011 if (_mmc_port->midimsg (mmc_buffer, nbytes, 0)) {
1012 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
1018 Session::mmc_step_timeout ()
1021 struct timeval diff;
1023 gettimeofday (&now, 0);
1025 timersub (&now, &last_mmc_step, &diff);
1026 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1028 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1029 /* too long or too slow, stop transport */
1030 request_transport_speed (0.0);
1031 step_queued = false;
1035 if (diff_usecs < 250000.0) {
1036 /* too short, just keep going */
1042 request_transport_speed (_transport_speed * 0.75);
1046 /*---------------------------------------------------------------------------
1048 ---------------------------------------------------------------------------*/
1051 Session::start_midi_thread ()
1053 if (pipe (midi_request_pipe)) {
1054 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1058 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1059 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1063 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1064 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1068 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1069 error << _("Session: could not create transport thread") << endmsg;
1077 Session::terminate_midi_thread ()
1081 MIDIRequest* request = new MIDIRequest;
1084 request->type = MIDIRequest::Quit;
1086 midi_requests.write (&request, 1);
1087 poke_midi_thread ();
1089 pthread_join (midi_thread, &status);
1094 Session::poke_midi_thread ()
1098 if (write (midi_request_pipe[1], &c, 1) != 1) {
1099 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1104 Session::_midi_thread_work (void* arg)
1106 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1107 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1109 ((Session *) arg)->midi_thread_work ();
1114 Session::midi_thread_work ()
1116 MIDIRequest* request;
1117 struct pollfd pfd[4];
1121 struct sched_param rtparam;
1124 vector<MIDI::Port*> ports;
1126 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1128 memset (&rtparam, 0, sizeof (rtparam));
1129 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1131 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1132 // do we care? not particularly.
1135 /* set up the port vector; 5 is the largest possible size for now */
1137 ports.assign (5, (MIDI::Port*) 0);
1143 pfd[nfds].fd = midi_request_pipe[0];
1144 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1147 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1148 pfd[nfds].fd = _mmc_port->selectable();
1149 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1150 ports[nfds] = _mmc_port;
1154 /* if MTC is being handled on a different port from MMC
1155 or we are not handling MMC at all, poll
1159 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1160 pfd[nfds].fd = _mtc_port->selectable();
1161 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1162 ports[nfds] = _mtc_port;
1166 if (_midi_clock_port && (_midi_clock_port != _mmc_port || !Config->get_mmc_control()) && _midi_clock_port->selectable() >= 0) {
1167 pfd[nfds].fd = _midi_clock_port->selectable();
1168 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1169 ports[nfds] = _midi_clock_port;
1173 /* if we are using MMC control, we obviously have to listen
1177 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1178 pfd[nfds].fd = _midi_port->selectable();
1179 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1180 ports[nfds] = _midi_port;
1184 if (!midi_timeouts.empty()) {
1185 timeout = 100; /* 10msecs */
1187 timeout = -1; /* if there is no data, we don't care */
1191 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1192 if (poll (pfd, nfds, timeout) < 0) {
1193 if (errno == EINTR) {
1194 /* gdb at work, perhaps */
1198 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1202 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1206 /* check the transport request pipe */
1208 if (pfd[0].revents & ~POLLIN) {
1209 error << _("Error on transport thread request pipe") << endmsg;
1213 if (pfd[0].revents & POLLIN) {
1217 // cerr << "MIDI request FIFO ready\n";
1220 /* empty the pipe of all current requests */
1223 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1226 if ((size_t) nread < sizeof (foo)) {
1231 } else if (nread == 0) {
1233 } else if (errno == EAGAIN) {
1236 fatal << _("Error reading from transport request pipe") << endmsg;
1241 while (midi_requests.read (&request, 1) == 1) {
1243 switch (request->type) {
1244 case MIDIRequest::PortChange:
1245 /* restart poll with new ports */
1246 // cerr << "rebind\n";
1250 case MIDIRequest::Quit:
1252 pthread_exit_pbd (0);
1270 /* now read the rest of the ports */
1272 for (int p = 1; p < nfds; ++p) {
1273 if ((pfd[p].revents & ~POLLIN)) {
1274 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1278 if (pfd[p].revents & POLLIN) {
1284 /* timeout driven */
1286 if (fds_ready < 2 && timeout != -1) {
1288 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1290 MidiTimeoutList::iterator tmp;
1295 midi_timeouts.erase (i);