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_mtc_port (string port_tag)
95 if (port_tag.length() == 0) {
97 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
98 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
102 if (_mtc_port == 0) {
112 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
113 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
119 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
123 Config->set_mtc_port_name (port_tag);
127 MTC_PortChanged(); /* EMIT SIGNAL */
128 change_midi_ports ();
134 Session::set_mmc_port (string port_tag)
137 if (port_tag.length() == 0) {
138 if (_mmc_port == 0) {
147 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
157 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
158 MMC_CommandSignature,
159 MMC_ResponseSignature);
163 (mem_fun (*this, &Session::mmc_deferred_play));
164 mmc->DeferredPlay.connect
165 (mem_fun (*this, &Session::mmc_deferred_play));
167 (mem_fun (*this, &Session::mmc_stop));
168 mmc->FastForward.connect
169 (mem_fun (*this, &Session::mmc_fast_forward));
171 (mem_fun (*this, &Session::mmc_rewind));
173 (mem_fun (*this, &Session::mmc_pause));
174 mmc->RecordPause.connect
175 (mem_fun (*this, &Session::mmc_record_pause));
176 mmc->RecordStrobe.connect
177 (mem_fun (*this, &Session::mmc_record_strobe));
178 mmc->RecordExit.connect
179 (mem_fun (*this, &Session::mmc_record_exit));
181 (mem_fun (*this, &Session::mmc_locate));
183 (mem_fun (*this, &Session::mmc_step));
185 (mem_fun (*this, &Session::mmc_shuttle));
186 mmc->TrackRecordStatusChange.connect
187 (mem_fun (*this, &Session::mmc_record_enable));
189 /* also handle MIDI SPP because its so common */
191 _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
192 _mmc_port->input()->contineu.connect (mem_fun (*this, &Session::spp_continue));
193 _mmc_port->input()->stop.connect (mem_fun (*this, &Session::spp_stop));
195 Config->set_mmc_port_name (port_tag);
198 MMC_PortChanged(); /* EMIT SIGNAL */
199 change_midi_ports ();
205 Session::set_midi_port (string port_tag)
208 if (port_tag.length() == 0) {
209 if (_midi_port == 0) {
218 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
224 /* XXX need something to forward this to control protocols ? or just
228 Config->set_midi_port_name (port_tag);
232 MIDI_PortChanged(); /* EMIT SIGNAL */
233 change_midi_ports ();
239 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
242 MIDI::Parser* input_parser;
245 if ((input_parser = port->input()) != 0) {
246 input_parser->trace (yn, &cout, "input: ");
251 if ((input_parser = _mmc_port->input()) != 0) {
252 input_parser->trace (yn, &cout, "input: ");
256 if (_mtc_port && _mtc_port != _mmc_port) {
257 if ((input_parser = _mtc_port->input()) != 0) {
258 input_parser->trace (yn, &cout, "input: ");
262 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
263 if ((input_parser = _midi_port->input()) != 0) {
264 input_parser->trace (yn, &cout, "input: ");
270 Config->set_trace_midi_input (yn);
274 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
277 MIDI::Parser* output_parser;
280 if ((output_parser = port->output()) != 0) {
281 output_parser->trace (yn, &cout, "output: ");
285 if ((output_parser = _mmc_port->output()) != 0) {
286 output_parser->trace (yn, &cout, "output: ");
290 if (_mtc_port && _mtc_port != _mmc_port) {
291 if ((output_parser = _mtc_port->output()) != 0) {
292 output_parser->trace (yn, &cout, "output: ");
296 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
297 if ((output_parser = _midi_port->output()) != 0) {
298 output_parser->trace (yn, &cout, "output: ");
305 Config->set_trace_midi_output (yn);
309 Session::get_trace_midi_input(MIDI::Port *port)
312 MIDI::Parser* input_parser;
314 if ((input_parser = port->input()) != 0) {
315 return input_parser->tracing();
320 if ((input_parser = _mmc_port->input()) != 0) {
321 return input_parser->tracing();
326 if ((input_parser = _mtc_port->input()) != 0) {
327 return input_parser->tracing();
332 if ((input_parser = _midi_port->input()) != 0) {
333 return input_parser->tracing();
343 Session::get_trace_midi_output(MIDI::Port *port)
346 MIDI::Parser* output_parser;
348 if ((output_parser = port->output()) != 0) {
349 return output_parser->tracing();
354 if ((output_parser = _mmc_port->output()) != 0) {
355 return output_parser->tracing();
360 if ((output_parser = _mtc_port->output()) != 0) {
361 return output_parser->tracing();
366 if ((output_parser = _midi_port->output()) != 0) {
367 return output_parser->tracing();
378 Session::setup_midi_control ()
380 outbound_mtc_smpte_frame = 0;
381 next_quarter_frame_to_send = 0;
383 /* setup the MMC buffer */
385 mmc_buffer[0] = 0xf0; // SysEx
386 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
387 mmc_buffer[2] = 0x7f; // "broadcast" device ID
388 mmc_buffer[3] = 0x6; // MCC
390 /* Set up the qtr frame message */
401 if (_mmc_port != 0) {
403 Config->set_send_mmc (session_send_mmc);
408 session_send_mmc = false;
411 if (_mtc_port != 0) {
413 Config->set_send_mtc (session_send_mtc);
416 session_send_mtc = false;
422 Session::midi_read (MIDI::Port* port)
426 /* reading from the MIDI port activates the Parser
427 that in turn generates signals that we care
428 about. the port is already set to NONBLOCK so that
429 can read freely here.
434 // cerr << "+++ READ ON " << port->name() << endl;
436 int nread = port->read (buf, sizeof (buf));
438 // cerr << "-- READ (" << nread << " ON " << port->name() << endl;
441 if ((size_t) nread < sizeof (buf)) {
446 } else if (nread == 0) {
448 } else if (errno == EAGAIN) {
451 fatal << string_compose(_("Error reading from MIDI port %1"), port->name()) << endmsg;
461 Session::spp_start (Parser& ignored)
463 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
464 request_transport_speed (1.0);
469 Session::spp_continue (Parser& ignored)
475 Session::spp_stop (Parser& ignored)
477 if (Config->get_mmc_control()) {
483 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
485 if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
486 request_transport_speed (1.0);
491 Session::mmc_record_pause (MIDI::MachineControl &mmc)
493 if (Config->get_mmc_control()) {
494 maybe_enable_record();
499 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
501 if (!Config->get_mmc_control())
504 /* record strobe does an implicit "Play" command */
506 if (_transport_speed != 1.0) {
508 /* start_transport() will move from Enabled->Recording, so we
509 don't need to do anything here except enable recording.
510 its not the same as maybe_enable_record() though, because
511 that *can* switch to Recording, which we do not want.
514 save_state ("", true);
515 g_atomic_int_set (&_record_status, Enabled);
516 RecordStateChanged (); /* EMIT SIGNAL */
518 request_transport_speed (1.0);
527 Session::mmc_record_exit (MIDI::MachineControl &mmc)
529 if (Config->get_mmc_control()) {
530 disable_record (false);
535 Session::mmc_stop (MIDI::MachineControl &mmc)
537 if (Config->get_mmc_control()) {
543 Session::mmc_pause (MIDI::MachineControl &mmc)
545 if (Config->get_mmc_control()) {
547 /* We support RECORD_PAUSE, so the spec says that
548 we must interpret PAUSE like RECORD_PAUSE if
552 if (actively_recording()) {
553 maybe_enable_record ();
560 static bool step_queued = false;
563 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
565 if (!Config->get_mmc_control()) {
570 struct timeval diff = { 0, 0 };
572 gettimeofday (&now, 0);
574 timersub (&now, &last_mmc_step, &diff);
576 gettimeofday (&now, 0);
577 timersub (&now, &last_mmc_step, &diff);
579 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
583 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
584 double cur_speed = (((steps * 0.5) * smpte_frames_per_second()) / diff_secs) / smpte_frames_per_second();
586 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
587 /* change direction */
588 step_speed = cur_speed;
590 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
596 cerr << "delta = " << diff_secs
597 << " ct = " << _transport_speed
598 << " steps = " << steps
599 << " new speed = " << cur_speed
600 << " speed = " << step_speed
604 request_transport_speed (step_speed);
608 midi_timeouts.push_back (mem_fun (*this, &Session::mmc_step_timeout));
614 Session::mmc_rewind (MIDI::MachineControl &mmc)
616 if (Config->get_mmc_control()) {
617 request_transport_speed(-8.0f);
622 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
624 if (Config->get_mmc_control()) {
625 request_transport_speed(8.0f);
630 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
632 if (!Config->get_mmc_control()) {
636 nframes_t target_frame;
639 smpte.hours = mmc_tc[0] & 0xf;
640 smpte.minutes = mmc_tc[1];
641 smpte.seconds = mmc_tc[2];
642 smpte.frames = mmc_tc[3];
643 smpte.rate = smpte_frames_per_second();
644 smpte.drop = smpte_drop_frames();
646 // Also takes smpte offset into account:
647 smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
649 if (target_frame > max_frames) {
650 target_frame = max_frames;
653 /* Some (all?) MTC/MMC devices do not send a full MTC frame
654 at the end of a locate, instead sending only an MMC
655 locate command. This causes the current position
656 of an MTC slave to become out of date. Catch this.
659 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
662 // cerr << "Locate *with* MTC slave\n";
663 mtcs->handle_locate (mmc_tc);
665 // cerr << "Locate without MTC slave\n";
666 request_locate (target_frame, false);
671 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
673 if (!Config->get_mmc_control()) {
677 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
678 speed *= Config->get_shuttle_speed_factor();
682 request_transport_speed (speed);
684 request_transport_speed (-speed);
689 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
691 if (Config->get_mmc_control()) {
693 RouteList::iterator i;
694 boost::shared_ptr<RouteList> r = routes.reader();
696 for (i = r->begin(); i != r->end(); ++i) {
699 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
700 if (trk == at->remote_control_id()) {
701 at->set_record_enable (enabled, &mmc);
711 Session::change_midi_ports ()
714 MIDIRequest* request = new MIDIRequest;
716 request->type = MIDIRequest::PortChange;
717 midi_requests.write (&request, 1);
722 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
723 * This resets the MTC code, the next quarter frame message that is sent will be
724 * the first one with the beginning of this cycle as the new start point.
726 * Audio thread only, realtime safe. MIDI::Manager::cycle_start must
727 * have been called with the appropriate nframes parameter this cycle.
730 Session::send_full_time_code(jack_nframes_t nframes)
732 /* This function could easily send at a given frame offset, but would
733 * that be useful? Does ardour do sub-block accurate locating? [DR] */
738 _send_smpte_update = false;
740 if (_mtc_port == 0 || !session_send_mtc) {
744 // Get smpte time for this transport frame
745 sample_to_smpte(_transport_frame, smpte, true /* use_offset */, false /* no subframes */);
747 transmitting_smpte_time = smpte;
748 outbound_mtc_smpte_frame = _transport_frame;
750 // I don't understand this bit yet.. [DR]
751 if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
752 // start MTC quarter frame transmission on an even frame
753 SMPTE::increment( transmitting_smpte_time );
754 outbound_mtc_smpte_frame += (jack_nframes_t) _frames_per_smpte_frame;
757 // Compensate for audio latency
758 outbound_mtc_smpte_frame += _worst_output_latency;
760 next_quarter_frame_to_send = 0;
762 // Sync slave to the same SMPTE time as we are on
770 msg[5] = mtc_smpte_bits | smpte.hours;
771 msg[6] = smpte.minutes;
772 msg[7] = smpte.seconds;
773 msg[8] = smpte.frames;
775 cerr << "MTC: Sending full time code at " << outbound_mtc_smpte_frame << endl;
777 // Send message at offset 0, sent time is for the start of this cycle
778 if (!_mtc_port->midimsg (msg, sizeof (msg), 0)) {
779 error << _("Session: could not send full MIDI time code") << endmsg;
787 /** Sends MTC (quarter-frame) messages for this cycle.
788 * Must be called exactly once per cycle from the audio thread. Realtime safe.
789 * This function assumes the state of full SMPTE is sane, eg. the slave is
790 * expecting quarter frame messages and has the right frame of reference (any
791 * full MTC SMPTE time messages that needed to be sent should have been sent
792 * earlier already this cycle by send_full_time_code)
795 Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
797 assert (next_quarter_frame_to_send >= 0);
798 assert (next_quarter_frame_to_send <= 7);
800 if (next_quarter_frame_to_send < 0)
802 printf("Negative????\n");
805 if (_mtc_port == 0 || !session_send_mtc || transmitting_smpte_time.negative
806 /*|| (next_quarter_frame_to_send < 0)*/ ) {
807 //printf("(MTC) Not sending MTC\n");
811 /* Duration of one quarter frame */
812 jack_nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
814 //cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
815 //<< " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
817 // FIXME: this should always be true
818 //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
819 // > _transport_frame);
822 // Send quarter frames for this cycle
823 while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
824 (next_quarter_frame_to_send * quarter_frame_duration))) {
826 //cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
828 switch (next_quarter_frame_to_send) {
830 mtc_msg[1] = 0x00 | (transmitting_smpte_time.frames & 0xf);
833 mtc_msg[1] = 0x10 | ((transmitting_smpte_time.frames & 0xf0) >> 4);
836 mtc_msg[1] = 0x20 | (transmitting_smpte_time.seconds & 0xf);
839 mtc_msg[1] = 0x30 | ((transmitting_smpte_time.seconds & 0xf0) >> 4);
842 mtc_msg[1] = 0x40 | (transmitting_smpte_time.minutes & 0xf);
845 mtc_msg[1] = 0x50 | ((transmitting_smpte_time.minutes & 0xf0) >> 4);
848 mtc_msg[1] = 0x60 | ((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf);
851 mtc_msg[1] = 0x70 | (((mtc_smpte_bits|transmitting_smpte_time.hours) & 0xf0) >> 4);
855 const jack_nframes_t msg_time = (outbound_mtc_smpte_frame
856 + (quarter_frame_duration * next_quarter_frame_to_send));
858 // This message must fall within this block or something is broken
859 assert(msg_time >= _transport_frame);
860 assert(msg_time < _transport_frame + nframes);
862 jack_nframes_t out_stamp = msg_time - _transport_frame;
863 assert(out_stamp < nframes);
865 if (!_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
866 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
871 /*cerr << "(MTC) SMPTE: " << transmitting_smpte_time.hours
872 << ":" << transmitting_smpte_time.minutes
873 << ":" << transmitting_smpte_time.seconds
874 << ":" << transmitting_smpte_time.frames
875 << ", qfm = " << next_quarter_frame_to_send
876 << ", stamp = " << out_stamp
877 << ", delta = " << _transport_frame + out_stamp - last_time << endl;*/
879 // Increment quarter frame counter
880 next_quarter_frame_to_send++;
882 if (next_quarter_frame_to_send >= 8) {
883 // Wrap quarter frame counter
884 next_quarter_frame_to_send = 0;
885 // Increment smpte time twice
886 SMPTE::increment( transmitting_smpte_time );
887 SMPTE::increment( transmitting_smpte_time );
888 // Re-calculate timing of first quarter frame
889 //smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
890 outbound_mtc_smpte_frame += 8 * quarter_frame_duration;
891 // Compensate for audio latency
892 outbound_mtc_smpte_frame += _worst_output_latency;
899 /***********************************************************************
901 **********************************************************************/
904 Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, nframes_t target_frame)
906 MIDIRequest* request;
908 if (_mtc_port == 0 || !session_send_mmc) {
912 request = new MIDIRequest;
913 request->type = MIDIRequest::SendMMC;
914 request->mmc_cmd = cmd;
915 request->locate_frame = target_frame;
917 midi_requests.write (&request, 1);
922 /** Send an MMC command at the given absolute timestamp (@a where).
924 * This must be called in the process thread, and @a where must fall within
925 * this process cycle or horrible things will happen.
928 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
930 using namespace MIDI;
934 if (_mmc_port == 0 || !session_send_mmc) {
935 //cerr << "Not delivering MMC " << _mmc_port << " - " << send_mmc << endl;
939 mmc_buffer[nbytes++] = cmd;
941 //cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
944 case MachineControl::cmdLocate:
945 smpte_time_subframes (where, smpte);
947 mmc_buffer[nbytes++] = 0x6; // byte count
948 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
949 mmc_buffer[nbytes++] = smpte.hours;
950 mmc_buffer[nbytes++] = smpte.minutes;
951 mmc_buffer[nbytes++] = smpte.seconds;
952 mmc_buffer[nbytes++] = smpte.frames;
953 mmc_buffer[nbytes++] = smpte.subframes;
956 case MachineControl::cmdStop:
959 case MachineControl::cmdPlay:
960 /* always convert Play into Deferred Play */
962 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
965 case MachineControl::cmdDeferredPlay:
968 case MachineControl::cmdRecordStrobe:
971 case MachineControl::cmdRecordExit:
974 case MachineControl::cmdRecordPause:
983 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
985 assert(where >= _transport_frame);
987 // FIXME: timestamp correct? [DR]
988 if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), where - _transport_frame)) {
989 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
991 cerr << "Sending MMC\n";
997 Session::mmc_step_timeout ()
1000 struct timeval diff;
1002 gettimeofday (&now, 0);
1004 timersub (&now, &last_mmc_step, &diff);
1005 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
1007 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
1008 /* too long or too slow, stop transport */
1009 request_transport_speed (0.0);
1010 step_queued = false;
1014 if (diff_usecs < 250000.0) {
1015 /* too short, just keep going */
1021 request_transport_speed (_transport_speed * 0.75);
1027 Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1029 // in another thread, really
1031 MIDIRequest* request = new MIDIRequest;
1033 request->type = MIDIRequest::SendMessage;
1034 request->port = port;
1037 request->data = data;
1039 midi_requests.write (&request, 1);
1040 poke_midi_thread ();
1045 Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
1047 // in another thread, really
1049 MIDIRequest* request = new MIDIRequest;
1051 request->type = MIDIRequest::Deliver;
1052 request->port = port;
1054 request->size = bufsize;
1056 midi_requests.write (&request, 1);
1057 poke_midi_thread ();
1063 This is aaalll gone.
1067 Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
1069 if (port == 0 || ev == MIDI::none) {
1073 midi_msg[0] = (ev & 0xF0) | (ch & 0xF);
1074 midi_msg[1] = data.controller_number;
1075 midi_msg[2] = data.value;
1077 port->write (midi_msg, 3);
1081 Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
1084 port->write (buf, size);
1087 /* this is part of the semantics of the Deliver request */
1094 /*---------------------------------------------------------------------------
1096 ---------------------------------------------------------------------------*/
1100 Session::start_midi_thread ()
1102 if (pipe (midi_request_pipe)) {
1103 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
1107 if (fcntl (midi_request_pipe[0], F_SETFL, O_NONBLOCK)) {
1108 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal read pipe (%1)"), strerror (errno)) << endmsg;
1112 if (fcntl (midi_request_pipe[1], F_SETFL, O_NONBLOCK)) {
1113 error << string_compose(_("UI: cannot set O_NONBLOCK on " "signal write pipe (%1)"), strerror (errno)) << endmsg;
1117 if (pthread_create_and_store ("transport", &midi_thread, 0, _midi_thread_work, this)) {
1118 error << _("Session: could not create transport thread") << endmsg;
1122 // pthread_detach (midi_thread);
1128 Session::terminate_midi_thread ()
1130 MIDIRequest* request = new MIDIRequest;
1133 request->type = MIDIRequest::Quit;
1135 midi_requests.write (&request, 1);
1136 poke_midi_thread ();
1138 pthread_join (midi_thread, &status);
1142 Session::poke_midi_thread ()
1146 if (write (midi_request_pipe[1], &c, 1) != 1) {
1147 error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
1152 Session::_midi_thread_work (void* arg)
1154 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
1155 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1157 ((Session *) arg)->midi_thread_work ();
1162 Session::midi_thread_work ()
1164 MIDIRequest* request;
1165 struct pollfd pfd[4];
1169 struct sched_param rtparam;
1172 vector<MIDI::Port*> ports;
1174 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
1176 memset (&rtparam, 0, sizeof (rtparam));
1177 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
1179 if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
1180 // do we care? not particularly.
1183 /* set up the port vector; 4 is the largest possible size for now */
1185 ports.push_back (0);
1186 ports.push_back (0);
1187 ports.push_back (0);
1188 ports.push_back (0);
1194 pfd[nfds].fd = midi_request_pipe[0];
1195 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1198 /* if we are using MMC control, we obviously have to listen
1199 on the appropriate port.
1202 if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
1203 pfd[nfds].fd = _mmc_port->selectable();
1204 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1205 ports[nfds] = _mmc_port;
1209 /* if MTC is being handled on a different port from MMC
1210 or we are not handling MMC at all, poll
1214 if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
1215 pfd[nfds].fd = _mtc_port->selectable();
1216 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1217 ports[nfds] = _mtc_port;
1221 if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
1222 pfd[nfds].fd = _midi_port->selectable();
1223 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
1224 ports[nfds] = _midi_port;
1228 if (!midi_timeouts.empty()) {
1229 timeout = 100; /* 10msecs */
1231 timeout = -1; /* if there is no data, we don't care */
1235 // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
1236 if (poll (pfd, nfds, timeout) < 0) {
1237 if (errno == EINTR) {
1238 /* gdb at work, perhaps */
1242 error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
1246 // cerr << "MIDI thread wakes at " << get_cycles () << endl;
1251 /* check the transport request pipe */
1253 if (pfd[0].revents & ~POLLIN) {
1254 error << _("Error on transport thread request pipe") << endmsg;
1258 if (pfd[0].revents & POLLIN) {
1262 // cerr << "MIDI request FIFO ready\n";
1265 /* empty the pipe of all current requests */
1268 size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
1271 if ((size_t) nread < sizeof (foo)) {
1276 } else if (nread == 0) {
1278 } else if (errno == EAGAIN) {
1281 fatal << _("Error reading from transport request pipe") << endmsg;
1286 while (midi_requests.read (&request, 1) == 1) {
1288 switch (request->type) {
1290 case MIDIRequest::SendFullMTC:
1291 // cerr << "send full MTC\n";
1292 send_full_time_code ();
1293 // cerr << "... done\n";
1296 case MIDIRequest::SendMTC:
1297 // cerr << "send qtr MTC\n";
1298 send_midi_time_code ();
1299 // cerr << "... done\n";
1302 case MIDIRequest::SendMMC:
1303 // cerr << "send MMC\n";
1304 deliver_mmc (request->mmc_cmd, request->locate_frame);
1305 // cerr << "... done\n";
1308 case MIDIRequest::SendMessage:
1309 // cerr << "send Message\n";
1310 deliver_midi_message (request->port, request->ev, request->chan, request->data);
1311 // cerr << "... done\n";
1314 case MIDIRequest::Deliver:
1315 // cerr << "deliver\n";
1316 deliver_data (_midi_port, request->buf, request->size);
1317 // cerr << "... done\n";
1320 case MIDIRequest::PortChange:
1321 /* restart poll with new ports */
1322 // cerr << "rebind\n";
1326 case MIDIRequest::Quit:
1328 pthread_exit_pbd (0);
1346 /* now read the rest of the ports */
1348 for (int p = 1; p < nfds; ++p) {
1349 if ((pfd[p].revents & ~POLLIN)) {
1350 // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
1354 if (pfd[p].revents & POLLIN) {
1356 midi_read (ports[p]);
1360 /* timeout driven */
1362 if (fds_ready < 2 && timeout != -1) {
1364 for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
1366 MidiTimeoutList::iterator tmp;
1371 midi_timeouts.erase (i);