Save/Restore MIDI Automation Controls (current CC, PGM)
[ardour.git] / libs / ardour / midi_track.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18 */
19 #include <cmath>
20
21 #ifdef COMPILER_MSVC
22 #include <float.h>
23
24 // 'std::isinf()' and 'std::isnan()' are not available in MSVC.
25 #define isinf_local(val) !((bool)_finite((double)val))
26 #define isnan_local(val) (bool)_isnan((double)val)
27 #else
28 #define isinf_local std::isinf
29 #define isnan_local std::isnan
30 #endif
31
32 #include "pbd/enumwriter.h"
33 #include "pbd/convert.h"
34 #include "evoral/midi_util.h"
35
36 #include "ardour/beats_frames_converter.h"
37 #include "ardour/buffer_set.h"
38 #include "ardour/debug.h"
39 #include "ardour/delivery.h"
40 #include "ardour/event_type_map.h"
41 #include "ardour/meter.h"
42 #include "ardour/midi_diskstream.h"
43 #include "ardour/midi_playlist.h"
44 #include "ardour/midi_port.h"
45 #include "ardour/midi_region.h"
46 #include "ardour/midi_track.h"
47 #include "ardour/monitor_control.h"
48 #include "ardour/parameter_types.h"
49 #include "ardour/port.h"
50 #include "ardour/processor.h"
51 #include "ardour/profile.h"
52 #include "ardour/route_group_specialized.h"
53 #include "ardour/session.h"
54 #include "ardour/session_playlists.h"
55 #include "ardour/utils.h"
56
57 #include "pbd/i18n.h"
58
59 namespace ARDOUR {
60 class InterThreadInfo;
61 class MidiSource;
62 class Region;
63 class SMFSource;
64 }
65
66 using namespace std;
67 using namespace ARDOUR;
68 using namespace PBD;
69
70 MidiTrack::MidiTrack (Session& sess, string name, TrackMode mode)
71         : Track (sess, name, PresentationInfo::MidiTrack, mode, DataType::MIDI)
72         , _immediate_events(6096) // FIXME: size?
73         , _step_edit_ring_buffer(64) // FIXME: size?
74         , _note_mode(Sustained)
75         , _step_editing (false)
76         , _input_active (true)
77 {
78         _session.SessionLoaded.connect_same_thread (*this, boost::bind (&MidiTrack::restore_controls, this));
79 }
80
81 MidiTrack::~MidiTrack ()
82 {
83 }
84
85 int
86 MidiTrack::init ()
87 {
88         if (Track::init ()) {
89                 return -1;
90         }
91
92         _input->changed.connect_same_thread (*this, boost::bind (&MidiTrack::track_input_active, this, _1, _2));
93
94         return 0;
95 }
96
97 boost::shared_ptr<Diskstream>
98 MidiTrack::create_diskstream ()
99 {
100         MidiDiskstream::Flag dflags = MidiDiskstream::Flag (MidiDiskstream::Recordable);
101
102         assert(_mode != Destructive);
103
104         return boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
105 }
106
107
108 bool
109 MidiTrack::can_be_record_safe ()
110 {
111         if (_step_editing) {
112                 return false;
113         }
114
115         return Track::can_be_record_safe ();
116 }
117
118 bool
119 MidiTrack::can_be_record_enabled ()
120 {
121         if (_step_editing) {
122                 return false;
123         }
124
125         return Track::can_be_record_enabled ();
126 }
127
128 void
129 MidiTrack::set_diskstream (boost::shared_ptr<Diskstream> ds)
130 {
131         /* We have to do this here, as Track::set_diskstream will cause a buffer refill,
132            and the diskstream must be set up to fill its buffers using the correct _note_mode.
133         */
134         boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream> (ds);
135         mds->set_note_mode (_note_mode);
136
137         Track::set_diskstream (ds);
138
139         mds->reset_tracker ();
140
141         _diskstream->set_track (this);
142         if (Profile->get_trx()) {
143                 _diskstream->set_destructive (false);
144         } else {
145                 _diskstream->set_destructive (_mode == Destructive);
146         }
147         _diskstream->set_record_enabled (false);
148
149         _diskstream_data_recorded_connection.disconnect ();
150         mds->DataRecorded.connect_same_thread (
151                 _diskstream_data_recorded_connection,
152                 boost::bind (&MidiTrack::diskstream_data_recorded, this, _1));
153
154         DiskstreamChanged (); /* EMIT SIGNAL */
155 }
156
157 boost::shared_ptr<MidiDiskstream>
158 MidiTrack::midi_diskstream() const
159 {
160         return boost::dynamic_pointer_cast<MidiDiskstream>(_diskstream);
161 }
162
163 int
164 MidiTrack::set_state (const XMLNode& node, int version)
165 {
166         XMLProperty const * prop;
167
168         /* This must happen before Track::set_state(), as there will be a buffer
169            fill during that call, and we must fill buffers using the correct
170            _note_mode.
171         */
172         if ((prop = node.property (X_("note-mode"))) != 0) {
173                 _note_mode = NoteMode (string_2_enum (prop->value(), _note_mode));
174         } else {
175                 _note_mode = Sustained;
176         }
177
178         if (Track::set_state (node, version)) {
179                 return -1;
180         }
181
182         // No destructive MIDI tracks (yet?)
183         _mode = Normal;
184
185         if ((prop = node.property ("input-active")) != 0) {
186                 set_input_active (string_is_affirmative (prop->value()));
187         }
188
189         ChannelMode playback_channel_mode = AllChannels;
190         ChannelMode capture_channel_mode = AllChannels;
191
192         if ((prop = node.property ("playback-channel-mode")) != 0) {
193                 playback_channel_mode = ChannelMode (string_2_enum(prop->value(), playback_channel_mode));
194         }
195         if ((prop = node.property ("capture-channel-mode")) != 0) {
196                 capture_channel_mode = ChannelMode (string_2_enum(prop->value(), capture_channel_mode));
197         }
198         if ((prop = node.property ("channel-mode")) != 0) {
199                 /* 3.0 behaviour where capture and playback modes were not separated */
200                 playback_channel_mode = ChannelMode (string_2_enum(prop->value(), playback_channel_mode));
201                 capture_channel_mode = playback_channel_mode;
202         }
203
204         unsigned int playback_channel_mask = 0xffff;
205         unsigned int capture_channel_mask = 0xffff;
206
207         if ((prop = node.property ("playback-channel-mask")) != 0) {
208                 sscanf (prop->value().c_str(), "0x%x", &playback_channel_mask);
209         }
210         if ((prop = node.property ("capture-channel-mask")) != 0) {
211                 sscanf (prop->value().c_str(), "0x%x", &capture_channel_mask);
212         }
213         if ((prop = node.property ("channel-mask")) != 0) {
214                 sscanf (prop->value().c_str(), "0x%x", &playback_channel_mask);
215                 capture_channel_mask = playback_channel_mask;
216         }
217
218         set_playback_channel_mode (playback_channel_mode, playback_channel_mask);
219         set_capture_channel_mode (capture_channel_mode, capture_channel_mask);
220
221         pending_state = const_cast<XMLNode*> (&node);
222
223         if (_session.state_of_the_state() & Session::Loading) {
224                 _session.StateReady.connect_same_thread (
225                         *this, boost::bind (&MidiTrack::set_state_part_two, this));
226         } else {
227                 set_state_part_two ();
228         }
229
230         return 0;
231 }
232
233 XMLNode&
234 MidiTrack::state(bool full_state)
235 {
236         XMLNode& root (Track::state(full_state));
237         XMLNode* freeze_node;
238         char buf[64];
239
240         if (_freeze_record.playlist) {
241                 XMLNode* inode;
242
243                 freeze_node = new XMLNode (X_("freeze-info"));
244                 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
245                 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
246
247                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
248                         inode = new XMLNode (X_("processor"));
249                         (*i)->id.print (buf, sizeof(buf));
250                         inode->add_property (X_("id"), buf);
251                         inode->add_child_copy ((*i)->state);
252
253                         freeze_node->add_child_nocopy (*inode);
254                 }
255
256                 root.add_child_nocopy (*freeze_node);
257         }
258
259         root.add_property("playback_channel-mode", enum_2_string(get_playback_channel_mode()));
260         root.add_property("capture_channel-mode", enum_2_string(get_capture_channel_mode()));
261         snprintf (buf, sizeof(buf), "0x%x", get_playback_channel_mask());
262         root.add_property("playback-channel-mask", buf);
263         snprintf (buf, sizeof(buf), "0x%x", get_capture_channel_mask());
264         root.add_property("capture-channel-mask", buf);
265
266         root.add_property ("note-mode", enum_2_string (_note_mode));
267         root.add_property ("step-editing", (_step_editing ? "yes" : "no"));
268         root.add_property ("input-active", (_input_active ? "yes" : "no"));
269
270         for (Controls::const_iterator c = _controls.begin(); c != _controls.end(); ++c) {
271                 if (boost::dynamic_pointer_cast<MidiTrack::MidiControl>(c->second)) {
272                         boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c->second);
273                         assert (ac);
274                         root.add_child_nocopy (ac->get_state ());
275                 }
276         }
277
278         return root;
279 }
280
281 void
282 MidiTrack::set_state_part_two ()
283 {
284         XMLNode* fnode;
285         XMLProperty const * prop;
286         LocaleGuard lg;
287
288         /* This is called after all session state has been restored but before
289            have been made ports and connections are established.
290         */
291
292         if (pending_state == 0) {
293                 return;
294         }
295
296         if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
297
298                 _freeze_record.state = Frozen;
299
300                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
301                         delete *i;
302                 }
303                 _freeze_record.processor_info.clear ();
304
305                 if ((prop = fnode->property (X_("playlist"))) != 0) {
306                         boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
307                         if (pl) {
308                                 _freeze_record.playlist = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
309                         } else {
310                                 _freeze_record.playlist.reset();
311                                 _freeze_record.state = NoFreeze;
312                         return;
313                         }
314                 }
315
316                 if ((prop = fnode->property (X_("state"))) != 0) {
317                         _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
318                 }
319
320                 XMLNodeConstIterator citer;
321                 XMLNodeList clist = fnode->children();
322
323                 for (citer = clist.begin(); citer != clist.end(); ++citer) {
324                         if ((*citer)->name() != X_("processor")) {
325                                 continue;
326                         }
327
328                         if ((prop = (*citer)->property (X_("id"))) == 0) {
329                                 continue;
330                         }
331
332                         FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
333                                                                                    boost::shared_ptr<Processor>());
334                         frii->id = prop->value ();
335                         _freeze_record.processor_info.push_back (frii);
336                 }
337         }
338
339         if (midi_diskstream ()) {
340                 midi_diskstream()->set_block_size (_session.get_block_size ());
341         }
342
343         return;
344 }
345
346 void
347 MidiTrack::restore_controls ()
348 {
349         // TODO order events (CC before PGM to set banks)
350         for (Controls::const_iterator c = _controls.begin(); c != _controls.end(); ++c) {
351                 boost::shared_ptr<MidiTrack::MidiControl> mctrl = boost::dynamic_pointer_cast<MidiTrack::MidiControl>(c->second);
352                 if (mctrl) {
353                         mctrl->restore_value();
354                 }
355         }
356 }
357
358 void
359 MidiTrack::update_controls(const BufferSet& bufs)
360 {
361         const MidiBuffer& buf = bufs.get_midi(0);
362         for (MidiBuffer::const_iterator e = buf.begin(); e != buf.end(); ++e) {
363                 const Evoral::Event<framepos_t>&         ev      = *e;
364                 const Evoral::Parameter                  param   = midi_parameter(ev.buffer(), ev.size());
365                 const boost::shared_ptr<Evoral::Control> control = this->control(param);
366                 if (control) {
367                         control->set_double(ev.value(), _session.transport_frame(), false);
368                 }
369         }
370 }
371
372 /** @param need_butler to be set to true if this track now needs the butler, otherwise it can be left alone
373  *  or set to false.
374  */
375 int
376 MidiTrack::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
377 {
378         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
379         if (!lm.locked()) {
380                 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
381                 framecnt_t playback_distance = diskstream->calculate_playback_distance(nframes);
382                 if (can_internal_playback_seek(::llabs(playback_distance))) {
383                         /* TODO should declick, and/or note-off */
384                         internal_playback_seek(playback_distance);
385                 }
386                 return 0;
387         }
388
389         boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
390
391         if (n_outputs().n_total() == 0 && _processors.empty()) {
392                 return 0;
393         }
394
395         if (!_active) {
396                 silence (nframes);
397                 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled())) {
398                         _meter->reset();
399                 }
400                 return 0;
401         }
402
403         framepos_t transport_frame = _session.transport_frame();
404
405         int dret;
406         framecnt_t playback_distance;
407
408         if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
409                 /* need to do this so that the diskstream sets its
410                    playback distance to zero, thus causing diskstream::commit
411                    to do nothing.
412                    */
413                 BufferSet bufs; /* empty set - is OK, since nothing will happen */
414
415                 dret = diskstream->process (bufs, transport_frame, 0, playback_distance, false);
416                 need_butler = diskstream->commit (playback_distance);
417                 return dret;
418         }
419
420         BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
421
422         fill_buffers_with_input (bufs, _input, nframes);
423
424         /* filter captured data before meter sees it */
425         _capture_filter.filter (bufs);
426
427         if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled())) {
428                 _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true);
429         }
430
431
432         _silent = false;
433
434         if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
435                 need_butler = diskstream->commit (playback_distance);
436                 silence (nframes);
437                 return dret;
438         }
439
440         /* note diskstream uses our filter to filter/map playback channels appropriately. */
441
442         if (monitoring_state() == MonitoringInput) {
443
444                 /* not actually recording, but we want to hear the input material anyway,
445                    at least potentially (depending on monitoring options)
446                 */
447
448                 /* because the playback buffer is event based and not a
449                  * continuous stream, we need to make sure that we empty
450                  * it of events every cycle to avoid it filling up with events
451                  * read from disk, while we are actually monitoring input
452                  */
453
454                 diskstream->flush_playback (start_frame, end_frame);
455
456         }
457
458
459         /* append immediate messages to the first MIDI buffer (thus sending it to the first output port) */
460
461         write_out_of_band_data (bufs, start_frame, end_frame, nframes);
462
463         /* final argument: don't waste time with automation if we're not recording or rolling */
464
465         process_output_buffers (bufs, start_frame, end_frame, nframes,
466                                 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
467
468         flush_processor_buffers_locked (nframes);
469
470         need_butler = diskstream->commit (playback_distance);
471
472         return 0;
473 }
474
475 int
476 MidiTrack::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool state_changing)
477 {
478         int ret = Track::no_roll (nframes, start_frame, end_frame, state_changing);
479
480         if (ret == 0 && _step_editing) {
481                 push_midi_input_to_step_edit_ringbuffer (nframes);
482         }
483
484         return ret;
485 }
486
487 void
488 MidiTrack::realtime_locate ()
489 {
490         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
491
492         if (!lm.locked ()) {
493                 return;
494         }
495
496         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
497                 (*i)->realtime_locate ();
498         }
499
500         midi_diskstream()->reset_tracker ();
501 }
502
503 void
504 MidiTrack::realtime_handle_transport_stopped ()
505 {
506         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
507
508         if (!lm.locked ()) {
509                 return;
510         }
511
512         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
513                 (*i)->realtime_handle_transport_stopped ();
514         }
515 }
516
517 void
518 MidiTrack::non_realtime_locate (framepos_t pos)
519 {
520         Track::non_realtime_locate(pos);
521
522         boost::shared_ptr<MidiPlaylist> playlist = midi_diskstream()->midi_playlist();
523         if (!playlist) {
524                 return;
525         }
526
527         /* Get the top unmuted region at this position. */
528         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(
529                 playlist->top_unmuted_region_at(pos));
530         if (!region) {
531                 return;
532         }
533
534         /* the source may be missing, but the control still referenced in the GUI */
535         if (!region->midi_source() || !region->model()) {
536                 return;
537         }
538
539         Glib::Threads::Mutex::Lock lm (_control_lock, Glib::Threads::TRY_LOCK);
540         if (!lm.locked()) {
541                 return;
542         }
543
544         /* Update track controllers based on its "automation". */
545         const framepos_t     origin = region->position() - region->start();
546         BeatsFramesConverter bfc(_session.tempo_map(), origin);
547         for (Controls::const_iterator c = _controls.begin(); c != _controls.end(); ++c) {
548                 boost::shared_ptr<MidiTrack::MidiControl> tcontrol;
549                 boost::shared_ptr<Evoral::Control>        rcontrol;
550                 if ((tcontrol = boost::dynamic_pointer_cast<MidiTrack::MidiControl>(c->second)) &&
551                     (rcontrol = region->control(tcontrol->parameter()))) {
552                         const Evoral::Beats pos_beats = bfc.from(pos - origin);
553                         if (rcontrol->list()->size() > 0) {
554                                 tcontrol->set_value(rcontrol->list()->eval(pos_beats.to_double()), Controllable::NoGroup);
555                         }
556                 }
557         }
558 }
559
560 void
561 MidiTrack::push_midi_input_to_step_edit_ringbuffer (framecnt_t nframes)
562 {
563         PortSet& ports (_input->ports());
564
565         for (PortSet::iterator p = ports.begin(DataType::MIDI); p != ports.end(DataType::MIDI); ++p) {
566
567                 Buffer& b (p->get_buffer (nframes));
568                 const MidiBuffer* const mb = dynamic_cast<MidiBuffer*>(&b);
569                 assert (mb);
570
571                 for (MidiBuffer::const_iterator e = mb->begin(); e != mb->end(); ++e) {
572
573                         const Evoral::Event<framepos_t> ev(*e, false);
574
575                         /* note on, since for step edit, note length is determined
576                            elsewhere
577                         */
578
579                         if (ev.is_note_on()) {
580                                 /* we don't care about the time for this purpose */
581                                 _step_edit_ring_buffer.write (0, ev.event_type(), ev.size(), ev.buffer());
582                         }
583                 }
584         }
585 }
586
587 void
588 MidiTrack::write_out_of_band_data (BufferSet& bufs, framepos_t /*start*/, framepos_t /*end*/, framecnt_t nframes)
589 {
590         MidiBuffer& buf (bufs.get_midi (0));
591
592         update_controls (bufs);
593
594         // Append immediate events
595
596         if (_immediate_events.read_space()) {
597
598                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 has %2 of immediate events to deliver\n",
599                                                             name(), _immediate_events.read_space()));
600
601                 /* write as many of the immediate events as we can, but give "true" as
602                  * the last argument ("stop on overflow in destination") so that we'll
603                  * ship the rest out next time.
604                  *
605                  * the Port::port_offset() + (nframes-1) argument puts all these events at the last
606                  * possible position of the output buffer, so that we do not
607                  * violate monotonicity when writing. Port::port_offset() will
608                  * be non-zero if we're in a split process cycle.
609                  */
610                 _immediate_events.read (buf, 0, 1, Port::port_offset() + nframes - 1, true);
611         }
612 }
613
614 int
615 MidiTrack::export_stuff (BufferSet&                   buffers,
616                          framepos_t                   start,
617                          framecnt_t                   nframes,
618                          boost::shared_ptr<Processor> endpoint,
619                          bool                         include_endpoint,
620                          bool                         for_export,
621                          bool                         for_freeze)
622 {
623         if (buffers.count().n_midi() == 0) {
624                 return -1;
625         }
626
627         boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
628
629         Glib::Threads::RWLock::ReaderLock rlock (_processor_lock);
630
631         boost::shared_ptr<MidiPlaylist> mpl = boost::dynamic_pointer_cast<MidiPlaylist>(diskstream->playlist());
632         if (!mpl) {
633                 return -2;
634         }
635
636         buffers.get_midi(0).clear();
637         if (mpl->read(buffers.get_midi(0), start, nframes, 0) != nframes) {
638                 return -1;
639         }
640
641         //bounce_process (buffers, start, nframes, endpoint, include_endpoint, for_export, for_freeze);
642
643         return 0;
644 }
645
646 boost::shared_ptr<Region>
647 MidiTrack::bounce (InterThreadInfo& itt)
648 {
649         return bounce_range (_session.current_start_frame(), _session.current_end_frame(), itt, main_outs(), false);
650 }
651
652 boost::shared_ptr<Region>
653 MidiTrack::bounce_range (framepos_t                   start,
654                          framepos_t                   end,
655                          InterThreadInfo&             itt,
656                          boost::shared_ptr<Processor> endpoint,
657                          bool                         include_endpoint)
658 {
659         vector<boost::shared_ptr<Source> > srcs;
660         return _session.write_one_track (*this, start, end, false, srcs, itt, endpoint, include_endpoint, false, false);
661 }
662
663 void
664 MidiTrack::freeze_me (InterThreadInfo& /*itt*/)
665 {
666         std::cerr << "MIDI freeze currently unsupported" << std::endl;
667 }
668
669 void
670 MidiTrack::unfreeze ()
671 {
672         _freeze_record.state = UnFrozen;
673         FreezeChange (); /* EMIT SIGNAL */
674 }
675
676 void
677 MidiTrack::set_note_mode (NoteMode m)
678 {
679         _note_mode = m;
680         midi_diskstream()->set_note_mode(m);
681 }
682
683 std::string
684 MidiTrack::describe_parameter (Evoral::Parameter param)
685 {
686         const std::string str(instrument_info().get_controller_name(param));
687         return str.empty() ? Automatable::describe_parameter(param) : str;
688 }
689
690 void
691 MidiTrack::midi_panic()
692 {
693         DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 delivers panic data\n", name()));
694         for (uint8_t channel = 0; channel <= 0xF; channel++) {
695                 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
696                 write_immediate_event(3, ev);
697                 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
698                 write_immediate_event(3, ev);
699                 ev[1] = MIDI_CTL_RESET_CONTROLLERS;
700                 write_immediate_event(3, ev);
701         }
702 }
703
704 /** \return true on success, false on failure (no buffer space left)
705  */
706 bool
707 MidiTrack::write_immediate_event(size_t size, const uint8_t* buf)
708 {
709         if (!Evoral::midi_event_is_valid(buf, size)) {
710                 cerr << "WARNING: Ignoring illegal immediate MIDI event" << endl;
711                 return false;
712         }
713         return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
714 }
715
716 void
717 MidiTrack::set_parameter_automation_state (Evoral::Parameter param, AutoState state)
718 {
719         switch (param.type()) {
720         case MidiCCAutomation:
721         case MidiPgmChangeAutomation:
722         case MidiPitchBenderAutomation:
723         case MidiChannelPressureAutomation:
724         case MidiNotePressureAutomation:
725         case MidiSystemExclusiveAutomation:
726                 /* The track control for MIDI parameters is for immediate events to act
727                    as a control surface, write/touch for them is not currently
728                    supported. */
729                 return;
730         default:
731                 Automatable::set_parameter_automation_state(param, state);
732         }
733 }
734
735 void
736 MidiTrack::MidiControl::restore_value ()
737 {
738         actually_set_value (get_value(), Controllable::NoGroup);
739 }
740
741 void
742 MidiTrack::MidiControl::actually_set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
743 {
744         const Evoral::Parameter &parameter = _list ? _list->parameter() : Control::parameter();
745         const Evoral::ParameterDescriptor &desc = EventTypeMap::instance().descriptor(parameter);
746
747         bool valid = false;
748         if (isinf_local(val)) {
749                 cerr << "MIDIControl value is infinity" << endl;
750         } else if (isnan_local(val)) {
751                 cerr << "MIDIControl value is NaN" << endl;
752         } else if (val < desc.lower) {
753                 cerr << "MIDIControl value is < " << desc.lower << endl;
754         } else if (val > desc.upper) {
755                 cerr << "MIDIControl value is > " << desc.upper << endl;
756         } else {
757                 valid = true;
758         }
759
760         if (!valid) {
761                 return;
762         }
763
764         assert(val <= desc.upper);
765         if ( ! _list || ! automation_playback()) {
766                 size_t size = 3;
767                 uint8_t ev[3] = { parameter.channel(), uint8_t (val), 0 };
768                 switch(parameter.type()) {
769                 case MidiCCAutomation:
770                         ev[0] += MIDI_CMD_CONTROL;
771                         ev[1] = parameter.id();
772                         ev[2] = int(val);
773                         break;
774
775                 case MidiPgmChangeAutomation:
776                         size = 2;
777                         ev[0] += MIDI_CMD_PGM_CHANGE;
778                         ev[1] = int(val);
779                         break;
780
781                 case MidiChannelPressureAutomation:
782                         size = 2;
783                         ev[0] += MIDI_CMD_CHANNEL_PRESSURE;
784                         ev[1] = int(val);
785                         break;
786
787                 case MidiNotePressureAutomation:
788                         ev[0] += MIDI_CMD_NOTE_PRESSURE;
789                         ev[1] = parameter.id();
790                         ev[2] = int(val);
791                         break;
792
793                 case MidiPitchBenderAutomation:
794                         ev[0] += MIDI_CMD_BENDER;
795                         ev[1] = 0x7F & int(val);
796                         ev[2] = 0x7F & (int(val) >> 7);
797                         break;
798
799                 default:
800                         assert(false);
801                 }
802                 _route->write_immediate_event(size,  ev);
803         }
804
805         AutomationControl::actually_set_value(val, group_override);
806 }
807
808 void
809 MidiTrack::set_step_editing (bool yn)
810 {
811         if (_session.record_status() != Session::Disabled) {
812                 return;
813         }
814
815         if (yn != _step_editing) {
816                 _step_editing = yn;
817                 StepEditStatusChange (yn);
818         }
819 }
820
821 boost::shared_ptr<SMFSource>
822 MidiTrack::write_source (uint32_t)
823 {
824         return midi_diskstream()->write_source ();
825 }
826
827 void
828 MidiTrack::set_playback_channel_mode(ChannelMode mode, uint16_t mask)
829 {
830         if (_playback_filter.set_channel_mode(mode, mask)) {
831                 _session.set_dirty();
832         }
833 }
834
835 void
836 MidiTrack::set_capture_channel_mode(ChannelMode mode, uint16_t mask)
837 {
838         if (_capture_filter.set_channel_mode(mode, mask)) {
839                 _session.set_dirty();
840         }
841 }
842
843 void
844 MidiTrack::set_playback_channel_mask (uint16_t mask)
845 {
846         if (_playback_filter.set_channel_mask(mask)) {
847                 _session.set_dirty();
848         }
849 }
850
851 void
852 MidiTrack::set_capture_channel_mask (uint16_t mask)
853 {
854         if (_capture_filter.set_channel_mask(mask)) {
855                 _session.set_dirty();
856         }
857 }
858
859 boost::shared_ptr<MidiPlaylist>
860 MidiTrack::midi_playlist ()
861 {
862         return midi_diskstream()->midi_playlist ();
863 }
864
865 void
866 MidiTrack::diskstream_data_recorded (boost::weak_ptr<MidiSource> src)
867 {
868         DataRecorded (src); /* EMIT SIGNAL */
869 }
870
871 bool
872 MidiTrack::input_active () const
873 {
874         return _input_active;
875 }
876
877 void
878 MidiTrack::set_input_active (bool yn)
879 {
880         if (yn != _input_active) {
881                 _input_active = yn;
882                 map_input_active (yn);
883                 InputActiveChanged (); /* EMIT SIGNAL */
884         }
885 }
886
887 void
888 MidiTrack::map_input_active (bool yn)
889 {
890         if (!_input) {
891                 return;
892         }
893
894         PortSet& ports (_input->ports());
895
896         for (PortSet::iterator p = ports.begin(DataType::MIDI); p != ports.end(DataType::MIDI); ++p) {
897                 boost::shared_ptr<MidiPort> mp = boost::dynamic_pointer_cast<MidiPort> (*p);
898                 if (yn != mp->input_active()) {
899                         mp->set_input_active (yn);
900                 }
901         }
902 }
903
904 void
905 MidiTrack::track_input_active (IOChange change, void* /* src */)
906 {
907         if (change.type & IOChange::ConfigurationChanged) {
908                 map_input_active (_input_active);
909         }
910 }
911
912 boost::shared_ptr<Diskstream>
913 MidiTrack::diskstream_factory (XMLNode const & node)
914 {
915         return boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, node));
916 }
917
918 boost::shared_ptr<MidiBuffer>
919 MidiTrack::get_gui_feed_buffer () const
920 {
921         return midi_diskstream()->get_gui_feed_buffer ();
922 }
923
924 void
925 MidiTrack::act_on_mute ()
926 {
927         /* this is called right after our mute status has changed.
928            if we are now muted, send suitable output to shutdown
929            all our notes.
930
931            XXX we should should also stop all relevant note trackers.
932         */
933
934         /* If we haven't got a diskstream yet, there's nothing to worry about,
935            and we can't call get_channel_mask() anyway.
936         */
937         if (!midi_diskstream()) {
938                 return;
939         }
940
941         if (muted() || _mute_master->muted_by_others_soloing_at (MuteMaster::AllPoints)) {
942                 /* only send messages for channels we are using */
943
944                 uint16_t mask = _playback_filter.get_channel_mask();
945
946                 for (uint8_t channel = 0; channel <= 0xF; channel++) {
947
948                         if ((1<<channel) & mask) {
949
950                                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 delivers mute message to channel %2\n", name(), channel+1));
951                                 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
952                                 write_immediate_event (3, ev);
953
954                                 /* Note we do not send MIDI_CTL_ALL_NOTES_OFF here, since this may
955                                    silence notes that came from another non-muted track. */
956                         }
957                 }
958
959                 /* Resolve active notes. */
960                 midi_diskstream()->resolve_tracker(_immediate_events, Port::port_offset());
961         }
962 }
963
964 void
965 MidiTrack::monitoring_changed (bool self, Controllable::GroupControlDisposition gcd)
966 {
967         Track::monitoring_changed (self, gcd);
968
969         /* monitoring state changed, so flush out any on notes at the
970          * port level.
971          */
972
973         PortSet& ports (_output->ports());
974
975         for (PortSet::iterator p = ports.begin(); p != ports.end(); ++p) {
976                 boost::shared_ptr<MidiPort> mp = boost::dynamic_pointer_cast<MidiPort> (*p);
977                 if (mp) {
978                         mp->require_resolve ();
979                 }
980         }
981
982         boost::shared_ptr<MidiDiskstream> md (midi_diskstream());
983
984         if (md) {
985                 md->reset_tracker ();
986         }
987 }
988
989 MonitorState
990 MidiTrack::monitoring_state () const
991 {
992         MonitorState ms = Track::monitoring_state();
993         if (ms == MonitoringSilence) {
994                 return MonitoringInput;
995         }
996         return ms;
997 }