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/redirect.h>
30 #include <ardour/midi_region.h>
31 #include <ardour/midi_source.h>
32 #include <ardour/route_group_specialized.h>
33 #include <ardour/insert.h>
34 #include <ardour/midi_playlist.h>
35 #include <ardour/panner.h>
36 #include <ardour/utils.h>
37 #include <ardour/buffer_set.h>
42 using namespace ARDOUR;
45 MidiTrack::MidiTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
46 : Track (sess, name, flag, mode, DataType::MIDI)
48 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
50 if (_flags & Hidden) {
51 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
53 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Recordable);
56 assert(mode != Destructive);
58 boost::shared_ptr<MidiDiskstream> ds (new MidiDiskstream (_session, name, dflags));
59 _session.add_diskstream (ds);
61 set_diskstream (boost::dynamic_pointer_cast<MidiDiskstream> (ds));
64 _freeze_record.state = NoFreeze;
65 _saved_meter_point = _meter_point;
68 set_input_minimum(ChanCount(DataType::MIDI, 1));
69 set_input_maximum(ChanCount(DataType::MIDI, 1));
70 set_output_minimum(ChanCount(DataType::MIDI, 1));
71 set_output_maximum(ChanCount(DataType::MIDI, 1));
74 MidiTrack::MidiTrack (Session& sess, const XMLNode& node)
77 _set_state(node, false);
79 set_input_minimum(ChanCount(DataType::MIDI, 1));
80 set_input_maximum(ChanCount(DataType::MIDI, 1));
81 set_output_minimum(ChanCount(DataType::MIDI, 1));
82 set_output_maximum(ChanCount(DataType::MIDI, 1));
85 MidiTrack::~MidiTrack ()
91 MidiTrack::set_diskstream (boost::shared_ptr<MidiDiskstream> ds)
94 _diskstream->set_io (*this);
95 _diskstream->set_destructive (_mode == Destructive);
97 _diskstream->set_record_enabled (false);
98 //_diskstream->monitor_input (false);
100 ic_connection.disconnect();
101 ic_connection = input_changed.connect (mem_fun (*_diskstream, &MidiDiskstream::handle_input_change));
103 DiskstreamChanged (); /* EMIT SIGNAL */
109 MidiTrack::use_diskstream (string name)
111 boost::shared_ptr<MidiDiskstream> dstream;
113 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream>(_session.diskstream_by_name (name))) == 0) {
114 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), name) << endmsg;
118 return set_diskstream (dstream);
122 MidiTrack::use_diskstream (const PBD::ID& id)
124 boost::shared_ptr<MidiDiskstream> dstream;
126 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream> (_session.diskstream_by_id (id))) == 0) {
127 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), id) << endmsg;
131 return set_diskstream (dstream);
134 boost::shared_ptr<MidiDiskstream>
135 MidiTrack::midi_diskstream() const
137 return boost::dynamic_pointer_cast<MidiDiskstream>(_diskstream);
141 MidiTrack::set_state (const XMLNode& node)
143 return _set_state (node, true);
147 MidiTrack::_set_state (const XMLNode& node, bool call_base)
149 const XMLProperty *prop;
150 XMLNodeConstIterator iter;
152 if (Route::_set_state (node, call_base)) {
156 if ((prop = node.property (X_("mode"))) != 0) {
157 _mode = TrackMode (string_2_enum (prop->value(), _mode));
162 if ((prop = node.property ("diskstream-id")) == 0) {
164 /* some old sessions use the diskstream name rather than the ID */
166 if ((prop = node.property ("diskstream")) == 0) {
167 fatal << _("programming error: MidiTrack given state without diskstream!") << endmsg;
172 if (use_diskstream (prop->value())) {
178 PBD::ID id (prop->value());
180 if (use_diskstream (id)) {
187 XMLNodeConstIterator niter;
190 nlist = node.children();
191 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
194 if (child->name() == X_("recenable")) {
195 _rec_enable_control.set_state (*child);
196 _session.add_controllable (&_rec_enable_control);
200 pending_state = const_cast<XMLNode*> (&node);
202 _session.StateReady.connect (mem_fun (*this, &MidiTrack::set_state_part_two));
208 MidiTrack::state(bool full_state)
210 XMLNode& root (Route::state(full_state));
211 XMLNode* freeze_node;
214 if (_freeze_record.playlist) {
217 freeze_node = new XMLNode (X_("freeze-info"));
218 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
219 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
221 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
222 inode = new XMLNode (X_("insert"));
223 (*i)->id.print (buf, sizeof(buf));
224 inode->add_property (X_("id"), buf);
225 inode->add_child_copy ((*i)->state);
227 freeze_node->add_child_nocopy (*inode);
230 root.add_child_nocopy (*freeze_node);
233 /* Alignment: act as a proxy for the diskstream */
235 XMLNode* align_node = new XMLNode (X_("alignment"));
236 AlignStyle as = _diskstream->alignment_style ();
237 align_node->add_property (X_("style"), enum_2_string (as));
238 root.add_child_nocopy (*align_node);
240 root.add_property (X_("mode"), enum_2_string (_mode));
242 /* we don't return diskstream state because we don't
243 own the diskstream exclusively. control of the diskstream
244 state is ceded to the Session, even if we create the
248 _diskstream->id().print (buf, sizeof(buf));
249 root.add_property ("diskstream-id", buf);
251 root.add_child_nocopy (_rec_enable_control.get_state());
257 MidiTrack::set_state_part_two ()
261 LocaleGuard lg (X_("POSIX"));
263 /* This is called after all session state has been restored but before
264 have been made ports and connections are established.
267 if (pending_state == 0) {
271 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
274 _freeze_record.have_mementos = false;
275 _freeze_record.state = Frozen;
277 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
280 _freeze_record.insert_info.clear ();
282 if ((prop = fnode->property (X_("playlist"))) != 0) {
283 boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
285 _freeze_record.playlist = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
287 _freeze_record.playlist.reset();
288 _freeze_record.state = NoFreeze;
293 if ((prop = fnode->property (X_("state"))) != 0) {
294 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
297 XMLNodeConstIterator citer;
298 XMLNodeList clist = fnode->children();
300 for (citer = clist.begin(); citer != clist.end(); ++citer) {
301 if ((*citer)->name() != X_("insert")) {
305 if ((prop = (*citer)->property (X_("id"))) == 0) {
309 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
310 boost::shared_ptr<Insert>());
311 frii->id = prop->value ();
312 _freeze_record.insert_info.push_back (frii);
316 /* Alignment: act as a proxy for the diskstream */
318 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
320 if ((prop = fnode->property (X_("style"))) != 0) {
322 /* fix for older sessions from before EnumWriter */
326 if (prop->value() == "capture") {
327 pstr = "CaptureTime";
328 } else if (prop->value() == "existing") {
329 pstr = "ExistingMaterial";
331 pstr = prop->value();
334 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
335 _diskstream->set_persistent_align_style (as);
342 MidiTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
343 bool session_state_changing, bool can_record, bool rec_monitors_input)
345 if (n_outputs().n_midi() == 0) {
351 silence (nframes, offset);
355 if (session_state_changing) {
357 /* XXX is this safe to do against transport state changes? */
359 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
363 midi_diskstream()->check_record_status (start_frame, nframes, can_record);
367 if (_have_internal_generator) {
368 /* since the instrument has no input streams,
369 there is no reason to send any signal
375 if (Config->get_auto_input()) {
376 if (Config->get_monitoring_model() == SoftwareMonitoring) {
377 send_silence = false;
382 if (_diskstream->record_enabled()) {
383 if (Config->get_monitoring_model() == SoftwareMonitoring) {
384 send_silence = false;
394 apply_gain_automation = false;
398 /* if we're sending silence, but we want the meters to show levels for the signal,
402 if (_have_internal_generator) {
403 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
405 if (_meter_point == MeterInput) {
406 just_meter_input (start_frame, end_frame, nframes, offset);
408 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
413 /* we're sending signal, but we may still want to meter the input.
416 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
423 MidiTrack::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
424 bool can_record, bool rec_monitors_input)
427 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
429 if (n_outputs().get_total() == 0 && _redirects.empty()) {
434 silence (nframes, offset);
438 nframes_t transport_frame = _session.transport_frame();
440 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
441 /* need to do this so that the diskstream sets its
442 playback distance to zero, thus causing diskstream::commit
445 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
450 if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
452 silence (nframes, offset);
457 /* special condition applies */
459 if (_meter_point == MeterInput) {
460 just_meter_input (start_frame, end_frame, nframes, offset);
463 if (diskstream->record_enabled() && !can_record && !Config->get_auto_input()) {
465 /* not actually recording, but we want to hear the input material anyway,
466 at least potentially (depending on monitoring options)
469 passthru (start_frame, end_frame, nframes, offset, 0, true);
473 XXX is it true that the earlier test on n_outputs()
474 means that we can avoid checking it again here? i think
475 so, because changing the i/o configuration of an IO
476 requires holding the AudioEngine lock, which we hold
477 while in the process() tree.
481 /* copy the diskstream data to all output buffers */
483 //const size_t limit = n_process_buffers().n_audio();
484 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
486 diskstream->get_playback(bufs.get_midi(0), start_frame, end_frame);
488 process_output_buffers (bufs, start_frame, end_frame, nframes, offset,
489 (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
497 MidiTrack::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
498 bool can_record, bool rec_monitors_input)
500 if (n_outputs().n_midi() == 0 && _redirects.empty()) {
505 silence (nframes, offset);
510 apply_gain_automation = false;
512 silence (nframes, offset);
514 return midi_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
518 MidiTrack::process_output_buffers (BufferSet& bufs,
519 nframes_t start_frame, nframes_t end_frame,
520 nframes_t nframes, nframes_t offset, bool with_redirects, int declick,
523 /* There's no such thing as a MIDI bus for the time being, to avoid diverging from trunk
524 * too much until the SoC settles down. We'll do all the MIDI route work here for now,
525 * but the long-term goal is to have Route::process_output_buffers handle everything */
528 if (with_redirects) {
529 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
531 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
532 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
539 IO::silence(nframes, offset);
541 deliver_output(bufs, start_frame, end_frame, nframes, offset);
546 MidiTrack::set_name (string str, void *src)
550 if (record_enabled() && _session.actively_recording()) {
551 /* this messes things up if done while recording */
555 if (_diskstream->set_name (str)) {
559 /* save state so that the statefile fully reflects any filename changes */
561 if ((ret = IO::set_name (str, src)) == 0) {
562 _session.save_state ("");
568 MidiTrack::export_stuff (BufferSet& bufs, nframes_t nframes, nframes_t end_frame)
574 MidiTrack::set_latency_delay (nframes_t longest_session_latency)
576 Route::set_latency_delay (longest_session_latency);
577 _diskstream->set_roll_delay (_roll_delay);
581 MidiTrack::bounce (InterThreadInfo& itt)
584 //vector<MidiSource*> srcs;
585 //_session.write_one_midi_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
590 MidiTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt)
593 //vector<MidiSource*> srcs;
594 //_session.write_one_midi_track (*this, start, end, false, srcs, itt);
598 MidiTrack::freeze (InterThreadInfo& itt)
603 MidiTrack::unfreeze ()
605 _freeze_record.state = UnFrozen;
606 FreezeChange (); /* EMIT SIGNAL */
610 MidiTrack::set_mode (TrackMode m)
616 if (_diskstream->set_destructive (m == Destructive)) {
622 TrackModeChanged (); /* EMIT SIGNAL */