2 Copyright (C) 2006 Paul Davis
3 By Dave Robillard, 2006
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.
19 #include <pbd/error.h>
20 #include <sigc++/retype.h>
21 #include <sigc++/retype_return.h>
22 #include <sigc++/bind.h>
24 #include <pbd/enumwriter.h>
26 #include <ardour/midi_track.h>
27 #include <ardour/midi_diskstream.h>
28 #include <ardour/session.h>
29 #include <ardour/io_processor.h>
30 #include <ardour/midi_region.h>
31 #include <ardour/midi_source.h>
32 #include <ardour/route_group_specialized.h>
33 #include <ardour/processor.h>
34 #include <ardour/midi_playlist.h>
35 #include <ardour/panner.h>
36 #include <ardour/utils.h>
37 #include <ardour/buffer_set.h>
38 #include <ardour/meter.h>
39 #include <ardour/midi_events.h>
44 using namespace ARDOUR;
47 MidiTrack::MidiTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
48 : Track (sess, name, flag, mode, DataType::MIDI)
49 , _immediate_events(1024) // FIXME: size?
50 , _note_mode(Sustained)
52 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
54 if (_flags & Hidden) {
55 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
57 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Recordable);
60 assert(mode != Destructive);
62 boost::shared_ptr<MidiDiskstream> ds (new MidiDiskstream (_session, name, dflags));
63 _session.add_diskstream (ds);
65 set_diskstream (boost::dynamic_pointer_cast<MidiDiskstream> (ds));
68 _freeze_record.state = NoFreeze;
69 _saved_meter_point = _meter_point;
72 set_input_minimum(ChanCount(DataType::MIDI, 1));
73 set_input_maximum(ChanCount(DataType::MIDI, 1));
74 set_output_minimum(ChanCount(DataType::MIDI, 1));
75 set_output_maximum(ChanCount(DataType::MIDI, 1));
77 PortCountChanged(ChanCount(DataType::MIDI, 2)); /* EMIT SIGNAL */
80 MidiTrack::MidiTrack (Session& sess, const XMLNode& node)
82 , _immediate_events(1024) // FIXME: size?
83 , _note_mode(Sustained)
85 _set_state(node, false);
87 set_input_minimum(ChanCount(DataType::MIDI, 1));
88 set_input_maximum(ChanCount(DataType::MIDI, 1));
89 set_output_minimum(ChanCount(DataType::MIDI, 1));
90 set_output_maximum(ChanCount(DataType::MIDI, 1));
92 PortCountChanged(ChanCount(DataType::MIDI, 2)); /* EMIT SIGNAL */
95 MidiTrack::~MidiTrack ()
101 MidiTrack::set_diskstream (boost::shared_ptr<MidiDiskstream> ds)
104 _diskstream->set_io (*this);
105 _diskstream->set_destructive (_mode == Destructive);
107 _diskstream->set_record_enabled (false);
108 //_diskstream->monitor_input (false);
110 ic_connection.disconnect();
111 ic_connection = input_changed.connect (mem_fun (*_diskstream, &MidiDiskstream::handle_input_change));
113 DiskstreamChanged (); /* EMIT SIGNAL */
119 MidiTrack::use_diskstream (string name)
121 boost::shared_ptr<MidiDiskstream> dstream;
123 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream>(_session.diskstream_by_name (name))) == 0) {
124 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), name) << endmsg;
128 return set_diskstream (dstream);
132 MidiTrack::use_diskstream (const PBD::ID& id)
134 boost::shared_ptr<MidiDiskstream> dstream;
136 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream> (_session.diskstream_by_id (id))) == 0) {
137 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), id) << endmsg;
141 return set_diskstream (dstream);
144 boost::shared_ptr<MidiDiskstream>
145 MidiTrack::midi_diskstream() const
147 return boost::dynamic_pointer_cast<MidiDiskstream>(_diskstream);
151 MidiTrack::set_state (const XMLNode& node)
153 return _set_state (node, true);
157 MidiTrack::_set_state (const XMLNode& node, bool call_base)
159 const XMLProperty *prop;
160 XMLNodeConstIterator iter;
162 if (Route::_set_state (node, call_base)) {
166 // No destructive MIDI tracks (yet?)
169 if ((prop = node.property (X_("note-mode"))) != 0) {
170 _note_mode = NoteMode (string_2_enum (prop->value(), _note_mode));
172 _note_mode = Sustained;
175 if ((prop = node.property ("diskstream-id")) == 0) {
177 /* some old sessions use the diskstream name rather than the ID */
179 if ((prop = node.property ("diskstream")) == 0) {
180 fatal << _("programming error: MidiTrack given state without diskstream!") << endmsg;
185 if (use_diskstream (prop->value())) {
191 PBD::ID id (prop->value());
193 if (use_diskstream (id)) {
200 XMLNodeConstIterator niter;
203 nlist = node.children();
204 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
207 if (child->name() == X_("recenable")) {
208 _rec_enable_control->set_state (*child);
209 _session.add_controllable (_rec_enable_control);
213 pending_state = const_cast<XMLNode*> (&node);
215 _session.StateReady.connect (mem_fun (*this, &MidiTrack::set_state_part_two));
221 MidiTrack::state(bool full_state)
223 XMLNode& root (Route::state(full_state));
224 XMLNode* freeze_node;
227 if (_freeze_record.playlist) {
230 freeze_node = new XMLNode (X_("freeze-info"));
231 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
232 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
234 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
235 inode = new XMLNode (X_("processor"));
236 (*i)->id.print (buf, sizeof(buf));
237 inode->add_property (X_("id"), buf);
238 inode->add_child_copy ((*i)->state);
240 freeze_node->add_child_nocopy (*inode);
243 root.add_child_nocopy (*freeze_node);
246 /* Alignment: act as a proxy for the diskstream */
248 XMLNode* align_node = new XMLNode (X_("alignment"));
249 AlignStyle as = _diskstream->alignment_style ();
250 align_node->add_property (X_("style"), enum_2_string (as));
251 root.add_child_nocopy (*align_node);
253 root.add_property (X_("note-mode"), enum_2_string (_note_mode));
255 /* we don't return diskstream state because we don't
256 own the diskstream exclusively. control of the diskstream
257 state is ceded to the Session, even if we create the
261 _diskstream->id().print (buf, sizeof(buf));
262 root.add_property ("diskstream-id", buf);
264 root.add_child_nocopy (_rec_enable_control->get_state());
270 MidiTrack::set_state_part_two ()
274 LocaleGuard lg (X_("POSIX"));
276 /* This is called after all session state has been restored but before
277 have been made ports and connections are established.
280 if (pending_state == 0) {
284 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
287 _freeze_record.have_mementos = false;
288 _freeze_record.state = Frozen;
290 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
293 _freeze_record.processor_info.clear ();
295 if ((prop = fnode->property (X_("playlist"))) != 0) {
296 boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
298 _freeze_record.playlist = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
300 _freeze_record.playlist.reset();
301 _freeze_record.state = NoFreeze;
306 if ((prop = fnode->property (X_("state"))) != 0) {
307 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
310 XMLNodeConstIterator citer;
311 XMLNodeList clist = fnode->children();
313 for (citer = clist.begin(); citer != clist.end(); ++citer) {
314 if ((*citer)->name() != X_("processor")) {
318 if ((prop = (*citer)->property (X_("id"))) == 0) {
322 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
323 boost::shared_ptr<Processor>());
324 frii->id = prop->value ();
325 _freeze_record.processor_info.push_back (frii);
329 /* Alignment: act as a proxy for the diskstream */
331 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
333 if ((prop = fnode->property (X_("style"))) != 0) {
335 /* fix for older sessions from before EnumWriter */
339 if (prop->value() == "capture") {
340 pstr = "CaptureTime";
341 } else if (prop->value() == "existing") {
342 pstr = "ExistingMaterial";
344 pstr = prop->value();
347 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
348 _diskstream->set_persistent_align_style (as);
355 MidiTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
356 bool session_state_changing, bool can_record, bool rec_monitors_input)
358 if (n_outputs().n_midi() == 0) {
363 silence (nframes, offset);
366 if (session_state_changing) {
368 /* XXX is this safe to do against transport state changes? */
370 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
374 midi_diskstream()->check_record_status (start_frame, nframes, can_record);
378 if (_have_internal_generator) {
379 /* since the instrument has no input streams,
380 there is no reason to send any signal
386 if (Config->get_auto_input()) {
387 if (Config->get_monitoring_model() == SoftwareMonitoring) {
388 send_silence = false;
393 if (_diskstream->record_enabled()) {
394 if (Config->get_monitoring_model() == SoftwareMonitoring) {
395 send_silence = false;
405 apply_gain_automation = false;
409 /* if we're sending silence, but we want the meters to show levels for the signal,
413 if (_have_internal_generator) {
414 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
416 if (_meter_point == MeterInput) {
417 just_meter_input (start_frame, end_frame, nframes, offset);
419 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
424 /* we're sending signal, but we may still want to meter the input.
427 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
434 MidiTrack::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
435 bool can_record, bool rec_monitors_input)
438 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
441 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
443 // automation snapshot can also be called from the non-rt context
444 // and it uses the redirect list, so we take the lock out here
445 automation_snapshot (start_frame);
449 if (n_outputs().n_total() == 0 && _processors.empty()) {
454 silence (nframes, offset);
458 nframes_t transport_frame = _session.transport_frame();
460 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
461 /* need to do this so that the diskstream sets its
462 playback distance to zero, thus causing diskstream::commit
465 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
470 if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
472 silence (nframes, offset);
477 /* special condition applies */
479 if (_meter_point == MeterInput) {
480 just_meter_input (start_frame, end_frame, nframes, offset);
483 if (diskstream->record_enabled() && !can_record && !Config->get_auto_input()) {
485 /* not actually recording, but we want to hear the input material anyway,
486 at least potentially (depending on monitoring options)
489 passthru (start_frame, end_frame, nframes, offset, 0, true);
493 XXX is it true that the earlier test on n_outputs()
494 means that we can avoid checking it again here? i think
495 so, because changing the i/o configuration of an IO
496 requires holding the AudioEngine lock, which we hold
497 while in the process() tree.
501 /* copy the diskstream data to all output buffers */
503 //const size_t limit = n_process_buffers().n_audio();
504 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
506 diskstream->get_playback(bufs.get_midi(0), start_frame, end_frame);
508 process_output_buffers (bufs, start_frame, end_frame, nframes, offset,
509 (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
517 MidiTrack::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
518 bool can_record, bool rec_monitors_input)
520 if (n_outputs().n_midi() == 0 && _processors.empty()) {
525 silence (nframes, offset);
530 apply_gain_automation = false;
532 silence (nframes, offset);
534 return midi_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
538 MidiTrack::process_output_buffers (BufferSet& bufs,
539 nframes_t start_frame, nframes_t end_frame,
540 nframes_t nframes, nframes_t offset, bool with_processors, int declick,
543 /* There's no such thing as a MIDI bus for the time being.
544 * We'll do all the MIDI route work here for now, but the long-term goal is to have
545 * Route::process_output_buffers handle everything */
547 if (meter && (_meter_point == MeterInput || _meter_point == MeterPreFader)) {
548 _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset);
551 // Run all processors
552 if (with_processors) {
553 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
555 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
556 (*i)->run_in_place (bufs, start_frame, end_frame, nframes, offset);
561 if (meter && (_meter_point == MeterPostFader)) {
562 _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset);
567 IO::silence(nframes, offset);
570 MidiBuffer& output_buf = bufs.get_midi(0);
571 write_controller_messages(output_buf, start_frame, end_frame, nframes, offset);
572 deliver_output(bufs, start_frame, end_frame, nframes, offset);
577 MidiTrack::write_controller_messages(MidiBuffer& output_buf, nframes_t start_frame, nframes_t end_frame,
578 nframes_t nframes, nframes_t offset)
580 BufferSet& mix_buffers = _session.get_mix_buffers(ChanCount(DataType::MIDI, 2));
582 /* FIXME: this could be more realtimey */
584 // Write immediate events (UI controls)
585 MidiBuffer& cc_buf = mix_buffers.get_midi(0);
586 cc_buf.silence(nframes, offset);
588 Byte buf[3]; // CC = 3 bytes
589 buf[0] = MIDI_CMD_CONTROL;
590 MidiEvent ev(0, 3, buf, false);
592 // Write track controller automation
594 // This now lives in MidiModel. Any need for track automation like this?
595 // Relative Velocity?
596 if (_session.transport_rolling()) {
597 for (Controls::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
598 const boost::shared_ptr<AutomationList> list = (*i).second->list();
600 if ( (!list->automation_playback())
601 || (list->parameter().type() != MidiCCAutomation))
604 double start = start_frame;
606 while ((*i).second->list()->rt_safe_earliest_event(start, end_frame, x, y)) {
607 assert(x >= start_frame);
608 assert(x <= end_frame);
610 const nframes_t stamp = (nframes_t)floor(x - start_frame);
611 assert(stamp < nframes);
617 ev.buffer()[1] = (Byte)list->parameter().id();
618 ev.buffer()[2] = (Byte)y;
620 cc_buf.push_back(ev);
622 start = x + 1; // FIXME? maybe?
627 /* FIXME: too much copying! */
629 // Merge cc buf into output
630 if (cc_buf.size() > 0) {
632 // Both CC and route, must merge
633 if (output_buf.size() > 0) {
635 MidiBuffer& mix_buf = mix_buffers.get_midi(1);
636 mix_buf.merge(output_buf, cc_buf);
637 output_buf.copy(mix_buf);
640 output_buf.copy(cc_buf);
645 // Append immediate events (UI controls)
646 _immediate_events.read(output_buf, 0, 0, offset + nframes-1); // all stamps = 0
650 MidiTrack::export_stuff (BufferSet& bufs, nframes_t nframes, nframes_t end_frame)
656 MidiTrack::set_latency_delay (nframes_t longest_session_latency)
658 Route::set_latency_delay (longest_session_latency);
659 _diskstream->set_roll_delay (_roll_delay);
663 MidiTrack::bounce (InterThreadInfo& itt)
666 //vector<MidiSource*> srcs;
667 //_session.write_one_midi_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
672 MidiTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt)
675 //vector<MidiSource*> srcs;
676 //_session.write_one_midi_track (*this, start, end, false, srcs, itt);
680 MidiTrack::freeze (InterThreadInfo& itt)
685 MidiTrack::unfreeze ()
687 _freeze_record.state = UnFrozen;
688 FreezeChange (); /* EMIT SIGNAL */
692 MidiTrack::set_note_mode (NoteMode m)
694 cout << _name << " SET NOTE MODE " << m << endl;
696 midi_diskstream()->set_note_mode(m);
699 /** \return true on success, false on failure (no buffer space left)
702 MidiTrack::write_immediate_event(size_t size, const Byte* buf)
704 return (_immediate_events.write(0, size, buf) == size);
708 MidiTrack::MidiControl::set_value(float val)
711 assert(val <= 127.0);
713 if ( ! _list->automation_playback()) {
714 Byte ev[3] = { MIDI_CMD_CONTROL, _list->parameter().id(), (int)val };
715 _route->write_immediate_event(3, ev);
718 AutomationControl::set_value(val);