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 <midi++/mmc.h>
30 #include <midi++/port.h>
31 #include <midi++/manager.h>
32 #include <pbd/error.h>
33 #include <pbd/lockmonitor.h>
34 #include <pbd/pthread_utils.h>
36 #include <ardour/configuration.h>
37 #include <ardour/audioengine.h>
38 #include <ardour/session.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/diskstream.h>
41 #include <ardour/slave.h>
42 #include <ardour/cycles.h>
43 #include <ardour/generic_midi_control_protocol.h>
48 using namespace ARDOUR;
49 //using namespace sigc;
52 MachineControl::CommandSignature MMC_CommandSignature;
53 MachineControl::ResponseSignature MMC_ResponseSignature;
55 MultiAllocSingleReleasePool Session::MIDIRequest::pool ("midi", sizeof (Session::MIDIRequest), 1024);
58 Session::use_config_midi_ports ()
62 if (default_mmc_port) {
63 set_mmc_port (default_mmc_port->name());
68 if (default_mtc_port) {
69 set_mtc_port (default_mtc_port->name());
74 if (default_midi_port) {
75 set_midi_port (default_midi_port->name());
84 /***********************************************************************
86 **********************************************************************/
89 Session::set_mmc_control (bool yn)
91 if (mmc_control == yn) {
99 ControlChanged (MMCControl); /* EMIT SIGNAL */
103 Session::set_midi_control (bool yn)
105 if (midi_control == yn) {
114 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
115 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
116 (*i)->reset_midi_control (_midi_port, midi_control);
120 ControlChanged (MidiControl); /* EMIT SIGNAL */
124 Session::set_send_mtc (bool yn)
126 /* set the persistent option value regardless */
128 send_midi_timecode = yn;
131 /* only set the internal flag if we have
135 if (_mtc_port == 0 || send_mtc == yn) {
140 ControlChanged (SendMTC); /* EMIT SIGNAL */
144 Session::set_send_mmc (bool yn)
146 if (_mmc_port == 0) {
150 if (send_midi_machine_control == yn) {
154 /* only set the internal flag if we have
162 /* set the persistent option value regardless */
164 send_midi_machine_control = yn;
167 ControlChanged (SendMMC); /* EMIT SIGNAL */
171 Session::set_midi_feedback (bool yn)
173 if (generic_midi_control_protocol) {
175 generic_midi_control_protocol->set_send (ControlProtocol::SendRoute);
177 generic_midi_control_protocol->set_send (ControlProtocol::SendWhat (0));
183 Session::get_midi_feedback () const
185 return generic_midi_control_protocol && generic_midi_control_protocol->active();
189 Session::get_send_mtc () const
195 Session::get_send_mmc () const
201 Session::set_mtc_port (string port_tag)
205 if (port_tag.length() == 0) {
207 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
208 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
212 if (_mtc_port == 0) {
222 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
223 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
229 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
233 Config->set_mtc_port_name (port_tag);
236 MTC_PortChanged(); /* EMIT SIGNAL */
237 change_midi_ports ();
243 Session::set_mmc_port (string port_tag)
245 if (port_tag.length() == 0) {
246 if (_mmc_port == 0) {
255 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
265 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
266 MMC_CommandSignature,
267 MMC_ResponseSignature);
271 (mem_fun (*this, &Session::mmc_deferred_play));
272 mmc->DeferredPlay.connect
273 (mem_fun (*this, &Session::mmc_deferred_play));
275 (mem_fun (*this, &Session::mmc_stop));
276 mmc->FastForward.connect
277 (mem_fun (*this, &Session::mmc_fast_forward));
279 (mem_fun (*this, &Session::mmc_rewind));
281 (mem_fun (*this, &Session::mmc_pause));
282 mmc->RecordPause.connect
283 (mem_fun (*this, &Session::mmc_record_pause));
284 mmc->RecordStrobe.connect
285 (mem_fun (*this, &Session::mmc_record_strobe));
286 mmc->RecordExit.connect
287 (mem_fun (*this, &Session::mmc_record_exit));
289 (mem_fun (*this, &Session::mmc_locate));
291 (mem_fun (*this, &Session::mmc_step));
293 (mem_fun (*this, &Session::mmc_shuttle));
294 mmc->TrackRecordStatusChange.connect
295 (mem_fun (*this, &Session::mmc_record_enable));
297 /* also handle MIDI SPP because its so common */
299 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
300 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
301 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
303 Config->set_mmc_port_name (port_tag);
306 MMC_PortChanged(); /* EMIT SIGNAL */
307 change_midi_ports ();
313 Session::set_midi_port (string port_tag)
315 if (port_tag.length() == 0) {
316 if (_midi_port == 0) {
325 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
331 if (generic_midi_control_protocol) {
332 generic_midi_control_protocol->set_port (port);
335 Config->set_midi_port_name (port_tag);
338 MIDI_PortChanged(); /* EMIT SIGNAL */
339 change_midi_ports ();
345 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
347 MIDI::Parser* input_parser;
350 if ((input_parser = port->input()) != 0) {
351 input_parser->trace (yn, &cout, "input: ");
356 if ((input_parser = _mmc_port->input()) != 0) {
357 input_parser->trace (yn, &cout, "input: ");
361 if (_mtc_port && _mtc_port != _mmc_port) {
362 if ((input_parser = _mtc_port->input()) != 0) {
363 input_parser->trace (yn, &cout, "input: ");
367 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
368 if ((input_parser = _midi_port->input()) != 0) {
369 input_parser->trace (yn, &cout, "input: ");
374 Config->set_trace_midi_input (yn);
378 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
380 MIDI::Parser* output_parser;
383 if ((output_parser = port->output()) != 0) {
384 output_parser->trace (yn, &cout, "output: ");
388 if ((output_parser = _mmc_port->output()) != 0) {
389 output_parser->trace (yn, &cout, "output: ");
393 if (_mtc_port && _mtc_port != _mmc_port) {
394 if ((output_parser = _mtc_port->output()) != 0) {
395 output_parser->trace (yn, &cout, "output: ");
399 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
400 if ((output_parser = _midi_port->output()) != 0) {
401 output_parser->trace (yn, &cout, "output: ");
407 Config->set_trace_midi_output (yn);
411 Session::get_trace_midi_input(MIDI::Port *port)
413 MIDI::Parser* input_parser;
415 if ((input_parser = port->input()) != 0) {
416 return input_parser->tracing();
421 if ((input_parser = _mmc_port->input()) != 0) {
422 return input_parser->tracing();
427 if ((input_parser = _mtc_port->input()) != 0) {
428 return input_parser->tracing();
433 if ((input_parser = _midi_port->input()) != 0) {
434 return input_parser->tracing();
443 Session::get_trace_midi_output(MIDI::Port *port)
445 MIDI::Parser* output_parser;
447 if ((output_parser = port->output()) != 0) {
448 return output_parser->tracing();
453 if ((output_parser = _mmc_port->output()) != 0) {
454 return output_parser->tracing();
459 if ((output_parser = _mtc_port->output()) != 0) {
460 return output_parser->tracing();
465 if ((output_parser = _midi_port->output()) != 0) {
466 return output_parser->tracing();
476 Session::setup_midi_control ()
478 outbound_mtc_smpte_frame = 0;
479 next_quarter_frame_to_send = -1;
481 /* setup the MMC buffer */
483 mmc_buffer[0] = 0xf0; // SysEx
484 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
485 mmc_buffer[2] = 0x7f; // "broadcast" device ID
486 mmc_buffer[3] = 0x6; // MCC
488 /* Set up the qtr frame message */
499 if (_mmc_port != 0) {
501 send_mmc = send_midi_machine_control;
509 if (_mtc_port != 0) {
511 send_mtc = send_midi_timecode;
520 Session::midi_read (MIDI::Port* port)
524 /* reading from the MIDI port activates the Parser
525 that in turn generates signals that we care
526 about. the port is already set to NONBLOCK so that
527 can read freely here.
532 // cerr << "+++ READ ON " << port->name() << endl;
534 int nread = port->read (buf, sizeof (buf));
536 // cerr << "-- READ (" << nread << " ON " << port->name() << endl;
539 if ((size_t) nread < sizeof (buf)) {
544 } else if (nread == 0) {
546 } else if (errno == EAGAIN) {
549 fatal << string_compose(_("Error reading from MIDI port %1"), port->name()) << endmsg;
558 Session::spp_start (Parser& ignored)
560 if (mmc_control && (_slave_type != MTC)) {
561 request_transport_speed (1.0);
566 Session::spp_continue (Parser& ignored)
572 Session::spp_stop (Parser& ignored)
580 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
582 if (mmc_control && (_slave_type != MTC)) {
583 request_transport_speed (1.0);
588 Session::mmc_record_pause (MIDI::MachineControl &mmc)
591 maybe_enable_record();
596 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
601 /* record strobe does an implicit "Play" command */
603 if (_transport_speed != 1.0) {
605 /* start_transport() will move from Enabled->Recording, so we
606 don't need to do anything here except enable recording.
607 its not the same as maybe_enable_record() though, because
608 that *can* switch to Recording, which we do not want.
611 save_state ("", true);
612 atomic_set (&_record_status, Enabled);
613 RecordStateChanged (); /* EMIT SIGNAL */
615 request_transport_speed (1.0);
624 Session::mmc_record_exit (MIDI::MachineControl &mmc)
627 disable_record (false);
632 Session::mmc_stop (MIDI::MachineControl &mmc)
640 Session::mmc_pause (MIDI::MachineControl &mmc)
644 /* We support RECORD_PAUSE, so the spec says that
645 we must interpret PAUSE like RECORD_PAUSE if
649 if (actively_recording()) {
650 maybe_enable_record ();
657 static bool step_queued = false;
661 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
668 struct timeval diff = { 0, 0 };
670 gettimeofday (&now, 0);
672 timersub (&now, &last_mmc_step, &diff);
674 gettimeofday (&now, 0);
675 timersub (&now, &last_mmc_step, &diff);
677 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
681 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
682 double cur_speed = (((steps * 0.5) * smpte_frames_per_second) / diff_secs) / smpte_frames_per_second;
684 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
685 /* change direction */
686 step_speed = cur_speed;
688 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
694 cerr << "delta = " << diff_secs
695 << " ct = " << _transport_speed
696 << " steps = " << steps
697 << " new speed = " << cur_speed
698 << " speed = " << step_speed
702 request_transport_speed (step_speed);
706 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
712 Session::mmc_rewind (MIDI::MachineControl &mmc)
715 request_transport_speed(-8.0f);
720 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
723 request_transport_speed(8.0f);
728 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
734 jack_nframes_t target_frame;
737 smpte.hours = mmc_tc[0] & 0xf;
738 smpte.minutes = mmc_tc[1];
739 smpte.seconds = mmc_tc[2];
740 smpte.frames = mmc_tc[3];
742 // Also takes smpte offset into account:
743 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
745 if (target_frame > max_frames) {
746 target_frame = max_frames;
749 /* Some (all?) MTC/MMC devices do not send a full MTC frame
750 at the end of a locate, instead sending only an MMC
751 locate command. This causes the current position
752 of an MTC slave to become out of date. Catch this.
755 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
758 // cerr << "Locate *with* MTC slave\n";
759 mtcs->handle_locate (mmc_tc);
761 // cerr << "Locate without MTC slave\n";
762 request_locate (target_frame, false);
767 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
769 cerr << "MMC shuttle, speed = " << speed << endl;
775 if (shuttle_speed_threshold >= 0 && speed > shuttle_speed_threshold) {
776 speed *= shuttle_speed_factor;
779 cerr << "requested MMC control speed = " << speed << endl;
782 request_transport_speed (speed);
784 request_transport_speed (-speed);
789 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
793 RouteList::iterator i;
794 RWLockMonitor (route_lock, false, __LINE__, __FILE__);
796 for (i = routes.begin(); i != routes.end(); ++i) {
799 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
800 if (trk == at->remote_control_id()) {
801 at->set_record_enable (enabled, &mmc);
810 Session::send_full_time_code_in_another_thread ()
812 send_time_code_in_another_thread (true);
816 Session::send_midi_time_code_in_another_thread ()
818 send_time_code_in_another_thread (false);
822 Session::send_time_code_in_another_thread (bool full)
824 jack_nframes_t two_smpte_frames_duration;
825 jack_nframes_t quarter_frame_duration;
827 /* Duration of two smpte frames */
828 two_smpte_frames_duration = ((long) _frames_per_smpte_frame) << 1;
830 /* Duration of one quarter frame */
831 quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
833 if (_transport_frame < (outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration)))
835 /* There is no work to do.
836 We throttle this here so that we don't overload
837 the transport thread with requests.
842 MIDIRequest* request = new MIDIRequest;
845 request->type = MIDIRequest::SendFullMTC;
847 request->type = MIDIRequest::SendMTC;
850 midi_requests.write (&request, 1);
855 Session::change_midi_ports ()
857 MIDIRequest* request = new MIDIRequest;
859 request->type = MIDIRequest::PortChange;
860 midi_requests.write (&request, 1);
865 Session::send_full_time_code ()
871 if (_mtc_port == 0 || !send_mtc) {
875 // Get smpte time for this transport frame
876 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
878 // Check for negative smpte time and prepare for quarter frame transmission
879 if (smpte.negative) {
880 // Negative mtc is not defined, so sync slave to smpte zero.
881 // When _transport_frame gets there we will start transmitting quarter frames
887 smpte.negative = false;
888 smpte_to_sample( smpte, outbound_mtc_smpte_frame, true, false );
889 transmitting_smpte_time = smpte;
891 transmitting_smpte_time = smpte;
892 outbound_mtc_smpte_frame = _transport_frame;
893 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
894 // start MTC quarter frame transmission on an even frame
895 smpte_increment( transmitting_smpte_time );
896 outbound_mtc_smpte_frame += (jack_nframes_t) _frames_per_smpte_frame;
900 // Compensate for audio latency
901 outbound_mtc_smpte_frame += _worst_output_latency;
903 next_quarter_frame_to_send = 0;
905 // Sync slave to the same smpte time as we are on (except if negative, see above)
913 msg[5] = mtc_smpte_bits | smpte.hours;
914 msg[6] = smpte.minutes;
915 msg[7] = smpte.seconds;
916 msg[8] = smpte.frames;
919 LockMonitor lm (midi_lock, __LINE__, __FILE__);
921 if (_mtc_port->midimsg (msg, sizeof (msg))) {
922 error << _("Session: could not send full MIDI time code") << endmsg;
932 Session::send_midi_time_code ()
934 if (_mtc_port == 0 || !send_mtc || transmitting_smpte_time.negative || (next_quarter_frame_to_send < 0) ) {
938 jack_nframes_t two_smpte_frames_duration;
939 jack_nframes_t quarter_frame_duration;
941 /* Duration of two smpte frames */
942 two_smpte_frames_duration = ((long) _frames_per_smpte_frame) << 1;
944 /* Duration of one quarter frame */
945 quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
947 while (_transport_frame >= (outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))) {
949 // Send quarter frames up to current time
951 LockMonitor lm (midi_lock, __LINE__, __FILE__);
953 switch(next_quarter_frame_to_send) {
955 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
958 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
961 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
964 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
967 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
970 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
973 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
976 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
980 if (_mtc_port->midimsg (mtc_msg, 2)) {
981 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
987 // cout << "smpte = " << transmitting_smpte_time.hours << ":" << transmitting_smpte_time.minutes << ":" << transmitting_smpte_time.seconds << ":" << transmitting_smpte_time.frames << ", qfm = " << next_quarter_frame_to_send << endl;
989 // Increment quarter frame counter
990 next_quarter_frame_to_send++;
992 if (next_quarter_frame_to_send >= 8) {
993 // Wrap quarter frame counter
994 next_quarter_frame_to_send = 0;
995 // Increment smpte time twice
996 smpte_increment( transmitting_smpte_time );
997 smpte_increment( transmitting_smpte_time );
998 // Re-calculate timing of first quarter frame
999 smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
1000 // Compensate for audio latency
1001 outbound_mtc_smpte_frame += _worst_output_latency;
1008 /***********************************************************************
1010 **********************************************************************/
1013 Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nframes_t target_frame)
1015 MIDIRequest* request;
1017 if (_mtc_port == 0 || !send_mmc) {
1021 request = new MIDIRequest;
1022 request->type = MIDIRequest::SendMMC;
1023 request->mmc_cmd = cmd;
1024 request->locate_frame = target_frame;
1026 midi_requests.write (&request, 1);
1027 poke_midi_thread ();
1031 Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
1033 using namespace MIDI;
1037 if (_mmc_port == 0 || !send_mmc) {
1041 mmc_buffer[nbytes++] = cmd;
1043 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
1046 case MachineControl::cmdLocate:
1047 smpte_time_subframes (where, smpte);
1049 mmc_buffer[nbytes++] = 0x6; // byte count
1050 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
1051 mmc_buffer[nbytes++] = smpte.hours;
1052 mmc_buffer[nbytes++] = smpte.minutes;
1053 mmc_buffer[nbytes++] = smpte.seconds;
1054 mmc_buffer[nbytes++] = smpte.frames;
1055 mmc_buffer[nbytes++] = smpte.subframes;
1058 case MachineControl::cmdStop:
1061 case MachineControl::cmdPlay:
1062 /* always convert Play into Deferred Play */
1063 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
1066 case MachineControl::cmdDeferredPlay:
1069 case MachineControl::cmdRecordStrobe:
1072 case MachineControl::cmdRecordExit:
1075 case MachineControl::cmdRecordPause:
1084 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
1086 LockMonitor lm (midi_lock, __LINE__, __FILE__);
1088 if (_mmc_port->write (mmc_buffer, nbytes) != nbytes) {
1089 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
1095 Session::mmc_step_timeout ()
1098 struct timeval diff;
1100 gettimeofday (&now, 0);
1102 timersub (&now, &last_mmc_step, &diff);
1103 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1105 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1106 /* too long or too slow, stop transport */
1107 request_transport_speed (0.0);
1108 step_queued = false;
1112 if (diff_usecs < 250000.0) {
1113 /* too short, just keep going */
1119 request_transport_speed (_transport_speed * 0.75);
1125 Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1127 // in another thread, really
1129 MIDIRequest* request = new MIDIRequest;
1131 request->type = MIDIRequest::SendMessage;
1132 request->port = port;
1135 request->data = data;
1137 midi_requests.write (&request, 1);
1138 poke_midi_thread ();
1142 Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
1144 // in another thread, really
1146 MIDIRequest* request = new MIDIRequest;
1148 request->type = MIDIRequest::Deliver;
1149 request->port = port;
1151 request->size = bufsize;
1153 midi_requests.write (&request, 1);
1154 poke_midi_thread ();
1158 Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1160 if (port == 0 || ev == MIDI::none) {
1164 midi_msg[0] = (ev & 0xF0) | (ch & 0xF);
1165 midi_msg[1] = data.controller_number;
1166 midi_msg[2] = data.value;
1168 port->write (midi_msg, 3);
1172 Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
1175 port->write (buf, size);
1178 /* this is part of the semantics of the Deliver request */
1183 /*---------------------------------------------------------------------------
1185 ---------------------------------------------------------------------------*/
1188 Session::start_midi_thread ()
1190 if (pipe (midi_request_pipe)) {
1191 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1195 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1196 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1200 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1201 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1205 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1206 error << _("Session: could not create transport thread") << endmsg;
1210 // pthread_detach (midi_thread);
1216 Session::terminate_midi_thread ()
1218 MIDIRequest* request = new MIDIRequest;
1221 request->type = MIDIRequest::Quit;
1223 midi_requests.write (&request, 1);
1224 poke_midi_thread ();
1226 pthread_join (midi_thread, &status);
1230 Session::poke_midi_thread ()
1234 if (write (midi_request_pipe[1], &c, 1) != 1) {
1235 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1240 Session::_midi_thread_work (void* arg)
1242 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1243 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1245 ((Session *) arg)->midi_thread_work ();
1250 Session::midi_thread_work ()
1252 MIDIRequest* request;
1253 struct pollfd pfd[4];
1257 struct sched_param rtparam;
1260 vector<MIDI::Port*> ports;
1262 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1264 memset (&rtparam, 0, sizeof (rtparam));
1265 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1267 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1268 // do we care? not particularly.
1271 /* set up the port vector; 4 is the largest possible size for now */
1273 ports.push_back (0);
1274 ports.push_back (0);
1275 ports.push_back (0);
1276 ports.push_back (0);
1282 pfd[nfds].fd = midi_request_pipe[0];
1283 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1286 /* if we are using MMC control, we obviously have to listen
1287 on the appropriate port.
1290 if (mmc_control && _mmc_port && _mmc_port->selectable() >= 0) {
1291 pfd[nfds].fd = _mmc_port->selectable();
1292 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1293 ports[nfds] = _mmc_port;
1297 /* if MTC is being handled on a different port from MMC
1298 or we are not handling MMC at all, poll
1302 if (_mtc_port && (_mtc_port != _mmc_port || !mmc_control) && _mtc_port->selectable() >= 0) {
1303 pfd[nfds].fd = _mtc_port->selectable();
1304 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1305 ports[nfds] = _mtc_port;
1309 if (_midi_port && (_midi_port != _mmc_port || !mmc_control) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1310 pfd[nfds].fd = _midi_port->selectable();
1311 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1312 ports[nfds] = _midi_port;
1316 if (!midi_timeouts.empty()) {
1317 timeout = 100; /* 10msecs */
1319 timeout = -1; /* if there is no data, we don't care */
1323 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1324 if (poll (pfd, nfds, timeout) < 0) {
1325 if (errno == EINTR) {
1326 /* gdb at work, perhaps */
1330 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1334 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1339 /* check the transport request pipe */
1341 if (pfd[0].revents & ~POLLIN) {
1342 error << _("Error on transport thread request pipe") << endmsg;
1346 if (pfd[0].revents & POLLIN) {
1350 // cerr << "MIDI request FIFO ready\n";
1353 /* empty the pipe of all current requests */
1356 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1359 if ((size_t) nread < sizeof (foo)) {
1364 } else if (nread == 0) {
1366 } else if (errno == EAGAIN) {
1369 fatal << _("Error reading from transport request pipe") << endmsg;
1374 while (midi_requests.read (&request, 1) == 1) {
1376 switch (request->type) {
1378 case MIDIRequest::SendFullMTC:
1379 // cerr << "send full MTC\n";
1380 send_full_time_code ();
1381 // cerr << "... done\n";
1384 case MIDIRequest::SendMTC:
1385 // cerr << "send qtr MTC\n";
1386 send_midi_time_code ();
1387 // cerr << "... done\n";
1390 case MIDIRequest::SendMMC:
1391 // cerr << "send MMC\n";
1392 deliver_mmc (request->mmc_cmd, request->locate_frame);
1393 // cerr << "... done\n";
1396 case MIDIRequest::SendMessage:
1397 // cerr << "send Message\n";
1398 deliver_midi_message (request->port, request->ev, request->chan, request->data);
1399 // cerr << "... done\n";
1402 case MIDIRequest::Deliver:
1403 // cerr << "deliver\n";
1404 deliver_data (_midi_port, request->buf, request->size);
1405 // cerr << "... done\n";
1408 case MIDIRequest::PortChange:
1409 /* restart poll with new ports */
1410 // cerr << "rebind\n";
1414 case MIDIRequest::Quit:
1416 pthread_exit_pbd (0);
1434 /* now read the rest of the ports */
1436 for (int p = 1; p < nfds; ++p) {
1437 if ((pfd[p].revents & ~POLLIN)) {
1438 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1442 if (pfd[p].revents & POLLIN) {
1444 midi_read (ports[p]);
1448 /* timeout driven */
1450 if (fds_ready < 2 && timeout != -1) {
1452 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1454 MidiTimeoutList::iterator tmp;
1459 midi_timeouts.erase (i);
1469 Session::get_mmc_control () const
1475 Session::get_midi_control () const
1477 return midi_control;