NO-OP: whitespace
[ardour.git] / libs / ardour / session_midi.cc
1 /*
2   Copyright (C) 1999-2002 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <string>
21 #include <cmath>
22 #include <cerrno>
23 #include <cassert>
24 #include <unistd.h>
25
26 #include <boost/shared_ptr.hpp>
27
28 #include <glibmm/main.h>
29
30 #include "midi++/mmc.h"
31 #include "midi++/port.h"
32
33 #include "pbd/error.h"
34 #include "pbd/pthread_utils.h"
35 #include "pbd/timersub.h"
36 #include "pbd/stacktrace.h"
37
38 #include "temporal/time.h"
39
40 #include "ardour/audio_track.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/debug.h"
43 #include "ardour/midi_port.h"
44 #include "ardour/midi_track.h"
45 #include "ardour/midi_ui.h"
46 #include "ardour/profile.h"
47 #include "ardour/session.h"
48 #include "ardour/transport_master.h"
49 #include "ardour/ticker.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace MIDI;
57 using namespace Glib;
58
59 void
60 Session::midi_panic()
61 {
62         {
63                 boost::shared_ptr<RouteList> r = routes.reader ();
64
65                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
66                         MidiTrack *track = dynamic_cast<MidiTrack*>((*i).get());
67                         if (track != 0) {
68                                 track->midi_panic();
69                         }
70                 }
71         }
72 }
73
74 void
75 Session::setup_midi_control ()
76 {
77         outbound_mtc_timecode_frame = 0;
78         next_quarter_frame_to_send = 0;
79
80         /* Set up the qtr frame message */
81
82         mtc_msg[0] = 0xf1;
83         mtc_msg[2] = 0xf1;
84         mtc_msg[4] = 0xf1;
85         mtc_msg[6] = 0xf1;
86         mtc_msg[8] = 0xf1;
87         mtc_msg[10] = 0xf1;
88         mtc_msg[12] = 0xf1;
89         mtc_msg[14] = 0xf1;
90 }
91
92 void
93 Session::spp_start ()
94 {
95         if (Config->get_mmc_control ()) {
96                 request_transport_speed (1.0);
97         }
98 }
99
100 void
101 Session::spp_continue ()
102 {
103         spp_start ();
104 }
105
106 void
107 Session::spp_stop ()
108 {
109         if (Config->get_mmc_control ()) {
110                 request_stop ();
111         }
112 }
113
114 void
115 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
116 {
117         if (Config->get_mmc_control ()) {
118                 request_transport_speed (1.0);
119         }
120 }
121
122 void
123 Session::mmc_record_pause (MIDI::MachineControl &/*mmc*/)
124 {
125         if (Config->get_mmc_control ()) {
126                 maybe_enable_record();
127         }
128 }
129
130 void
131 Session::mmc_record_strobe (MIDI::MachineControl &/*mmc*/)
132 {
133         if (Profile->get_trx()) {
134
135                 /* In Tracks Live, there is no concept of punch, so we just
136                    treat RecordStrobe like RecordPause. This violates the MMC
137                    specification.
138                 */
139
140                 if (Config->get_mmc_control()) {
141                         maybe_enable_record();
142                 }
143                 return;
144         }
145
146         if (!Config->get_mmc_control() || (_step_editors > 0)) {
147                 return;
148         }
149
150         /* record strobe does an implicit "Play" command */
151
152         if (_transport_speed != 1.0) {
153
154                 /* start_transport() will move from Enabled->Recording, so we
155                    don't need to do anything here except enable recording.
156                    its not the same as maybe_enable_record() though, because
157                    that *can* switch to Recording, which we do not want.
158                 */
159
160                 save_state ("", true);
161                 g_atomic_int_set (&_record_status, Enabled);
162                 RecordStateChanged (); /* EMIT SIGNAL */
163
164                 request_transport_speed (1.0);
165
166         } else {
167
168                 enable_record ();
169         }
170 }
171
172 void
173 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
174 {
175         if (Config->get_mmc_control ()) {
176                 disable_record (false);
177         }
178 }
179
180 void
181 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
182 {
183         if (Config->get_mmc_control ()) {
184                 request_stop ();
185         }
186 }
187
188 void
189 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
190 {
191         if (Config->get_mmc_control ()) {
192
193                 /* We support RECORD_PAUSE, so the spec says that
194                    we must interpret PAUSE like RECORD_PAUSE if
195                    recording.
196                 */
197
198                 if (actively_recording()) {
199                         maybe_enable_record ();
200                 } else {
201                         request_stop ();
202                 }
203         }
204 }
205
206 static bool step_queued = false;
207
208 void
209 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
210 {
211         if (!Config->get_mmc_control ()) {
212                 return;
213         }
214
215         struct timeval now;
216         struct timeval diff = { 0, 0 };
217
218         gettimeofday (&now, 0);
219
220         timersub (&now, &last_mmc_step, &diff);
221
222         gettimeofday (&now, 0);
223         timersub (&now, &last_mmc_step, &diff);
224
225         if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
226                 return;
227         }
228
229         double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
230         double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
231
232         if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
233                 /* change direction */
234                 step_speed = cur_speed;
235         } else {
236                 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
237         }
238
239         step_speed *= 0.25;
240
241 #if 0
242         cerr << "delta = " << diff_secs
243              << " ct = " << _transport_speed
244              << " steps = " << steps
245              << " new speed = " << cur_speed
246              << " speed = " << step_speed
247              << endl;
248 #endif
249
250         request_transport_speed_nonzero (step_speed);
251         last_mmc_step = now;
252
253         if (!step_queued) {
254                 if (midi_control_ui) {
255                         RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
256                         tsrc->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout));
257                         tsrc->attach (midi_control_ui->main_loop()->get_context());
258                         step_queued = true;
259                 }
260         }
261 }
262
263 void
264 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
265 {
266         if (Config->get_mmc_control ()) {
267                 request_transport_speed(-8.0f);
268         }
269 }
270
271 void
272 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
273 {
274         if (Config->get_mmc_control ()) {
275                 request_transport_speed(8.0f);
276         }
277 }
278
279 void
280 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
281 {
282         if (!Config->get_mmc_control ()) {
283                 return;
284         }
285
286         samplepos_t target_sample;
287         Timecode::Time timecode;
288
289         timecode.hours = mmc_tc[0] & 0xf;
290         timecode.minutes = mmc_tc[1];
291         timecode.seconds = mmc_tc[2];
292         timecode.frames = mmc_tc[3];
293         timecode.rate = timecode_frames_per_second();
294         timecode.drop = timecode_drop_frames();
295
296         // Also takes timecode offset into account:
297         timecode_to_sample( timecode, target_sample, true /* use_offset */, false /* use_subframes */ );
298
299         if (target_sample > max_samplepos) {
300                 target_sample = max_samplepos;
301         }
302
303         /* Some (all?) MTC/MMC devices do not send a full MTC frame
304            at the end of a locate, instead sending only an MMC
305            locate command. This causes the current position
306            of an MTC slave to become out of date. Catch this.
307         */
308
309         boost::shared_ptr<MTC_TransportMaster> mtcs = boost::dynamic_pointer_cast<MTC_TransportMaster> (transport_master());
310
311         if (mtcs) {
312                 // cerr << "Locate *with* MTC slave\n";
313                 mtcs->handle_locate (mmc_tc);
314         } else {
315                 // cerr << "Locate without MTC slave\n";
316                 request_locate (target_sample, false);
317         }
318 }
319
320 void
321 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
322 {
323         if (!Config->get_mmc_control ()) {
324                 return;
325         }
326
327         if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
328                 speed *= Config->get_shuttle_speed_factor();
329         }
330
331         if (forw) {
332                 request_transport_speed_nonzero (speed);
333         } else {
334                 request_transport_speed_nonzero (-speed);
335         }
336 }
337
338 boost::shared_ptr<Route>
339 Session::get_midi_nth_route_by_id (PresentationInfo::order_t n) const
340 {
341         PresentationInfo::Flag f;
342
343         /* These numbers are defined by the MMC specification.
344          */
345
346         if (n == 318) {
347                 f = PresentationInfo::MasterOut;
348         } else if (n == 319) {
349                 f = PresentationInfo::MonitorOut;
350         } else {
351                 f = PresentationInfo::Route;
352         }
353
354         boost::shared_ptr<RouteList> r = routes.reader ();
355         PresentationInfo::order_t match_cnt = 0;
356
357         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
358                 if ((*i)->presentation_info().flag_match (f)) {
359                         if (match_cnt++ == n) {
360                                 return *i;
361                         }
362                 }
363         }
364
365         return boost::shared_ptr<Route>();
366 }
367
368 void
369 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
370 {
371         if (!Config->get_mmc_control ()) {
372                 return;
373         }
374
375         boost::shared_ptr<Route> r = get_midi_nth_route_by_id (trk);
376
377         if (r) {
378                 boost::shared_ptr<AudioTrack> at;
379
380                 if ((at = boost::dynamic_pointer_cast<AudioTrack> (r))) {
381                         at->rec_enable_control()->set_value (enabled, Controllable::UseGroup);
382                 }
383         }
384 }
385
386 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
387  * This resets the MTC code, the next quarter frame message that is sent will be
388  * the first one with the beginning of this cycle as the new start point.
389  * @param t time to send.
390  */
391 int
392 Session::send_full_time_code (samplepos_t const t, MIDI::pframes_t nframes)
393 {
394         /* This function could easily send at a given sample offset, but would
395          * that be useful?  Does ardour do sub-block accurate locating? [DR] */
396
397         MIDI::byte msg[10];
398         Timecode::Time timecode;
399
400         _send_timecode_update = false;
401
402         if (_engine.freewheeling() || !Config->get_send_mtc()) {
403                 return 0;
404         }
405
406         if (transport_master_is_external() && !transport_master()->locked()) {
407                 return 0;
408         }
409
410         // Get timecode time for the given time
411         sample_to_timecode (t, timecode, true /* use_offset */, false /* no subframes */);
412
413         // sample-align outbound to rounded (no subframes) timecode
414         samplepos_t mtc_tc;
415         timecode_to_sample(timecode, mtc_tc, true, false);
416         outbound_mtc_timecode_frame = mtc_tc;
417         transmitting_timecode_time = timecode;
418
419         LatencyRange mtc_out_latency = {0, 0}; // TODO cache this, update on engine().GraphReordered()
420         _midi_ports->mtc_output_port ()->get_connected_latency_range (ltc_out_latency, true);
421         sampleoffset_t mtc_offset = mtc_out_latency.max;
422
423         // only if rolling.. ?
424         outbound_mtc_timecode_frame += mtc_offset;
425
426         // outbound_mtc_timecode_frame needs to be >= _transport_sample
427         // or a new full timecode will be queued next cycle.
428         while (outbound_mtc_timecode_frame < t) {
429                 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
430                 outbound_mtc_timecode_frame += _samples_per_timecode_frame;
431         }
432
433         double const quarter_frame_duration = ((samplecnt_t) _samples_per_timecode_frame) / 4.0;
434         if (ceil((t - mtc_tc) / quarter_frame_duration) > 0) {
435                 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
436                 outbound_mtc_timecode_frame += _samples_per_timecode_frame;
437         }
438
439         DEBUG_TRACE (DEBUG::MTC, string_compose ("Full MTC TC %1 (off %2)\n", outbound_mtc_timecode_frame, mtc_offset));
440
441         /* according to MTC spec 24, 30 drop and 30 non-drop TC, the frame-number represented by 8 quarter frames must be even. */
442         if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
443                 /* start MTC quarter frame transmission on an even frame */
444                 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
445                 outbound_mtc_timecode_frame += _samples_per_timecode_frame;
446         }
447
448         next_quarter_frame_to_send = 0;
449
450         // Sync slave to the same Timecode time as we are on
451         msg[0] = 0xf0;
452         msg[1] = 0x7f;
453         msg[2] = 0x7f;
454         msg[3] = 0x1;
455         msg[4] = 0x1;
456         msg[9] = 0xf7;
457
458         msg[5] = mtc_timecode_bits | (timecode.hours % 24);
459         msg[6] = timecode.minutes;
460         msg[7] = timecode.seconds;
461         msg[8] = timecode.frames;
462
463         // Send message at offset 0, sent time is for the start of this cycle
464
465         MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer (nframes));
466         mb.push_back (Port::port_offset(), sizeof (msg), msg);
467
468         _pframes_since_last_mtc = 0;
469         return 0;
470 }
471
472 /** Send MTC (quarter-frame) messages for this cycle.
473  * Must be called exactly once per cycle from the process thread.  Realtime safe.
474  * This function assumes the state of full Timecode is sane, eg. the slave is
475  * expecting quarter frame messages and has the right frame of reference (any
476  * full MTC Timecode time messages that needed to be sent should have been sent
477  * earlier already this cycle by send_full_time_code)
478  */
479 int
480 Session::send_midi_time_code_for_cycle (samplepos_t start_sample, samplepos_t end_sample, ARDOUR::pframes_t nframes)
481 {
482         // start_sample == start_sample  for normal cycles
483         // start_sample > _transport_sample  for split cycles
484         if (_engine.freewheeling() || !_send_qf_mtc || transmitting_timecode_time.negative || (next_quarter_frame_to_send < 0)) {
485                 // cerr << "(MTC) Not sending MTC\n";
486                 return 0;
487         }
488         if (transport_master_is_external() && !transport_master()->locked()) {
489                 return 0;
490         }
491
492         if (_transport_speed < 0) {
493                 // we don't support rolling backwards
494                 return 0;
495         }
496
497         /* MTC is max. 30 fps - assert() below will fail
498          * TODO actually limit it to 24,25,29df,30fps
499          * talk to oofus, first.
500          */
501         if (Timecode::timecode_to_frames_per_second(config.get_timecode_format()) > 30) {
502                 return 0;
503         }
504
505         assert (next_quarter_frame_to_send >= 0);
506         assert (next_quarter_frame_to_send <= 7);
507
508         /* Duration of one quarter frame */
509         double const quarter_frame_duration = _samples_per_timecode_frame / 4.0;
510
511         DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 MT %3 QF %4 QD %5\n",
512                                                  _transport_sample, start_sample, outbound_mtc_timecode_frame,
513                                                  next_quarter_frame_to_send, quarter_frame_duration));
514
515         if (rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration)) < _transport_sample) {
516                 // send full timecode and set outbound_mtc_timecode_frame, next_quarter_frame_to_send
517                 send_full_time_code (_transport_sample, nframes);
518         }
519
520         if (rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration)) < start_sample) {
521                 // no QF for this cycle
522                 return 0;
523         }
524
525         /* Send quarter frames for this cycle */
526         while (end_sample > rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))) {
527
528                 DEBUG_TRACE (DEBUG::MTC, string_compose ("next sample to send: %1\n", next_quarter_frame_to_send));
529
530                 switch (next_quarter_frame_to_send) {
531                 case 0:
532                         mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
533                         break;
534                 case 1:
535                         mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
536                         break;
537                 case 2:
538                         mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
539                         break;
540                 case 3:
541                         mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
542                         break;
543                 case 4:
544                         mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
545                         break;
546                 case 5:
547                         mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
548                         break;
549                 case 6:
550                         mtc_msg[1] = 0x60 | ((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf);
551                         break;
552                 case 7:
553                         mtc_msg[1] = 0x70 | (((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf0) >> 4);
554                         break;
555                 }
556
557                 const samplepos_t msg_time = rint(outbound_mtc_timecode_frame   + (quarter_frame_duration * next_quarter_frame_to_send));
558
559                 // This message must fall within this block or something is broken
560                 assert (msg_time >= start_sample);
561                 assert (msg_time < end_sample);
562
563                 /* convert from session samples back to JACK samples using the transport speed */
564                 ARDOUR::pframes_t const out_stamp = (msg_time - start_sample) / _transport_speed;
565                 assert (out_stamp < nframes);
566
567                 MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer(nframes));
568                 if (!mb.push_back (out_stamp, 2, mtc_msg)) {
569                         error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
570                               << endmsg;
571                         return -1;
572                 }
573
574 #ifndef NDEBUG
575                 if (DEBUG_ENABLED(DEBUG::MTC)) {
576                         DEBUG_STR_DECL(foo);
577                         DEBUG_STR_APPEND(foo,"sending ");
578                         DEBUG_STR_APPEND(foo, transmitting_timecode_time);
579                         DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
580                                                                  out_stamp));
581                 }
582 #endif
583
584                 // Increment quarter frame counter
585                 next_quarter_frame_to_send++;
586
587                 if (next_quarter_frame_to_send >= 8) {
588                         // Wrap quarter frame counter
589                         next_quarter_frame_to_send = 0;
590                         // Increment timecode time twice
591                         Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
592                         Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
593                         // Increment timing of first quarter frame
594                         outbound_mtc_timecode_frame += 2.0 * _samples_per_timecode_frame;
595                 }
596         }
597
598         return 0;
599 }
600
601 /***********************************************************************
602  OUTBOUND MMC STUFF
603 **********************************************************************/
604
605 void
606 Session::send_immediate_mmc (MachineControlCommand c)
607 {
608         if (AudioEngine::instance()->in_process_thread()) {
609                 _mmc->send (c, Port::port_offset());
610         } else {
611                 _mmc->send (c, 0);
612         }
613
614 }
615
616 bool
617 Session::mmc_step_timeout ()
618 {
619         struct timeval now;
620         struct timeval diff;
621         double diff_usecs;
622         gettimeofday (&now, 0);
623
624         timersub (&now, &last_mmc_step, &diff);
625         diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
626
627         if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
628                 /* too long or too slow, stop transport */
629                 request_transport_speed (0.0);
630                 step_queued = false;
631                 return false;
632         }
633
634         if (diff_usecs < 250000.0) {
635                 /* too short, just keep going */
636                 return true;
637         }
638
639         /* slow it down */
640
641         request_transport_speed_nonzero (_transport_speed * 0.75);
642         return true;
643 }
644
645 /***********************************************************************
646  OUTBOUND SYSTEM COMMON STUFF
647 **********************************************************************/
648
649
650 void
651 Session::send_song_position_pointer (samplepos_t)
652 {
653         if (midi_clock) {
654                 /* Do nothing for the moment */
655         }
656 }
657
658 int
659 Session::start_midi_thread ()
660 {
661         if (midi_control_ui) { return 0; }
662         midi_control_ui = new MidiControlUI (*this);
663         midi_control_ui->run ();
664         return 0;
665 }
666
667 boost::shared_ptr<ARDOUR::Port>
668 Session::midi_input_port () const
669 {
670         return _midi_ports->midi_input_port ();
671 }
672
673 boost::shared_ptr<ARDOUR::Port>
674 Session::midi_output_port () const
675 {
676         return _midi_ports->midi_output_port ();
677 }
678
679 boost::shared_ptr<ARDOUR::Port>
680 Session::mmc_output_port () const
681 {
682         return _midi_ports->mmc_output_port ();
683 }
684
685 boost::shared_ptr<ARDOUR::Port>
686 Session::mmc_input_port () const
687 {
688         return _midi_ports->mmc_input_port ();
689 }
690
691 boost::shared_ptr<ARDOUR::Port>
692 Session::scene_output_port () const
693 {
694         return _midi_ports->scene_output_port ();
695 }
696
697 boost::shared_ptr<ARDOUR::Port>
698 Session::scene_input_port () const
699 {
700         return _midi_ports->scene_input_port ();
701 }
702
703 boost::shared_ptr<MidiPort>
704 Session::midi_clock_output_port () const
705 {
706         return _midi_ports->midi_clock_output_port ();
707 }
708
709
710 boost::shared_ptr<MidiPort>
711 Session::mtc_output_port () const
712 {
713         return _midi_ports->mtc_output_port ();
714 }
715
716 void
717 Session::midi_track_presentation_info_changed (PropertyChange const& what_changed, boost::weak_ptr<MidiTrack> mt)
718 {
719         if (!Config->get_midi_input_follows_selection()) {
720                 return;
721         }
722
723         if (!what_changed.contains (Properties::selected)) {
724                 return;
725         }
726
727         boost::shared_ptr<MidiTrack> new_midi_target (mt.lock ());
728
729         if (new_midi_target->is_selected()) {
730                 rewire_selected_midi (new_midi_target);
731         }
732 }
733
734 void
735 Session::rewire_selected_midi (boost::shared_ptr<MidiTrack> new_midi_target)
736 {
737         if (!new_midi_target) {
738                 return;
739         }
740
741         boost::shared_ptr<MidiTrack> old_midi_target = current_midi_target.lock ();
742
743         if (new_midi_target == old_midi_target) {
744                 return;
745         }
746
747         vector<string> msp;
748         AudioEngine::instance()->get_midi_selection_ports (msp);
749
750         if (!msp.empty()) {
751
752                 if (old_midi_target) {
753                         old_midi_target->input()->disconnect (this);
754                 }
755
756                 for (vector<string>::const_iterator p = msp.begin(); p != msp.end(); ++p) {
757                         PortManager::MidiPortInformation mpi (AudioEngine::instance()->midi_port_information (*p));
758
759                         /* if a port is marked for control data, do not
760                          * disconnect it from everything since it may also be
761                          * used via a control surface or some other
762                          * functionality.
763                          */
764
765                         if (!(mpi.properties & MidiPortControl)) {
766                                 /* disconnect the port from everything */
767                                 AudioEngine::instance()->disconnect (*p);
768                         }
769                         /* connect it to the new target */
770                         new_midi_target->input()->connect (new_midi_target->input()->nth(0), (*p), this);
771                 }
772         }
773
774         current_midi_target = new_midi_target;
775 }
776
777 void
778 Session::rewire_midi_selection_ports ()
779 {
780         if (!Config->get_midi_input_follows_selection()) {
781                 return;
782         }
783
784         boost::shared_ptr<MidiTrack> target = current_midi_target.lock();
785
786         if (!target) {
787                 return;
788         }
789
790         vector<string> msp;
791         AudioEngine::instance()->get_midi_selection_ports (msp);
792
793         if (msp.empty()) {
794                 return;
795         }
796
797         target->input()->disconnect (this);
798
799         for (vector<string>::const_iterator p = msp.begin(); p != msp.end(); ++p) {
800                 AudioEngine::instance()->disconnect (*p);
801                 target->input()->connect (target->input()->nth (0), (*p), this);
802         }
803 }