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.
30 #include <midi++/mmc.h>
31 #include <midi++/port.h>
32 #include <midi++/manager.h>
33 #include <pbd/error.h>
34 #include <glibmm/thread.h>
35 #include <pbd/pthread_utils.h>
37 #include <ardour/configuration.h>
38 #include <ardour/audioengine.h>
39 #include <ardour/session.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/slave.h>
43 #include <ardour/cycles.h>
44 #include <ardour/smpte.h>
49 using namespace ARDOUR;
50 //using namespace sigc;
53 MachineControl::CommandSignature MMC_CommandSignature;
54 MachineControl::ResponseSignature MMC_ResponseSignature;
56 MultiAllocSingleReleasePool Session::MIDIRequest::pool ("midi", sizeof (Session::MIDIRequest), 1024);
59 Session::use_config_midi_ports ()
63 if (default_mmc_port) {
64 set_mmc_port (default_mmc_port->name());
69 if (default_mtc_port) {
70 set_mtc_port (default_mtc_port->name());
75 if (default_midi_port) {
76 set_midi_port (default_midi_port->name());
85 /***********************************************************************
87 **********************************************************************/
90 Session::set_mmc_control (bool yn)
93 if (mmc_control == yn) {
101 ControlChanged (MMCControl); /* EMIT SIGNAL */
105 Session::set_midi_control (bool yn)
108 if (midi_control == yn) {
117 Glib::RWLock::ReaderLock guard (route_lock);
118 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
119 (*i)->reset_midi_control (_midi_port, midi_control);
123 ControlChanged (MidiControl); /* EMIT SIGNAL */
127 Session::set_send_mtc (bool yn)
129 /* set the persistent option value regardless */
131 send_midi_timecode = yn;
134 /* only set the internal flag if we have
138 if (_mtc_port == 0 || send_mtc == yn) {
143 ControlChanged (SendMTC); /* EMIT SIGNAL */
147 Session::set_send_mmc (bool yn)
149 cerr << "set send mmc " << yn << endl;
151 if (_mmc_port == 0) {
156 if (send_midi_machine_control == yn) {
161 /* only set the internal flag if we have
170 /* set the persistent option value regardless */
172 send_midi_machine_control = yn;
175 ControlChanged (SendMMC); /* EMIT SIGNAL */
179 Session::set_midi_feedback (bool yn)
184 Session::get_midi_feedback () const
190 Session::get_send_mtc () const
196 Session::get_send_mmc () const
202 Session::set_mtc_port (string port_tag)
206 if (port_tag.length() == 0) {
208 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
209 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
213 if (_mtc_port == 0) {
223 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
224 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
230 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
234 Config->set_mtc_port_name (port_tag);
237 MTC_PortChanged(); /* EMIT SIGNAL */
238 change_midi_ports ();
244 Session::set_mmc_port (string port_tag)
246 if (port_tag.length() == 0) {
247 if (_mmc_port == 0) {
256 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
266 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
267 MMC_CommandSignature,
268 MMC_ResponseSignature);
272 (mem_fun (*this, &Session::mmc_deferred_play));
273 mmc->DeferredPlay.connect
274 (mem_fun (*this, &Session::mmc_deferred_play));
276 (mem_fun (*this, &Session::mmc_stop));
277 mmc->FastForward.connect
278 (mem_fun (*this, &Session::mmc_fast_forward));
280 (mem_fun (*this, &Session::mmc_rewind));
282 (mem_fun (*this, &Session::mmc_pause));
283 mmc->RecordPause.connect
284 (mem_fun (*this, &Session::mmc_record_pause));
285 mmc->RecordStrobe.connect
286 (mem_fun (*this, &Session::mmc_record_strobe));
287 mmc->RecordExit.connect
288 (mem_fun (*this, &Session::mmc_record_exit));
290 (mem_fun (*this, &Session::mmc_locate));
292 (mem_fun (*this, &Session::mmc_step));
294 (mem_fun (*this, &Session::mmc_shuttle));
295 mmc->TrackRecordStatusChange.connect
296 (mem_fun (*this, &Session::mmc_record_enable));
298 /* also handle MIDI SPP because its so common */
300 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
301 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
302 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
304 Config->set_mmc_port_name (port_tag);
307 MMC_PortChanged(); /* EMIT SIGNAL */
308 change_midi_ports ();
314 Session::set_midi_port (string port_tag)
316 if (port_tag.length() == 0) {
317 if (_midi_port == 0) {
326 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
332 /* XXX need something to forward this to control protocols ? or just
336 Config->set_midi_port_name (port_tag);
339 MIDI_PortChanged(); /* EMIT SIGNAL */
340 change_midi_ports ();
346 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
348 MIDI::Parser* input_parser;
351 if ((input_parser = port->input()) != 0) {
352 input_parser->trace (yn, &cout, "input: ");
357 if ((input_parser = _mmc_port->input()) != 0) {
358 input_parser->trace (yn, &cout, "input: ");
362 if (_mtc_port && _mtc_port != _mmc_port) {
363 if ((input_parser = _mtc_port->input()) != 0) {
364 input_parser->trace (yn, &cout, "input: ");
368 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
369 if ((input_parser = _midi_port->input()) != 0) {
370 input_parser->trace (yn, &cout, "input: ");
375 Config->set_trace_midi_input (yn);
379 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
381 MIDI::Parser* output_parser;
384 if ((output_parser = port->output()) != 0) {
385 output_parser->trace (yn, &cout, "output: ");
389 if ((output_parser = _mmc_port->output()) != 0) {
390 output_parser->trace (yn, &cout, "output: ");
394 if (_mtc_port && _mtc_port != _mmc_port) {
395 if ((output_parser = _mtc_port->output()) != 0) {
396 output_parser->trace (yn, &cout, "output: ");
400 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
401 if ((output_parser = _midi_port->output()) != 0) {
402 output_parser->trace (yn, &cout, "output: ");
408 Config->set_trace_midi_output (yn);
412 Session::get_trace_midi_input(MIDI::Port *port)
414 MIDI::Parser* input_parser;
416 if ((input_parser = port->input()) != 0) {
417 return input_parser->tracing();
422 if ((input_parser = _mmc_port->input()) != 0) {
423 return input_parser->tracing();
428 if ((input_parser = _mtc_port->input()) != 0) {
429 return input_parser->tracing();
434 if ((input_parser = _midi_port->input()) != 0) {
435 return input_parser->tracing();
444 Session::get_trace_midi_output(MIDI::Port *port)
446 MIDI::Parser* output_parser;
448 if ((output_parser = port->output()) != 0) {
449 return output_parser->tracing();
454 if ((output_parser = _mmc_port->output()) != 0) {
455 return output_parser->tracing();
460 if ((output_parser = _mtc_port->output()) != 0) {
461 return output_parser->tracing();
466 if ((output_parser = _midi_port->output()) != 0) {
467 return output_parser->tracing();
477 Session::setup_midi_control ()
479 outbound_mtc_smpte_frame = 0;
480 next_quarter_frame_to_send = 0;
482 /* setup the MMC buffer */
484 mmc_buffer[0] = 0xf0; // SysEx
485 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
486 mmc_buffer[2] = 0x7f; // "broadcast" device ID
487 mmc_buffer[3] = 0x6; // MCC
489 /* Set up the qtr frame message */
500 if (_mmc_port != 0) {
502 send_mmc = send_midi_machine_control;
510 if (_mtc_port != 0) {
512 send_mtc = send_midi_timecode;
522 Session::midi_read (MIDI::Port* port)
526 /* reading from the MIDI port activates the Parser
527 that in turn generates signals that we care
528 about. the port is already set to NONBLOCK so that
529 can read freely here.
534 // cerr << "+++ READ ON " << port->name() << endl;
536 int nread = port->read (buf, sizeof (buf));
538 // cerr << "-- READ (" << nread << " ON " << port->name() << endl;
541 if ((size_t) nread < sizeof (buf)) {
546 } else if (nread == 0) {
548 } else if (errno == EAGAIN) {
551 fatal << string_compose(_("Error reading from MIDI port %1"), port->name()) << endmsg;
561 Session::spp_start (Parser& ignored)
563 if (mmc_control && (_slave_type != MTC)) {
564 request_transport_speed (1.0);
569 Session::spp_continue (Parser& ignored)
575 Session::spp_stop (Parser& ignored)
583 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
585 if (mmc_control && (_slave_type != MTC)) {
586 request_transport_speed (1.0);
591 Session::mmc_record_pause (MIDI::MachineControl &mmc)
594 maybe_enable_record();
599 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
604 /* record strobe does an implicit "Play" command */
606 if (_transport_speed != 1.0) {
608 /* start_transport() will move from Enabled->Recording, so we
609 don't need to do anything here except enable recording.
610 its not the same as maybe_enable_record() though, because
611 that *can* switch to Recording, which we do not want.
614 save_state ("", true);
615 g_atomic_int_set (&_record_status, Enabled);
616 RecordStateChanged (); /* EMIT SIGNAL */
618 request_transport_speed (1.0);
627 Session::mmc_record_exit (MIDI::MachineControl &mmc)
630 disable_record (false);
635 Session::mmc_stop (MIDI::MachineControl &mmc)
643 Session::mmc_pause (MIDI::MachineControl &mmc)
647 /* We support RECORD_PAUSE, so the spec says that
648 we must interpret PAUSE like RECORD_PAUSE if
652 if (actively_recording()) {
653 maybe_enable_record ();
660 static bool step_queued = false;
664 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
671 struct timeval diff = { 0, 0 };
673 gettimeofday (&now, 0);
675 timersub (&now, &last_mmc_step, &diff);
677 gettimeofday (&now, 0);
678 timersub (&now, &last_mmc_step, &diff);
680 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
684 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
685 double cur_speed = (((steps * 0.5) * smpte_frames_per_second) / diff_secs) / smpte_frames_per_second;
687 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
688 /* change direction */
689 step_speed = cur_speed;
691 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
697 cerr << "delta = " << diff_secs
698 << " ct = " << _transport_speed
699 << " steps = " << steps
700 << " new speed = " << cur_speed
701 << " speed = " << step_speed
705 request_transport_speed (step_speed);
709 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
715 Session::mmc_rewind (MIDI::MachineControl &mmc)
718 request_transport_speed(-8.0f);
723 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
726 request_transport_speed(8.0f);
731 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
737 jack_nframes_t target_frame;
740 smpte.hours = mmc_tc[0] & 0xf;
741 smpte.minutes = mmc_tc[1];
742 smpte.seconds = mmc_tc[2];
743 smpte.frames = mmc_tc[3];
745 // Also takes smpte offset into account:
746 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
748 if (target_frame > max_frames) {
749 target_frame = max_frames;
752 /* Some (all?) MTC/MMC devices do not send a full MTC frame
753 at the end of a locate, instead sending only an MMC
754 locate command. This causes the current position
755 of an MTC slave to become out of date. Catch this.
758 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
761 // cerr << "Locate *with* MTC slave\n";
762 mtcs->handle_locate (mmc_tc);
764 // cerr << "Locate without MTC slave\n";
765 request_locate (target_frame, false);
770 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
772 cerr << "MMC shuttle, speed = " << speed << endl;
778 if (shuttle_speed_threshold >= 0 && speed > shuttle_speed_threshold) {
779 speed *= shuttle_speed_factor;
782 cerr << "requested MMC control speed = " << speed << endl;
785 request_transport_speed (speed);
787 request_transport_speed (-speed);
792 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
796 RouteList::iterator i;
797 Glib::RWLock::ReaderLock guard (route_lock);
799 for (i = routes.begin(); i != routes.end(); ++i) {
802 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
803 if (trk == at->remote_control_id()) {
804 at->set_record_enable (enabled, &mmc);
814 Session::change_midi_ports ()
817 MIDIRequest* request = new MIDIRequest;
819 request->type = MIDIRequest::PortChange;
820 midi_requests.write (&request, 1);
825 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
826 * This resets the MTC code, the next quarter frame message that is sent will be
827 * the first one with the beginning of this cycle as the new start point.
829 * Audio thread only, realtime safe. MIDI::Manager::cycle_start must
830 * have been called with the appropriate nframes parameter this cycle.
833 Session::send_full_time_code(jack_nframes_t nframes)
835 /* This function could easily send at a given frame offset, but would
836 * that be useful? Does ardour do sub-block accurate locating? [DR] */
841 _send_smpte_update = false;
843 if (_mtc_port == 0 || !send_mtc) {
847 // Get smpte time for this transport frame
848 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
850 transmitting_smpte_time = smpte;
851 outbound_mtc_smpte_frame = _transport_frame;
853 // I don't understand this bit yet.. [DR]
854 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
855 // start MTC quarter frame transmission on an even frame
856 SMPTE::increment( transmitting_smpte_time );
857 outbound_mtc_smpte_frame += (jack_nframes_t) _frames_per_smpte_frame;
860 // Compensate for audio latency
861 outbound_mtc_smpte_frame += _worst_output_latency;
863 next_quarter_frame_to_send = 0;
865 // Sync slave to the same SMPTE time as we are on
873 msg[5] = mtc_smpte_bits | smpte.hours;
874 msg[6] = smpte.minutes;
875 msg[7] = smpte.seconds;
876 msg[8] = smpte.frames;
878 cerr << "MTC: Sending full time code at " << outbound_mtc_smpte_frame << endl;
880 // Send message at offset 0, sent time is for the start of this cycle
881 if (!_mtc_port->midimsg (msg, sizeof (msg), 0)) {
882 error << _("Session: could not send full MIDI time code") << endmsg;
890 /** Sends MTC (quarter-frame) messages for this cycle.
891 * Must be called exactly once per cycle from the audio thread. Realtime safe.
892 * This function assumes the state of full SMPTE is sane, eg. the slave is
893 * expecting quarter frame messages and has the right frame of reference (any
894 * full MTC SMPTE time messages that needed to be sent should have been sent
895 * earlier already this cycle by send_full_time_code)
898 Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
900 assert (next_quarter_frame_to_send >= 0);
901 assert (next_quarter_frame_to_send <= 7);
903 if (next_quarter_frame_to_send < 0)
905 printf("Negative????\n");
908 if (_mtc_port == 0 || !send_mtc || transmitting_smpte_time.negative
909 /*|| (next_quarter_frame_to_send < 0)*/ ) {
910 //printf("(MTC) Not sending MTC\n");
914 /* Duration of one quarter frame */
915 jack_nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
917 //cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
918 //<< " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
920 // FIXME: this should always be true
921 //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
922 // > _transport_frame);
925 // Send quarter frames for this cycle
926 while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
927 (next_quarter_frame_to_send * quarter_frame_duration))) {
929 //cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
931 switch (next_quarter_frame_to_send) {
933 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
936 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
939 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
942 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
945 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
948 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
951 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
954 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
958 const jack_nframes_t msg_time = (outbound_mtc_smpte_frame
959 + (quarter_frame_duration * next_quarter_frame_to_send));
961 // This message must fall within this block or something is broken
962 assert(msg_time >= _transport_frame);
963 assert(msg_time < _transport_frame + nframes);
965 jack_nframes_t out_stamp = msg_time - _transport_frame;
966 assert(out_stamp < nframes);
968 if (!_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
969 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
974 /*cerr << "(MTC) SMPTE: " << transmitting_smpte_time.hours
975 << ":" << transmitting_smpte_time.minutes
976 << ":" << transmitting_smpte_time.seconds
977 << ":" << transmitting_smpte_time.frames
978 << ", qfm = " << next_quarter_frame_to_send
979 << ", stamp = " << out_stamp
980 << ", delta = " << _transport_frame + out_stamp - last_time << endl;*/
982 // Increment quarter frame counter
983 next_quarter_frame_to_send++;
985 if (next_quarter_frame_to_send >= 8) {
986 // Wrap quarter frame counter
987 next_quarter_frame_to_send = 0;
988 // Increment smpte time twice
989 SMPTE::increment( transmitting_smpte_time );
990 SMPTE::increment( transmitting_smpte_time );
991 // Re-calculate timing of first quarter frame
992 //smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
993 outbound_mtc_smpte_frame += 8 * quarter_frame_duration;
994 // Compensate for audio latency
995 outbound_mtc_smpte_frame += _worst_output_latency;
1002 /***********************************************************************
1004 **********************************************************************/
1007 Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nframes_t target_frame)
1009 MIDIRequest* request;
1011 if (_mtc_port == 0 || !send_mmc) {
1015 request = new MIDIRequest;
1016 request->type = MIDIRequest::SendMMC;
1017 request->mmc_cmd = cmd;
1018 request->locate_frame = target_frame;
1020 midi_requests.write (&request, 1);
1021 poke_midi_thread ();
1025 /** Send an MMC command at the given absolute timestamp (@a where).
1027 * This must be called in the process thread, and @a where must fall within
1028 * this process cycle or horrible things will happen.
1031 Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
1033 using namespace MIDI;
1037 if (_mmc_port == 0 || !send_mmc) {
1038 //cerr << "Not delivering MMC " << _mmc_port << " - " << send_mmc << endl;
1042 mmc_buffer[nbytes++] = cmd;
1044 //cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
1047 case MachineControl::cmdLocate:
1048 smpte_time_subframes (where, smpte);
1050 mmc_buffer[nbytes++] = 0x6; // byte count
1051 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
1052 mmc_buffer[nbytes++] = smpte.hours;
1053 mmc_buffer[nbytes++] = smpte.minutes;
1054 mmc_buffer[nbytes++] = smpte.seconds;
1055 mmc_buffer[nbytes++] = smpte.frames;
1056 mmc_buffer[nbytes++] = smpte.subframes;
1059 case MachineControl::cmdStop:
1062 case MachineControl::cmdPlay:
1063 /* always convert Play into Deferred Play */
1065 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
1068 case MachineControl::cmdDeferredPlay:
1071 case MachineControl::cmdRecordStrobe:
1074 case MachineControl::cmdRecordExit:
1077 case MachineControl::cmdRecordPause:
1086 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
1088 assert(where >= _transport_frame);
1090 // FIXME: timestamp correct? [DR]
1091 if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), where - _transport_frame)) {
1092 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
1094 cerr << "Sending MMC\n";
1100 Session::mmc_step_timeout ()
1103 struct timeval diff;
1105 gettimeofday (&now, 0);
1107 timersub (&now, &last_mmc_step, &diff);
1108 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1110 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1111 /* too long or too slow, stop transport */
1112 request_transport_speed (0.0);
1113 step_queued = false;
1117 if (diff_usecs < 250000.0) {
1118 /* too short, just keep going */
1124 request_transport_speed (_transport_speed * 0.75);
1130 Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1132 // in another thread, really
1134 MIDIRequest* request = new MIDIRequest;
1136 request->type = MIDIRequest::SendMessage;
1137 request->port = port;
1140 request->data = data;
1142 midi_requests.write (&request, 1);
1143 poke_midi_thread ();
1148 Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
1150 // in another thread, really
1152 MIDIRequest* request = new MIDIRequest;
1154 request->type = MIDIRequest::Deliver;
1155 request->port = port;
1157 request->size = bufsize;
1159 midi_requests.write (&request, 1);
1160 poke_midi_thread ();
1166 This is aaalll gone.
1170 Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1172 if (port == 0 || ev == MIDI::none) {
1176 midi_msg[0] = (ev & 0xF0) | (ch & 0xF);
1177 midi_msg[1] = data.controller_number;
1178 midi_msg[2] = data.value;
1180 port->write (midi_msg, 3);
1184 Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
1187 port->write (buf, size);
1190 /* this is part of the semantics of the Deliver request */
1197 /*---------------------------------------------------------------------------
1199 ---------------------------------------------------------------------------*/
1203 Session::start_midi_thread ()
1205 if (pipe (midi_request_pipe)) {
1206 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1210 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1211 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1215 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1216 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1220 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1221 error << _("Session: could not create transport thread") << endmsg;
1225 // pthread_detach (midi_thread);
1231 Session::terminate_midi_thread ()
1233 MIDIRequest* request = new MIDIRequest;
1236 request->type = MIDIRequest::Quit;
1238 midi_requests.write (&request, 1);
1239 poke_midi_thread ();
1241 pthread_join (midi_thread, &status);
1245 Session::poke_midi_thread ()
1249 if (write (midi_request_pipe[1], &c, 1) != 1) {
1250 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1255 Session::_midi_thread_work (void* arg)
1257 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1258 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1260 ((Session *) arg)->midi_thread_work ();
1265 Session::midi_thread_work ()
1267 MIDIRequest* request;
1268 struct pollfd pfd[4];
1272 struct sched_param rtparam;
1275 vector<MIDI::Port*> ports;
1277 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1279 memset (&rtparam, 0, sizeof (rtparam));
1280 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1282 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1283 // do we care? not particularly.
1286 /* set up the port vector; 4 is the largest possible size for now */
1288 ports.push_back (0);
1289 ports.push_back (0);
1290 ports.push_back (0);
1291 ports.push_back (0);
1297 pfd[nfds].fd = midi_request_pipe[0];
1298 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1301 /* if we are using MMC control, we obviously have to listen
1302 on the appropriate port.
1305 if (mmc_control && _mmc_port && _mmc_port->selectable() >= 0) {
1306 pfd[nfds].fd = _mmc_port->selectable();
1307 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1308 ports[nfds] = _mmc_port;
1312 /* if MTC is being handled on a different port from MMC
1313 or we are not handling MMC at all, poll
1317 if (_mtc_port && (_mtc_port != _mmc_port || !mmc_control) && _mtc_port->selectable() >= 0) {
1318 pfd[nfds].fd = _mtc_port->selectable();
1319 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1320 ports[nfds] = _mtc_port;
1324 if (_midi_port && (_midi_port != _mmc_port || !mmc_control) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1325 pfd[nfds].fd = _midi_port->selectable();
1326 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1327 ports[nfds] = _midi_port;
1331 if (!midi_timeouts.empty()) {
1332 timeout = 100; /* 10msecs */
1334 timeout = -1; /* if there is no data, we don't care */
1338 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1339 if (poll (pfd, nfds, timeout) < 0) {
1340 if (errno == EINTR) {
1341 /* gdb at work, perhaps */
1345 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1349 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1354 /* check the transport request pipe */
1356 if (pfd[0].revents & ~POLLIN) {
1357 error << _("Error on transport thread request pipe") << endmsg;
1361 if (pfd[0].revents & POLLIN) {
1365 // cerr << "MIDI request FIFO ready\n";
1368 /* empty the pipe of all current requests */
1371 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1374 if ((size_t) nread < sizeof (foo)) {
1379 } else if (nread == 0) {
1381 } else if (errno == EAGAIN) {
1384 fatal << _("Error reading from transport request pipe") << endmsg;
1389 while (midi_requests.read (&request, 1) == 1) {
1391 switch (request->type) {
1393 case MIDIRequest::SendFullMTC:
1394 // cerr << "send full MTC\n";
1395 send_full_time_code ();
1396 // cerr << "... done\n";
1399 case MIDIRequest::SendMTC:
1400 // cerr << "send qtr MTC\n";
1401 send_midi_time_code ();
1402 // cerr << "... done\n";
1405 case MIDIRequest::SendMMC:
1406 // cerr << "send MMC\n";
1407 deliver_mmc (request->mmc_cmd, request->locate_frame);
1408 // cerr << "... done\n";
1411 case MIDIRequest::SendMessage:
1412 // cerr << "send Message\n";
1413 deliver_midi_message (request->port, request->ev, request->chan, request->data);
1414 // cerr << "... done\n";
1417 case MIDIRequest::Deliver:
1418 // cerr << "deliver\n";
1419 deliver_data (_midi_port, request->buf, request->size);
1420 // cerr << "... done\n";
1423 case MIDIRequest::PortChange:
1424 /* restart poll with new ports */
1425 // cerr << "rebind\n";
1429 case MIDIRequest::Quit:
1431 pthread_exit_pbd (0);
1449 /* now read the rest of the ports */
1451 for (int p = 1; p < nfds; ++p) {
1452 if ((pfd[p].revents & ~POLLIN)) {
1453 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1457 if (pfd[p].revents & POLLIN) {
1459 midi_read (ports[p]);
1463 /* timeout driven */
1465 if (fds_ready < 2 && timeout != -1) {
1467 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1469 MidiTimeoutList::iterator tmp;
1474 midi_timeouts.erase (i);
1485 Session::get_mmc_control () const
1491 Session::get_midi_control () const
1493 return midi_control;