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;
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;
663 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
670 struct timeval diff = { 0, 0 };
672 gettimeofday (&now, 0);
674 timersub (&now, &last_mmc_step, &diff);
676 gettimeofday (&now, 0);
677 timersub (&now, &last_mmc_step, &diff);
679 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
683 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
684 double cur_speed = (((steps * 0.5) * smpte_frames_per_second) / diff_secs) / smpte_frames_per_second;
686 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
687 /* change direction */
688 step_speed = cur_speed;
690 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
696 cerr << "delta = " << diff_secs
697 << " ct = " << _transport_speed
698 << " steps = " << steps
699 << " new speed = " << cur_speed
700 << " speed = " << step_speed
704 request_transport_speed (step_speed);
708 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
714 Session::mmc_rewind (MIDI::MachineControl &mmc)
717 request_transport_speed(-8.0f);
722 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
725 request_transport_speed(8.0f);
730 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
736 jack_nframes_t target_frame;
739 smpte.hours = mmc_tc[0] & 0xf;
740 smpte.minutes = mmc_tc[1];
741 smpte.seconds = mmc_tc[2];
742 smpte.frames = mmc_tc[3];
744 // Also takes smpte offset into account:
745 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
747 if (target_frame > max_frames) {
748 target_frame = max_frames;
751 /* Some (all?) MTC/MMC devices do not send a full MTC frame
752 at the end of a locate, instead sending only an MMC
753 locate command. This causes the current position
754 of an MTC slave to become out of date. Catch this.
757 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
760 // cerr << "Locate *with* MTC slave\n";
761 mtcs->handle_locate (mmc_tc);
763 // cerr << "Locate without MTC slave\n";
764 request_locate (target_frame, false);
769 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
771 cerr << "MMC shuttle, speed = " << speed << endl;
777 if (shuttle_speed_threshold >= 0 && speed > shuttle_speed_threshold) {
778 speed *= shuttle_speed_factor;
781 cerr << "requested MMC control speed = " << speed << endl;
784 request_transport_speed (speed);
786 request_transport_speed (-speed);
791 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
795 RouteList::iterator i;
796 Glib::RWLock::ReaderLock guard (route_lock);
798 for (i = routes.begin(); i != routes.end(); ++i) {
801 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
802 if (trk == at->remote_control_id()) {
803 at->set_record_enable (enabled, &mmc);
813 Session::change_midi_ports ()
816 MIDIRequest* request = new MIDIRequest;
818 request->type = MIDIRequest::PortChange;
819 midi_requests.write (&request, 1);
824 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
825 * This resets the MTC code, the next quarter frame message that is sent will be
826 * the first one with the beginning of this cycle as the new start point.
828 * Audio thread only, realtime safe. MIDI::Manager::cycle_start must
829 * have been called with the appropriate nframes parameter this cycle.
832 Session::send_full_time_code(jack_nframes_t nframes)
834 /* This function could easily send at a given frame offset, but would
835 * that be useful? Does ardour do sub-block accurate locating? [DR] */
840 _send_smpte_update = false;
842 if (_mtc_port == 0 || !send_mtc) {
846 // Get smpte time for this transport frame
847 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
849 transmitting_smpte_time = smpte;
850 outbound_mtc_smpte_frame = _transport_frame;
852 // I don't understand this bit yet.. [DR]
853 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
854 // start MTC quarter frame transmission on an even frame
855 SMPTE::increment( transmitting_smpte_time );
856 outbound_mtc_smpte_frame += (jack_nframes_t) _frames_per_smpte_frame;
859 // Compensate for audio latency
860 outbound_mtc_smpte_frame += _worst_output_latency;
862 next_quarter_frame_to_send = 0;
864 // Sync slave to the same SMPTE time as we are on
872 msg[5] = mtc_smpte_bits | smpte.hours;
873 msg[6] = smpte.minutes;
874 msg[7] = smpte.seconds;
875 msg[8] = smpte.frames;
877 cerr << "MTC: Sending full time code at " << outbound_mtc_smpte_frame << endl;
879 // Send message at offset 0, sent time is for the start of this cycle
880 if (!_mtc_port->midimsg (msg, sizeof (msg), 0)) {
881 error << _("Session: could not send full MIDI time code") << endmsg;
889 /** Sends MTC (quarter-frame) messages for this cycle.
890 * Must be called exactly once per cycle from the audio thread. Realtime safe.
891 * This function assumes the state of full SMPTE is sane, eg. the slave is
892 * expecting quarter frame messages and has the right frame of reference (any
893 * full MTC SMPTE time messages that needed to be sent should have been sent
894 * earlier already this cycle by send_full_time_code)
897 Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
899 assert (next_quarter_frame_to_send >= 0);
900 assert (next_quarter_frame_to_send <= 7);
902 if (next_quarter_frame_to_send < 0)
904 printf("Negative????\n");
907 if (_mtc_port == 0 || !send_mtc || transmitting_smpte_time.negative
908 /*|| (next_quarter_frame_to_send < 0)*/ ) {
909 //printf("(MTC) Not sending MTC\n");
913 /* Duration of one quarter frame */
914 jack_nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
916 //cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
917 //<< " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
919 // FIXME: this should always be true
920 //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
921 // > _transport_frame);
924 // Send quarter frames for this cycle
925 while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
926 (next_quarter_frame_to_send * quarter_frame_duration))) {
928 //cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
930 switch (next_quarter_frame_to_send) {
932 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
935 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
938 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
941 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
944 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
947 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
950 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
953 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
957 const jack_nframes_t msg_time = (outbound_mtc_smpte_frame
958 + (quarter_frame_duration * next_quarter_frame_to_send));
960 // This message must fall within this block or something is broken
961 assert(msg_time >= _transport_frame);
962 assert(msg_time < _transport_frame + nframes);
964 jack_nframes_t out_stamp = msg_time - _transport_frame;
965 assert(out_stamp < nframes);
967 if (!_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
968 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
973 /*cerr << "(MTC) SMPTE: " << transmitting_smpte_time.hours
974 << ":" << transmitting_smpte_time.minutes
975 << ":" << transmitting_smpte_time.seconds
976 << ":" << transmitting_smpte_time.frames
977 << ", qfm = " << next_quarter_frame_to_send
978 << ", stamp = " << out_stamp
979 << ", delta = " << _transport_frame + out_stamp - last_time << endl;*/
981 // Increment quarter frame counter
982 next_quarter_frame_to_send++;
984 if (next_quarter_frame_to_send >= 8) {
985 // Wrap quarter frame counter
986 next_quarter_frame_to_send = 0;
987 // Increment smpte time twice
988 SMPTE::increment( transmitting_smpte_time );
989 SMPTE::increment( transmitting_smpte_time );
990 // Re-calculate timing of first quarter frame
991 //smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
992 outbound_mtc_smpte_frame += 8 * quarter_frame_duration;
993 // Compensate for audio latency
994 outbound_mtc_smpte_frame += _worst_output_latency;
1001 /***********************************************************************
1003 **********************************************************************/
1006 Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nframes_t target_frame)
1008 MIDIRequest* request;
1010 if (_mtc_port == 0 || !send_mmc) {
1014 request = new MIDIRequest;
1015 request->type = MIDIRequest::SendMMC;
1016 request->mmc_cmd = cmd;
1017 request->locate_frame = target_frame;
1019 midi_requests.write (&request, 1);
1020 poke_midi_thread ();
1024 /** Send an MMC command at the given absolute timestamp (@a where).
1026 * This must be called in the process thread, and @a where must fall within
1027 * this process cycle or horrible things will happen.
1030 Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
1032 using namespace MIDI;
1036 if (_mmc_port == 0 || !send_mmc) {
1037 //cerr << "Not delivering MMC " << _mmc_port << " - " << send_mmc << endl;
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 */
1064 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
1067 case MachineControl::cmdDeferredPlay:
1070 case MachineControl::cmdRecordStrobe:
1073 case MachineControl::cmdRecordExit:
1076 case MachineControl::cmdRecordPause:
1085 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
1087 assert(where >= _transport_frame);
1089 // FIXME: timestamp correct? [DR]
1090 if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), where - _transport_frame)) {
1091 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
1093 cerr << "Sending MMC\n";
1099 Session::mmc_step_timeout ()
1102 struct timeval diff;
1104 gettimeofday (&now, 0);
1106 timersub (&now, &last_mmc_step, &diff);
1107 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1109 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1110 /* too long or too slow, stop transport */
1111 request_transport_speed (0.0);
1112 step_queued = false;
1116 if (diff_usecs < 250000.0) {
1117 /* too short, just keep going */
1123 request_transport_speed (_transport_speed * 0.75);
1129 Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1131 // in another thread, really
1133 MIDIRequest* request = new MIDIRequest;
1135 request->type = MIDIRequest::SendMessage;
1136 request->port = port;
1139 request->data = data;
1141 midi_requests.write (&request, 1);
1142 poke_midi_thread ();
1147 Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
1149 // in another thread, really
1151 MIDIRequest* request = new MIDIRequest;
1153 request->type = MIDIRequest::Deliver;
1154 request->port = port;
1156 request->size = bufsize;
1158 midi_requests.write (&request, 1);
1159 poke_midi_thread ();
1165 This is aaalll gone.
1169 Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1171 if (port == 0 || ev == MIDI::none) {
1175 midi_msg[0] = (ev & 0xF0) | (ch & 0xF);
1176 midi_msg[1] = data.controller_number;
1177 midi_msg[2] = data.value;
1179 port->write (midi_msg, 3);
1183 Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
1186 port->write (buf, size);
1189 /* this is part of the semantics of the Deliver request */
1196 /*---------------------------------------------------------------------------
1198 ---------------------------------------------------------------------------*/
1202 Session::start_midi_thread ()
1204 if (pipe (midi_request_pipe)) {
1205 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1209 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1210 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1214 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1215 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1219 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1220 error << _("Session: could not create transport thread") << endmsg;
1224 // pthread_detach (midi_thread);
1230 Session::terminate_midi_thread ()
1232 MIDIRequest* request = new MIDIRequest;
1235 request->type = MIDIRequest::Quit;
1237 midi_requests.write (&request, 1);
1238 poke_midi_thread ();
1240 pthread_join (midi_thread, &status);
1244 Session::poke_midi_thread ()
1248 if (write (midi_request_pipe[1], &c, 1) != 1) {
1249 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1254 Session::_midi_thread_work (void* arg)
1256 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1257 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1259 ((Session *) arg)->midi_thread_work ();
1264 Session::midi_thread_work ()
1266 MIDIRequest* request;
1267 struct pollfd pfd[4];
1271 struct sched_param rtparam;
1274 vector<MIDI::Port*> ports;
1276 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1278 memset (&rtparam, 0, sizeof (rtparam));
1279 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1281 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1282 // do we care? not particularly.
1285 /* set up the port vector; 4 is the largest possible size for now */
1287 ports.push_back (0);
1288 ports.push_back (0);
1289 ports.push_back (0);
1290 ports.push_back (0);
1296 pfd[nfds].fd = midi_request_pipe[0];
1297 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1300 /* if we are using MMC control, we obviously have to listen
1301 on the appropriate port.
1304 if (mmc_control && _mmc_port && _mmc_port->selectable() >= 0) {
1305 pfd[nfds].fd = _mmc_port->selectable();
1306 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1307 ports[nfds] = _mmc_port;
1311 /* if MTC is being handled on a different port from MMC
1312 or we are not handling MMC at all, poll
1316 if (_mtc_port && (_mtc_port != _mmc_port || !mmc_control) && _mtc_port->selectable() >= 0) {
1317 pfd[nfds].fd = _mtc_port->selectable();
1318 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1319 ports[nfds] = _mtc_port;
1323 if (_midi_port && (_midi_port != _mmc_port || !mmc_control) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1324 pfd[nfds].fd = _midi_port->selectable();
1325 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1326 ports[nfds] = _midi_port;
1330 if (!midi_timeouts.empty()) {
1331 timeout = 100; /* 10msecs */
1333 timeout = -1; /* if there is no data, we don't care */
1337 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1338 if (poll (pfd, nfds, timeout) < 0) {
1339 if (errno == EINTR) {
1340 /* gdb at work, perhaps */
1344 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1348 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1353 /* check the transport request pipe */
1355 if (pfd[0].revents & ~POLLIN) {
1356 error << _("Error on transport thread request pipe") << endmsg;
1360 if (pfd[0].revents & POLLIN) {
1364 // cerr << "MIDI request FIFO ready\n";
1367 /* empty the pipe of all current requests */
1370 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1373 if ((size_t) nread < sizeof (foo)) {
1378 } else if (nread == 0) {
1380 } else if (errno == EAGAIN) {
1383 fatal << _("Error reading from transport request pipe") << endmsg;
1388 while (midi_requests.read (&request, 1) == 1) {
1390 switch (request->type) {
1392 case MIDIRequest::SendFullMTC:
1393 // cerr << "send full MTC\n";
1394 send_full_time_code ();
1395 // cerr << "... done\n";
1398 case MIDIRequest::SendMTC:
1399 // cerr << "send qtr MTC\n";
1400 send_midi_time_code ();
1401 // cerr << "... done\n";
1404 case MIDIRequest::SendMMC:
1405 // cerr << "send MMC\n";
1406 deliver_mmc (request->mmc_cmd, request->locate_frame);
1407 // cerr << "... done\n";
1410 case MIDIRequest::SendMessage:
1411 // cerr << "send Message\n";
1412 deliver_midi_message (request->port, request->ev, request->chan, request->data);
1413 // cerr << "... done\n";
1416 case MIDIRequest::Deliver:
1417 // cerr << "deliver\n";
1418 deliver_data (_midi_port, request->buf, request->size);
1419 // cerr << "... done\n";
1422 case MIDIRequest::PortChange:
1423 /* restart poll with new ports */
1424 // cerr << "rebind\n";
1428 case MIDIRequest::Quit:
1430 pthread_exit_pbd (0);
1448 /* now read the rest of the ports */
1450 for (int p = 1; p < nfds; ++p) {
1451 if ((pfd[p].revents & ~POLLIN)) {
1452 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1456 if (pfd[p].revents & POLLIN) {
1458 midi_read (ports[p]);
1462 /* timeout driven */
1464 if (fds_ready < 2 && timeout != -1) {
1466 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1468 MidiTimeoutList::iterator tmp;
1473 midi_timeouts.erase (i);
1484 Session::get_mmc_control () const
1490 Session::get_midi_control () const
1492 return midi_control;