2 Copyright (C) 2006 Paul Davis
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.
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.
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.
18 #include "pbd/error.h"
20 #include "ardour/amp.h"
21 #include "ardour/audioengine.h"
22 #include "ardour/audiofilesource.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/debug.h"
25 #include "ardour/delivery.h"
26 #include "ardour/disk_reader.h"
27 #include "ardour/disk_writer.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/io_processor.h"
30 #include "ardour/meter.h"
31 #include "ardour/midi_region.h"
32 #include "ardour/monitor_control.h"
33 #include "ardour/playlist.h"
34 #include "ardour/playlist_factory.h"
35 #include "ardour/port.h"
36 #include "ardour/processor.h"
37 #include "ardour/profile.h"
38 #include "ardour/region_factory.h"
39 #include "ardour/record_enable_control.h"
40 #include "ardour/record_safe_control.h"
41 #include "ardour/route_group_specialized.h"
42 #include "ardour/session.h"
43 #include "ardour/session_playlists.h"
44 #include "ardour/smf_source.h"
45 #include "ardour/track.h"
46 #include "ardour/types_convert.h"
47 #include "ardour/utils.h"
52 using namespace ARDOUR;
55 Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type)
56 : Route (sess, name, flag, default_type)
57 , _saved_meter_point (_meter_point)
58 , _disk_io_point (DiskIOPreFader)
60 , _alignment_choice (Automatic)
62 _freeze_record.state = NoFreeze;
68 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
71 _disk_reader->set_route (boost::shared_ptr<Route>());
72 _disk_reader.reset ();
76 _disk_writer->set_route (boost::shared_ptr<Route>());
77 _disk_writer.reset ();
88 DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
90 if (_mode == Destructive && !Profile->get_trx()) {
91 dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
92 } else if (_mode == NonLayered){
93 dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
96 _disk_reader.reset (new DiskReader (_session, name(), dflags));
97 _disk_reader->set_block_size (_session.get_block_size ());
98 _disk_reader->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
100 _disk_writer.reset (new DiskWriter (_session, name(), dflags));
101 _disk_writer->set_block_size (_session.get_block_size ());
102 _disk_writer->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
106 /* disk writer and reader processors will be added when Route calls
107 * setup_invisible_processors_oh_children_of_mine ().
110 boost::shared_ptr<Route> rp (boost::dynamic_pointer_cast<Route> (shared_from_this()));
111 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
113 _record_enable_control.reset (new RecordEnableControl (_session, EventTypeMap::instance().to_symbol (RecEnableAutomation), *this));
114 add_control (_record_enable_control);
116 _record_safe_control.reset (new RecordSafeControl (_session, EventTypeMap::instance().to_symbol (RecSafeAutomation), *this));
117 add_control (_record_safe_control);
119 _monitoring_control.reset (new MonitorControl (_session, EventTypeMap::instance().to_symbol (MonitoringAutomation), *this));
120 add_control (_monitoring_control);
122 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
124 _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
125 _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
126 _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
128 _input->changed.connect_same_thread (*this, boost::bind (&Track::input_changed, this));
134 Track::input_changed ()
136 if (_disk_writer && _alignment_choice == Automatic) {
137 set_align_choice_from_io ();
148 Track::state (bool full)
150 XMLNode& root (Route::state (full));
152 if (_playlists[DataType::AUDIO]) {
153 root.set_property (X_("audio-playlist"), _playlists[DataType::AUDIO]->id().to_s());
156 if (_playlists[DataType::MIDI]) {
157 root.set_property (X_("midi-playlist"), _playlists[DataType::MIDI]->id().to_s());
160 root.add_child_nocopy (_monitoring_control->get_state ());
161 root.add_child_nocopy (_record_safe_control->get_state ());
162 root.add_child_nocopy (_record_enable_control->get_state ());
164 root.set_property (X_("saved-meter-point"), _saved_meter_point);
165 root.set_property (X_("disk-io-point"), _disk_io_point);
166 root.set_property (X_("alignment-choice"), _alignment_choice);
172 Track::set_state (const XMLNode& node, int version)
174 if (Route::set_state (node, version)) {
180 if (version >= 3000 && version < 4000) {
181 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
182 /* XXX if we remember anything from stored DiskStream
183 state (older Ardour versions) that is needed by a
184 DiskReader or DiskWriter, we should cook up a new
185 XMLNode here, populate it with that information
186 (child nodes, properties, etc.) and then call
187 ::set_state() on the writer/reader.
189 But at present (June 2017), there's no such state.
194 std::string playlist_id;
196 if (node.get_property (X_("audio-playlist"), playlist_id)) {
197 find_and_use_playlist (DataType::AUDIO, PBD::ID (playlist_id));
200 if (node.get_property (X_("midi-playlist"), playlist_id)) {
201 find_and_use_playlist (DataType::MIDI, PBD::ID (playlist_id));
204 XMLNodeList nlist = node.children();
205 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
208 if (child->name() == Controllable::xml_node_name) {
210 if (!child->get_property ("name", name)) {
214 if (name == _record_enable_control->name()) {
215 _record_enable_control->set_state (*child, version);
216 } else if (name == _record_safe_control->name()) {
217 _record_safe_control->set_state (*child, version);
218 } else if (name == _monitoring_control->name()) {
219 _monitoring_control->set_state (*child, version);
224 if (!node.get_property (X_("saved-meter-point"), _saved_meter_point)) {
225 _saved_meter_point = _meter_point;
228 if (!node.get_property (X_("saved-meter-point"), _disk_io_point)) {
229 _disk_io_point = DiskIOPreFader;
234 if (node.get_property (X_("alignment-choice"), ac)) {
235 set_align_choice (ac, true);
242 Track::get_template ()
244 return state (false);
247 Track::FreezeRecord::~FreezeRecord ()
249 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
255 Track::freeze_state() const
257 return _freeze_record.state;
263 bool will_record = true;
264 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
273 Track::prep_record_enabled (bool yn)
275 if (yn && _record_safe_control->get_value()) {
279 if (!can_be_record_enabled()) {
283 /* keep track of the meter point as it was before we rec-enabled */
284 if (!_disk_writer->record_enabled()) {
285 _saved_meter_point = _meter_point;
291 will_follow = _disk_writer->prep_record_enable ();
293 will_follow = _disk_writer->prep_record_disable ();
298 if (_meter_point != MeterCustom) {
299 set_meter_point (MeterInput);
302 set_meter_point (_saved_meter_point);
310 Track::record_enable_changed (bool, Controllable::GroupControlDisposition)
312 _disk_writer->set_record_enabled (_record_enable_control->get_value());
316 Track::record_safe_changed (bool, Controllable::GroupControlDisposition)
318 _disk_writer->set_record_safe (_record_safe_control->get_value());
322 Track::can_be_record_safe ()
324 return !_record_enable_control->get_value() && _disk_writer && _session.writable() && (_freeze_record.state != Frozen);
328 Track::can_be_record_enabled ()
330 return !_record_safe_control->get_value() && _disk_writer && !_disk_writer->record_safe() && _session.writable() && (_freeze_record.state != Frozen);
334 Track::parameter_changed (string const & p)
336 if (p == "track-name-number") {
337 resync_track_name ();
339 else if (p == "track-name-take") {
340 resync_track_name ();
342 else if (p == "take-name") {
343 if (_session.config.get_track_name_take()) {
344 resync_track_name ();
350 Track::resync_track_name ()
356 Track::set_name (const string& str)
364 if (_record_enable_control->get_value()) {
365 /* when re-arm'ed the file (named after the track) is already ready to rolll */
369 string diskstream_name = "";
370 if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
371 // Note: any text is fine, legalize_for_path() fixes this later
372 diskstream_name += _session.config.get_take_name ();
373 diskstream_name += "_";
375 const int64_t tracknumber = track_number();
376 if (tracknumber > 0 && _session.config.get_track_name_number()) {
377 char num[64], fmt[10];
378 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
379 snprintf(num, sizeof(num), fmt, tracknumber);
380 diskstream_name += num;
381 diskstream_name += "_";
383 diskstream_name += str;
385 if (diskstream_name == _diskstream_name) {
388 _diskstream_name = diskstream_name;
390 _disk_writer->set_write_source_name (diskstream_name);
392 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
394 if (_playlists[data_type()]->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
395 /* Only rename the diskstream (and therefore the playlist) if
396 a) the playlist has never had a region added to it and
397 b) there is only one playlist for this track.
399 If (a) is not followed, people can get confused if, say,
400 they have notes about a playlist with a given name and then
401 it changes (see mantis #4759).
403 If (b) is not followed, we rename the current playlist and not
404 the other ones, which is a bit confusing (see mantis #4977).
406 _disk_reader->set_name (str);
407 _disk_writer->set_name (str);
410 for (uint32_t n = 0; n < DataType::num_types; ++n) {
412 _playlists[n]->set_name (str);
416 /* save state so that the statefile fully reflects any filename changes */
418 if ((ret = Route::set_name (str)) == 0) {
419 _session.save_state ("");
426 Track::set_latency_compensation (framecnt_t longest_session_latency)
428 Route::set_latency_compensation (longest_session_latency);
429 _disk_reader->set_roll_delay (_roll_delay);
433 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
435 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
441 bool can_record = _session.actively_recording ();
443 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
445 if (n_outputs().n_total() == 0 && !ARDOUR::Profile->get_mixbus()) {
446 //Note: Mixbus has its own output mechanism, so we should operate even if no explicit outputs are assigned
450 /* not active ... do the minimum possible by just outputting silence */
454 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled())) {
460 if (session_state_changing) {
461 if (_session.transport_speed() != 0.0f) {
462 /* we're rolling but some state is changing (e.g. our
463 disk reader contents) so we cannot use them. Be
464 silent till this is over. Don't declick.
466 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
468 passthru_silence (start_frame, end_frame, nframes, 0);
471 /* we're really not rolling, so we're either delivery silence or actually
472 monitoring, both of which are safe to do while session_state_changing is true.
476 _disk_writer->check_record_status (start_frame, can_record);
480 MonitorState const s = monitoring_state ();
481 /* we are not rolling, so be silent even if we are monitoring disk, as there
482 will be no disk data coming in.
485 case MonitoringSilence:
491 case MonitoringInput:
499 //if we have an internal generator, let it play regardless of monitoring state
500 if (_have_internal_generator) {
504 _amp->apply_gain_automation (false);
506 /* if have_internal_generator, or .. */
510 if (_meter_point == MeterInput) {
511 /* still need input monitoring and metering */
513 bool const track_rec = _disk_writer->record_enabled ();
514 bool const auto_input = _session.config.get_auto_input ();
515 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
516 bool const tape_machine_mode = Config->get_tape_machine_mode ();
517 bool no_meter = false;
519 /* this needs a proper K-map
520 * and should be separated into a function similar to monitoring_state()
521 * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
523 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
525 if (!auto_input && !track_rec) {
528 else if (tape_machine_mode && !track_rec && auto_input) {
531 else if (!software_monitor && tape_machine_mode && !track_rec) {
534 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
539 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
540 _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true);
541 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, _session.transport_speed(), nframes);
543 _input->process_input (_meter, start_frame, end_frame, _session.transport_speed(), nframes);
547 passthru_silence (start_frame, end_frame, nframes, 0);
551 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
553 fill_buffers_with_input (bufs, _input, nframes);
555 if (_meter_point == MeterInput) {
556 _meter->run (bufs, start_frame, end_frame, _session.transport_speed(), nframes, true);
559 passthru (bufs, start_frame, end_frame, nframes, false);
562 flush_processor_buffers_locked (nframes);
568 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
570 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
572 // XXX DISK reader needs to seek ahead the correct distance ?? OR DOES IT ?
573 //framecnt_t playback_distance = _disk_reader->calculate_playback_distance(nframes);
574 //if (can_internal_playback_seek(playback_distance)) {
575 // internal_playback_seek(playback_distance);
580 if (n_outputs().n_total() == 0 && _processors.empty()) {
590 _amp->apply_gain_automation(false);
593 flush_processor_buffers_locked (nframes);
595 //BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
596 // XXXX DISKWRITER/READER ADVANCE, SET need_butler
600 boost::shared_ptr<Playlist>
603 return _playlists[data_type()];
607 Track::request_input_monitoring (bool m)
609 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
610 AudioEngine::instance()->request_input_monitoring ((*i)->name(), m);
615 Track::ensure_input_monitoring (bool m)
617 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
618 AudioEngine::instance()->ensure_input_monitoring ((*i)->name(), m);
623 Track::destructive () const
625 return _disk_writer->destructive ();
628 list<boost::shared_ptr<Source> > &
629 Track::last_capture_sources ()
631 return _disk_writer->last_capture_sources ();
635 Track::update_latency_information ()
637 Glib::Threads::RWLock::ReaderLock lr (_processor_lock);
638 framecnt_t chain_latency = _input->latency ();
640 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) {
641 (*p)->set_input_latency (chain_latency);
642 chain_latency += (*p)->signal_latency ();
647 Track::steal_write_source_name()
649 return _disk_writer->steal_write_source_name ();
653 Track::reset_write_sources (bool r, bool force)
655 _disk_writer->reset_write_sources (r, force);
659 Track::playback_buffer_load () const
661 return _disk_reader->buffer_load ();
665 Track::capture_buffer_load () const
667 return _disk_writer->buffer_load ();
673 return _disk_reader->do_refill ();
677 Track::do_flush (RunContext c, bool force)
679 return _disk_writer->do_flush (c, force);
683 Track::set_pending_overwrite (bool o)
685 _disk_reader->set_pending_overwrite (o);
689 Track::seek (framepos_t p, bool complete_refill)
691 if (_disk_reader->seek (p, complete_refill)) {
694 return _disk_writer->seek (p, complete_refill);
698 Track::hidden () const
700 return _disk_writer->hidden () || _disk_reader->hidden();
704 Track::can_internal_playback_seek (framecnt_t p)
706 return _disk_reader->can_internal_playback_seek (p);
710 Track::internal_playback_seek (framecnt_t p)
712 return _disk_reader->internal_playback_seek (p);
716 Track::non_realtime_locate (framepos_t p)
718 Route::non_realtime_locate (p);
721 /* don't waste i/o cycles and butler calls
722 for hidden (secret) tracks
724 _disk_reader->non_realtime_locate (p);
725 _disk_writer->non_realtime_locate (p);
730 Track::non_realtime_speed_change ()
732 _disk_reader->non_realtime_speed_change ();
736 Track::overwrite_existing_buffers ()
738 return _disk_reader->overwrite_existing_buffers ();
742 Track::get_captured_frames (uint32_t n) const
744 return _disk_writer->get_captured_frames (n);
748 Track::set_loop (Location* l)
750 if (_disk_reader->set_loop (l)) {
753 return _disk_writer->set_loop (l);
757 Track::transport_looped (framepos_t p)
759 return _disk_writer->transport_looped (p);
763 Track::realtime_speed_change ()
765 if (_disk_reader->realtime_speed_change ()) {
768 return _disk_writer->realtime_speed_change ();
772 Track::realtime_handle_transport_stopped ()
774 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
780 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
781 (*i)->realtime_handle_transport_stopped ();
786 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
788 _disk_writer->transport_stopped_wallclock (n, t, g);
792 Track::pending_overwrite () const
794 return _disk_reader->pending_overwrite ();
798 Track::prepare_to_stop (framepos_t t, framepos_t a)
800 _disk_writer->prepare_to_stop (t, a);
804 Track::set_slaved (bool s)
806 _disk_reader->set_slaved (s);
807 _disk_writer->set_slaved (s);
813 return _disk_reader->output_streams();
817 Track::get_capture_start_frame (uint32_t n) const
819 return _disk_writer->get_capture_start_frame (n);
823 Track::alignment_style () const
825 return _disk_writer->alignment_style ();
829 Track::alignment_choice () const
831 return _disk_writer->alignment_choice ();
835 Track::current_capture_start () const
837 return _disk_writer->current_capture_start ();
841 Track::current_capture_end () const
843 return _disk_writer->current_capture_end ();
847 Track::playlist_modified ()
849 _disk_reader->playlist_modified ();
853 Track::find_and_use_playlist (DataType dt, PBD::ID const & id)
855 boost::shared_ptr<Playlist> playlist;
857 if ((playlist = _session.playlists->by_id (id)) == 0) {
862 error << string_compose(_("DiskIOProcessor: \"%1\" isn't an playlist"), id.to_s()) << endmsg;
866 return use_playlist (dt, playlist);
870 Track::use_playlist (DataType dt, boost::shared_ptr<Playlist> p)
874 if ((ret = _disk_reader->use_playlist (dt, p)) == 0) {
875 if ((ret = _disk_writer->use_playlist (dt, p)) == 0) {
876 p->set_orig_track_id (id());
888 Track::use_copy_playlist ()
890 assert (_playlists[data_type()]);
892 if (_playlists[data_type()] == 0) {
893 error << string_compose(_("DiskIOProcessor %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
898 boost::shared_ptr<Playlist> playlist;
900 newname = Playlist::bump_name (_playlists[data_type()]->name(), _session);
902 if ((playlist = PlaylistFactory::create (_playlists[data_type()], newname)) == 0) {
906 playlist->reset_shares();
908 return use_playlist (data_type(), playlist);
912 Track::use_new_playlist ()
915 boost::shared_ptr<Playlist> playlist = _playlists[data_type()];
918 newname = Playlist::bump_name (playlist->name(), _session);
920 newname = Playlist::bump_name (_name, _session);
923 playlist = PlaylistFactory::create (data_type(), _session, newname, hidden());
929 return use_playlist (data_type(), playlist);
933 Track::set_align_choice (AlignChoice ac, bool force)
937 _alignment_choice = Automatic;
938 set_align_choice_from_io ();
944 _disk_writer->set_align_choice (ac, force);
945 _alignment_choice = ac;
949 Track::set_align_style (AlignStyle s, bool force)
951 _disk_writer->set_align_style (s, force);
955 Track::set_align_choice_from_io ()
957 bool have_physical = false;
961 vector<string> connections;
962 boost::shared_ptr<Port> p;
966 p = _input->nth (n++);
972 if (p->get_connections (connections) != 0) {
973 if (AudioEngine::instance()->port_is_physical (connections[0])) {
974 have_physical = true;
979 connections.clear ();
984 // compensate for latency when bouncing from master or mixbus.
985 // we need to use "ExistingMaterial" to pick up the master bus' latency
986 // see also Route::direct_feeds_according_to_reality
988 ios.push_back (_input);
989 if (_session.master_out() && ios.fed_by (_session.master_out()->output())) {
990 have_physical = true;
992 for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) {
993 if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) {
994 have_physical = true;
1000 _disk_writer->set_align_style (ExistingMaterial);
1002 _disk_writer->set_align_style (CaptureTime);
1007 Track::set_block_size (pframes_t n)
1009 Route::set_block_size (n);
1010 _disk_reader->set_block_size (n);
1011 _disk_writer->set_block_size (n);
1015 Track::adjust_playback_buffering ()
1018 _disk_reader->adjust_buffering ();
1023 Track::adjust_capture_buffering ()
1026 _disk_writer->adjust_buffering ();
1030 #ifdef USE_TRACKS_CODE_FEATURES
1032 /* This is the Tracks version of Track::monitoring_state().
1034 * Ardour developers: try to flag or fix issues if parts of the libardour API
1035 * change in ways that invalidate this
1039 Track::monitoring_state () const
1041 /* Explicit requests */
1043 if (_monitoring != MonitorInput) {
1044 return MonitoringInput;
1047 if (_monitoring & MonitorDisk) {
1048 return MonitoringDisk;
1051 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
1052 I don't think it's ever going to be too pretty too look at.
1055 // GZ: NOT USED IN TRACKS
1056 //bool const auto_input = _session.config.get_auto_input ();
1057 //bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
1058 //bool const tape_machine_mode = Config->get_tape_machine_mode ();
1060 bool const roll = _session.transport_rolling ();
1061 bool const track_rec = _diskstream->record_enabled ();
1062 bool session_rec = _session.actively_recording ();
1066 if (!session_rec && roll) {
1067 return MonitoringDisk;
1069 return MonitoringInput;
1075 return MonitoringDisk;
1079 return MonitoringSilence;
1084 /* This is the Ardour/Mixbus version of Track::monitoring_state().
1086 * Tracks developers: do NOT modify this method under any circumstances.
1090 Track::monitoring_state () const
1092 /* Explicit requests */
1093 MonitorChoice m (_monitoring_control->monitoring_choice());
1095 if (m != MonitorAuto) {
1097 MonitorState ms ((MonitorState) 0);
1099 if (m & MonitorInput) {
1100 ms = MonitoringInput;
1103 if (m & MonitorDisk) {
1104 ms = MonitorState (ms | MonitoringDisk);
1110 switch (_session.config.get_session_monitoring ()) {
1112 return MonitoringDisk;
1115 return MonitoringInput;
1121 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
1122 I don't think it's ever going to be too pretty too look at.
1125 bool const roll = _session.transport_rolling ();
1126 bool const track_rec = _disk_writer->record_enabled ();
1127 bool const auto_input = _session.config.get_auto_input ();
1128 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
1129 bool const tape_machine_mode = Config->get_tape_machine_mode ();
1132 /* I suspect that just use actively_recording() is good enough all the
1133 * time, but just to keep the semantics the same as they were before
1134 * sept 26th 2012, we differentiate between the cases where punch is
1135 * enabled and those where it is not.
1137 * rg: I suspect this is not the case: monitoring may differ
1140 if (_session.config.get_punch_in() || _session.config.get_punch_out() || _session.preroll_record_punch_enabled ()) {
1141 session_rec = _session.actively_recording ();
1143 session_rec = _session.get_record_enabled();
1148 if (!session_rec && roll && auto_input) {
1149 return MonitoringDisk;
1151 return software_monitor ? MonitoringInput : MonitoringSilence;
1156 if (tape_machine_mode) {
1158 return MonitoringDisk;
1162 if (!roll && auto_input) {
1163 return software_monitor ? MonitoringInput : MonitoringSilence;
1165 return MonitoringDisk;
1171 abort(); /* NOTREACHED */
1172 return MonitoringSilence;
1178 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
1180 /* never declick if there is an internal generator - we just want it to
1181 keep generating sound without interruption.
1183 ditto if we are monitoring inputs.
1186 if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
1191 declick = _pending_declick;
1195 Amp::declick (bufs, nframes, declick);
1200 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
1202 if (_roll_delay > nframes) {
1204 _roll_delay -= nframes;
1205 silence_unlocked (nframes);
1206 /* transport frame is not legal for caller to use */
1209 } else if (_roll_delay > 0) {
1211 nframes -= _roll_delay;
1212 silence_unlocked (_roll_delay);
1213 transport_frame += _roll_delay;
1215 /* shuffle all the port buffers for things that lead "out" of this Route
1216 to reflect that we just wrote _roll_delay frames of silence.
1219 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1220 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1221 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1223 iop->increment_port_buffer_offset (_roll_delay);
1226 _output->increment_port_buffer_offset (_roll_delay);
1236 Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
1238 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1239 (*i)->monitoring_changed ();
1244 Track::metering_state () const
1247 if (_session.transport_rolling ()) {
1248 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1249 rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled());
1251 // track no_roll() always metering if
1252 rv = _meter_point == MeterInput;
1254 return rv ? MeteringInput : MeteringRoute;
1258 Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure)
1260 if (Route::set_processor_state (node, prop, new_order, must_configure)) {
1264 if (prop->value() == "diskreader") {
1266 _disk_reader->set_state (node, Stateful::current_state_version);
1267 new_order.push_back (_disk_reader);
1270 } else if (prop->value() == "diskwriter") {
1272 _disk_writer->set_state (node, Stateful::current_state_version);
1273 new_order.push_back (_disk_writer);
1278 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
1283 Track::use_captured_sources (SourceList& srcs, CaptureInfos const & capture_info)
1289 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
1290 boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
1293 use_captured_audio_sources (srcs, capture_info);
1297 use_captured_midi_sources (srcs, capture_info);
1302 Track::use_captured_midi_sources (SourceList& srcs, CaptureInfos const & capture_info)
1304 if (srcs.empty() || data_type() != DataType::MIDI) {
1308 boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
1309 boost::shared_ptr<Playlist> pl = _playlists[DataType::MIDI];
1310 boost::shared_ptr<MidiRegion> midi_region;
1311 CaptureInfos::const_iterator ci;
1317 framecnt_t total_capture = 0;
1319 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1320 total_capture += (*ci)->frames;
1323 /* we will want to be able to keep (over)writing the source
1324 but we don't want it to be removable. this also differs
1325 from the audio situation, where the source at this point
1326 must be considered immutable. luckily, we can rely on
1327 MidiSource::mark_streaming_write_completed() to have
1328 already done the necessary work for that.
1331 string whole_file_region_name;
1332 whole_file_region_name = region_name_from_path (mfs->name(), true);
1334 /* Register a new region with the Session that
1335 describes the entire source. Do this first
1336 so that any sub-regions will obviously be
1337 children of this one (later!)
1343 plist.add (Properties::name, whole_file_region_name);
1344 plist.add (Properties::whole_file, true);
1345 plist.add (Properties::automatic, true);
1346 plist.add (Properties::start, 0);
1347 plist.add (Properties::length, total_capture);
1348 plist.add (Properties::layer, 0);
1350 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1352 midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1353 midi_region->special_set_position (capture_info.front()->start);
1356 catch (failed_constructor& err) {
1357 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1361 pl->clear_changes ();
1364 /* Session frame time of the initial capture in this pass, which is where the source starts */
1365 framepos_t initial_capture = 0;
1366 if (!capture_info.empty()) {
1367 initial_capture = capture_info.front()->start;
1370 BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start);
1371 const framepos_t preroll_off = _session.preroll_record_trim_len ();
1373 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1377 RegionFactory::region_name (region_name, mfs->name(), false);
1379 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
1380 _name, (*ci)->start, (*ci)->frames, region_name));
1383 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1388 /* start of this region is the offset between the start of its capture and the start of the whole pass */
1389 plist.add (Properties::start, (*ci)->start - initial_capture);
1390 plist.add (Properties::length, (*ci)->frames);
1391 plist.add (Properties::length_beats, converter.from((*ci)->frames).to_double());
1392 plist.add (Properties::name, region_name);
1394 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1395 midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1396 if (preroll_off > 0) {
1397 midi_region->trim_front ((*ci)->start - initial_capture + preroll_off);
1401 catch (failed_constructor& err) {
1402 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1403 continue; /* XXX is this OK? */
1406 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1408 pl->add_region (midi_region, (*ci)->start + preroll_off);
1412 _session.add_command (new StatefulDiffCommand (pl));
1416 Track::use_captured_audio_sources (SourceList& srcs, CaptureInfos const & capture_info)
1418 if (srcs.empty() || data_type() != DataType::AUDIO) {
1422 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
1423 boost::shared_ptr<Playlist> pl = _playlists[DataType::AUDIO];
1424 boost::shared_ptr<AudioRegion> region;
1430 /* destructive tracks have a single, never changing region */
1432 if (destructive()) {
1434 /* send a signal that any UI can pick up to do the right thing. there is
1435 a small problem here in that a UI may need the peak data to be ready
1436 for the data that was recorded and this isn't interlocked with that
1437 process. this problem is deferred to the UI.
1440 pl->LayeringChanged(); // XXX this may not get the UI to do the right thing
1444 string whole_file_region_name;
1445 whole_file_region_name = region_name_from_path (afs->name(), true);
1447 /* Register a new region with the Session that
1448 describes the entire source. Do this first
1449 so that any sub-regions will obviously be
1450 children of this one (later!)
1456 plist.add (Properties::start, afs->last_capture_start_frame());
1457 plist.add (Properties::length, afs->length(0));
1458 plist.add (Properties::name, whole_file_region_name);
1459 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1460 rx->set_automatic (true);
1461 rx->set_whole_file (true);
1463 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1464 region->special_set_position (afs->natural_position());
1468 catch (failed_constructor& err) {
1469 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1473 pl->clear_changes ();
1474 pl->set_capture_insertion_in_progress (true);
1477 const framepos_t preroll_off = _session.preroll_record_trim_len ();
1478 framecnt_t buffer_position = afs->last_capture_start_frame ();
1479 CaptureInfos::const_iterator ci;
1481 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1485 RegionFactory::region_name (region_name, whole_file_region_name, false);
1487 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
1488 _name, (*ci)->start, (*ci)->frames, region_name, buffer_position));
1494 plist.add (Properties::start, buffer_position);
1495 plist.add (Properties::length, (*ci)->frames);
1496 plist.add (Properties::name, region_name);
1498 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1499 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1500 if (preroll_off > 0) {
1501 region->trim_front (buffer_position + preroll_off);
1505 catch (failed_constructor& err) {
1506 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1507 continue; /* XXX is this OK? */
1510 pl->add_region (region, (*ci)->start + preroll_off, 1, _disk_writer->non_layered());
1511 pl->set_layer (region, DBL_MAX);
1513 buffer_position += (*ci)->frames;
1517 pl->set_capture_insertion_in_progress (false);
1518 _session.add_command (new StatefulDiffCommand (pl));
1522 __attribute__((annotate("realtime")))
1525 Track::setup_invisible_processors_oh_children_of_mine (ProcessorList& processors)
1527 ProcessorList::iterator insert_pos;
1529 switch (_disk_io_point) {
1530 case DiskIOPreFader:
1531 insert_pos = find (processors.begin(), processors.end(), _trim);
1532 if (insert_pos != processors.end()) {
1533 insert_pos = processors.insert (insert_pos, _disk_writer);
1534 processors.insert (insert_pos, _disk_reader);
1537 case DiskIOPostFader:
1538 insert_pos = find (processors.begin(), processors.end(), _main_outs);
1539 if (insert_pos != processors.end()) {
1540 insert_pos = processors.insert (insert_pos, _disk_writer);
1541 processors.insert (insert_pos, _disk_reader);
1549 Track::set_disk_io_position (DiskIOPoint diop)
1551 bool display = false;
1561 _disk_writer->set_display_to_user (display);
1562 _disk_reader->set_display_to_user (display);
1564 const bool changed = (diop != _disk_io_point);
1566 _disk_io_point = diop;
1569 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
1570 configure_processors (0);