2 Copyright (C) 2006 Paul Davis
3 Author: David Robillard
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.
20 #include <strings.h> // for ffs(3)
22 #include "pbd/enumwriter.h"
23 #include "pbd/convert.h"
24 #include "evoral/midi_util.h"
26 #include "ardour/buffer_set.h"
27 #include "ardour/debug.h"
28 #include "ardour/delivery.h"
29 #include "ardour/meter.h"
30 #include "ardour/midi_diskstream.h"
31 #include "ardour/midi_playlist.h"
32 #include "ardour/midi_port.h"
33 #include "ardour/midi_track.h"
34 #include "ardour/port.h"
35 #include "ardour/processor.h"
36 #include "ardour/session.h"
37 #include "ardour/session_playlists.h"
38 #include "ardour/utils.h"
43 class InterThreadInfo;
50 using namespace ARDOUR;
53 MidiTrack::MidiTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
54 : Track (sess, name, flag, mode, DataType::MIDI)
55 , _immediate_events(1024) // FIXME: size?
56 , _step_edit_ring_buffer(64) // FIXME: size?
57 , _note_mode(Sustained)
58 , _step_editing (false)
59 , _input_active (true)
60 , _playback_channel_mask(0x0000ffff)
61 , _capture_channel_mask(0x0000ffff)
65 MidiTrack::~MidiTrack ()
76 _input->changed.connect_same_thread (*this, boost::bind (&MidiTrack::track_input_active, this, _1, _2));
81 boost::shared_ptr<Diskstream>
82 MidiTrack::create_diskstream ()
84 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
86 if (_flags & Auditioner) {
87 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
89 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Recordable);
92 assert(_mode != Destructive);
94 return boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
99 MidiTrack::set_record_enabled (bool yn, void *src)
105 Track::set_record_enabled (yn, src);
109 MidiTrack::set_diskstream (boost::shared_ptr<Diskstream> ds)
111 /* We have to do this here, as Track::set_diskstream will cause a buffer refill,
112 and the diskstream must be set up to fill its buffers using the correct _note_mode.
114 boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream> (ds);
115 mds->set_note_mode (_note_mode);
117 Track::set_diskstream (ds);
119 mds->reset_tracker ();
121 _diskstream->set_track (this);
122 _diskstream->set_destructive (_mode == Destructive);
123 _diskstream->set_record_enabled (false);
125 _diskstream_data_recorded_connection.disconnect ();
126 mds->DataRecorded.connect_same_thread (
127 _diskstream_data_recorded_connection,
128 boost::bind (&MidiTrack::diskstream_data_recorded, this, _1));
130 DiskstreamChanged (); /* EMIT SIGNAL */
133 boost::shared_ptr<MidiDiskstream>
134 MidiTrack::midi_diskstream() const
136 return boost::dynamic_pointer_cast<MidiDiskstream>(_diskstream);
140 MidiTrack::set_state (const XMLNode& node, int version)
142 const XMLProperty *prop;
144 /* This must happen before Track::set_state(), as there will be a buffer
145 fill during that call, and we must fill buffers using the correct
148 if ((prop = node.property (X_("note-mode"))) != 0) {
149 _note_mode = NoteMode (string_2_enum (prop->value(), _note_mode));
151 _note_mode = Sustained;
154 if (Track::set_state (node, version)) {
158 // No destructive MIDI tracks (yet?)
161 if ((prop = node.property ("input-active")) != 0) {
162 set_input_active (string_is_affirmative (prop->value()));
165 ChannelMode playback_channel_mode = AllChannels;
166 ChannelMode capture_channel_mode = AllChannels;
168 if ((prop = node.property ("playback-channel-mode")) != 0) {
169 playback_channel_mode = ChannelMode (string_2_enum(prop->value(), playback_channel_mode));
171 if ((prop = node.property ("capture-channel-mode")) != 0) {
172 capture_channel_mode = ChannelMode (string_2_enum(prop->value(), capture_channel_mode));
174 if ((prop = node.property ("channel-mode")) != 0) {
175 /* 3.0 behaviour where capture and playback modes were not separated */
176 playback_channel_mode = ChannelMode (string_2_enum(prop->value(), playback_channel_mode));
177 capture_channel_mode = playback_channel_mode;
180 unsigned int playback_channel_mask = 0xffff;
181 unsigned int capture_channel_mask = 0xffff;
183 if ((prop = node.property ("playback-channel-mask")) != 0) {
184 sscanf (prop->value().c_str(), "0x%x", &playback_channel_mask);
186 if ((prop = node.property ("capture-channel-mask")) != 0) {
187 sscanf (prop->value().c_str(), "0x%x", &capture_channel_mask);
189 if ((prop = node.property ("channel-mask")) != 0) {
190 sscanf (prop->value().c_str(), "0x%x", &playback_channel_mask);
191 capture_channel_mask = playback_channel_mask;
194 set_playback_channel_mode (playback_channel_mode, playback_channel_mask);
195 set_capture_channel_mode (capture_channel_mode, capture_channel_mask);
197 pending_state = const_cast<XMLNode*> (&node);
199 if (_session.state_of_the_state() & Session::Loading) {
200 _session.StateReady.connect_same_thread (
201 *this, boost::bind (&MidiTrack::set_state_part_two, this));
203 set_state_part_two ();
210 MidiTrack::state(bool full_state)
212 XMLNode& root (Track::state(full_state));
213 XMLNode* freeze_node;
216 if (_freeze_record.playlist) {
219 freeze_node = new XMLNode (X_("freeze-info"));
220 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
221 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
223 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
224 inode = new XMLNode (X_("processor"));
225 (*i)->id.print (buf, sizeof(buf));
226 inode->add_property (X_("id"), buf);
227 inode->add_child_copy ((*i)->state);
229 freeze_node->add_child_nocopy (*inode);
232 root.add_child_nocopy (*freeze_node);
235 root.add_property("playback_channel-mode", enum_2_string(get_playback_channel_mode()));
236 root.add_property("capture_channel-mode", enum_2_string(get_capture_channel_mode()));
237 snprintf (buf, sizeof(buf), "0x%x", get_playback_channel_mask());
238 root.add_property("playback-channel-mask", buf);
239 snprintf (buf, sizeof(buf), "0x%x", get_capture_channel_mask());
240 root.add_property("capture-channel-mask", buf);
242 root.add_property ("note-mode", enum_2_string (_note_mode));
243 root.add_property ("step-editing", (_step_editing ? "yes" : "no"));
244 root.add_property ("input-active", (_input_active ? "yes" : "no"));
250 MidiTrack::set_state_part_two ()
254 LocaleGuard lg (X_("POSIX"));
256 /* This is called after all session state has been restored but before
257 have been made ports and connections are established.
260 if (pending_state == 0) {
264 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
266 _freeze_record.state = Frozen;
268 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
271 _freeze_record.processor_info.clear ();
273 if ((prop = fnode->property (X_("playlist"))) != 0) {
274 boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
276 _freeze_record.playlist = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
278 _freeze_record.playlist.reset();
279 _freeze_record.state = NoFreeze;
284 if ((prop = fnode->property (X_("state"))) != 0) {
285 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
288 XMLNodeConstIterator citer;
289 XMLNodeList clist = fnode->children();
291 for (citer = clist.begin(); citer != clist.end(); ++citer) {
292 if ((*citer)->name() != X_("processor")) {
296 if ((prop = (*citer)->property (X_("id"))) == 0) {
300 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
301 boost::shared_ptr<Processor>());
302 frii->id = prop->value ();
303 _freeze_record.processor_info.push_back (frii);
307 if (midi_diskstream ()) {
308 midi_diskstream()->set_block_size (_session.get_block_size ());
314 /** @param need_butler to be set to true if this track now needs the butler, otherwise it can be left alone
318 MidiTrack::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
320 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
322 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
323 framecnt_t playback_distance = diskstream->calculate_playback_distance(nframes);
324 if (can_internal_playback_seek(std::llabs(playback_distance))) {
325 /* TODO should declick, and/or note-off */
326 internal_playback_seek(playback_distance);
331 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
333 if (n_outputs().n_total() == 0 && _processors.empty()) {
339 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
345 framepos_t transport_frame = _session.transport_frame();
348 framecnt_t playback_distance;
350 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
351 /* need to do this so that the diskstream sets its
352 playback distance to zero, thus causing diskstream::commit
355 BufferSet bufs; /* empty set - is OK, since nothing will happen */
357 dret = diskstream->process (bufs, transport_frame, 0, playback_distance, false);
358 need_butler = diskstream->commit (playback_distance);
362 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
364 fill_buffers_with_input (bufs, _input, nframes);
366 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
367 _meter->run (bufs, start_frame, end_frame, nframes, true);
370 /* filter captured data before the diskstream sees it */
372 filter_channels (bufs, get_capture_channel_mode(), get_capture_channel_mask());
376 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
377 need_butler = diskstream->commit (playback_distance);
382 /* filter playback data before we do anything else */
384 filter_channels (bufs, get_playback_channel_mode(), get_playback_channel_mask ());
386 if (monitoring_state() == MonitoringInput) {
388 /* not actually recording, but we want to hear the input material anyway,
389 at least potentially (depending on monitoring options)
392 /* because the playback buffer is event based and not a
393 * continuous stream, we need to make sure that we empty
394 * it of events every cycle to avoid it filling up with events
395 * read from disk, while we are actually monitoring input
398 diskstream->flush_playback (start_frame, end_frame);
403 /* append immediate messages to the first MIDI buffer (thus sending it to the first output port) */
405 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
407 /* final argument: don't waste time with automation if we're not recording or rolling */
409 process_output_buffers (bufs, start_frame, end_frame, nframes,
410 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
412 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
413 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
415 d->flush_buffers (nframes);
419 need_butler = diskstream->commit (playback_distance);
425 MidiTrack::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool state_changing)
427 int ret = Track::no_roll (nframes, start_frame, end_frame, state_changing);
429 if (ret == 0 && _step_editing) {
430 push_midi_input_to_step_edit_ringbuffer (nframes);
437 MidiTrack::realtime_locate ()
439 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
445 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
446 (*i)->realtime_locate ();
449 midi_diskstream()->reset_tracker ();
453 MidiTrack::realtime_handle_transport_stopped ()
455 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
461 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
462 (*i)->realtime_handle_transport_stopped ();
467 MidiTrack::push_midi_input_to_step_edit_ringbuffer (framecnt_t nframes)
469 PortSet& ports (_input->ports());
471 for (PortSet::iterator p = ports.begin(DataType::MIDI); p != ports.end(DataType::MIDI); ++p) {
473 Buffer& b (p->get_buffer (nframes));
474 const MidiBuffer* const mb = dynamic_cast<MidiBuffer*>(&b);
477 for (MidiBuffer::const_iterator e = mb->begin(); e != mb->end(); ++e) {
479 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
481 /* note on, since for step edit, note length is determined
485 if (ev.is_note_on()) {
486 /* we don't care about the time for this purpose */
487 _step_edit_ring_buffer.write (0, ev.type(), ev.size(), ev.buffer());
494 MidiTrack::filter_channels (BufferSet& bufs, ChannelMode mode, uint32_t mask)
496 if (mode == AllChannels) {
500 MidiBuffer& buf (bufs.get_midi (0));
502 for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ) {
504 Evoral::MIDIEvent<framepos_t> ev(*e, false);
506 if (ev.is_channel_event()) {
509 if (0 == ((1<<ev.channel()) & mask)) {
516 ev.set_channel (ffs (mask) - 1);
520 /* handled by the opening if() */
531 MidiTrack::write_out_of_band_data (BufferSet& bufs, framepos_t /*start*/, framepos_t /*end*/, framecnt_t nframes)
533 MidiBuffer& buf (bufs.get_midi (0));
535 // Append immediate events
537 if (_immediate_events.read_space()) {
539 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 has %2 of immediate events to deliver\n",
540 name(), _immediate_events.read_space()));
542 /* write as many of the immediate events as we can, but give "true" as
543 * the last argument ("stop on overflow in destination") so that we'll
544 * ship the rest out next time.
546 * the (nframes-1) argument puts all these events at the last
547 * possible position of the output buffer, so that we do not
548 * violate monotonicity when writing.
551 _immediate_events.read (buf, 0, 1, nframes-1, true);
556 MidiTrack::export_stuff (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framecnt_t /*nframes*/,
557 boost::shared_ptr<Processor> /*endpoint*/, bool /*include_endpoint*/, bool /*forexport*/)
562 boost::shared_ptr<Region>
563 MidiTrack::bounce (InterThreadInfo& /*itt*/)
565 std::cerr << "MIDI bounce currently unsupported" << std::endl;
566 return boost::shared_ptr<Region> ();
570 boost::shared_ptr<Region>
571 MidiTrack::bounce_range (framepos_t /*start*/, framepos_t /*end*/, InterThreadInfo& /*itt*/,
572 boost::shared_ptr<Processor> /*endpoint*/, bool /*include_endpoint*/)
574 std::cerr << "MIDI bounce range currently unsupported" << std::endl;
575 return boost::shared_ptr<Region> ();
579 MidiTrack::freeze_me (InterThreadInfo& /*itt*/)
581 std::cerr << "MIDI freeze currently unsupported" << std::endl;
585 MidiTrack::unfreeze ()
587 _freeze_record.state = UnFrozen;
588 FreezeChange (); /* EMIT SIGNAL */
592 MidiTrack::set_note_mode (NoteMode m)
595 midi_diskstream()->set_note_mode(m);
599 MidiTrack::describe_parameter (Evoral::Parameter param)
601 const std::string str(instrument_info().get_controller_name(param));
602 return str.empty() ? Automatable::describe_parameter(param) : str;
606 MidiTrack::midi_panic()
608 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 delivers panic data\n", name()));
609 for (uint8_t channel = 0; channel <= 0xF; channel++) {
610 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
611 write_immediate_event(3, ev);
612 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
613 write_immediate_event(3, ev);
614 ev[1] = MIDI_CTL_RESET_CONTROLLERS;
615 write_immediate_event(3, ev);
619 /** \return true on success, false on failure (no buffer space left)
622 MidiTrack::write_immediate_event(size_t size, const uint8_t* buf)
624 if (!Evoral::midi_event_is_valid(buf, size)) {
625 cerr << "WARNING: Ignoring illegal immediate MIDI event" << endl;
628 const uint32_t type = EventTypeMap::instance().midi_event_type(buf[0]);
629 return (_immediate_events.write(0, type, size, buf) == size);
633 MidiTrack::MidiControl::set_value(double val)
636 if (std::isinf(val)) {
637 cerr << "MIDIControl value is infinity" << endl;
638 } else if (std::isnan(val)) {
639 cerr << "MIDIControl value is NaN" << endl;
640 } else if (val < _list->parameter().min()) {
641 cerr << "MIDIControl value is < " << _list->parameter().min() << endl;
642 } else if (val > _list->parameter().max()) {
643 cerr << "MIDIControl value is > " << _list->parameter().max() << endl;
652 assert(val <= _list->parameter().max());
653 if ( ! automation_playback()) {
655 uint8_t ev[3] = { _list->parameter().channel(), uint8_t (val), 0 };
656 switch(_list->parameter().type()) {
657 case MidiCCAutomation:
658 ev[0] += MIDI_CMD_CONTROL;
659 ev[1] = _list->parameter().id();
663 case MidiPgmChangeAutomation:
665 ev[0] += MIDI_CMD_PGM_CHANGE;
669 case MidiChannelPressureAutomation:
671 ev[0] += MIDI_CMD_CHANNEL_PRESSURE;
675 case MidiPitchBenderAutomation:
676 ev[0] += MIDI_CMD_BENDER;
677 ev[1] = 0x7F & int(val);
678 ev[2] = 0x7F & (int(val) >> 7);
684 _route->write_immediate_event(size, ev);
687 AutomationControl::set_value(val);
691 MidiTrack::set_step_editing (bool yn)
693 if (_session.record_status() != Session::Disabled) {
697 if (yn != _step_editing) {
699 StepEditStatusChange (yn);
703 boost::shared_ptr<SMFSource>
704 MidiTrack::write_source (uint32_t)
706 return midi_diskstream()->write_source ();
710 MidiTrack::set_playback_channel_mode(ChannelMode mode, uint16_t mask)
712 ChannelMode old = get_playback_channel_mode ();
713 uint16_t old_mask = get_playback_channel_mask ();
715 if (old != mode || mask != old_mask) {
716 _set_playback_channel_mode (mode, mask);
717 PlaybackChannelModeChanged ();
718 _session.set_dirty ();
723 MidiTrack::set_capture_channel_mode(ChannelMode mode, uint16_t mask)
725 ChannelMode old = get_capture_channel_mode ();
726 uint16_t old_mask = get_capture_channel_mask ();
728 if (old != mode || mask != old_mask) {
729 _set_capture_channel_mode (mode, mask);
730 CaptureChannelModeChanged ();
731 _session.set_dirty ();
736 MidiTrack::set_playback_channel_mask (uint16_t mask)
738 uint16_t old = get_playback_channel_mask();
741 _set_playback_channel_mask (mask);
742 PlaybackChannelMaskChanged ();
743 _session.set_dirty ();
748 MidiTrack::set_capture_channel_mask (uint16_t mask)
750 uint16_t old = get_capture_channel_mask();
753 _set_capture_channel_mask (mask);
754 CaptureChannelMaskChanged ();
755 _session.set_dirty ();
759 boost::shared_ptr<MidiPlaylist>
760 MidiTrack::midi_playlist ()
762 return midi_diskstream()->midi_playlist ();
766 MidiTrack::diskstream_data_recorded (boost::weak_ptr<MidiSource> src)
768 DataRecorded (src); /* EMIT SIGNAL */
772 MidiTrack::input_active () const
774 return _input_active;
778 MidiTrack::set_input_active (bool yn)
780 if (yn != _input_active) {
782 map_input_active (yn);
783 InputActiveChanged (); /* EMIT SIGNAL */
788 MidiTrack::map_input_active (bool yn)
794 PortSet& ports (_input->ports());
796 for (PortSet::iterator p = ports.begin(DataType::MIDI); p != ports.end(DataType::MIDI); ++p) {
797 boost::shared_ptr<MidiPort> mp = boost::dynamic_pointer_cast<MidiPort> (*p);
798 if (yn != mp->input_active()) {
799 mp->set_input_active (yn);
805 MidiTrack::track_input_active (IOChange change, void* /* src */)
807 if (change.type & IOChange::ConfigurationChanged) {
808 map_input_active (_input_active);
812 boost::shared_ptr<Diskstream>
813 MidiTrack::diskstream_factory (XMLNode const & node)
815 return boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, node));
818 boost::shared_ptr<MidiBuffer>
819 MidiTrack::get_gui_feed_buffer () const
821 return midi_diskstream()->get_gui_feed_buffer ();
825 MidiTrack::act_on_mute ()
827 /* this is called right after our mute status has changed.
828 if we are now muted, send suitable output to shutdown
831 XXX we should should also stop all relevant note trackers.
834 /* If we haven't got a diskstream yet, there's nothing to worry about,
835 and we can't call get_channel_mask() anyway.
837 if (!midi_diskstream()) {
842 /* only send messages for channels we are using */
844 uint16_t mask = get_playback_channel_mask();
846 for (uint8_t channel = 0; channel <= 0xF; channel++) {
848 if ((1<<channel) & mask) {
850 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("%1 delivers mute message to channel %2\n", name(), channel+1));
851 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
852 write_immediate_event (3, ev);
853 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
854 write_immediate_event (3, ev);
861 MidiTrack::set_monitoring (MonitorChoice mc)
863 if (mc != _monitoring) {
865 Track::set_monitoring (mc);
867 /* monitoring state changed, so flush out any on notes at the
871 PortSet& ports (_output->ports());
873 for (PortSet::iterator p = ports.begin(); p != ports.end(); ++p) {
874 boost::shared_ptr<MidiPort> mp = boost::dynamic_pointer_cast<MidiPort> (*p);
876 mp->require_resolve ();
880 boost::shared_ptr<MidiDiskstream> md (midi_diskstream());
883 md->reset_tracker ();
889 MidiTrack::monitoring_state () const
891 MonitorState ms = Track::monitoring_state();
892 if (ms == MonitoringSilence) {
893 return MonitoringInput;