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 <glibmm/thread.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/audio_diskstream.h>
41 #include <ardour/slave.h>
42 #include <ardour/cycles.h>
43 #include <ardour/smpte.h>
48 using namespace ARDOUR;
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_mtc_port (string port_tag)
92 MIDI::byte old_device_id = 0;
93 bool reset_id = false;
95 if (port_tag.length() == 0) {
105 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
112 old_device_id = mmc->device_id();
117 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
118 MMC_CommandSignature,
119 MMC_ResponseSignature);
122 mmc->set_device_id (old_device_id);
126 (mem_fun (*this, &Session::mmc_deferred_play));
127 mmc->DeferredPlay.connect
128 (mem_fun (*this, &Session::mmc_deferred_play));
130 (mem_fun (*this, &Session::mmc_stop));
131 mmc->FastForward.connect
132 (mem_fun (*this, &Session::mmc_fast_forward));
134 (mem_fun (*this, &Session::mmc_rewind));
136 (mem_fun (*this, &Session::mmc_pause));
137 mmc->RecordPause.connect
138 (mem_fun (*this, &Session::mmc_record_pause));
139 mmc->RecordStrobe.connect
140 (mem_fun (*this, &Session::mmc_record_strobe));
141 mmc->RecordExit.connect
142 (mem_fun (*this, &Session::mmc_record_exit));
144 (mem_fun (*this, &Session::mmc_locate));
146 (mem_fun (*this, &Session::mmc_step));
148 (mem_fun (*this, &Session::mmc_shuttle));
149 mmc->TrackRecordStatusChange.connect
150 (mem_fun (*this, &Session::mmc_record_enable));
153 /* also handle MIDI SPP because its so common */
155 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
156 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
157 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
159 Config->set_mmc_port_name (port_tag);
162 MMC_PortChanged(); /* EMIT SIGNAL */
163 change_midi_ports ();
170 Session::set_mmc_device_id (uint32_t device_id)
173 mmc->set_device_id (device_id);
178 Session::set_mmc_port (string port_tag)
181 MIDI::byte old_device_id = 0;
182 bool reset_id = false;
184 if (port_tag.length() == 0) {
185 if (_mmc_port == 0) {
194 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
201 old_device_id = mmc->device_id();
206 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
207 MMC_CommandSignature,
208 MMC_ResponseSignature);
211 mmc->set_device_id (old_device_id);
215 (mem_fun (*this, &Session::mmc_deferred_play));
216 mmc->DeferredPlay.connect
217 (mem_fun (*this, &Session::mmc_deferred_play));
219 (mem_fun (*this, &Session::mmc_stop));
220 mmc->FastForward.connect
221 (mem_fun (*this, &Session::mmc_fast_forward));
223 (mem_fun (*this, &Session::mmc_rewind));
225 (mem_fun (*this, &Session::mmc_pause));
226 mmc->RecordPause.connect
227 (mem_fun (*this, &Session::mmc_record_pause));
228 mmc->RecordStrobe.connect
229 (mem_fun (*this, &Session::mmc_record_strobe));
230 mmc->RecordExit.connect
231 (mem_fun (*this, &Session::mmc_record_exit));
233 (mem_fun (*this, &Session::mmc_locate));
235 (mem_fun (*this, &Session::mmc_step));
237 (mem_fun (*this, &Session::mmc_shuttle));
238 mmc->TrackRecordStatusChange.connect
239 (mem_fun (*this, &Session::mmc_record_enable));
242 /* also handle MIDI SPP because its so common */
244 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
245 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
246 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
248 Config->set_mmc_port_name (port_tag);
252 MMC_PortChanged(); /* EMIT SIGNAL */
253 change_midi_ports ();
259 Session::set_midi_port (string port_tag)
262 if (port_tag.length() == 0) {
263 if (_midi_port == 0) {
272 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
278 /* XXX need something to forward this to control protocols ? or just
282 Config->set_midi_port_name (port_tag);
286 MIDI_PortChanged(); /* EMIT SIGNAL */
287 change_midi_ports ();
293 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
296 MIDI::Parser* input_parser;
299 if ((input_parser = port->input()) != 0) {
300 input_parser->trace (yn, &cout, "input: ");
305 if ((input_parser = _mmc_port->input()) != 0) {
306 input_parser->trace (yn, &cout, "input: ");
310 if (_mtc_port && _mtc_port != _mmc_port) {
311 if ((input_parser = _mtc_port->input()) != 0) {
312 input_parser->trace (yn, &cout, "input: ");
316 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
317 if ((input_parser = _midi_port->input()) != 0) {
318 input_parser->trace (yn, &cout, "input: ");
324 Config->set_trace_midi_input (yn);
328 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
331 MIDI::Parser* output_parser;
334 if ((output_parser = port->output()) != 0) {
335 output_parser->trace (yn, &cout, "output: ");
339 if ((output_parser = _mmc_port->output()) != 0) {
340 output_parser->trace (yn, &cout, "output: ");
344 if (_mtc_port && _mtc_port != _mmc_port) {
345 if ((output_parser = _mtc_port->output()) != 0) {
346 output_parser->trace (yn, &cout, "output: ");
350 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
351 if ((output_parser = _midi_port->output()) != 0) {
352 output_parser->trace (yn, &cout, "output: ");
359 Config->set_trace_midi_output (yn);
363 Session::get_trace_midi_input(MIDI::Port *port)
366 MIDI::Parser* input_parser;
368 if ((input_parser = port->input()) != 0) {
369 return input_parser->tracing();
374 if ((input_parser = _mmc_port->input()) != 0) {
375 return input_parser->tracing();
380 if ((input_parser = _mtc_port->input()) != 0) {
381 return input_parser->tracing();
386 if ((input_parser = _midi_port->input()) != 0) {
387 return input_parser->tracing();
397 Session::get_trace_midi_output(MIDI::Port *port)
400 MIDI::Parser* output_parser;
402 if ((output_parser = port->output()) != 0) {
403 return output_parser->tracing();
408 if ((output_parser = _mmc_port->output()) != 0) {
409 return output_parser->tracing();
414 if ((output_parser = _mtc_port->output()) != 0) {
415 return output_parser->tracing();
420 if ((output_parser = _midi_port->output()) != 0) {
421 return output_parser->tracing();
432 Session::setup_midi_control ()
434 outbound_mtc_smpte_frame = 0;
435 next_quarter_frame_to_send = 0;
437 /* setup the MMC buffer */
439 mmc_buffer[0] = 0xf0; // SysEx
440 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
441 mmc_buffer[2] = 0x7f; // "broadcast" device ID
442 mmc_buffer[3] = 0x6; // MCC
444 /* Set up the qtr frame message */
458 Session::midi_read (MIDI::Port* port)
462 /* reading from the MIDI port activates the Parser
463 that in turn generates signals that we care
464 about. the port is already set to NONBLOCK so that
465 can read freely here.
470 // cerr << "+++ READ ON " << port->name() << endl;
472 int nread = port->read (buf, sizeof (buf));
474 // cerr << "-- READ (" << nread << " ON " << port->name() << endl;
477 if ((size_t) nread < sizeof (buf)) {
482 } else if (nread == 0) {
484 } else if (errno == EAGAIN) {
487 fatal << string_compose(_("Error reading from MIDI port %1"), port->name()) << endmsg;
497 Session::spp_start (Parser& ignored)
499 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
500 request_transport_speed (1.0);
505 Session::spp_continue (Parser& ignored)
511 Session::spp_stop (Parser& ignored)
513 if (Config->get_mmc_control()) {
519 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
521 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
522 request_transport_speed (1.0);
527 Session::mmc_record_pause (MIDI::MachineControl &mmc)
529 if (Config->get_mmc_control()) {
530 maybe_enable_record();
535 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
537 if (!Config->get_mmc_control())
540 /* record strobe does an implicit "Play" command */
542 if (_transport_speed != 1.0) {
544 /* start_transport() will move from Enabled->Recording, so we
545 don't need to do anything here except enable recording.
546 its not the same as maybe_enable_record() though, because
547 that *can* switch to Recording, which we do not want.
550 save_state ("", true);
551 g_atomic_int_set (&_record_status, Enabled);
552 RecordStateChanged (); /* EMIT SIGNAL */
554 request_transport_speed (1.0);
563 Session::mmc_record_exit (MIDI::MachineControl &mmc)
565 if (Config->get_mmc_control()) {
566 disable_record (false);
571 Session::mmc_stop (MIDI::MachineControl &mmc)
573 if (Config->get_mmc_control()) {
579 Session::mmc_pause (MIDI::MachineControl &mmc)
581 if (Config->get_mmc_control()) {
583 /* We support RECORD_PAUSE, so the spec says that
584 we must interpret PAUSE like RECORD_PAUSE if
588 if (actively_recording()) {
589 maybe_enable_record ();
596 static bool step_queued = false;
599 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
601 if (!Config->get_mmc_control()) {
606 struct timeval diff = { 0, 0 };
608 gettimeofday (&now, 0);
610 timersub (&now, &last_mmc_step, &diff);
612 gettimeofday (&now, 0);
613 timersub (&now, &last_mmc_step, &diff);
615 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
619 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
620 double cur_speed = (((steps * 0.5) * smpte_frames_per_second()) / diff_secs) / smpte_frames_per_second();
622 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
623 /* change direction */
624 step_speed = cur_speed;
626 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
632 cerr << "delta = " << diff_secs
633 << " ct = " << _transport_speed
634 << " steps = " << steps
635 << " new speed = " << cur_speed
636 << " speed = " << step_speed
640 request_transport_speed (step_speed);
644 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
650 Session::mmc_rewind (MIDI::MachineControl &mmc)
652 if (Config->get_mmc_control()) {
653 request_transport_speed(-8.0f);
658 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
660 if (Config->get_mmc_control()) {
661 request_transport_speed(8.0f);
666 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
668 if (!Config->get_mmc_control()) {
672 nframes_t target_frame;
675 smpte.hours = mmc_tc[0] & 0xf;
676 smpte.minutes = mmc_tc[1];
677 smpte.seconds = mmc_tc[2];
678 smpte.frames = mmc_tc[3];
679 smpte.rate = smpte_frames_per_second();
680 smpte.drop = smpte_drop_frames();
682 // Also takes smpte offset into account:
683 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
685 if (target_frame > max_frames) {
686 target_frame = max_frames;
689 /* Some (all?) MTC/MMC devices do not send a full MTC frame
690 at the end of a locate, instead sending only an MMC
691 locate command. This causes the current position
692 of an MTC slave to become out of date. Catch this.
695 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
698 // cerr << "Locate *with* MTC slave\n";
699 mtcs->handle_locate (mmc_tc);
701 // cerr << "Locate without MTC slave\n";
702 request_locate (target_frame, false);
707 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
709 if (!Config->get_mmc_control()) {
713 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
714 speed *= Config->get_shuttle_speed_factor();
718 request_transport_speed (speed);
720 request_transport_speed (-speed);
725 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
727 if (Config->get_mmc_control()) {
729 RouteList::iterator i;
730 boost::shared_ptr<RouteList> r = routes.reader();
732 for (i = r->begin(); i != r->end(); ++i) {
735 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
736 if (trk == at->remote_control_id()) {
737 at->set_record_enable (enabled, &mmc);
747 Session::change_midi_ports ()
750 MIDIRequest* request = new MIDIRequest;
752 request->type = MIDIRequest::PortChange;
753 midi_requests.write (&request, 1);
758 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
759 * This resets the MTC code, the next quarter frame message that is sent will be
760 * the first one with the beginning of this cycle as the new start point.
762 * Audio thread only, realtime safe. MIDI::Manager::cycle_start must
763 * have been called with the appropriate nframes parameter this cycle.
766 Session::send_full_time_code(nframes_t nframes)
768 /* This function could easily send at a given frame offset, but would
769 * that be useful? Does ardour do sub-block accurate locating? [DR] */
774 _send_smpte_update = false;
776 if (_mtc_port == 0 || !session_send_mtc) {
780 // Get smpte time for this transport frame
781 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
783 transmitting_smpte_time = smpte;
784 outbound_mtc_smpte_frame = _transport_frame;
786 // I don't understand this bit yet.. [DR]
787 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
788 // start MTC quarter frame transmission on an even frame
789 SMPTE::increment( transmitting_smpte_time );
790 outbound_mtc_smpte_frame += (nframes_t) _frames_per_smpte_frame;
793 // Compensate for audio latency
794 outbound_mtc_smpte_frame += _worst_output_latency;
796 next_quarter_frame_to_send = 0;
798 // Sync slave to the same SMPTE time as we are on
806 msg[5] = mtc_smpte_bits | smpte.hours;
807 msg[6] = smpte.minutes;
808 msg[7] = smpte.seconds;
809 msg[8] = smpte.frames;
811 cerr << "MTC: Sending full time code at " << outbound_mtc_smpte_frame << endl;
813 // Send message at offset 0, sent time is for the start of this cycle
814 if (!_mtc_port->midimsg (msg, sizeof (msg), 0)) {
815 error << _("Session: could not send full MIDI time code") << endmsg;
823 /** Sends MTC (quarter-frame) messages for this cycle.
824 * Must be called exactly once per cycle from the audio thread. Realtime safe.
825 * This function assumes the state of full SMPTE is sane, eg. the slave is
826 * expecting quarter frame messages and has the right frame of reference (any
827 * full MTC SMPTE time messages that needed to be sent should have been sent
828 * earlier already this cycle by send_full_time_code)
831 Session::send_midi_time_code_for_cycle(nframes_t nframes)
833 assert (next_quarter_frame_to_send >= 0);
834 assert (next_quarter_frame_to_send <= 7);
836 if (next_quarter_frame_to_send < 0)
838 printf("Negative????\n");
841 if (_mtc_port == 0 || !session_send_mtc || transmitting_smpte_time.negative
842 /*|| (next_quarter_frame_to_send < 0)*/ ) {
843 //printf("(MTC) Not sending MTC\n");
847 /* Duration of one quarter frame */
848 nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
850 //cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
851 //<< " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
853 // FIXME: this should always be true
854 //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
855 // > _transport_frame);
858 // Send quarter frames for this cycle
859 while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
860 (next_quarter_frame_to_send * quarter_frame_duration))) {
862 //cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
864 switch (next_quarter_frame_to_send) {
866 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
869 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
872 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
875 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
878 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
881 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
884 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
887 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
891 const nframes_t msg_time = (outbound_mtc_smpte_frame
892 + (quarter_frame_duration * next_quarter_frame_to_send));
894 // This message must fall within this block or something is broken
895 assert(msg_time >= _transport_frame);
896 assert(msg_time < _transport_frame + nframes);
898 nframes_t out_stamp = msg_time - _transport_frame;
899 assert(out_stamp < nframes);
901 if (!_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
902 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
907 /*cerr << "(MTC) SMPTE: " << transmitting_smpte_time.hours
908 << ":" << transmitting_smpte_time.minutes
909 << ":" << transmitting_smpte_time.seconds
910 << ":" << transmitting_smpte_time.frames
911 << ", qfm = " << next_quarter_frame_to_send
912 << ", stamp = " << out_stamp
913 << ", delta = " << _transport_frame + out_stamp - last_time << endl;*/
915 // Increment quarter frame counter
916 next_quarter_frame_to_send++;
918 if (next_quarter_frame_to_send >= 8) {
919 // Wrap quarter frame counter
920 next_quarter_frame_to_send = 0;
921 // Increment smpte time twice
922 SMPTE::increment( transmitting_smpte_time );
923 SMPTE::increment( transmitting_smpte_time );
924 // Re-calculate timing of first quarter frame
925 //smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
926 outbound_mtc_smpte_frame += 8 * quarter_frame_duration;
927 // Compensate for audio latency
928 outbound_mtc_smpte_frame += _worst_output_latency;
935 /***********************************************************************
937 **********************************************************************/
940 Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, nframes_t target_frame)
942 MIDIRequest* request;
944 if (_mtc_port == 0 || !session_send_mmc) {
948 request = new MIDIRequest;
949 request->type = MIDIRequest::SendMMC;
950 request->mmc_cmd = cmd;
951 request->locate_frame = target_frame;
953 midi_requests.write (&request, 1);
958 /** Send an MMC command at the given absolute timestamp (@a where).
960 * This must be called in the process thread, and @a where must fall within
961 * this process cycle or horrible things will happen.
964 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
966 using namespace MIDI;
970 if (_mmc_port == 0 || !session_send_mmc) {
971 //cerr << "Not delivering MMC " << _mmc_port << " - " << send_mmc << endl;
975 mmc_buffer[nbytes++] = cmd;
977 //cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
980 case MachineControl::cmdLocate:
981 smpte_time_subframes (where, smpte);
983 mmc_buffer[nbytes++] = 0x6; // byte count
984 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
985 mmc_buffer[nbytes++] = smpte.hours;
986 mmc_buffer[nbytes++] = smpte.minutes;
987 mmc_buffer[nbytes++] = smpte.seconds;
988 mmc_buffer[nbytes++] = smpte.frames;
989 mmc_buffer[nbytes++] = smpte.subframes;
992 case MachineControl::cmdStop:
995 case MachineControl::cmdPlay:
996 /* always convert Play into Deferred Play */
998 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
1001 case MachineControl::cmdDeferredPlay:
1004 case MachineControl::cmdRecordStrobe:
1007 case MachineControl::cmdRecordExit:
1010 case MachineControl::cmdRecordPause:
1019 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
1021 assert(where >= _transport_frame);
1023 // FIXME: timestamp correct? [DR]
1024 if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), where - _transport_frame)) {
1025 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
1027 cerr << "Sending MMC\n";
1033 Session::mmc_step_timeout ()
1036 struct timeval diff;
1038 gettimeofday (&now, 0);
1040 timersub (&now, &last_mmc_step, &diff);
1041 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1043 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1044 /* too long or too slow, stop transport */
1045 request_transport_speed (0.0);
1046 step_queued = false;
1050 if (diff_usecs < 250000.0) {
1051 /* too short, just keep going */
1057 request_transport_speed (_transport_speed * 0.75);
1063 Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1065 // in another thread, really
1067 MIDIRequest* request = new MIDIRequest;
1069 request->type = MIDIRequest::SendMessage;
1070 request->port = port;
1073 request->data = data;
1075 midi_requests.write (&request, 1);
1076 poke_midi_thread ();
1081 Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
1083 // in another thread, really
1085 MIDIRequest* request = new MIDIRequest;
1087 request->type = MIDIRequest::Deliver;
1088 request->port = port;
1090 request->size = bufsize;
1092 midi_requests.write (&request, 1);
1093 poke_midi_thread ();
1099 This is aaalll gone.
1103 Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1105 if (port == 0 || ev == MIDI::none) {
1109 midi_msg[0] = (ev & 0xF0) | (ch & 0xF);
1110 midi_msg[1] = data.controller_number;
1111 midi_msg[2] = data.value;
1113 port->write (midi_msg, 3);
1117 Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
1120 port->write (buf, size);
1123 /* this is part of the semantics of the Deliver request */
1130 /*---------------------------------------------------------------------------
1132 ---------------------------------------------------------------------------*/
1136 Session::start_midi_thread ()
1138 if (pipe (midi_request_pipe)) {
1139 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1143 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1144 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1148 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1149 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1153 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1154 error << _("Session: could not create transport thread") << endmsg;
1158 // pthread_detach (midi_thread);
1164 Session::terminate_midi_thread ()
1167 MIDIRequest* request = new MIDIRequest;
1170 request->type = MIDIRequest::Quit;
1172 midi_requests.write (&request, 1);
1173 poke_midi_thread ();
1175 pthread_join (midi_thread, &status);
1180 Session::poke_midi_thread ()
1184 if (write (midi_request_pipe[1], &c, 1) != 1) {
1185 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1190 Session::_midi_thread_work (void* arg)
1192 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1193 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1195 ((Session *) arg)->midi_thread_work ();
1200 Session::midi_thread_work ()
1202 MIDIRequest* request;
1203 struct pollfd pfd[4];
1207 struct sched_param rtparam;
1210 vector<MIDI::Port*> ports;
1212 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1214 memset (&rtparam, 0, sizeof (rtparam));
1215 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1217 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1218 // do we care? not particularly.
1221 /* set up the port vector; 4 is the largest possible size for now */
1223 ports.push_back (0);
1224 ports.push_back (0);
1225 ports.push_back (0);
1226 ports.push_back (0);
1232 pfd[nfds].fd = midi_request_pipe[0];
1233 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1236 /* if we are using MMC control, we obviously have to listen
1237 on the appropriate port.
1240 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1241 pfd[nfds].fd = _mmc_port->selectable();
1242 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1243 ports[nfds] = _mmc_port;
1247 /* if MTC is being handled on a different port from MMC
1248 or we are not handling MMC at all, poll
1252 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1253 pfd[nfds].fd = _mtc_port->selectable();
1254 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1255 ports[nfds] = _mtc_port;
1259 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1260 pfd[nfds].fd = _midi_port->selectable();
1261 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1262 ports[nfds] = _midi_port;
1266 if (!midi_timeouts.empty()) {
1267 timeout = 100; /* 10msecs */
1269 timeout = -1; /* if there is no data, we don't care */
1273 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1274 if (poll (pfd, nfds, timeout) < 0) {
1275 if (errno == EINTR) {
1276 /* gdb at work, perhaps */
1280 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1284 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1289 /* check the transport request pipe */
1291 if (pfd[0].revents & ~POLLIN) {
1292 error << _("Error on transport thread request pipe") << endmsg;
1296 if (pfd[0].revents & POLLIN) {
1300 // cerr << "MIDI request FIFO ready\n";
1303 /* empty the pipe of all current requests */
1306 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1309 if ((size_t) nread < sizeof (foo)) {
1314 } else if (nread == 0) {
1316 } else if (errno == EAGAIN) {
1319 fatal << _("Error reading from transport request pipe") << endmsg;
1324 while (midi_requests.read (&request, 1) == 1) {
1326 switch (request->type) {
1328 case MIDIRequest::SendFullMTC:
1329 // cerr << "send full MTC\n";
1330 send_full_time_code ();
1331 // cerr << "... done\n";
1334 case MIDIRequest::SendMTC:
1335 // cerr << "send qtr MTC\n";
1336 send_midi_time_code ();
1337 // cerr << "... done\n";
1340 case MIDIRequest::SendMMC:
1341 // cerr << "send MMC\n";
1342 deliver_mmc (request->mmc_cmd, request->locate_frame);
1343 // cerr << "... done\n";
1346 case MIDIRequest::SendMessage:
1347 // cerr << "send Message\n";
1348 deliver_midi_message (request->port, request->ev, request->chan, request->data);
1349 // cerr << "... done\n";
1352 case MIDIRequest::Deliver:
1353 // cerr << "deliver\n";
1354 deliver_data (_midi_port, request->buf, request->size);
1355 // cerr << "... done\n";
1358 case MIDIRequest::PortChange:
1359 /* restart poll with new ports */
1360 // cerr << "rebind\n";
1364 case MIDIRequest::Quit:
1366 pthread_exit_pbd (0);
1384 /* now read the rest of the ports */
1386 for (int p = 1; p < nfds; ++p) {
1387 if ((pfd[p].revents & ~POLLIN)) {
1388 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1392 if (pfd[p].revents & POLLIN) {
1394 midi_read (ports[p]);
1398 /* timeout driven */
1400 if (fds_ready < 2 && timeout != -1) {
1402 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1404 MidiTimeoutList::iterator tmp;
1409 midi_timeouts.erase (i);