2 * Copyright (C) 1999-2019 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2007 Jesse Chappell <jesse@essej.net>
4 * Copyright (C) 2006-2009 Sampo Savolainen <v2@iki.fi>
5 * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
6 * Copyright (C) 2006-2016 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2008-2009 Hans Baier <hansfbaier@googlemail.com>
9 * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
10 * Copyright (C) 2013-2017 Nick Mainsbridge <mainsbridge@gmail.com>
11 * Copyright (C) 2014-2019 Ben Loftis <ben@harrisonconsoles.com>
12 * Copyright (C) 2015 GZharun <grygoriiz@wavesglobal.com>
13 * Copyright (C) 2016-2018 Len Ovens <len@ovenwerks.net>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include <cstdio> /* sprintf(3) ... grrr */
42 #include <glibmm/threads.h>
43 #include <glibmm/miscutils.h>
44 #include <glibmm/fileutils.h>
46 #include <boost/algorithm/string/erase.hpp>
48 #include "pbd/basename.h"
49 #include "pbd/convert.h"
50 #include "pbd/error.h"
51 #include "pbd/file_utils.h"
54 #include "pbd/pthread_utils.h"
55 #include "pbd/search_path.h"
56 #include "pbd/stacktrace.h"
57 #include "pbd/stl_delete.h"
58 #include "pbd/replace_all.h"
59 #include "pbd/types_convert.h"
60 #include "pbd/unwind.h"
62 #include "ardour/amp.h"
63 #include "ardour/analyser.h"
64 #include "ardour/async_midi_port.h"
65 #include "ardour/audio_buffer.h"
66 #include "ardour/audio_port.h"
67 #include "ardour/audio_track.h"
68 #include "ardour/audioengine.h"
69 #include "ardour/audiofilesource.h"
70 #include "ardour/auditioner.h"
71 #include "ardour/boost_debug.h"
72 #include "ardour/buffer_manager.h"
73 #include "ardour/buffer_set.h"
74 #include "ardour/bundle.h"
75 #include "ardour/butler.h"
76 #include "ardour/click.h"
77 #include "ardour/control_protocol_manager.h"
78 #include "ardour/data_type.h"
79 #include "ardour/debug.h"
80 #include "ardour/disk_reader.h"
81 #include "ardour/directory_names.h"
82 #include "ardour/filename_extensions.h"
83 #include "ardour/gain_control.h"
84 #include "ardour/graph.h"
85 #include "ardour/luabindings.h"
86 #include "ardour/midiport_manager.h"
87 #include "ardour/scene_changer.h"
88 #include "ardour/midi_patch_manager.h"
89 #include "ardour/midi_track.h"
90 #include "ardour/midi_ui.h"
91 #include "ardour/operations.h"
92 #include "ardour/playlist.h"
93 #include "ardour/playlist_factory.h"
94 #include "ardour/plugin.h"
95 #include "ardour/plugin_insert.h"
96 #include "ardour/process_thread.h"
97 #include "ardour/profile.h"
98 #include "ardour/rc_configuration.h"
99 #include "ardour/recent_sessions.h"
100 #include "ardour/region.h"
101 #include "ardour/region_factory.h"
102 #include "ardour/revision.h"
103 #include "ardour/route_graph.h"
104 #include "ardour/route_group.h"
105 #include "ardour/rt_tasklist.h"
106 #include "ardour/send.h"
107 #include "ardour/selection.h"
108 #include "ardour/session.h"
109 #include "ardour/session_directory.h"
110 #include "ardour/session_playlists.h"
111 #include "ardour/smf_source.h"
112 #include "ardour/solo_isolate_control.h"
113 #include "ardour/source_factory.h"
114 #include "ardour/speakers.h"
115 #include "ardour/tempo.h"
116 #include "ardour/ticker.h"
117 #include "ardour/transport_fsm.h"
118 #include "ardour/transport_master.h"
119 #include "ardour/transport_master_manager.h"
120 #include "ardour/track.h"
121 #include "ardour/types_convert.h"
122 #include "ardour/user_bundle.h"
123 #include "ardour/utils.h"
124 #include "ardour/vca_manager.h"
125 #include "ardour/vca.h"
127 #include "midi++/port.h"
128 #include "midi++/mmc.h"
130 #include "LuaBridge/LuaBridge.h"
132 #include <glibmm/checksum.h>
141 using namespace ARDOUR;
144 bool Session::_disable_all_loaded_plugins = false;
145 bool Session::_bypass_all_loaded_plugins = false;
146 guint Session::_name_id_counter = 0;
148 PBD::Signal1<void,std::string> Session::Dialog;
149 PBD::Signal0<int> Session::AskAboutPendingState;
150 PBD::Signal2<int, samplecnt_t, samplecnt_t> Session::AskAboutSampleRateMismatch;
151 PBD::Signal2<void, samplecnt_t, samplecnt_t> Session::NotifyAboutSampleRateMismatch;
152 PBD::Signal0<void> Session::SendFeedback;
153 PBD::Signal3<int,Session*,std::string,DataType> Session::MissingFile;
155 PBD::Signal1<void, samplepos_t> Session::StartTimeChanged;
156 PBD::Signal1<void, samplepos_t> Session::EndTimeChanged;
157 PBD::Signal2<void,std::string, std::string> Session::Exported;
158 PBD::Signal1<int,boost::shared_ptr<Playlist> > Session::AskAboutPlaylistDeletion;
159 PBD::Signal0<void> Session::Quit;
160 PBD::Signal0<void> Session::FeedbackDetected;
161 PBD::Signal0<void> Session::SuccessfulGraphSort;
162 PBD::Signal2<void,std::string,std::string> Session::VersionMismatch;
164 const samplecnt_t Session::bounce_chunk_size = 8192;
165 static void clean_up_session_event (SessionEvent* ev) { delete ev; }
166 const SessionEvent::RTeventCallback Session::rt_cleanup (clean_up_session_event);
167 const uint32_t Session::session_end_shift = 0;
169 /** @param snapshot_name Snapshot name, without .ardour suffix */
170 Session::Session (AudioEngine &eng,
171 const string& fullpath,
172 const string& snapshot_name,
173 BusProfile const * bus_profile,
175 : _playlists (new SessionPlaylists)
177 , process_function (&Session::process_with_events)
178 , _bounce_processing_active (false)
179 , waiting_for_sync_offset (false)
180 , _base_sample_rate (0)
181 , _nominal_sample_rate (0)
182 , _current_sample_rate (0)
183 , _record_status (Disabled)
184 , _transport_sample (0)
186 , _session_range_location (0)
187 , _session_range_is_free (true)
189 , _remaining_latency_preroll (0)
190 , _engine_speed (1.0)
191 , _transport_speed (0)
192 , _default_transport_speed (1.0)
193 , _last_transport_speed (0)
194 , _signalled_varispeed (0)
195 , _target_transport_speed (0.0)
196 , auto_play_legal (false)
197 , _requested_return_sample (-1)
198 , current_block_size (0)
199 , _worst_output_latency (0)
200 , _worst_input_latency (0)
201 , _worst_route_latency (0)
202 , _send_latency_changes (0)
203 , _have_captured (false)
204 , _non_soloed_outs_muted (false)
207 , _solo_isolated_cnt (0)
209 , _was_seamless (Config->get_seamless_loop ())
210 , _under_nsm_control (false)
212 , transport_master_tracking_state (Stopped)
213 , master_wait_end (0)
214 , post_export_sync (false)
215 , post_export_position (0)
217 , _export_rolling (false)
218 , _realtime_export (false)
219 , _region_export (false)
220 , _export_preroll (0)
221 , _pre_export_mmc_enabled (false)
222 , _name (snapshot_name)
224 , _send_qf_mtc (false)
225 , _pframes_since_last_mtc (0)
227 , loop_changing (false)
229 , _session_dir (new SessionDirectory (fullpath))
230 , _current_snapshot_name (snapshot_name)
232 , state_was_pending (false)
233 , _state_of_the_state (StateOfTheState (CannotSave | InitialConnecting | Loading))
235 , _save_queued (false)
236 , _last_roll_location (0)
237 , _last_roll_or_reversal_location (0)
238 , _last_record_location (0)
239 , pending_auto_loop (false)
240 , _mempool ("Session", 3145728)
241 , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
243 , _butler (new Butler (*this))
244 , _transport_fsm (new TransportFSM (*this))
245 , _post_transport_work (0)
246 , _locations (new Locations (*this))
247 , _ignore_skips_updates (false)
248 , _rt_thread_active (false)
249 , _rt_emit_pending (false)
250 , _ac_thread_active (0)
251 , _latency_recompute_pending (0)
253 , outbound_mtc_timecode_frame (0)
254 , next_quarter_frame_to_send (-1)
255 , _samples_per_timecode_frame (0)
256 , _frames_per_hour (0)
257 , _timecode_frames_per_hour (0)
258 , last_timecode_valid (false)
259 , last_timecode_when (0)
260 , _send_timecode_update (false)
272 , ltc_timecode_offset (0)
273 , ltc_timecode_negative_offset (false)
274 , midi_control_ui (0)
276 , _all_route_group (new RouteGroup (*this, "all"))
277 , routes (new RouteList)
278 , _adding_routes_in_progress (false)
279 , _reconnecting_routes_in_progress (false)
280 , _route_deletion_in_progress (false)
281 , destructive_index (0)
282 , _track_number_decimals(1)
283 , default_fade_steepness (0)
284 , default_fade_msecs (0)
285 , _total_free_4k_blocks (0)
286 , _total_free_4k_blocks_uncertain (false)
287 , no_questions_about_missing_files (false)
290 , _bundles (new BundleList)
291 , _bundle_xml_node (0)
294 , _click_rec_only (false)
296 , click_emphasis_data (0)
298 , click_emphasis_length (0)
299 , _clicks_cleared (0)
300 , _count_in_samples (0)
301 , _play_range (false)
302 , _range_selection (-1,-1)
303 , _object_selection (-1,-1)
304 , _preroll_record_trim_len (0)
305 , _count_in_once (false)
307 , first_file_data_format_reset (true)
308 , first_file_header_format_reset (true)
309 , have_looped (false)
310 , _have_rec_enabled_track (false)
311 , _have_rec_disabled_track (true)
313 , _suspend_timecode_transmission (0)
314 , _speakers (new Speakers)
315 , _ignore_route_processor_changes (0)
320 , _vca_manager (new VCAManager (*this))
321 , _selection (new CoreSelection (*this))
322 , _global_locate_pending (false)
324 created_with = string_compose ("%1 %2", PROGRAM_NAME, revision);
326 pthread_mutex_init (&_rt_emit_mutex, 0);
327 pthread_cond_init (&_rt_emit_cond, 0);
329 pthread_mutex_init (&_auto_connect_mutex, 0);
330 pthread_cond_init (&_auto_connect_cond, 0);
332 init_name_id_counter (1); // reset for new sessions, start at 1
333 VCA::set_next_vca_number (1); // reset for new sessions, start at 1
335 pre_engine_init (fullpath); // sets _is_new
339 assert (AudioEngine::instance()->running());
340 immediately_post_engine ();
344 Stateful::loading_state_version = CURRENT_SESSION_FILE_VERSION;
346 if (create (mix_template, bus_profile)) {
348 throw SessionException (_("Session initialization failed"));
351 /* if a mix template was provided, then ::create() will
352 * have copied it into the session and we need to load it
353 * so that we have the state ready for ::set_state()
354 * after the engine is started.
356 * Note that we do NOT try to get the sample rate from
357 * the template at this time, though doing so would
358 * be easy if we decided this was an appropriate part
362 if (!mix_template.empty()) {
364 if (load_state (_current_snapshot_name)) {
365 throw SessionException (_("Failed to load template/snapshot state"));
367 } catch (PBD::unknown_enumeration& e) {
368 throw SessionException (_("Failed to parse template/snapshot state"));
370 store_recent_templates (mix_template);
373 /* load default session properties - if any */
378 if (load_state (_current_snapshot_name)) {
379 throw SessionException (_("Failed to load state"));
383 int err = post_engine_init ();
389 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Failed to create background threads.")));
393 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid TempoMap in session-file.")));
396 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid or corrupt session state.")));
399 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Port registration failed.")));
402 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Unexpected exception during session setup, possibly invalid audio/midi engine parameters. Please see stdout/stderr for details")));
407 store_recent_sessions (_name, _path);
409 bool was_dirty = dirty();
412 PresentationInfo::Change.connect_same_thread (*this, boost::bind (&Session::notify_presentation_info_change, this));
414 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, false));
415 config.ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, true));
418 DirtyChanged (); /* EMIT SIGNAL */
421 StartTimeChanged.connect_same_thread (*this, boost::bind (&Session::start_time_changed, this, _1));
422 EndTimeChanged.connect_same_thread (*this, boost::bind (&Session::end_time_changed, this, _1));
424 LatentSend::ChangedLatency.connect_same_thread (*this, boost::bind (&Session::send_latency_compensation_change, this));
425 Latent::DisableSwitchChanged.connect_same_thread (*this, boost::bind (&Session::queue_latency_recompute, this));
427 emit_thread_start ();
428 auto_connect_thread_start ();
430 /* hook us up to the engine since we are now completely constructed */
432 BootMessage (_("Connect to engine"));
434 _engine.set_session (this);
435 _engine.reset_timebase ();
437 ensure_subdirs (); // archived or zipped sessions may lack peaks/ analysis/ etc
439 if (!mix_template.empty ()) {
440 /* ::create() unsets _is_new after creating the session.
441 * But for templated sessions, the sample-rate is initially unset
442 * (not read from template), so we need to save it (again).
450 BootMessage (_("Session loading complete"));
462 Session::next_name_id ()
464 return g_atomic_int_add (&_name_id_counter, 1);
468 Session::name_id_counter ()
470 return g_atomic_int_get (&_name_id_counter);
474 Session::init_name_id_counter (guint n)
476 g_atomic_int_set (&_name_id_counter, n);
480 Session::immediately_post_engine ()
482 /* Do various initializations that should take place directly after we
483 * know that the engine is running, but before we either create a
484 * session or set state for an existing one.
487 _rt_tasklist.reset (new RTTaskList ());
489 if (how_many_dsp_threads () > 1) {
490 /* For now, only create the graph if we are using >1 DSP threads, as
491 it is a bit slower than the old code with 1 thread.
493 _process_graph.reset (new Graph (*this));
496 /* Restart transport FSM */
498 _transport_fsm->start ();
500 /* every time we reconnect, do stuff ... */
502 _engine.Running.connect_same_thread (*this, boost::bind (&Session::engine_running, this));
504 if (synced_to_engine()) {
505 _engine.transport_stop ();
508 if (config.get_jack_time_master()) {
509 _engine.transport_locate (_transport_sample);
513 BootMessage (_("Set up LTC"));
515 BootMessage (_("Set up Click"));
517 BootMessage (_("Set up standard connections"));
521 catch (failed_constructor& err) {
525 /* TODO, connect in different thread. (PortRegisteredOrUnregistered may be in RT context)
527 _engine.PortRegisteredOrUnregistered.connect_same_thread (*this, boost::bind (&Session::setup_bundles, this));
529 // set samplerate for plugins added early
530 // e.g from templates or MB channelstrip
531 set_block_size (_engine.samples_per_cycle());
532 set_sample_rate (_engine.sample_rate());
540 vector<void*> debug_pointers;
542 /* if we got to here, leaving pending capture state around
546 remove_pending_capture_state ();
550 _state_of_the_state = StateOfTheState (CannotSave | Deletion);
553 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
557 /* disconnect from any and all signals that we are connected to */
559 Port::PortSignalDrop (); /* EMIT SIGNAL */
562 /* shutdown control surface protocols while we still have ports
563 * and the engine to move data to any devices.
565 ControlProtocolManager::instance().drop_protocols ();
567 /* stop auto dis/connecting */
568 auto_connect_thread_terminate ();
570 MIDI::Name::MidiPatchManager::instance().remove_search_path(session_directory().midi_patch_path());
572 _engine.remove_session ();
574 /* deregister all ports - there will be no process or any other
575 * callbacks from the engine any more.
578 Port::PortDrop (); /* EMIT SIGNAL */
580 /* remove I/O objects that we (the session) own */
582 _ltc_output.reset ();
585 Glib::Threads::Mutex::Lock lm (controllables_lock);
586 for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
587 (*i)->DropReferences (); /* EMIT SIGNAL */
589 controllables.clear ();
592 /* clear history so that no references to objects are held any more */
596 /* clear state tree so that no references to objects are held any more */
602 /* unregister all lua functions, drop held references (if any) */
603 Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
605 lua.do_command ("Session = nil");
613 lua.collect_garbage ();
616 /* reset dynamic state version back to default */
617 Stateful::loading_state_version = 0;
619 _butler->drop_references ();
623 delete _all_route_group;
625 DEBUG_TRACE (DEBUG::Destruction, "delete route groups\n");
626 for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
630 if (click_data != default_click) {
631 delete [] click_data;
634 if (click_emphasis_data != default_click_emphasis) {
635 delete [] click_emphasis_data;
640 /* need to remove auditioner before monitoring section
641 * otherwise it is re-connected.
642 * Note: If a session was never successfully loaded, there
643 * may not yet be an auditioner.
646 auditioner->drop_references ();
650 /* drop references to routes held by the monitoring section
651 * specifically _monitor_out aux/listen references */
652 remove_monitor_section();
654 /* clear out any pending dead wood from RCU managed objects */
659 DiskReader::free_working_buffers();
661 /* tell everyone who is still standing that we're about to die */
664 /* tell everyone to drop references and delete objects as we go */
666 DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
667 RegionFactory::delete_all_regions ();
669 /* Do this early so that VCAs no longer hold references to routes */
671 DEBUG_TRACE (DEBUG::Destruction, "delete vcas\n");
674 DEBUG_TRACE (DEBUG::Destruction, "delete routes\n");
676 /* reset these three references to special routes before we do the usual route delete thing */
678 _master_out.reset ();
679 _monitor_out.reset ();
682 RCUWriter<RouteList> writer (routes);
683 boost::shared_ptr<RouteList> r = writer.get_copy ();
685 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
686 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for route %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
687 (*i)->drop_references ();
691 /* writer goes out of scope and updates master */
696 DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
697 Glib::Threads::Mutex::Lock lm (source_lock);
698 for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
699 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->name(), i->second.use_count()));
700 i->second->drop_references ();
706 /* not strictly necessary, but doing it here allows the shared_ptr debugging to work */
709 emit_thread_terminate ();
711 pthread_cond_destroy (&_rt_emit_cond);
712 pthread_mutex_destroy (&_rt_emit_mutex);
714 pthread_cond_destroy (&_auto_connect_cond);
715 pthread_mutex_destroy (&_auto_connect_mutex);
717 delete _scene_changer; _scene_changer = 0;
718 delete midi_control_ui; midi_control_ui = 0;
720 delete _mmc; _mmc = 0;
721 delete _midi_ports; _midi_ports = 0;
722 delete _locations; _locations = 0;
727 /* clear event queue, the session is gone, nobody is interested in
728 * those anymore, but they do leak memory if not removed
730 while (!immediate_events.empty ()) {
731 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
732 SessionEvent *ev = immediate_events.front ();
733 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Drop event: %1\n", enum_2_string (ev->type)));
734 immediate_events.pop_front ();
738 case SessionEvent::AutoLoop:
739 case SessionEvent::Skip:
740 case SessionEvent::PunchIn:
741 case SessionEvent::PunchOut:
742 case SessionEvent::RangeStop:
743 case SessionEvent::RangeLocate:
747 case SessionEvent::RealTimeOperation:
754 del = del && !_remove_event (ev);
762 /* unregister all dropped ports, process pending port deletion. */
763 // this may call ARDOUR::Port::drop ... jack_port_unregister ()
764 // jack1 cannot cope with removing ports while processing
765 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
766 AudioEngine::instance()->clear_pending_port_deletions ();
769 DEBUG_TRACE (DEBUG::Destruction, "delete selection\n");
773 _transport_fsm->stop ();
775 DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n");
778 Controllable::dump_registry ();
781 BOOST_SHOW_POINTERS ();
785 Session::setup_ltc ()
789 _ltc_output.reset (new IO (*this, X_("LTC Out"), IO::Output));
791 if (state_tree && (child = find_named_node (*state_tree->root(), X_("LTC Out"))) != 0) {
792 _ltc_output->set_state (*(child->children().front()), Stateful::loading_state_version);
795 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
796 _ltc_output->ensure_io (ChanCount (DataType::AUDIO, 1), true, this);
797 // TODO use auto-connect thread
798 reconnect_ltc_output ();
802 /* fix up names of LTC ports because we don't want the normal
803 * IO style of NAME/TYPE-{in,out}N
806 _ltc_output->nth (0)->set_name (X_("LTC-out"));
810 Session::setup_click ()
814 boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (GainAutomation)));
815 boost::shared_ptr<GainControl> gain_control = boost::shared_ptr<GainControl> (new GainControl (*this, Evoral::Parameter(GainAutomation), gl));
817 _click_io.reset (new ClickIO (*this, X_("Click")));
818 _click_gain.reset (new Amp (*this, _("Fader"), gain_control, true));
819 _click_gain->activate ();
821 setup_click_state (state_tree->root());
823 setup_click_state (0);
828 Session::setup_click_state (const XMLNode* node)
830 const XMLNode* child = 0;
832 if (node && (child = find_named_node (*node, "Click")) != 0) {
834 /* existing state for Click */
837 if (Stateful::loading_state_version < 3000) {
838 c = _click_io->set_state_2X (*child->children().front(), Stateful::loading_state_version, false);
840 const XMLNodeList& children (child->children());
841 XMLNodeList::const_iterator i = children.begin();
842 if ((c = _click_io->set_state (**i, Stateful::loading_state_version)) == 0) {
844 if (i != children.end()) {
845 c = _click_gain->set_state (**i, Stateful::loading_state_version);
851 _clicking = Config->get_clicking ();
855 error << _("could not setup Click I/O") << endmsg;
862 /* default state for Click: dual-mono to first 2 physical outputs */
865 _engine.get_physical_outputs (DataType::AUDIO, outs);
867 for (uint32_t physport = 0; physport < 2; ++physport) {
868 if (outs.size() > physport) {
869 if (_click_io->add_port (outs[physport], this)) {
870 // relax, even though its an error
875 if (_click_io->n_ports () > ChanCount::ZERO) {
876 _clicking = Config->get_clicking ();
882 Session::get_physical_ports (vector<string>& inputs, vector<string>& outputs, DataType type,
883 MidiPortFlags include, MidiPortFlags exclude)
885 _engine.get_physical_inputs (type, inputs, include, exclude);
886 _engine.get_physical_outputs (type, outputs, include, exclude);
891 Session::auto_connect_master_bus ()
893 if (!_master_out || !Config->get_auto_connect_standard_busses() || _monitor_out) {
897 /* if requested auto-connect the outputs to the first N physical ports.
900 uint32_t limit = _master_out->n_outputs().n_total();
901 vector<string> outputs[DataType::num_types];
903 for (uint32_t i = 0; i < DataType::num_types; ++i) {
904 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
907 for (uint32_t n = 0; n < limit; ++n) {
908 boost::shared_ptr<Port> p = _master_out->output()->nth (n);
910 if (outputs[p->type()].size() > n) {
911 connect_to = outputs[p->type()][n];
914 if (!connect_to.empty() && p->connected_to (connect_to) == false) {
915 if (_master_out->output()->connect (p, connect_to, this)) {
916 error << string_compose (_("cannot connect master output %1 to %2"), n, connect_to)
925 Session::remove_monitor_section ()
931 /* allow deletion when session is unloaded */
932 if (!_engine.running() && !deletion_in_progress ()) {
933 error << _("Cannot remove monitor section while the engine is offline.") << endmsg;
937 /* force reversion to Solo-In-Place */
938 Config->set_solo_control_is_listen_control (false);
940 /* if we are auditioning, cancel it ... this is a workaround
941 to a problem (auditioning does not execute the process graph,
942 which is needed to remove routes when using >1 core for processing)
946 if (!deletion_in_progress ()) {
947 /* Hold process lock while doing this so that we don't hear bits and
948 * pieces of audio as we work on each route.
951 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
953 /* Connect tracks to monitor section. Note that in an
954 existing session, the internal sends will already exist, but we want the
955 routes to notice that they connect to the control out specifically.
959 boost::shared_ptr<RouteList> r = routes.reader ();
960 ProcessorChangeBlocker pcb (this, false);
962 for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
964 if ((*x)->is_monitor()) {
966 } else if ((*x)->is_master()) {
969 (*x)->remove_aux_or_listen (_monitor_out);
974 remove_route (_monitor_out);
975 if (deletion_in_progress ()) {
979 auto_connect_master_bus ();
982 auditioner->connect ();
985 MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
989 Session::add_monitor_section ()
993 if (!_engine.running()) {
994 error << _("Cannot create monitor section while the engine is offline.") << endmsg;
998 if (_monitor_out || !_master_out) {
1002 boost::shared_ptr<Route> r (new Route (*this, _("Monitor"), PresentationInfo::MonitorOut, DataType::AUDIO));
1008 BOOST_MARK_ROUTE(r);
1011 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1012 r->input()->ensure_io (_master_out->output()->n_ports(), false, this);
1013 r->output()->ensure_io (_master_out->output()->n_ports(), false, this);
1015 error << _("Cannot create monitor section. 'Monitor' Port name is not unique.") << endmsg;
1020 add_routes (rl, false, false, false, 0);
1022 assert (_monitor_out);
1024 /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
1025 are undefined, at best.
1028 uint32_t limit = _monitor_out->n_inputs().n_audio();
1032 /* connect the inputs to the master bus outputs. this
1033 * represents a separate data feed from the internal sends from
1034 * each route. as of jan 2011, it allows the monitor section to
1035 * conditionally ignore either the internal sends or the normal
1036 * input feed, but we should really find a better way to do
1040 _master_out->output()->disconnect (this);
1042 for (uint32_t n = 0; n < limit; ++n) {
1043 boost::shared_ptr<AudioPort> p = _monitor_out->input()->ports().nth_audio_port (n);
1044 boost::shared_ptr<AudioPort> o = _master_out->output()->ports().nth_audio_port (n);
1047 string connect_to = o->name();
1048 if (_monitor_out->input()->connect (p, connect_to, this)) {
1049 error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
1057 /* if monitor section is not connected, connect it to physical outs
1060 if ((Config->get_auto_connect_standard_busses () || Profile->get_mixbus ()) && !_monitor_out->output()->connected ()) {
1062 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1064 boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1067 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1069 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1070 Config->get_monitor_bus_preferred_bundle())
1076 /* Monitor bus is audio only */
1078 vector<string> outputs[DataType::num_types];
1080 for (uint32_t i = 0; i < DataType::num_types; ++i) {
1081 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1084 uint32_t mod = outputs[DataType::AUDIO].size();
1085 uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1089 for (uint32_t n = 0; n < limit; ++n) {
1091 boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1093 if (outputs[DataType::AUDIO].size() > (n % mod)) {
1094 connect_to = outputs[DataType::AUDIO][n % mod];
1097 if (!connect_to.empty()) {
1098 if (_monitor_out->output()->connect (p, connect_to, this)) {
1099 error << string_compose (
1100 _("cannot connect control output %1 to %2"),
1111 /* Hold process lock while doing this so that we don't hear bits and
1112 * pieces of audio as we work on each route.
1115 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1117 /* Connect tracks to monitor section. Note that in an
1118 existing session, the internal sends will already exist, but we want the
1119 routes to notice that they connect to the control out specifically.
1123 boost::shared_ptr<RouteList> rls = routes.reader ();
1125 ProcessorChangeBlocker pcb (this, false /* XXX */);
1127 for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1129 if ((*x)->is_monitor()) {
1131 } else if ((*x)->is_master()) {
1134 (*x)->enable_monitor_send ();
1139 auditioner->connect ();
1142 MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
1146 Session::reset_monitor_section ()
1148 /* Process lock should be held by the caller.*/
1150 if (!_monitor_out) {
1154 uint32_t limit = _master_out->n_outputs().n_audio();
1156 /* connect the inputs to the master bus outputs. this
1157 * represents a separate data feed from the internal sends from
1158 * each route. as of jan 2011, it allows the monitor section to
1159 * conditionally ignore either the internal sends or the normal
1160 * input feed, but we should really find a better way to do
1164 _master_out->output()->disconnect (this);
1165 _monitor_out->output()->disconnect (this);
1167 // monitor section follow master bus - except midi
1168 ChanCount mon_chn (_master_out->output()->n_ports());
1169 mon_chn.set_midi (0);
1171 _monitor_out->input()->ensure_io (mon_chn, false, this);
1172 _monitor_out->output()->ensure_io (mon_chn, false, this);
1174 for (uint32_t n = 0; n < limit; ++n) {
1175 boost::shared_ptr<AudioPort> p = _monitor_out->input()->ports().nth_audio_port (n);
1176 boost::shared_ptr<AudioPort> o = _master_out->output()->ports().nth_audio_port (n);
1179 string connect_to = o->name();
1180 if (_monitor_out->input()->connect (p, connect_to, this)) {
1181 error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
1188 /* connect monitor section to physical outs
1191 if (Config->get_auto_connect_standard_busses()) {
1193 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1195 boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1198 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1200 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1201 Config->get_monitor_bus_preferred_bundle())
1207 /* Monitor bus is audio only */
1209 vector<string> outputs[DataType::num_types];
1211 for (uint32_t i = 0; i < DataType::num_types; ++i) {
1212 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1215 uint32_t mod = outputs[DataType::AUDIO].size();
1216 uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1220 for (uint32_t n = 0; n < limit; ++n) {
1222 boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1224 if (outputs[DataType::AUDIO].size() > (n % mod)) {
1225 connect_to = outputs[DataType::AUDIO][n % mod];
1228 if (!connect_to.empty()) {
1229 if (_monitor_out->output()->connect (p, connect_to, this)) {
1230 error << string_compose (
1231 _("cannot connect control output %1 to %2"),
1242 /* Connect tracks to monitor section. Note that in an
1243 existing session, the internal sends will already exist, but we want the
1244 routes to notice that they connect to the control out specifically.
1248 boost::shared_ptr<RouteList> rls = routes.reader ();
1250 ProcessorChangeBlocker pcb (this, false);
1252 for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1254 if ((*x)->is_monitor()) {
1256 } else if ((*x)->is_master()) {
1259 (*x)->enable_monitor_send ();
1265 Session::add_master_bus (ChanCount const& count)
1267 if (master_out ()) {
1273 boost::shared_ptr<Route> r (new Route (*this, _("Master"), PresentationInfo::MasterOut, DataType::AUDIO));
1278 BOOST_MARK_ROUTE(r);
1281 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1282 r->input()->ensure_io (count, false, this);
1283 r->output()->ensure_io (count, false, this);
1287 add_routes (rl, false, false, false, PresentationInfo::max_order);
1292 Session::hookup_io ()
1294 /* stop graph reordering notifications from
1295 causing resorts, etc.
1298 _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
1302 /* we delay creating the auditioner till now because
1303 it makes its own connections to ports.
1307 boost::shared_ptr<Auditioner> a (new Auditioner (*this));
1309 throw failed_constructor ();
1314 catch (failed_constructor& err) {
1315 warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
1319 /* load bundles, which we may have postponed earlier on */
1320 if (_bundle_xml_node) {
1321 load_bundles (*_bundle_xml_node);
1322 delete _bundle_xml_node;
1325 /* Tell all IO objects to connect themselves together */
1327 IO::enable_connecting ();
1329 /* Now tell all "floating" ports to connect to whatever
1330 they should be connected to.
1333 AudioEngine::instance()->reconnect_ports ();
1334 TransportMasterManager::instance().reconnect_ports ();
1336 /* Anyone who cares about input state, wake up and do something */
1338 IOConnectionsComplete (); /* EMIT SIGNAL */
1340 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
1342 /* now handle the whole enchilada as if it was one
1343 graph reorder event.
1348 /* update the full solo state, which can't be
1349 correctly determined on a per-route basis, but
1350 needs the global overview that only the session
1354 update_route_solo_state ();
1358 Session::track_playlist_changed (boost::weak_ptr<Track> wp)
1360 boost::shared_ptr<Track> track = wp.lock ();
1365 boost::shared_ptr<Playlist> playlist;
1367 if ((playlist = track->playlist()) != 0) {
1368 playlist->RegionAdded.connect_same_thread (*this, boost::bind (&Session::playlist_region_added, this, _1));
1369 playlist->RangesMoved.connect_same_thread (*this, boost::bind (&Session::playlist_ranges_moved, this, _1));
1370 playlist->RegionsExtended.connect_same_thread (*this, boost::bind (&Session::playlist_regions_extended, this, _1));
1375 Session::record_enabling_legal () const
1377 if (Config->get_all_safe()) {
1384 Session::set_track_monitor_input_status (bool yn)
1386 boost::shared_ptr<RouteList> rl = routes.reader ();
1387 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1388 boost::shared_ptr<AudioTrack> tr = boost::dynamic_pointer_cast<AudioTrack> (*i);
1389 if (tr && tr->rec_enable_control()->get_value()) {
1390 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
1391 tr->request_input_monitoring (yn);
1397 Session::auto_punch_start_changed (Location* location)
1399 replace_event (SessionEvent::PunchIn, location->start());
1401 if (get_record_enabled() && config.get_punch_in() && !actively_recording ()) {
1402 /* capture start has been changed, so save new pending state */
1403 save_state ("", true);
1408 Session::auto_punch_end_changed (Location* location)
1410 replace_event (SessionEvent::PunchOut, location->end());
1414 Session::auto_punch_changed (Location* location)
1416 auto_punch_start_changed (location);
1417 auto_punch_end_changed (location);
1420 /** @param loc A loop location.
1421 * @param pos Filled in with the start time of the required fade-out (in session samples).
1422 * @param length Filled in with the length of the required fade-out.
1425 Session::auto_loop_declick_range (Location* loc, samplepos_t & pos, samplepos_t & length)
1427 pos = max (loc->start(), loc->end() - 64);
1428 length = loc->end() - pos;
1432 Session::auto_loop_changed (Location* location)
1434 replace_event (SessionEvent::AutoLoop, location->end(), location->start());
1437 auto_loop_declick_range (location, dcp, dcl);
1439 bool rolling = transport_rolling ();
1441 if (rolling && play_loop) {
1443 if (_transport_sample < location->start() || _transport_sample > location->end()) {
1444 // relocate to beginning of loop
1445 clear_events (SessionEvent::LocateRoll);
1446 request_locate (location->start(), true);
1450 clear_events (SessionEvent::AutoLoop);
1453 /* possibly move playhead if not rolling; if we are rolling we'll move
1454 to the loop start on stop if that is appropriate.
1459 if (!rolling && select_playhead_priority_target (pos)) {
1460 if (pos == location->start()) {
1461 request_locate (pos);
1465 last_loopend = location->end();
1470 Session::set_auto_punch_location (Location* location)
1474 if ((existing = _locations->auto_punch_location()) != 0 && existing != location) {
1475 punch_connections.drop_connections();
1476 existing->set_auto_punch (false, this);
1477 clear_events (SessionEvent::PunchIn);
1478 clear_events (SessionEvent::PunchOut);
1479 auto_punch_location_changed (0);
1484 if (location == 0) {
1488 if (location->end() <= location->start()) {
1489 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1493 punch_connections.drop_connections ();
1495 location->StartChanged.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_start_changed, this, location));
1496 location->EndChanged.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_end_changed, this, location));
1497 location->Changed.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_changed, this, location));
1499 location->set_auto_punch (true, this);
1501 auto_punch_changed (location);
1503 auto_punch_location_changed (location);
1507 Session::set_session_extents (samplepos_t start, samplepos_t end)
1510 if ((existing = _locations->session_range_location()) == 0) {
1511 //if there is no existing session, we need to make a new session location (should never happen)
1512 existing = new Location (*this, 0, 0, _("session"), Location::IsSessionRange, 0);
1516 error << _("Session: you can't use that location for session start/end)") << endmsg;
1520 existing->set( start, end );
1526 Session::set_auto_loop_location (Location* location)
1530 if ((existing = _locations->auto_loop_location()) != 0 && existing != location) {
1531 loop_connections.drop_connections ();
1532 existing->set_auto_loop (false, this);
1533 remove_event (existing->end(), SessionEvent::AutoLoop);
1536 auto_loop_declick_range (existing, dcp, dcl);
1537 auto_loop_location_changed (0);
1542 if (location == 0) {
1546 if (location->end() <= location->start()) {
1547 error << _("You cannot use this location for auto-loop because it has zero or negative length") << endmsg;
1551 last_loopend = location->end();
1553 loop_connections.drop_connections ();
1555 location->StartChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1556 location->EndChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1557 location->Changed.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1558 location->FlagsChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1560 location->set_auto_loop (true, this);
1562 if (Config->get_loop_is_mode() && play_loop && Config->get_seamless_loop()) {
1563 // set all tracks to use internal looping
1564 boost::shared_ptr<RouteList> rl = routes.reader ();
1565 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1566 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1567 if (tr && !tr->is_private_route()) {
1568 tr->set_loop (location);
1573 /* take care of our stuff first */
1575 auto_loop_changed (location);
1577 /* now tell everyone else */
1579 auto_loop_location_changed (location);
1583 Session::update_marks (Location*)
1589 Session::update_skips (Location* loc, bool consolidate)
1591 if (_ignore_skips_updates) {
1595 Locations::LocationList skips;
1598 PBD::Unwinder<bool> uw (_ignore_skips_updates, true);
1599 consolidate_skips (loc);
1602 sync_locations_to_skips ();
1608 Session::consolidate_skips (Location* loc)
1610 Locations::LocationList all_locations = _locations->list ();
1612 for (Locations::LocationList::iterator l = all_locations.begin(); l != all_locations.end(); ) {
1614 if (!(*l)->is_skip ()) {
1619 /* don't test against self */
1626 switch (Evoral::coverage ((*l)->start(), (*l)->end(), loc->start(), loc->end())) {
1627 case Evoral::OverlapInternal:
1628 case Evoral::OverlapExternal:
1629 case Evoral::OverlapStart:
1630 case Evoral::OverlapEnd:
1631 /* adjust new location to cover existing one */
1632 loc->set_start (min (loc->start(), (*l)->start()));
1633 loc->set_end (max (loc->end(), (*l)->end()));
1634 /* we don't need this one any more */
1635 _locations->remove (*l);
1636 /* the location has been deleted, so remove reference to it in our local list */
1637 l = all_locations.erase (l);
1640 case Evoral::OverlapNone:
1648 Session::sync_locations_to_skips ()
1650 /* This happens asynchronously (in the audioengine thread). After the clear is done, we will call
1651 * Session::_sync_locations_to_skips() from the audioengine thread.
1653 clear_events (SessionEvent::Skip, boost::bind (&Session::_sync_locations_to_skips, this));
1657 Session::_sync_locations_to_skips ()
1659 /* called as a callback after existing Skip events have been cleared from a realtime audioengine thread */
1661 Locations::LocationList const & locs (_locations->list());
1663 for (Locations::LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1665 Location* location = *i;
1667 if (location->is_skip() && location->is_skipping()) {
1668 SessionEvent* ev = new SessionEvent (SessionEvent::Skip, SessionEvent::Add, location->start(), location->end(), 1.0);
1676 Session::location_added (Location *location)
1678 if (location->is_auto_punch()) {
1679 set_auto_punch_location (location);
1682 if (location->is_auto_loop()) {
1683 set_auto_loop_location (location);
1686 if (location->is_session_range()) {
1687 /* no need for any signal handling or event setting with the session range,
1688 because we keep a direct reference to it and use its start/end directly.
1690 _session_range_location = location;
1693 if (location->is_mark()) {
1694 /* listen for per-location signals that require us to do any * global updates for marks */
1696 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1697 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1698 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1699 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1700 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1703 if (location->is_range_marker()) {
1704 /* listen for per-location signals that require us to do any * global updates for marks */
1706 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1707 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1708 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1709 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1710 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1713 if (location->is_skip()) {
1714 /* listen for per-location signals that require us to update skip-locate events */
1716 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1717 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1718 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1719 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, false));
1720 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1722 update_skips (location, true);
1729 Session::location_removed (Location *location)
1731 if (location->is_auto_loop()) {
1732 set_auto_loop_location (0);
1734 set_track_loop (false);
1739 if (location->is_auto_punch()) {
1740 set_auto_punch_location (0);
1743 if (location->is_session_range()) {
1744 /* this is never supposed to happen */
1745 error << _("programming error: session range removed!") << endl;
1748 if (location->is_skip()) {
1750 update_skips (location, false);
1757 Session::locations_changed ()
1759 _locations->apply (*this, &Session::_locations_changed);
1763 Session::_locations_changed (const Locations::LocationList& locations)
1765 /* There was some mass-change in the Locations object.
1767 * We might be re-adding a location here but it doesn't actually matter
1768 * for all the locations that the Session takes an interest in.
1772 PBD::Unwinder<bool> protect_ignore_skip_updates (_ignore_skips_updates, true);
1773 for (Locations::LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1774 location_added (*i);
1778 update_skips (NULL, false);
1782 Session::enable_record ()
1784 if (_transport_speed != 0.0 && _transport_speed != 1.0) {
1785 /* no recording at anything except normal speed */
1790 RecordState rs = (RecordState) g_atomic_int_get (&_record_status);
1792 if (rs == Recording) {
1796 if (g_atomic_int_compare_and_exchange (&_record_status, rs, Recording)) {
1798 _last_record_location = _transport_sample;
1799 send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordStrobe));
1801 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1802 set_track_monitor_input_status (true);
1805 RecordStateChanged ();
1812 Session::set_all_tracks_record_enabled (bool enable )
1814 boost::shared_ptr<RouteList> rl = routes.reader();
1815 set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), enable, Controllable::NoGroup);
1819 Session::disable_record (bool rt_context, bool force)
1823 if ((rs = (RecordState) g_atomic_int_get (&_record_status)) != Disabled) {
1825 if (!Config->get_latched_record_enable () || force) {
1826 g_atomic_int_set (&_record_status, Disabled);
1827 send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordExit));
1829 if (rs == Recording) {
1830 g_atomic_int_set (&_record_status, Enabled);
1834 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1835 set_track_monitor_input_status (false);
1838 RecordStateChanged (); /* emit signal */
1843 Session::step_back_from_record ()
1845 if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
1847 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1848 set_track_monitor_input_status (false);
1851 RecordStateChanged (); /* emit signal */
1856 Session::maybe_enable_record (bool rt_context)
1858 if (_step_editors > 0) {
1862 g_atomic_int_set (&_record_status, Enabled);
1864 /* This function is currently called from somewhere other than an RT thread.
1865 * (except maybe lua scripts, which can use rt_context = true)
1866 * This save_state() call therefore doesn't impact anything. Doing it here
1867 * means that we save pending state of which sources the next record will use,
1868 * which gives us some chance of recovering from a crash during the record.
1872 save_state ("", true);
1875 if (_transport_speed) {
1876 if (!config.get_punch_in()) {
1880 send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordPause));
1881 RecordStateChanged (); /* EMIT SIGNAL */
1888 Session::audible_sample (bool* latent_locate) const
1890 if (latent_locate) {
1891 *latent_locate = false;
1896 if (synced_to_engine()) {
1897 /* Note: this is basically just sync-to-JACK */
1898 ret = _engine.transport_sample();
1900 ret = _transport_sample;
1905 if (!transport_rolling()) {
1909 #if 0 // TODO looping
1910 if (_transport_speed > 0.0f) {
1911 if (play_loop && have_looped) {
1912 /* the play-position wrapped at the loop-point
1913 * ardour is already playing the beginning of the loop,
1914 * but due to playback latency, the "audible frame"
1915 * is still at the end of the loop.
1917 Location *location = _locations->auto_loop_location();
1918 sampleoffset_t lo = location->start() - ret;
1920 ret = location->end () - lo;
1921 if (latent_locate) {
1922 *latent_locate = true;
1926 } else if (_transport_speed < 0.0f) {
1927 /* XXX wot? no backward looping? */
1931 return std::max ((samplepos_t)0, ret);
1935 Session::preroll_samples (samplepos_t pos) const
1937 const float pr = Config->get_preroll_seconds();
1938 if (pos >= 0 && pr < 0) {
1939 const Tempo& tempo = _tempo_map->tempo_at_sample (pos);
1940 const Meter& meter = _tempo_map->meter_at_sample (pos);
1941 return meter.samples_per_bar (tempo, sample_rate()) * -pr;
1946 return pr * sample_rate();
1950 Session::set_sample_rate (samplecnt_t frames_per_second)
1952 /** \fn void Session::set_sample_size(samplecnt_t)
1953 the AudioEngine object that calls this guarantees
1954 that it will not be called while we are also in
1955 ::process(). Its fine to do things that block
1959 if (_base_sample_rate == 0) {
1960 _base_sample_rate = frames_per_second;
1962 else if (_base_sample_rate != frames_per_second && frames_per_second != _nominal_sample_rate) {
1963 NotifyAboutSampleRateMismatch (_base_sample_rate, frames_per_second);
1965 _nominal_sample_rate = frames_per_second;
1970 reset_write_sources (false);
1972 // XXX we need some equivalent to this, somehow
1973 // SndFileSource::setup_standard_crossfades (frames_per_second);
1977 /* XXX need to reset/reinstantiate all LADSPA plugins */
1981 Session::set_block_size (pframes_t nframes)
1983 /* the AudioEngine guarantees
1984 that it will not be called while we are also in
1985 ::process(). It is therefore fine to do things that block
1990 current_block_size = nframes;
1994 boost::shared_ptr<RouteList> r = routes.reader ();
1996 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1997 (*i)->set_block_size (nframes);
2000 boost::shared_ptr<RouteList> rl = routes.reader ();
2001 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2002 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2004 tr->set_block_size (nframes);
2008 set_worst_io_latencies ();
2014 trace_terminal (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> rbase, bool sends_only)
2016 boost::shared_ptr<Route> r2;
2018 if (r1->feeds (rbase) && rbase->feeds (r1)) {
2019 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
2023 /* make a copy of the existing list of routes that feed r1 */
2025 Route::FedBy existing (r1->fed_by());
2027 /* for each route that feeds r1, recurse, marking it as feeding
2031 for (Route::FedBy::iterator i = existing.begin(); i != existing.end(); ++i) {
2032 if (!(r2 = i->r.lock ())) {
2033 /* (*i) went away, ignore it */
2037 /* r2 is a route that feeds r1 which somehow feeds base. mark
2038 base as being fed by r2
2041 rbase->add_fed_by (r2, i->sends_only || sends_only);
2045 /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
2049 if (r1->feeds (r2) && r2->feeds (r1)) {
2053 /* now recurse, so that we can mark base as being fed by
2054 all routes that feed r2
2057 trace_terminal (r2, rbase, i->sends_only || sends_only);
2064 Session::resort_routes ()
2066 /* don't do anything here with signals emitted
2067 by Routes during initial setup or while we
2068 are being destroyed.
2071 if (inital_connect_or_deletion_in_progress ()) {
2075 if (_route_deletion_in_progress) {
2080 RCUWriter<RouteList> writer (routes);
2081 boost::shared_ptr<RouteList> r = writer.get_copy ();
2082 resort_routes_using (r);
2083 /* writer goes out of scope and forces update */
2087 if (DEBUG_ENABLED(DEBUG::Graph)) {
2088 boost::shared_ptr<RouteList> rl = routes.reader ();
2089 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2090 DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name()));
2092 const Route::FedBy& fb ((*i)->fed_by());
2094 for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) {
2095 boost::shared_ptr<Route> sf = f->r.lock();
2097 DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only));
2106 /** This is called whenever we need to rebuild the graph of how we will process
2108 * @param r List of routes, in any order.
2112 Session::resort_routes_using (boost::shared_ptr<RouteList> r)
2114 /* We are going to build a directed graph of our routes;
2115 this is where the edges of that graph are put.
2120 /* Go through all routes doing two things:
2122 * 1. Collect the edges of the route graph. Each of these edges
2123 * is a pair of routes, one of which directly feeds the other
2124 * either by a JACK connection or by an internal send.
2126 * 2. Begin the process of making routes aware of which other
2127 * routes directly or indirectly feed them. This information
2128 * is used by the solo code.
2131 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2133 /* Clear out the route's list of direct or indirect feeds */
2134 (*i)->clear_fed_by ();
2136 for (RouteList::iterator j = r->begin(); j != r->end(); ++j) {
2138 bool via_sends_only;
2140 /* See if this *j feeds *i according to the current state of the JACK
2141 connections and internal sends.
2143 if ((*j)->direct_feeds_according_to_reality (*i, &via_sends_only)) {
2144 /* add the edge to the graph (part #1) */
2145 edges.add (*j, *i, via_sends_only);
2146 /* tell the route (for part #2) */
2147 (*i)->add_fed_by (*j, via_sends_only);
2152 /* Attempt a topological sort of the route graph */
2153 boost::shared_ptr<RouteList> sorted_routes = topological_sort (r, edges);
2155 if (sorted_routes) {
2156 /* We got a satisfactory topological sort, so there is no feedback;
2159 Note: the process graph rechain does not require a
2160 topologically-sorted list, but hey ho.
2162 if (_process_graph) {
2163 _process_graph->rechain (sorted_routes, edges);
2166 _current_route_graph = edges;
2168 /* Complete the building of the routes' lists of what directly
2169 or indirectly feeds them.
2171 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2172 trace_terminal (*i, *i, false);
2175 *r = *sorted_routes;
2178 DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
2179 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2180 DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 presentation order %2\n", (*i)->name(), (*i)->presentation_info().order()));
2184 SuccessfulGraphSort (); /* EMIT SIGNAL */
2187 /* The topological sort failed, so we have a problem. Tell everyone
2188 and stick to the old graph; this will continue to be processed, so
2189 until the feedback is fixed, what is played back will not quite
2190 reflect what is actually connected. Note also that we do not
2191 do trace_terminal here, as it would fail due to an endless recursion,
2192 so the solo code will think that everything is still connected
2196 FeedbackDetected (); /* EMIT SIGNAL */
2201 /** Find a route name starting with \a base, maybe followed by the
2202 * lowest \a id. \a id will always be added if \a definitely_add_number
2203 * is true on entry; otherwise it will only be added if required
2204 * to make the name unique.
2206 * Names are constructed like e.g. "Audio 3" for base="Audio" and id=3.
2207 * The available route name with the lowest ID will be used, and \a id
2208 * will be set to the ID.
2210 * \return false if a route name could not be found, and \a track_name
2211 * and \a id do not reflect a free route name.
2214 Session::find_route_name (string const & base, uint32_t& id, string& name, bool definitely_add_number)
2216 /* the base may conflict with ports that do not belong to existing
2217 routes, but hidden objects like the click track. So check port names
2218 before anything else.
2221 for (map<string,bool>::const_iterator reserved = reserved_io_names.begin(); reserved != reserved_io_names.end(); ++reserved) {
2222 if (base == reserved->first) {
2223 /* Check if this reserved name already exists, and if
2224 so, disallow it without a numeric suffix.
2226 if (!reserved->second || route_by_name (reserved->first)) {
2227 definitely_add_number = true;
2236 /* if we have "base 1" already, it doesn't make sense to add "base"
2237 * if "base 1" has been deleted, adding "base" is no worse than "base 1"
2239 if (!definitely_add_number && route_by_name (base) == 0 && (route_by_name (string_compose("%1 1", base)) == 0)) {
2240 /* just use the base */
2246 name = string_compose ("%1 %2", base, id);
2248 if (route_by_name (name) == 0) {
2254 } while (id < (UINT_MAX-1));
2259 /** Count the total ins and outs of all non-hidden tracks in the session and return them in in and out */
2261 Session::count_existing_track_channels (ChanCount& in, ChanCount& out)
2263 in = ChanCount::ZERO;
2264 out = ChanCount::ZERO;
2266 boost::shared_ptr<RouteList> r = routes.reader ();
2268 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2269 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2273 assert (!tr->is_auditioner()); // XXX remove me
2274 in += tr->n_inputs();
2275 out += tr->n_outputs();
2280 Session::default_track_name_pattern (DataType t)
2283 case DataType::AUDIO:
2287 case DataType::MIDI:
2294 /** Caller must not hold process lock
2295 * @param name_template string to use for the start of the name, or "" to use "MIDI".
2296 * @param instrument plugin info for the instrument to insert pre-fader, if any
2298 list<boost::shared_ptr<MidiTrack> >
2299 Session::new_midi_track (const ChanCount& input, const ChanCount& output, bool strict_io,
2300 boost::shared_ptr<PluginInfo> instrument, Plugin::PresetRecord* pset,
2301 RouteGroup* route_group, uint32_t how_many,
2302 string name_template, PresentationInfo::order_t order,
2306 uint32_t track_id = 0;
2308 RouteList new_routes;
2309 list<boost::shared_ptr<MidiTrack> > ret;
2311 const string name_pattern = default_track_name_pattern (DataType::MIDI);
2312 bool const use_number = (how_many != 1) || name_template.empty () || (name_template == name_pattern);
2315 if (!find_route_name (name_template.empty() ? _("MIDI") : name_template, ++track_id, track_name, use_number)) {
2316 error << "cannot find name for new midi track" << endmsg;
2320 boost::shared_ptr<MidiTrack> track;
2323 track.reset (new MidiTrack (*this, track_name, mode));
2325 if (track->init ()) {
2330 track->set_strict_io (true);
2333 BOOST_MARK_TRACK (track);
2336 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2337 if (track->input()->ensure_io (input, false, this)) {
2338 error << "cannot configure " << input << " out configuration for new midi track" << endmsg;
2342 if (track->output()->ensure_io (output, false, this)) {
2343 error << "cannot configure " << output << " out configuration for new midi track" << endmsg;
2349 route_group->add (track);
2352 new_routes.push_back (track);
2353 ret.push_back (track);
2356 catch (failed_constructor &err) {
2357 error << _("Session: could not create new midi track.") << endmsg;
2361 catch (AudioEngine::PortRegistrationFailure& pfe) {
2363 error << string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with more ports if you need this many tracks."), PROGRAM_NAME) << endmsg;
2371 if (!new_routes.empty()) {
2372 StateProtector sp (this);
2373 add_routes (new_routes, true, true, false, order);
2376 for (RouteList::iterator r = new_routes.begin(); r != new_routes.end(); ++r) {
2377 PluginPtr plugin = instrument->load (*this);
2379 warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2383 plugin->load_preset (*pset);
2385 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2387 pi->set_strict_io (true);
2390 (*r)->add_processor (pi, PreFader);
2392 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2393 (*r)->move_instrument_down (false);
2403 Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name_template, bool strict_io,
2404 boost::shared_ptr<PluginInfo> instrument, Plugin::PresetRecord* pset,
2405 PresentationInfo::Flag flag, PresentationInfo::order_t order)
2408 uint32_t bus_id = 0;
2412 bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Midi Bus");
2415 if (!find_route_name (name_template.empty () ? _("Midi Bus") : name_template, ++bus_id, bus_name, use_number)) {
2416 error << "cannot find name for new midi bus" << endmsg;
2421 boost::shared_ptr<Route> bus (new Route (*this, bus_name, flag, DataType::AUDIO)); // XXX Editor::add_routes is not ready for ARDOUR::DataType::MIDI
2428 bus->set_strict_io (true);
2431 BOOST_MARK_ROUTE(bus);
2434 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2436 if (bus->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2437 error << _("cannot configure new midi bus input") << endmsg;
2442 if (bus->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2443 error << _("cannot configure new midi bus output") << endmsg;
2449 route_group->add (bus);
2452 bus->add_internal_return ();
2453 ret.push_back (bus);
2456 catch (failed_constructor &err) {
2457 error << _("Session: could not create new audio route.") << endmsg;
2461 catch (AudioEngine::PortRegistrationFailure& pfe) {
2462 error << pfe.what() << endmsg;
2472 StateProtector sp (this);
2473 add_routes (ret, false, false, false, order);
2476 for (RouteList::iterator r = ret.begin(); r != ret.end(); ++r) {
2477 PluginPtr plugin = instrument->load (*this);
2479 warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2483 plugin->load_preset (*pset);
2485 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2487 pi->set_strict_io (true);
2490 (*r)->add_processor (pi, PreFader);
2492 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2493 (*r)->move_instrument_down (false);
2505 Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr<Route> wmt)
2507 boost::shared_ptr<Route> midi_track (wmt.lock());
2513 if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) {
2515 if (change.after.n_audio() <= change.before.n_audio()) {
2519 /* new audio ports: make sure the audio goes somewhere useful,
2520 * unless the user has no-auto-connect selected.
2522 * The existing ChanCounts don't matter for this call as they are only
2523 * to do with matching input and output indices, and we are only changing
2526 auto_connect_route (midi_track, false, ChanCount(), change.before);
2531 Session::ensure_stripable_sort_order ()
2534 get_stripables (sl);
2535 sl.sort (Stripable::Sorter ());
2537 bool change = false;
2538 PresentationInfo::order_t order = 0;
2540 for (StripableList::iterator si = sl.begin(); si != sl.end(); ++si) {
2541 boost::shared_ptr<Stripable> s (*si);
2542 assert (!s->is_auditioner ()); // XXX remove me
2543 if (s->is_monitor ()) {
2546 if (order != s->presentation_info().order()) {
2547 s->set_presentation_order (order);
2556 Session::ensure_route_presentation_info_gap (PresentationInfo::order_t first_new_order, uint32_t how_many)
2558 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("ensure order gap starting at %1 for %2\n", first_new_order, how_many));
2560 if (first_new_order == PresentationInfo::max_order) {
2561 /* adding at end, no worries */
2565 /* create a gap in the presentation info to accomodate @param how_many
2569 get_stripables (sl);
2571 for (StripableList::iterator si = sl.begin(); si != sl.end(); ++si) {
2572 boost::shared_ptr<Stripable> s (*si);
2574 if (s->presentation_info().special (false)) {
2578 if (!s->presentation_info().order_set()) {
2582 if (s->presentation_info().order () >= first_new_order) {
2583 s->set_presentation_order (s->presentation_info().order () + how_many);
2588 /** Caller must not hold process lock
2589 * @param name_template string to use for the start of the name, or "" to use "Audio".
2591 list< boost::shared_ptr<AudioTrack> >
2592 Session::new_audio_track (int input_channels, int output_channels, RouteGroup* route_group,
2593 uint32_t how_many, string name_template, PresentationInfo::order_t order,
2597 uint32_t track_id = 0;
2599 RouteList new_routes;
2600 list<boost::shared_ptr<AudioTrack> > ret;
2602 const string name_pattern = default_track_name_pattern (DataType::AUDIO);
2603 bool const use_number = (how_many != 1) || name_template.empty () || (name_template == name_pattern);
2607 if (!find_route_name (name_template.empty() ? _(name_pattern.c_str()) : name_template, ++track_id, track_name, use_number)) {
2608 error << "cannot find name for new audio track" << endmsg;
2612 boost::shared_ptr<AudioTrack> track;
2615 track.reset (new AudioTrack (*this, track_name, mode));
2617 if (track->init ()) {
2621 if (Profile->get_mixbus ()) {
2622 track->set_strict_io (true);
2625 BOOST_MARK_TRACK (track);
2628 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2630 if (track->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
2631 error << string_compose (
2632 _("cannot configure %1 in/%2 out configuration for new audio track"),
2633 input_channels, output_channels)
2638 if (track->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
2639 error << string_compose (
2640 _("cannot configure %1 in/%2 out configuration for new audio track"),
2641 input_channels, output_channels)
2648 route_group->add (track);
2651 new_routes.push_back (track);
2652 ret.push_back (track);
2655 catch (failed_constructor &err) {
2656 error << _("Session: could not create new audio track.") << endmsg;
2660 catch (AudioEngine::PortRegistrationFailure& pfe) {
2662 error << pfe.what() << endmsg;
2670 if (!new_routes.empty()) {
2671 StateProtector sp (this);
2672 add_routes (new_routes, true, true, false, order);
2678 /** Caller must not hold process lock.
2679 * @param name_template string to use for the start of the name, or "" to use "Bus".
2682 Session::new_audio_route (int input_channels, int output_channels, RouteGroup* route_group, uint32_t how_many, string name_template,
2683 PresentationInfo::Flag flags, PresentationInfo::order_t order)
2686 uint32_t bus_id = 0;
2690 bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Bus");
2693 if (!find_route_name (name_template.empty () ? _("Bus") : name_template, ++bus_id, bus_name, use_number)) {
2694 error << "cannot find name for new audio bus" << endmsg;
2699 boost::shared_ptr<Route> bus (new Route (*this, bus_name, flags, DataType::AUDIO));
2705 if (Profile->get_mixbus ()) {
2706 bus->set_strict_io (true);
2709 BOOST_MARK_ROUTE(bus);
2712 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2714 if (bus->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
2715 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
2716 input_channels, output_channels)
2722 if (bus->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
2723 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
2724 input_channels, output_channels)
2731 route_group->add (bus);
2734 bus->add_internal_return ();
2735 ret.push_back (bus);
2738 catch (failed_constructor &err) {
2739 error << _("Session: could not create new audio route.") << endmsg;
2743 catch (AudioEngine::PortRegistrationFailure& pfe) {
2744 error << pfe.what() << endmsg;
2754 StateProtector sp (this);
2756 if (flags == PresentationInfo::FoldbackBus) {
2757 add_routes (ret, false, false, true, order); // no autoconnect
2759 add_routes (ret, false, true, true, order); // autoconnect // outputs only
2768 Session::new_route_from_template (uint32_t how_many, PresentationInfo::order_t insert_at, const std::string& template_path, const std::string& name_base,
2769 PlaylistDisposition pd)
2773 if (!tree.read (template_path.c_str())) {
2777 return new_route_from_template (how_many, insert_at, *tree.root(), name_base, pd);
2781 Session::new_route_from_template (uint32_t how_many, PresentationInfo::order_t insert_at, XMLNode& node, const std::string& name_base, PlaylistDisposition pd)
2784 uint32_t number = 0;
2785 const uint32_t being_added = how_many;
2786 /* This will prevent the use of any existing XML-provided PBD::ID
2789 Stateful::ForceIDRegeneration force_ids;
2790 IO::disable_connecting ();
2794 /* We're going to modify the node contents a bit so take a
2795 * copy. The node may be re-used when duplicating more than once.
2798 XMLNode node_copy (node);
2799 std::vector<boost::shared_ptr<Playlist> > shared_playlists;
2804 if (!name_base.empty()) {
2806 /* if we're adding more than one routes, force
2807 * all the names of the new routes to be
2808 * numbered, via the final parameter.
2811 if (!find_route_name (name_base.c_str(), ++number, name, (being_added > 1))) {
2812 fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
2813 abort(); /*NOTREACHED*/
2818 string const route_name = node_copy.property(X_("name"))->value ();
2820 /* generate a new name by adding a number to the end of the template name */
2821 if (!find_route_name (route_name.c_str(), ++number, name, true)) {
2822 fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
2823 abort(); /*NOTREACHED*/
2827 /* figure out the appropriate playlist setup. The track
2828 * (if the Route we're creating is a track) will find
2832 if (pd == CopyPlaylist) {
2834 PBD::ID playlist_id;
2836 if (node_copy.get_property (X_("audio-playlist"), playlist_id)) {
2837 boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2838 playlist = PlaylistFactory::create (playlist, string_compose ("%1.1", name));
2839 playlist->reset_shares ();
2840 node_copy.set_property (X_("audio-playlist"), playlist->id());
2843 if (node_copy.get_property (X_("midi-playlist"), playlist_id)) {
2844 boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2845 playlist = PlaylistFactory::create (playlist, string_compose ("%1.1", name));
2846 playlist->reset_shares ();
2847 node_copy.set_property (X_("midi-playlist"), playlist->id());
2850 } else if (pd == SharePlaylist) {
2851 PBD::ID playlist_id;
2853 if (node_copy.get_property (X_("audio-playlist"), playlist_id)) {
2854 boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2855 shared_playlists.push_back (playlist);
2858 if (node_copy.get_property (X_("midi-playlist"), playlist_id)) {
2859 boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2860 shared_playlists.push_back (playlist);
2863 } else { /* NewPlaylist */
2867 if (node_copy.get_property (X_("audio-playlist"), pid)) {
2868 boost::shared_ptr<Playlist> playlist = PlaylistFactory::create (DataType::AUDIO, *this, name, false);
2869 node_copy.set_property (X_("audio-playlist"), playlist->id());
2872 if (node_copy.get_property (X_("midi-playlist"), pid)) {
2873 boost::shared_ptr<Playlist> playlist = PlaylistFactory::create (DataType::MIDI, *this, name, false);
2874 node_copy.set_property (X_("midi-playlist"), playlist->id());
2878 /* Fix up new name in the XML node */
2880 Route::set_name_in_state (node_copy, name);
2882 /* trim bitslots from listen sends so that new ones are used */
2883 XMLNodeList children = node_copy.children ();
2884 for (XMLNodeList::iterator i = children.begin(); i != children.end(); ++i) {
2885 if ((*i)->name() == X_("Processor")) {
2886 /* ForceIDRegeneration does not catch the following */
2887 XMLProperty const * role = (*i)->property (X_("role"));
2888 XMLProperty const * type = (*i)->property (X_("type"));
2889 if (role && role->value() == X_("Aux")) {
2890 /* check if the target bus exists.
2891 * we should not save aux-sends in templates.
2893 XMLProperty const * target = (*i)->property (X_("target"));
2895 (*i)->set_property ("type", "dangling-aux-send");
2898 boost::shared_ptr<Route> r = route_by_id (target->value());
2899 if (!r || boost::dynamic_pointer_cast<Track>(r)) {
2900 (*i)->set_property ("type", "dangling-aux-send");
2904 if (role && role->value() == X_("Listen")) {
2905 (*i)->remove_property (X_("bitslot"));
2907 else if (role && (role->value() == X_("Send") || role->value() == X_("Aux"))) {
2908 Delivery::Role xrole;
2909 uint32_t bitslot = 0;
2910 xrole = Delivery::Role (string_2_enum (role->value(), xrole));
2911 std::string name = Send::name_and_id_new_send(*this, xrole, bitslot, false);
2912 (*i)->remove_property (X_("bitslot"));
2913 (*i)->remove_property (X_("name"));
2914 (*i)->set_property ("bitslot", bitslot);
2915 (*i)->set_property ("name", name);
2916 XMLNodeList io_kids = (*i)->children ();
2917 for (XMLNodeList::iterator j = io_kids.begin(); j != io_kids.end(); ++j) {
2918 if ((*j)->name() != X_("IO")) {
2921 (*j)->remove_property (X_("name"));
2922 (*j)->set_property ("name", name);
2925 else if (type && type->value() == X_("intreturn")) {
2926 (*i)->remove_property (X_("bitslot"));
2927 (*i)->set_property ("ignore-bitslot", "1");
2929 else if (type && type->value() == X_("return")) {
2930 // Return::set_state() generates a new one
2931 (*i)->remove_property (X_("bitslot"));
2933 else if (type && type->value() == X_("port")) {
2934 IOProcessor::prepare_for_reset (**i, name);
2939 /* new routes start off unsoloed to avoid issues related to
2940 upstream / downstream buses.
2942 node_copy.remove_node_and_delete (X_("Controllable"), X_("name"), X_("solo"));
2944 boost::shared_ptr<Route> route (XMLRouteFactory (node_copy, 3000));
2947 error << _("Session: cannot create track/bus from template description") << endmsg;
2951 /* Fix up sharing of playlists with the new Route/Track */
2953 for (vector<boost::shared_ptr<Playlist> >::iterator sp = shared_playlists.begin(); sp != shared_playlists.end(); ++sp) {
2954 (*sp)->share_with (route->id());
2957 if (boost::dynamic_pointer_cast<Track>(route)) {
2958 /* force input/output change signals so that the new diskstream
2959 picks up the configuration of the route. During session
2960 loading this normally happens in a different way.
2963 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2965 IOChange change (IOChange::Type (IOChange::ConfigurationChanged | IOChange::ConnectionsChanged));
2966 change.after = route->input()->n_ports();
2967 route->input()->changed (change, this);
2968 change.after = route->output()->n_ports();
2969 route->output()->changed (change, this);
2972 ret.push_back (route);
2975 catch (failed_constructor &err) {
2976 error << _("Session: could not create new route from template") << endmsg;
2980 catch (AudioEngine::PortRegistrationFailure& pfe) {
2981 error << pfe.what() << endmsg;
2986 IO::enable_connecting ();
2995 StateProtector sp (this);
2997 add_routes (ret, true, true, false, insert_at);
3000 IO::enable_connecting ();
3006 Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, bool save, PresentationInfo::order_t order)
3009 PBD::Unwinder<bool> aip (_adding_routes_in_progress, true);
3010 add_routes_inner (new_routes, input_auto_connect, output_auto_connect, order);
3013 error << _("Adding new tracks/busses failed") << endmsg;
3021 save_state (_current_snapshot_name);
3024 update_route_record_state ();
3026 RouteAdded (new_routes); /* EMIT SIGNAL */
3030 Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, PresentationInfo::order_t order)
3032 ChanCount existing_inputs;
3033 ChanCount existing_outputs;
3037 count_existing_track_channels (existing_inputs, existing_outputs);
3040 RCUWriter<RouteList> writer (routes);
3041 boost::shared_ptr<RouteList> r = writer.get_copy ();
3042 n_routes = r->size();
3043 r->insert (r->end(), new_routes.begin(), new_routes.end());
3045 /* if there is no control out and we're not in the middle of loading,
3046 * resort the graph here. if there is a control out, we will resort
3047 * toward the end of this method. if we are in the middle of loading,
3048 * we will resort when done.
3051 if (!_monitor_out && IO::connecting_legal) {
3052 resort_routes_using (r);
3056 /* monitor is not part of the order */
3058 assert (n_routes > 0);
3063 PresentationInfo::ChangeSuspender cs;
3064 ensure_route_presentation_info_gap (order, new_routes.size());
3066 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x, ++added) {
3068 boost::weak_ptr<Route> wpr (*x);
3069 boost::shared_ptr<Route> r (*x);
3071 r->solo_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_solo_changed, this, _1, _2,wpr));
3072 r->solo_isolate_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_solo_isolated_changed, this, wpr));
3073 r->mute_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_mute_changed, this));
3075 r->output()->changed.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies_x, this, _1, _2));
3076 r->processors_changed.connect_same_thread (*this, boost::bind (&Session::route_processors_changed, this, _1));
3077 r->processor_latency_changed.connect_same_thread (*this, boost::bind (&Session::queue_latency_recompute, this));
3079 if (r->is_master()) {
3083 if (r->is_monitor()) {
3087 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (r);
3089 tr->PlaylistChanged.connect_same_thread (*this, boost::bind (&Session::track_playlist_changed, this, boost::weak_ptr<Track> (tr)));
3090 track_playlist_changed (boost::weak_ptr<Track> (tr));
3091 tr->rec_enable_control()->Changed.connect_same_thread (*this, boost::bind (&Session::update_route_record_state, this));
3093 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (tr);
3095 mt->StepEditStatusChange.connect_same_thread (*this, boost::bind (&Session::step_edit_status_change, this, _1));
3096 mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr<Route>(mt)));
3097 mt->presentation_info().PropertyChanged.connect_same_thread (*this, boost::bind (&Session::midi_track_presentation_info_changed, this, _1, boost::weak_ptr<MidiTrack>(mt)));
3101 if (!r->presentation_info().special (false)) {
3103 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("checking PI state for %1\n", r->name()));
3105 /* presentation info order may already have been set from XML */
3107 if (!r->presentation_info().order_set()) {
3108 if (order == PresentationInfo::max_order) {
3109 /* just add to the end */
3110 r->set_presentation_order (n_routes + added);
3111 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order not set, set to NR %1 + %2 = %3\n", n_routes, added, n_routes + added));
3113 r->set_presentation_order (order + added);
3114 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order not set, set to %1 + %2 = %3\n", order, added, order + added));
3117 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order already set to %1\n", r->presentation_info().order()));
3121 #if !defined(__APPLE__) && !defined(__FreeBSD__)
3122 /* clang complains: 'operator<<' should be declared prior to the call site or in an associated namespace of one of its
3123 * arguments std::ostream& operator<<(std::ostream& o, ARDOUR::PresentationInfo const& rid)"
3125 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("added route %1, group order %2 type %3 (summary: %4)\n",
3127 r->presentation_info().order(),
3128 enum_2_string (r->presentation_info().flags()),
3129 r->presentation_info()));
3133 if (input_auto_connect || output_auto_connect) {
3134 auto_connect_route (r, input_auto_connect, ChanCount (), ChanCount (), existing_inputs, existing_outputs);
3135 existing_inputs += r->n_inputs();
3136 existing_outputs += r->n_outputs();
3141 ensure_stripable_sort_order ();
3144 if (_monitor_out && IO::connecting_legal) {
3145 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
3147 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
3148 if ((*x)->is_monitor()) {
3150 } else if ((*x)->is_master()) {
3153 (*x)->enable_monitor_send ();
3158 reassign_track_numbers ();
3162 Session::globally_set_send_gains_to_zero (boost::shared_ptr<Route> dest)
3164 boost::shared_ptr<RouteList> r = routes.reader ();
3165 boost::shared_ptr<Send> s;
3167 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3168 if ((s = (*i)->internal_send_for (dest)) != 0) {
3169 s->amp()->gain_control()->set_value (GAIN_COEFF_ZERO, Controllable::NoGroup);
3175 Session::globally_set_send_gains_to_unity (boost::shared_ptr<Route> dest)
3177 boost::shared_ptr<RouteList> r = routes.reader ();
3178 boost::shared_ptr<Send> s;
3180 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3181 if ((s = (*i)->internal_send_for (dest)) != 0) {
3182 s->amp()->gain_control()->set_value (GAIN_COEFF_UNITY, Controllable::NoGroup);
3188 Session::globally_set_send_gains_from_track(boost::shared_ptr<Route> dest)
3190 boost::shared_ptr<RouteList> r = routes.reader ();
3191 boost::shared_ptr<Send> s;
3193 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3194 if ((s = (*i)->internal_send_for (dest)) != 0) {
3195 s->amp()->gain_control()->set_value ((*i)->gain_control()->get_value(), Controllable::NoGroup);
3200 /** @param include_buses true to add sends to buses and tracks, false for just tracks */
3202 Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p, bool include_buses)
3204 boost::shared_ptr<RouteList> r = routes.reader ();
3205 boost::shared_ptr<RouteList> t (new RouteList);
3207 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3208 /* no MIDI sends because there are no MIDI busses yet */
3209 if (include_buses || boost::dynamic_pointer_cast<AudioTrack>(*i)) {
3214 add_internal_sends (dest, p, t);
3218 Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::shared_ptr<RouteList> senders)
3220 for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
3221 add_internal_send (dest, (*i)->before_processor_for_placement (p), *i);
3226 Session::add_internal_send (boost::shared_ptr<Route> dest, int index, boost::shared_ptr<Route> sender)
3228 add_internal_send (dest, sender->before_processor_for_index (index), sender);
3232 Session::add_internal_send (boost::shared_ptr<Route> dest, boost::shared_ptr<Processor> before, boost::shared_ptr<Route> sender)
3234 if (sender->is_monitor() || sender->is_master() || sender == dest || dest->is_monitor() || dest->is_master()) {
3238 if (!dest->internal_return()) {
3239 dest->add_internal_return ();
3242 sender->add_aux_send (dest, before);
3248 Session::remove_routes (boost::shared_ptr<RouteList> routes_to_remove)
3250 bool mute_changed = false;
3251 bool send_selected = false;
3253 { // RCU Writer scope
3254 PBD::Unwinder<bool> uw_flag (_route_deletion_in_progress, true);
3255 RCUWriter<RouteList> writer (routes);
3256 boost::shared_ptr<RouteList> rs = writer.get_copy ();
3258 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3260 if (_selection->selected (*iter)) {
3261 send_selected = true;
3264 if (*iter == _master_out) {
3268 /* speed up session deletion, don't do the solo dance */
3269 if (!deletion_in_progress ()) {
3270 (*iter)->solo_control()->set_value (0.0, Controllable::NoGroup);
3273 if ((*iter)->mute_control()->muted ()) {
3274 mute_changed = true;
3279 /* deleting the master out seems like a dumb
3280 idea, but its more of a UI policy issue
3284 if (*iter == _master_out) {
3285 _master_out = boost::shared_ptr<Route> ();
3288 if (*iter == _monitor_out) {
3289 _monitor_out.reset ();
3292 // We need to disconnect the route's inputs and outputs
3294 (*iter)->input()->disconnect (0);
3295 (*iter)->output()->disconnect (0);
3297 /* if the route had internal sends sending to it, remove them */
3299 if (!deletion_in_progress () && (*iter)->internal_return()) {
3301 boost::shared_ptr<RouteList> r = routes.reader ();
3302 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3303 boost::shared_ptr<Send> s = (*i)->internal_send_for (*iter);
3305 (*i)->remove_processor (s);
3310 /* if the monitoring section had a pointer to this route, remove it */
3311 if (_monitor_out && !(*iter)->is_master() && !(*iter)->is_monitor()) {
3312 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
3313 ProcessorChangeBlocker pcb (this, false);
3314 (*iter)->remove_aux_or_listen (_monitor_out);
3317 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*iter);
3318 if (mt && mt->step_editing()) {
3319 if (_step_editors > 0) {
3325 /* writer goes out of scope, forces route list update */
3327 } // end of RCU Writer scope
3330 MuteChanged (); /* EMIT SIGNAL */
3333 update_route_solo_state ();
3334 update_latency_compensation ();
3337 /* Re-sort routes to remove the graph's current references to the one that is
3338 * going away, then flush old references out of the graph.
3341 routes.flush (); // maybe unsafe, see below.
3344 if (_process_graph && !deletion_in_progress() && _engine.running()) {
3345 _process_graph->clear_other_chain ();
3348 /* get rid of it from the dead wood collection in the route list manager */
3349 /* XXX i think this is unsafe as it currently stands, but i am not sure. (pd, october 2nd, 2006) */
3353 /* remove these routes from the selection if appropriate, and signal
3354 * the change *before* we call DropReferences for them.
3357 if (send_selected && !deletion_in_progress()) {
3358 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3359 _selection->remove_stripable_by_id ((*iter)->id());
3362 pc.add (Properties::selected);
3363 PresentationInfo::Change (pc);
3366 /* try to cause everyone to drop their references
3367 * and unregister ports from the backend
3370 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3371 (*iter)->drop_references ();
3374 if (deletion_in_progress()) {
3379 pc.add (Properties::order);
3380 PresentationInfo::Change (pc);
3382 /* save the new state of the world */
3384 if (save_state (_current_snapshot_name)) {
3385 save_history (_current_snapshot_name);
3388 update_route_record_state ();
3392 Session::remove_route (boost::shared_ptr<Route> route)
3394 boost::shared_ptr<RouteList> rl (new RouteList);
3395 rl->push_back (route);
3400 Session::route_mute_changed ()
3402 MuteChanged (); /* EMIT SIGNAL */
3407 Session::route_listen_changed (Controllable::GroupControlDisposition group_override, boost::weak_ptr<Route> wpr)
3409 boost::shared_ptr<Route> route (wpr.lock());
3415 assert (Config->get_solo_control_is_listen_control());
3417 if (route->solo_control()->soloed_by_self_or_masters()) {
3419 if (Config->get_exclusive_solo()) {
3421 RouteGroup* rg = route->route_group ();
3422 const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3424 boost::shared_ptr<RouteList> r = routes.reader ();
3426 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3427 if ((*i) == route) {
3428 /* already changed */
3432 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3433 /* route does not get solo propagated to it */
3437 if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3438 /* this route is a part of the same solo group as the route
3439 * that was changed. Changing that route did change or will
3440 * change all group members appropriately, so we can ignore it
3445 (*i)->solo_control()->set_value (0.0, Controllable::NoGroup);
3451 } else if (_listen_cnt > 0) {
3458 Session::route_solo_isolated_changed (boost::weak_ptr<Route> wpr)
3460 boost::shared_ptr<Route> route (wpr.lock());
3466 bool send_changed = false;
3468 if (route->solo_isolate_control()->solo_isolated()) {
3469 if (_solo_isolated_cnt == 0) {
3470 send_changed = true;
3472 _solo_isolated_cnt++;
3473 } else if (_solo_isolated_cnt > 0) {
3474 _solo_isolated_cnt--;
3475 if (_solo_isolated_cnt == 0) {
3476 send_changed = true;
3481 IsolatedChanged (); /* EMIT SIGNAL */
3486 Session::route_solo_changed (bool self_solo_changed, Controllable::GroupControlDisposition group_override, boost::weak_ptr<Route> wpr)
3488 DEBUG_TRACE (DEBUG::Solo, string_compose ("route solo change, self = %1, update\n", self_solo_changed));
3490 boost::shared_ptr<Route> route (wpr.lock());
3496 if (Config->get_solo_control_is_listen_control()) {
3497 route_listen_changed (group_override, wpr);
3501 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1: self %2 masters %3 transition %4\n", route->name(), route->self_soloed(), route->solo_control()->get_masters_value(), route->solo_control()->transitioned_into_solo()));
3503 if (route->solo_control()->transitioned_into_solo() == 0) {
3504 /* route solo changed by upstream/downstream or clear all solo state; not interesting
3507 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 not self-soloed nor soloed by master (%2), ignoring\n", route->name(), route->solo_control()->get_masters_value()));
3511 boost::shared_ptr<RouteList> r = routes.reader ();
3512 int32_t delta = route->solo_control()->transitioned_into_solo ();
3514 /* the route may be a member of a group that has shared-solo
3515 * semantics. If so, then all members of that group should follow the
3516 * solo of the changed route. But ... this is optional, controlled by a
3517 * Controllable::GroupControlDisposition.
3519 * The first argument to the signal that this method is connected to is the
3520 * GroupControlDisposition value that was used to change solo.
3522 * If the solo change was done with group semantics (either InverseGroup
3523 * (force the entire group to change even if the group shared solo is
3524 * disabled) or UseGroup (use the group, which may or may not have the
3525 * shared solo property enabled)) then as we propagate the change to
3526 * the entire session we should IGNORE THE GROUP that the changed route
3530 RouteGroup* rg = route->route_group ();
3531 const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3533 DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate to session, group accounted for ? %1\n", group_already_accounted_for));
3535 if (delta == 1 && Config->get_exclusive_solo()) {
3537 /* new solo: disable all other solos, but not the group if its solo-enabled */
3539 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3541 if ((*i) == route) {
3542 /* already changed */
3546 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3547 /* route does not get solo propagated to it */
3551 if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3552 /* this route is a part of the same solo group as the route
3553 * that was changed. Changing that route did change or will
3554 * change all group members appropriately, so we can ignore it
3560 (*i)->solo_control()->set_value (0.0, group_override);
3564 DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate solo change, delta = %1\n", delta));
3566 RouteList uninvolved;
3568 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1\n", route->name()));
3570 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3571 bool via_sends_only;
3572 bool in_signal_flow;
3574 if ((*i) == route) {
3575 /* already changed */
3579 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3580 /* route does not get solo propagated to it */
3581 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 excluded from solo because iso = %2 can_solo = %3\n", (*i)->name(), (*i)->solo_isolate_control()->solo_isolated(),
3586 if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3587 /* this route is a part of the same solo group as the route
3588 * that was changed. Changing that route did change or will
3589 * change all group members appropriately, so we can ignore it
3595 in_signal_flow = false;
3597 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed from %1\n", (*i)->name()));
3599 if ((*i)->feeds (route, &via_sends_only)) {
3600 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tthere is a feed from %1\n", (*i)->name()));
3601 if (!via_sends_only) {
3602 if (!route->soloed_by_others_upstream()) {
3603 (*i)->solo_control()->mod_solo_by_others_downstream (delta);
3605 DEBUG_TRACE (DEBUG::Solo, "\talready soloed by others upstream\n");
3608 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tthere is a send-only feed from %1\n", (*i)->name()));
3610 in_signal_flow = true;
3612 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tno feed from %1\n", (*i)->name()));
3615 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed to %1\n", (*i)->name()));
3617 if (route->feeds (*i, &via_sends_only)) {
3618 /* propagate solo upstream only if routing other than
3619 sends is involved, but do consider the other route
3620 (*i) to be part of the signal flow even if only
3623 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 feeds %2 via sends only %3 sboD %4 sboU %5\n",
3627 route->soloed_by_others_downstream(),
3628 route->soloed_by_others_upstream()));
3629 if (!via_sends_only) {
3630 //NB. Triggers Invert Push, which handles soloed by downstream
3631 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tmod %1 by %2\n", (*i)->name(), delta));
3632 (*i)->solo_control()->mod_solo_by_others_upstream (delta);
3634 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tfeed to %1 ignored, sends-only\n", (*i)->name()));
3636 in_signal_flow = true;
3638 DEBUG_TRACE (DEBUG::Solo, string_compose("\tno feed to %1\n", (*i)->name()) );
3641 if (!in_signal_flow) {
3642 uninvolved.push_back (*i);
3646 DEBUG_TRACE (DEBUG::Solo, "propagation complete\n");
3648 /* now notify that the mute state of the routes not involved in the signal
3649 pathway of the just-solo-changed route may have altered.
3652 for (RouteList::iterator i = uninvolved.begin(); i != uninvolved.end(); ++i) {
3653 DEBUG_TRACE (DEBUG::Solo, string_compose ("mute change for %1, which neither feeds or is fed by %2\n", (*i)->name(), route->name()));
3654 (*i)->act_on_mute ();
3655 /* Session will emit SoloChanged() after all solo changes are
3656 * complete, which should be used by UIs to update mute status
3662 Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
3664 /* now figure out if anything that matters is soloed (or is "listening")*/
3666 bool something_soloed = false;
3667 bool something_listening = false;
3668 uint32_t listeners = 0;
3669 uint32_t isolated = 0;
3672 r = routes.reader();
3675 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3676 if ((*i)->can_solo()) {
3677 if (Config->get_solo_control_is_listen_control()) {
3678 if ((*i)->solo_control()->soloed_by_self_or_masters()) {
3680 something_listening = true;
3683 (*i)->set_listen (false);
3684 if ((*i)->can_solo() && (*i)->solo_control()->soloed_by_self_or_masters()) {
3685 something_soloed = true;
3690 if ((*i)->solo_isolate_control()->solo_isolated()) {
3695 if (something_soloed != _non_soloed_outs_muted) {
3696 _non_soloed_outs_muted = something_soloed;
3697 SoloActive (_non_soloed_outs_muted); /* EMIT SIGNAL */
3700 if (something_listening != _listening) {
3701 _listening = something_listening;
3702 SoloActive (_listening);
3705 _listen_cnt = listeners;
3707 if (isolated != _solo_isolated_cnt) {
3708 _solo_isolated_cnt = isolated;
3709 IsolatedChanged (); /* EMIT SIGNAL */
3712 DEBUG_TRACE (DEBUG::Solo, string_compose ("solo state updated by session, soloed? %1 listeners %2 isolated %3\n",
3713 something_soloed, listeners, isolated));
3716 SoloChanged (); /* EMIT SIGNAL */
3721 Session::muted () const
3723 // TODO consider caching the value on every MuteChanged signal,
3724 // Note that API users may also subscribe to MuteChanged and hence
3725 // this method needs to be called first.
3728 get_stripables (all);
3729 for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
3730 assert (!(*i)->is_auditioner()); // XXX remove me
3731 if ((*i)->is_monitor()) {
3734 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(*i);
3735 if (r && !r->active()) {
3738 boost::shared_ptr<MuteControl> mc = (*i)->mute_control();
3739 if (mc && mc->muted ()) {
3747 std::vector<boost::weak_ptr<AutomationControl> >
3748 Session::cancel_all_mute ()
3751 get_stripables (all);
3752 std::vector<boost::weak_ptr<AutomationControl> > muted;
3753 boost::shared_ptr<ControlList> cl (new ControlList);
3754 for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
3755 assert (!(*i)->is_auditioner());
3756 if ((*i)->is_monitor()) {
3759 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*i);
3760 if (r && !r->active()) {
3763 boost::shared_ptr<AutomationControl> ac = (*i)->mute_control();
3764 if (ac && ac->get_value () > 0) {
3766 muted.push_back (boost::weak_ptr<AutomationControl>(ac));
3769 if (!cl->empty ()) {
3770 set_controls (cl, 0.0, PBD::Controllable::UseGroup);
3776 Session::get_stripables (StripableList& sl, PresentationInfo::Flag fl) const
3778 boost::shared_ptr<RouteList> r = routes.reader ();
3779 for (RouteList::iterator it = r->begin(); it != r->end(); ++it) {
3780 if ((*it)->presentation_info ().flags () & fl) {
3785 if (fl & PresentationInfo::VCA) {
3786 VCAList v = _vca_manager->vcas ();
3787 sl.insert (sl.end(), v.begin(), v.end());
3792 Session::get_stripables () const
3794 PresentationInfo::Flag fl = PresentationInfo::AllStripables;
3796 Session::get_stripables (rv, fl);
3797 rv.sort (Stripable::Sorter ());
3802 Session::get_routelist (bool mixer_order, PresentationInfo::Flag fl) const
3804 boost::shared_ptr<RouteList> r = routes.reader ();
3806 for (RouteList::iterator it = r->begin(); it != r->end(); ++it) {
3807 if ((*it)->presentation_info ().flags () & fl) {
3811 rv.sort (Stripable::Sorter (mixer_order));
3815 boost::shared_ptr<RouteList>
3816 Session::get_routes_with_internal_returns() const
3818 boost::shared_ptr<RouteList> r = routes.reader ();
3819 boost::shared_ptr<RouteList> rl (new RouteList);
3821 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3822 if ((*i)->internal_return ()) {
3830 Session::io_name_is_legal (const std::string& name) const
3832 boost::shared_ptr<RouteList> r = routes.reader ();
3834 for (map<string,bool>::const_iterator reserved = reserved_io_names.begin(); reserved != reserved_io_names.end(); ++reserved) {
3835 if (name == reserved->first) {
3836 if (!route_by_name (reserved->first)) {
3837 /* first instance of a reserved name is allowed for some */
3838 return reserved->second;
3840 /* all other instances of a reserved name are not allowed */
3845 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3846 if ((*i)->name() == name) {
3850 if ((*i)->has_io_processor_named (name)) {
3859 Session::set_exclusive_input_active (boost::shared_ptr<RouteList> rl, bool onoff, bool flip_others)
3862 vector<string> connections;
3864 /* if we are passed only a single route and we're not told to turn
3865 * others off, then just do the simple thing.
3868 if (flip_others == false && rl->size() == 1) {
3869 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (rl->front());
3871 mt->set_input_active (onoff);
3876 for (RouteList::iterator rt = rl->begin(); rt != rl->end(); ++rt) {
3878 PortSet& ps ((*rt)->input()->ports());
3880 for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
3881 p->get_connections (connections);
3884 for (vector<string>::iterator s = connections.begin(); s != connections.end(); ++s) {
3885 routes_using_input_from (*s, rl2);
3888 /* scan all relevant routes to see if others are on or off */
3890 bool others_are_already_on = false;
3892 for (RouteList::iterator r = rl2.begin(); r != rl2.end(); ++r) {
3894 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
3900 if ((*r) != (*rt)) {
3901 if (mt->input_active()) {
3902 others_are_already_on = true;
3905 /* this one needs changing */
3906 mt->set_input_active (onoff);
3912 /* globally reverse other routes */
3914 for (RouteList::iterator r = rl2.begin(); r != rl2.end(); ++r) {
3915 if ((*r) != (*rt)) {
3916 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
3918 mt->set_input_active (!others_are_already_on);
3927 Session::routes_using_input_from (const string& str, RouteList& rl)
3929 boost::shared_ptr<RouteList> r = routes.reader();
3931 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3932 if ((*i)->input()->connected_to (str)) {
3938 boost::shared_ptr<Route>
3939 Session::route_by_name (string name) const
3941 boost::shared_ptr<RouteList> r = routes.reader ();
3943 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3944 if ((*i)->name() == name) {
3949 return boost::shared_ptr<Route> ((Route*) 0);
3952 boost::shared_ptr<Route>
3953 Session::route_by_id (PBD::ID id) const
3955 boost::shared_ptr<RouteList> r = routes.reader ();
3957 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3958 if ((*i)->id() == id) {
3963 return boost::shared_ptr<Route> ((Route*) 0);
3967 boost::shared_ptr<Stripable>
3968 Session::stripable_by_id (PBD::ID id) const
3971 get_stripables (sl);
3973 for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
3974 if ((*s)->id() == id) {
3979 return boost::shared_ptr<Stripable>();
3982 boost::shared_ptr<Processor>
3983 Session::processor_by_id (PBD::ID id) const
3985 boost::shared_ptr<RouteList> r = routes.reader ();
3987 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3988 boost::shared_ptr<Processor> p = (*i)->Route::processor_by_id (id);
3994 return boost::shared_ptr<Processor> ();
3997 boost::shared_ptr<Route>
3998 Session::get_remote_nth_route (PresentationInfo::order_t n) const
4000 return boost::dynamic_pointer_cast<Route> (get_remote_nth_stripable (n, PresentationInfo::Route));
4003 boost::shared_ptr<Stripable>
4004 Session::get_remote_nth_stripable (PresentationInfo::order_t n, PresentationInfo::Flag flags) const
4007 PresentationInfo::order_t match_cnt = 0;
4009 get_stripables (sl);
4010 sl.sort (Stripable::Sorter());
4012 for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
4014 if ((*s)->presentation_info().hidden()) {
4015 /* if the caller didn't explicitly ask for hidden
4016 stripables, ignore hidden ones. This matches
4017 the semantics of the pre-PresentationOrder
4018 "get by RID" logic of Ardour 4.x and earlier.
4020 XXX at some point we should likely reverse
4021 the logic of the flags, because asking for "the
4022 hidden stripables" is not going to be common,
4023 whereas asking for visible ones is normal.
4026 if (! (flags & PresentationInfo::Hidden)) {
4031 if ((*s)->presentation_info().flag_match (flags)) {
4032 if (match_cnt++ == n) {
4038 /* there is no nth stripable that matches the given flags */
4039 return boost::shared_ptr<Stripable>();
4042 boost::shared_ptr<Route>
4043 Session::route_by_selected_count (uint32_t id) const
4045 RouteList r (*(routes.reader ()));
4046 r.sort (Stripable::Sorter());
4048 RouteList::iterator i;
4050 for (i = r.begin(); i != r.end(); ++i) {
4051 if ((*i)->is_selected()) {
4059 return boost::shared_ptr<Route> ();
4063 Session::reassign_track_numbers ()
4067 RouteList r (*(routes.reader ()));
4068 r.sort (Stripable::Sorter());
4070 StateProtector sp (this);
4072 for (RouteList::iterator i = r.begin(); i != r.end(); ++i) {
4073 assert (!(*i)->is_auditioner());
4074 if (boost::dynamic_pointer_cast<Track> (*i)) {
4075 (*i)->set_track_number(++tn);
4077 else if (!(*i)->is_master() && !(*i)->is_monitor()) {
4078 (*i)->set_track_number(--bn);
4081 const uint32_t decimals = ceilf (log10f (tn + 1));
4082 const bool decimals_changed = _track_number_decimals != decimals;
4083 _track_number_decimals = decimals;
4085 if (decimals_changed && config.get_track_name_number ()) {
4086 for (RouteList::iterator i = r.begin(); i != r.end(); ++i) {
4087 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*i);
4089 t->resync_track_name();
4092 // trigger GUI re-layout
4093 config.ParameterChanged("track-name-number");
4097 if (DEBUG_ENABLED(DEBUG::OrderKeys)) {
4098 boost::shared_ptr<RouteList> rl = routes.reader ();
4099 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4100 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("%1 numbered %2\n", (*i)->name(), (*i)->track_number()));
4108 Session::playlist_region_added (boost::weak_ptr<Region> w)
4110 boost::shared_ptr<Region> r = w.lock ();
4115 /* These are the operations that are currently in progress... */
4116 list<GQuark> curr = _current_trans_quarks;
4119 /* ...and these are the operations during which we want to update
4120 the session range location markers.
4123 ops.push_back (Operations::capture);
4124 ops.push_back (Operations::paste);
4125 ops.push_back (Operations::duplicate_region);
4126 ops.push_back (Operations::insert_file);
4127 ops.push_back (Operations::insert_region);
4128 ops.push_back (Operations::drag_region_brush);
4129 ops.push_back (Operations::region_drag);
4130 ops.push_back (Operations::selection_grab);
4131 ops.push_back (Operations::region_fill);
4132 ops.push_back (Operations::fill_selection);
4133 ops.push_back (Operations::create_region);
4134 ops.push_back (Operations::region_copy);
4135 ops.push_back (Operations::fixed_time_region_copy);
4138 /* See if any of the current operations match the ones that we want */
4140 set_intersection (_current_trans_quarks.begin(), _current_trans_quarks.end(), ops.begin(), ops.end(), back_inserter (in));
4142 /* If so, update the session range markers */
4144 maybe_update_session_range (r->position (), r->last_sample ());
4148 /** Update the session range markers if a is before the current start or
4149 * b is after the current end.
4152 Session::maybe_update_session_range (samplepos_t a, samplepos_t b)
4158 samplepos_t session_end_marker_shift_samples = session_end_shift * _nominal_sample_rate;
4160 if (_session_range_location == 0) {
4162 set_session_range_location (a, b + session_end_marker_shift_samples);
4166 if (_session_range_is_free && (a < _session_range_location->start())) {
4167 _session_range_location->set_start (a);
4170 if (_session_range_is_free && (b > _session_range_location->end())) {
4171 _session_range_location->set_end (b);
4177 Session::set_session_range_is_free (bool yn)
4179 _session_range_is_free = yn;
4183 Session::playlist_ranges_moved (list<Evoral::RangeMove<samplepos_t> > const & ranges)
4185 for (list<Evoral::RangeMove<samplepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
4186 maybe_update_session_range (i->to, i->to + i->length);
4191 Session::playlist_regions_extended (list<Evoral::Range<samplepos_t> > const & ranges)
4193 for (list<Evoral::Range<samplepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
4194 maybe_update_session_range (i->from, i->to);
4198 /* Region management */
4200 boost::shared_ptr<Region>
4201 Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
4203 const RegionFactory::RegionMap& regions (RegionFactory::regions());
4204 RegionFactory::RegionMap::const_iterator i;
4205 boost::shared_ptr<Region> region;
4207 Glib::Threads::Mutex::Lock lm (region_lock);
4209 for (i = regions.begin(); i != regions.end(); ++i) {
4213 if (region->whole_file()) {
4215 if (child->source_equivalent (region)) {
4221 return boost::shared_ptr<Region> ();
4225 Session::destroy_sources (list<boost::shared_ptr<Source> > srcs)
4227 set<boost::shared_ptr<Region> > relevant_regions;
4229 for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ++s) {
4230 RegionFactory::get_regions_using_source (*s, relevant_regions);
4233 for (set<boost::shared_ptr<Region> >::iterator r = relevant_regions.begin(); r != relevant_regions.end(); ) {
4234 set<boost::shared_ptr<Region> >::iterator tmp;
4239 _playlists->destroy_region (*r);
4240 RegionFactory::map_remove (*r);
4242 (*r)->drop_sources ();
4243 (*r)->drop_references ();
4245 relevant_regions.erase (r);
4250 for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ) {
4253 Glib::Threads::Mutex::Lock ls (source_lock);
4254 /* remove from the main source list */
4255 sources.erase ((*s)->id());
4258 (*s)->mark_for_remove ();
4259 (*s)->drop_references ();
4269 Session::remove_last_capture ()
4271 list<boost::shared_ptr<Source> > srcs;
4273 boost::shared_ptr<RouteList> rl = routes.reader ();
4274 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4275 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
4280 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4283 srcs.insert (srcs.end(), l.begin(), l.end());
4288 destroy_sources (srcs);
4290 save_state (_current_snapshot_name);
4296 Session::get_last_capture_sources (std::list<boost::shared_ptr<Source> >& srcs)
4298 boost::shared_ptr<RouteList> rl = routes.reader ();
4299 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4300 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
4305 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4308 srcs.insert (srcs.end(), l.begin(), l.end());
4314 /* Source Management */
4317 Session::add_source (boost::shared_ptr<Source> source)
4319 pair<SourceMap::key_type, SourceMap::mapped_type> entry;
4320 pair<SourceMap::iterator,bool> result;
4322 entry.first = source->id();
4323 entry.second = source;
4326 Glib::Threads::Mutex::Lock lm (source_lock);
4327 result = sources.insert (entry);
4330 if (result.second) {
4332 /* yay, new source */
4334 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
4337 if (!fs->within_session()) {
4338 ensure_search_path_includes (Glib::path_get_dirname (fs->path()), fs->type());
4344 boost::shared_ptr<AudioFileSource> afs;
4346 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
4347 if (Config->get_auto_analyse_audio()) {
4348 Analyser::queue_source_for_analysis (source, false);
4352 source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
4354 SourceAdded(source);
4359 Session::remove_source (boost::weak_ptr<Source> src)
4361 if (deletion_in_progress ()) {
4365 SourceMap::iterator i;
4366 boost::shared_ptr<Source> source = src.lock();
4373 Glib::Threads::Mutex::Lock lm (source_lock);
4375 if ((i = sources.find (source->id())) != sources.end()) {
4377 SourceRemoved(source);
4381 if (!in_cleanup () && !loading ()) {
4383 /* save state so we don't end up with a session file
4384 * referring to non-existent sources.
4387 save_state (_current_snapshot_name);
4391 boost::shared_ptr<Source>
4392 Session::source_by_id (const PBD::ID& id)
4394 Glib::Threads::Mutex::Lock lm (source_lock);
4395 SourceMap::iterator i;
4396 boost::shared_ptr<Source> source;
4398 if ((i = sources.find (id)) != sources.end()) {
4405 boost::shared_ptr<AudioFileSource>
4406 Session::audio_source_by_path_and_channel (const string& path, uint16_t chn) const
4408 /* Restricted to audio files because only audio sources have channel
4412 Glib::Threads::Mutex::Lock lm (source_lock);
4414 for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4415 boost::shared_ptr<AudioFileSource> afs
4416 = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
4418 if (afs && afs->path() == path && chn == afs->channel()) {
4423 return boost::shared_ptr<AudioFileSource>();
4426 boost::shared_ptr<MidiSource>
4427 Session::midi_source_by_path (const std::string& path, bool need_source_lock) const
4429 /* Restricted to MIDI files because audio sources require a channel
4430 for unique identification, in addition to a path.
4433 Glib::Threads::Mutex::Lock lm (source_lock, Glib::Threads::NOT_LOCK);
4434 if (need_source_lock) {
4438 for (SourceMap::const_iterator s = sources.begin(); s != sources.end(); ++s) {
4439 boost::shared_ptr<MidiSource> ms
4440 = boost::dynamic_pointer_cast<MidiSource>(s->second);
4441 boost::shared_ptr<FileSource> fs
4442 = boost::dynamic_pointer_cast<FileSource>(s->second);
4444 if (ms && fs && fs->path() == path) {
4449 return boost::shared_ptr<MidiSource>();
4453 Session::count_sources_by_origin (const string& path)
4456 Glib::Threads::Mutex::Lock lm (source_lock);
4458 for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
4459 boost::shared_ptr<FileSource> fs
4460 = boost::dynamic_pointer_cast<FileSource>(i->second);
4462 if (fs && fs->origin() == path) {
4471 peak_file_helper (const string& peak_path, const string& file_path, const string& file_base, bool hash) {
4473 std::string checksum = Glib::Checksum::compute_checksum(Glib::Checksum::CHECKSUM_SHA1, file_path + G_DIR_SEPARATOR + file_base);
4474 return Glib::build_filename (peak_path, checksum + peakfile_suffix);
4476 return Glib::build_filename (peak_path, file_base + peakfile_suffix);
4481 Session::construct_peak_filepath (const string& filepath, const bool in_session, const bool old_peak_name) const
4483 string interchange_dir_string = string (interchange_dir_name) + G_DIR_SEPARATOR;
4485 if (Glib::path_is_absolute (filepath)) {
4487 /* rip the session dir from the audiofile source */
4489 string session_path;
4490 bool in_another_session = true;
4492 if (filepath.find (interchange_dir_string) != string::npos) {
4494 session_path = Glib::path_get_dirname (filepath); /* now ends in audiofiles */
4495 session_path = Glib::path_get_dirname (session_path); /* now ends in session name */
4496 session_path = Glib::path_get_dirname (session_path); /* now ends in interchange */
4497 session_path = Glib::path_get_dirname (session_path); /* now has session path */
4499 /* see if it is within our session */
4501 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4502 if (i->path == session_path) {
4503 in_another_session = false;
4508 in_another_session = false;
4512 if (in_another_session) {
4513 SessionDirectory sd (session_path);
4514 return peak_file_helper (sd.peak_path(), "", Glib::path_get_basename (filepath), !old_peak_name);
4518 /* 1) if file belongs to this session
4519 * it may be a relative path (interchange/...)
4520 * or just basename (session_state, remove source)
4521 * -> just use the basename
4523 std::string filename = Glib::path_get_basename (filepath);
4526 /* 2) if the file is outside our session dir:
4527 * (imported but not copied) add the path for check-summming */
4529 path = Glib::path_get_dirname (filepath);
4532 return peak_file_helper (_session_dir->peak_path(), path, Glib::path_get_basename (filepath), !old_peak_name);
4536 Session::new_audio_source_path_for_embedded (const std::string& path)
4540 * we know that the filename is already unique because it exists
4541 * out in the filesystem.
4543 * However, when we bring it into the session, we could get a
4546 * Eg. two embedded files:
4551 * When merged into session, these collide.
4553 * There will not be a conflict with in-memory sources
4554 * because when the source was created we already picked
4555 * a unique name for it.
4557 * This collision is not likely to be common, but we have to guard
4558 * against it. So, if there is a collision, take the md5 hash of the
4559 * the path, and use that as the filename instead.
4562 SessionDirectory sdir (get_best_session_directory_for_new_audio());
4563 string base = Glib::path_get_basename (path);
4564 string newpath = Glib::build_filename (sdir.sound_path(), base);
4566 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
4570 md5.digestString (path.c_str());
4571 md5.writeToString ();
4572 base = md5.digestChars;
4574 string ext = get_suffix (path);
4581 newpath = Glib::build_filename (sdir.sound_path(), base);
4583 /* if this collides, we're screwed */
4585 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
4586 error << string_compose (_("Merging embedded file %1: name collision AND md5 hash collision!"), path) << endmsg;
4595 /** Return true if there are no audio file sources that use @param name as
4596 * the filename component of their path.
4598 * Return false otherwise.
4600 * This method MUST ONLY be used to check in-session, mono files since it
4601 * hard-codes the channel of the audio file source we are looking for as zero.
4603 * If/when Ardour supports native files in non-mono formats, the logic here
4604 * will need to be revisited.
4607 Session::audio_source_name_is_unique (const string& name)
4609 std::vector<string> sdirs = source_search_path (DataType::AUDIO);
4610 vector<space_and_path>::iterator i;
4611 uint32_t existing = 0;
4613 for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4615 /* note that we search *without* the extension so that
4616 we don't end up both "Audio 1-1.wav" and "Audio 1-1.caf"
4617 in the event that this new name is required for
4618 a file format change.
4621 const string spath = *i;
4623 if (matching_unsuffixed_filename_exists_in (spath, name)) {
4628 /* it is possible that we have the path already
4629 * assigned to a source that has not yet been written
4630 * (ie. the write source for a diskstream). we have to
4631 * check this in order to make sure that our candidate
4632 * path isn't used again, because that can lead to
4633 * two Sources point to the same file with different
4634 * notions of their removability.
4638 string possible_path = Glib::build_filename (spath, name);
4640 if (audio_source_by_path_and_channel (possible_path, 0)) {
4646 return (existing == 0);
4650 Session::format_audio_source_name (const string& legalized_base, uint32_t nchan, uint32_t chan, bool destructive, bool take_required, uint32_t cnt, bool related_exists)
4653 const string ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
4655 sstr << legalized_base;
4657 if (take_required || related_exists) {
4668 } else if (nchan > 2) {
4673 /* XXX what? more than 26 channels! */
4684 /** Return a unique name based on \a base for a new internal audio source */
4686 Session::new_audio_source_path (const string& base, uint32_t nchan, uint32_t chan, bool destructive, bool take_required)
4689 string possible_name;
4690 const uint32_t limit = 9999; // arbitrary limit on number of files with the same basic name
4692 bool some_related_source_name_exists = false;
4694 legalized = legalize_for_path (base);
4696 // Find a "version" of the base name that doesn't exist in any of the possible directories.
4698 for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
4700 possible_name = format_audio_source_name (legalized, nchan, chan, destructive, take_required, cnt, some_related_source_name_exists);
4702 if (audio_source_name_is_unique (possible_name)) {
4706 some_related_source_name_exists = true;
4709 error << string_compose(
4710 _("There are already %1 recordings for %2, which I consider too many."),
4711 limit, base) << endmsg;
4713 throw failed_constructor();
4717 /* We've established that the new name does not exist in any session
4718 * directory, so now find out which one we should use for this new
4722 SessionDirectory sdir (get_best_session_directory_for_new_audio());
4724 std::string s = Glib::build_filename (sdir.sound_path(), possible_name);
4729 /** Return a unique name based on `base` for a new internal MIDI source */
4731 Session::new_midi_source_path (const string& base, bool need_lock)
4733 string possible_path;
4734 string possible_name;
4736 possible_name = legalize_for_path (base);
4738 // Find a "version" of the file name that doesn't exist in any of the possible directories.
4739 std::vector<string> sdirs = source_search_path(DataType::MIDI);
4741 /* - the main session folder is the first in the vector.
4742 * - after checking all locations for file-name uniqueness,
4743 * we keep the one from the last iteration as new file name
4744 * - midi files are small and should just be kept in the main session-folder
4746 * -> reverse the array, check main session folder last and use that as location
4749 std::reverse(sdirs.begin(), sdirs.end());
4752 possible_name = bump_name_once (possible_name, '-');
4754 vector<space_and_path>::iterator i;
4755 uint32_t existing = 0;
4757 for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4759 possible_path = Glib::build_filename (*i, possible_name + ".mid");
4761 if (Glib::file_test (possible_path, Glib::FILE_TEST_EXISTS)) {
4765 if (midi_source_by_path (possible_path, need_lock)) {
4770 if (possible_path.size () >= PATH_MAX) {
4771 error << string_compose(
4772 _("There are already many recordings for %1, resulting in a too long file-path %2."),
4773 base, possible_path) << endmsg;
4778 if (existing == 0) {
4783 /* No need to "find best location" for software/app-based RAID, because
4784 MIDI is so small that we always put it in the same place.
4787 return possible_path;
4791 /** Create a new within-session audio source */
4792 boost::shared_ptr<AudioFileSource>
4793 Session::create_audio_source_for_session (size_t n_chans, string const & base, uint32_t chan, bool destructive)
4795 const string path = new_audio_source_path (base, n_chans, chan, destructive, true);
4797 if (!path.empty()) {
4798 return boost::dynamic_pointer_cast<AudioFileSource> (
4799 SourceFactory::createWritable (DataType::AUDIO, *this, path, destructive, sample_rate(), true, true));
4801 throw failed_constructor ();
4805 /** Create a new within-session MIDI source */
4806 boost::shared_ptr<MidiSource>
4807 Session::create_midi_source_for_session (string const & basic_name)
4809 const string path = new_midi_source_path (basic_name);
4811 if (!path.empty()) {
4812 return boost::dynamic_pointer_cast<SMFSource> (
4813 SourceFactory::createWritable (
4814 DataType::MIDI, *this, path, false, sample_rate()));
4816 throw failed_constructor ();
4820 /** Create a new within-session MIDI source */
4821 boost::shared_ptr<MidiSource>
4822 Session::create_midi_source_by_stealing_name (boost::shared_ptr<Track> track)
4824 /* the caller passes in the track the source will be used in,
4825 so that we can keep the numbering sane.
4827 Rationale: a track with the name "Foo" that has had N
4828 captures carried out so far will ALREADY have a write source
4829 named "Foo-N+1.mid" waiting to be used for the next capture.
4831 If we call new_midi_source_name() we will get "Foo-N+2". But
4832 there is no region corresponding to "Foo-N+1", so when
4833 "Foo-N+2" appears in the track, the gap presents the user
4834 with odd behaviour - why did it skip past Foo-N+1?
4836 We could explain this to the user in some odd way, but
4837 instead we rename "Foo-N+1.mid" as "Foo-N+2.mid", and then
4840 If that attempted rename fails, we get "Foo-N+2.mid" anyway.
4843 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (track);
4845 std::string name = track->steal_write_source_name ();
4848 return boost::shared_ptr<MidiSource>();
4851 /* MIDI files are small, just put them in the first location of the
4852 session source search path.
4855 const string path = Glib::build_filename (source_search_path (DataType::MIDI).front(), name);
4857 return boost::dynamic_pointer_cast<SMFSource> (
4858 SourceFactory::createWritable (
4859 DataType::MIDI, *this, path, false, sample_rate()));
4863 Session::playlist_is_active (boost::shared_ptr<Playlist> playlist)
4865 Glib::Threads::Mutex::Lock lm (_playlists->lock);
4866 for (SessionPlaylists::List::iterator i = _playlists->playlists.begin(); i != _playlists->playlists.end(); i++) {
4867 if ( (*i) == playlist ) {
4875 Session::add_playlist (boost::shared_ptr<Playlist> playlist, bool unused)
4877 if (playlist->hidden()) {
4881 _playlists->add (playlist);
4884 playlist->release();
4891 Session::remove_playlist (boost::weak_ptr<Playlist> weak_playlist)
4893 if (deletion_in_progress ()) {
4897 boost::shared_ptr<Playlist> playlist (weak_playlist.lock());
4903 _playlists->remove (playlist);
4909 Session::set_audition (boost::shared_ptr<Region> r)
4911 pending_audition_region = r;
4912 add_post_transport_work (PostTransportAudition);
4913 _butler->schedule_transport_work ();
4917 Session::audition_playlist ()
4919 SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
4920 ev->region.reset ();
4926 Session::register_lua_function (
4927 const std::string& name,
4928 const std::string& script,
4929 const LuaScriptParamList& args
4932 Glib::Threads::Mutex::Lock lm (lua_lock);
4934 lua_State* L = lua.getState();
4936 const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
4937 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
4938 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
4939 if ((*i)->optional && !(*i)->is_set) { continue; }
4940 tbl_arg[(*i)->name] = (*i)->value;
4942 (*_lua_add)(name, bytecode, tbl_arg); // throws luabridge::LuaException
4945 LuaScriptsChanged (); /* EMIT SIGNAL */
4950 Session::unregister_lua_function (const std::string& name)
4952 Glib::Threads::Mutex::Lock lm (lua_lock);
4953 (*_lua_del)(name); // throws luabridge::LuaException
4954 lua.collect_garbage ();
4957 LuaScriptsChanged (); /* EMIT SIGNAL */
4961 std::vector<std::string>
4962 Session::registered_lua_functions ()
4964 Glib::Threads::Mutex::Lock lm (lua_lock);
4965 std::vector<std::string> rv;
4968 luabridge::LuaRef list ((*_lua_list)());
4969 for (luabridge::Iterator i (list); !i.isNil (); ++i) {
4970 if (!i.key ().isString ()) { assert(0); continue; }
4971 rv.push_back (i.key ().cast<std::string> ());
4978 static void _lua_print (std::string s) {
4979 std::cout << "SessionLua: " << s << "\n";
4984 Session::try_run_lua (pframes_t nframes)
4986 if (_n_lua_scripts == 0) return;
4987 Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
4989 try { (*_lua_run)(nframes); } catch (...) { }
4990 lua.collect_garbage_step ();
4995 Session::setup_lua ()
4998 lua.Print.connect (&_lua_print);
5002 "function ArdourSession ()"
5003 " local self = { scripts = {}, instances = {} }"
5005 " local remove = function (n)"
5006 " self.scripts[n] = nil"
5007 " self.instances[n] = nil"
5008 " Session:scripts_changed()" // call back
5011 " local addinternal = function (n, f, a)"
5012 " assert(type(n) == 'string', 'function-name must be string')"
5013 " assert(type(f) == 'function', 'Given script is a not a function')"
5014 " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
5015 " assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')"
5016 " self.scripts[n] = { ['f'] = f, ['a'] = a }"
5017 " local env = { print = print, tostring = tostring, assert = assert, ipairs = ipairs, error = error, select = select, string = string, type = type, tonumber = tonumber, collectgarbage = collectgarbage, pairs = pairs, math = math, table = table, pcall = pcall, bit32=bit32, Session = Session, PBD = PBD, Timecode = Timecode, Evoral = Evoral, C = C, ARDOUR = ARDOUR }"
5018 " self.instances[n] = load (string.dump(f, true), nil, nil, env)(a)"
5019 " Session:scripts_changed()" // call back
5022 " local add = function (n, b, a)"
5023 " assert(type(b) == 'string', 'ByteCode must be string')"
5024 " load (b)()" // assigns f
5025 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
5026 " addinternal (n, load(f), a)"
5029 " local run = function (...)"
5030 " for n, s in pairs (self.instances) do"
5031 " local status, err = pcall (s, ...)"
5032 " if not status then"
5033 " print ('fn \"'.. n .. '\": ', err)"
5040 " local cleanup = function ()"
5041 " self.scripts = nil"
5042 " self.instances = nil"
5045 " local list = function ()"
5047 " for n, _ in pairs (self.scripts) do"
5053 " local function basic_serialize (o)"
5054 " if type(o) == \"number\" then"
5055 " return tostring(o)"
5057 " return string.format(\"%q\", o)"
5061 " local function serialize (name, value)"
5062 " local rv = name .. ' = '"
5064 " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
5065 " return rv .. basic_serialize(value) .. ' '"
5066 " elseif type(value) == \"table\" then"
5068 " for k,v in pairs(value) do"
5069 " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
5070 " rv = rv .. serialize(fieldname, v) .. ' '"
5071 " collectgarbage()" // string concatenation allocates a new string :(
5074 " elseif type(value) == \"function\" then"
5075 " return rv .. string.format(\"%q\", string.dump(value, true))"
5077 " error('cannot save a ' .. type(value))"
5082 " local save = function ()"
5083 " return (serialize('scripts', self.scripts))"
5086 " local restore = function (state)"
5087 " self.scripts = {}"
5089 " for n, s in pairs (scripts) do"
5090 " addinternal (n, load(s['f']), s['a'])"
5094 " return { run = run, add = add, remove = remove,"
5095 " list = list, restore = restore, save = save, cleanup = cleanup}"
5098 " sess = ArdourSession ()"
5099 " ArdourSession = nil"
5101 "function ardour () end"
5104 lua_State* L = lua.getState();
5107 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
5108 lua.do_command ("sess = nil"); // hide it.
5109 lua.do_command ("collectgarbage()");
5111 _lua_run = new luabridge::LuaRef(lua_sess["run"]);
5112 _lua_add = new luabridge::LuaRef(lua_sess["add"]);
5113 _lua_del = new luabridge::LuaRef(lua_sess["remove"]);
5114 _lua_list = new luabridge::LuaRef(lua_sess["list"]);
5115 _lua_save = new luabridge::LuaRef(lua_sess["save"]);
5116 _lua_load = new luabridge::LuaRef(lua_sess["restore"]);
5117 _lua_cleanup = new luabridge::LuaRef(lua_sess["cleanup"]);
5118 } catch (luabridge::LuaException const& e) {
5119 fatal << string_compose (_("programming error: %1"),
5120 std::string ("Failed to setup session Lua interpreter") + e.what ())
5122 abort(); /*NOTREACHED*/
5124 fatal << string_compose (_("programming error: %1"),
5125 X_("Failed to setup session Lua interpreter"))
5127 abort(); /*NOTREACHED*/
5131 LuaBindings::stddef (L);
5132 LuaBindings::common (L);
5133 LuaBindings::dsp (L);
5135 luabridge::push <Session *> (L, this);
5136 lua_setglobal (L, "Session");
5140 Session::scripts_changed ()
5142 assert (!lua_lock.trylock()); // must hold lua_lock
5145 luabridge::LuaRef list ((*_lua_list)());
5147 for (luabridge::Iterator i (list); !i.isNil (); ++i) {
5148 if (!i.key ().isString ()) { assert(0); continue; }
5151 _n_lua_scripts = cnt;
5152 } catch (luabridge::LuaException const& e) {
5153 fatal << string_compose (_("programming error: %1"),
5154 std::string ("Indexing Lua Session Scripts failed.") + e.what ())
5156 abort(); /*NOTREACHED*/
5158 fatal << string_compose (_("programming error: %1"),
5159 X_("Indexing Lua Session Scripts failed."))
5161 abort(); /*NOTREACHED*/
5166 Session::non_realtime_set_audition ()
5168 assert (pending_audition_region);
5169 auditioner->audition_region (pending_audition_region);
5170 pending_audition_region.reset ();
5171 AuditionActive (true); /* EMIT SIGNAL */
5175 Session::audition_region (boost::shared_ptr<Region> r)
5177 SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
5183 Session::cancel_audition ()
5188 if (auditioner->auditioning()) {
5189 auditioner->cancel_audition ();
5190 AuditionActive (false); /* EMIT SIGNAL */
5195 Session::is_auditioning () const
5197 /* can be called before we have an auditioner object */
5199 return auditioner->auditioning();
5206 Session::graph_reordered ()
5208 /* don't do this stuff if we are setting up connections
5209 from a set_state() call or creating new tracks. Ditto for deletion.
5212 if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _reconnecting_routes_in_progress || _route_deletion_in_progress) {
5218 /* force all diskstreams to update their capture offset values to
5219 * reflect any changes in latencies within the graph.
5221 update_latency_compensation (true);
5224 /** @return Number of samples that there is disk space available to write,
5227 boost::optional<samplecnt_t>
5228 Session::available_capture_duration ()
5230 Glib::Threads::Mutex::Lock lm (space_lock);
5232 if (_total_free_4k_blocks_uncertain) {
5233 return boost::optional<samplecnt_t> ();
5236 float sample_bytes_on_disk = 4.0; // keep gcc happy
5238 switch (config.get_native_file_data_format()) {
5240 sample_bytes_on_disk = 4.0;
5244 sample_bytes_on_disk = 3.0;
5248 sample_bytes_on_disk = 2.0;
5252 /* impossible, but keep some gcc versions happy */
5253 fatal << string_compose (_("programming error: %1"),
5254 X_("illegal native file data format"))
5256 abort(); /*NOTREACHED*/
5259 double scale = 4096.0 / sample_bytes_on_disk;
5261 if (_total_free_4k_blocks * scale > (double) max_samplecnt) {
5262 return max_samplecnt;
5265 return (samplecnt_t) floor (_total_free_4k_blocks * scale);
5269 Session::tempo_map_changed (const PropertyChange&)
5273 _playlists->update_after_tempo_map_change ();
5275 _locations->apply (*this, &Session::update_locations_after_tempo_map_change);
5281 Session::update_locations_after_tempo_map_change (const Locations::LocationList& loc)
5283 for (Locations::LocationList::const_iterator i = loc.begin(); i != loc.end(); ++i) {
5284 (*i)->recompute_samples_from_beat ();
5288 /** Ensures that all buffers (scratch, send, silent, etc) are allocated for
5289 * the given count with the current block size.
5292 Session::ensure_buffers (ChanCount howmany)
5294 BufferManager::ensure_buffers (howmany, bounce_processing() ? bounce_chunk_size : 0);
5298 Session::ensure_buffer_set(BufferSet& buffers, const ChanCount& count)
5300 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
5301 buffers.ensure_buffers(*t, count.get(*t), _engine.raw_buffer_size(*t));
5306 Session::next_insert_id ()
5308 /* this doesn't really loop forever. just think about it */
5311 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < insert_bitset.size(); ++n) {
5312 if (!insert_bitset[n]) {
5313 insert_bitset[n] = true;
5319 /* none available, so resize and try again */
5321 insert_bitset.resize (insert_bitset.size() + 16, false);
5326 Session::next_send_id ()
5328 /* this doesn't really loop forever. just think about it */
5331 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < send_bitset.size(); ++n) {
5332 if (!send_bitset[n]) {
5333 send_bitset[n] = true;
5339 /* none available, so resize and try again */
5341 send_bitset.resize (send_bitset.size() + 16, false);
5346 Session::next_aux_send_id ()
5348 /* this doesn't really loop forever. just think about it */
5351 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < aux_send_bitset.size(); ++n) {
5352 if (!aux_send_bitset[n]) {
5353 aux_send_bitset[n] = true;
5359 /* none available, so resize and try again */
5361 aux_send_bitset.resize (aux_send_bitset.size() + 16, false);
5366 Session::next_return_id ()
5368 /* this doesn't really loop forever. just think about it */
5371 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < return_bitset.size(); ++n) {
5372 if (!return_bitset[n]) {
5373 return_bitset[n] = true;
5379 /* none available, so resize and try again */
5381 return_bitset.resize (return_bitset.size() + 16, false);
5386 Session::mark_send_id (uint32_t id)
5388 if (id >= send_bitset.size()) {
5389 send_bitset.resize (id+16, false);
5391 if (send_bitset[id]) {
5392 warning << string_compose (_("send ID %1 appears to be in use already"), id) << endmsg;
5394 send_bitset[id] = true;
5398 Session::mark_aux_send_id (uint32_t id)
5400 if (id >= aux_send_bitset.size()) {
5401 aux_send_bitset.resize (id+16, false);
5403 if (aux_send_bitset[id]) {
5404 warning << string_compose (_("aux send ID %1 appears to be in use already"), id) << endmsg;
5406 aux_send_bitset[id] = true;
5410 Session::mark_return_id (uint32_t id)
5412 if (id >= return_bitset.size()) {
5413 return_bitset.resize (id+16, false);
5415 if (return_bitset[id]) {
5416 warning << string_compose (_("return ID %1 appears to be in use already"), id) << endmsg;
5418 return_bitset[id] = true;
5422 Session::mark_insert_id (uint32_t id)
5424 if (id >= insert_bitset.size()) {
5425 insert_bitset.resize (id+16, false);
5427 if (insert_bitset[id]) {
5428 warning << string_compose (_("insert ID %1 appears to be in use already"), id) << endmsg;
5430 insert_bitset[id] = true;
5434 Session::unmark_send_id (uint32_t id)
5436 if (id < send_bitset.size()) {
5437 send_bitset[id] = false;
5442 Session::unmark_aux_send_id (uint32_t id)
5444 if (id < aux_send_bitset.size()) {
5445 aux_send_bitset[id] = false;
5450 Session::unmark_return_id (uint32_t id)
5452 if (deletion_in_progress ()) {
5455 if (id < return_bitset.size()) {
5456 return_bitset[id] = false;
5461 Session::unmark_insert_id (uint32_t id)
5463 if (id < insert_bitset.size()) {
5464 insert_bitset[id] = false;
5469 Session::reset_native_file_format ()
5471 boost::shared_ptr<RouteList> rl = routes.reader ();
5473 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
5474 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5476 /* don't save state as we do this, there's no point */
5477 _state_of_the_state = StateOfTheState (_state_of_the_state | InCleanup);
5478 tr->reset_write_sources (false);
5479 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
5485 Session::route_name_unique (string n) const
5487 boost::shared_ptr<RouteList> r = routes.reader ();
5489 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5490 if ((*i)->name() == n) {
5499 Session::route_name_internal (string n) const
5501 if (auditioner && auditioner->name() == n) {
5505 if (_click_io && _click_io->name() == n) {
5513 Session::freeze_all (InterThreadInfo& itt)
5515 boost::shared_ptr<RouteList> r = routes.reader ();
5517 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
5519 boost::shared_ptr<Track> t;
5521 if ((t = boost::dynamic_pointer_cast<Track>(*i)) != 0) {
5522 /* XXX this is wrong because itt.progress will keep returning to zero at the start
5532 boost::shared_ptr<Region>
5533 Session::write_one_track (Track& track, samplepos_t start, samplepos_t end,
5534 bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs,
5535 InterThreadInfo& itt,
5536 boost::shared_ptr<Processor> endpoint, bool include_endpoint,
5537 bool for_export, bool for_freeze)
5539 boost::shared_ptr<Region> result;
5540 boost::shared_ptr<Playlist> playlist;
5541 boost::shared_ptr<Source> source;
5542 ChanCount diskstream_channels (track.n_channels());
5543 samplepos_t position;
5544 samplecnt_t this_chunk;
5546 samplepos_t latency_skip;
5548 samplepos_t len = end - start;
5549 bool need_block_size_reset = false;
5550 ChanCount const max_proc = track.max_processor_streams ();
5551 string legal_playlist_name;
5552 string possible_path;
5554 DataType data_type = track.data_type();
5557 error << string_compose (_("Cannot write a range where end <= start (e.g. %1 <= %2)"),
5558 end, start) << endmsg;
5562 diskstream_channels = track.bounce_get_output_streams (diskstream_channels, endpoint,
5563 include_endpoint, for_export, for_freeze);
5565 if (data_type == DataType::MIDI && endpoint && !for_export && !for_freeze && diskstream_channels.n(DataType::AUDIO) > 0) {
5566 data_type = DataType::AUDIO;
5569 if (diskstream_channels.n(data_type) < 1) {
5570 error << _("Cannot write a range with no data.") << endmsg;
5574 // block all process callback handling
5576 block_processing ();
5579 // synchronize with AudioEngine::process_callback()
5580 // make sure processing is not currently running
5581 // and processing_blocked() is honored before
5582 // acquiring thread buffers
5583 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
5586 _bounce_processing_active = true;
5588 /* call tree *MUST* hold route_lock */
5590 if ((playlist = track.playlist()) == 0) {
5594 legal_playlist_name = "(bounce)" + legalize_for_path (playlist->name());
5596 for (uint32_t chan_n = 0; chan_n < diskstream_channels.n(data_type); ++chan_n) {
5598 string base_name = string_compose ("%1-%2-bounce", playlist->name(), chan_n);
5599 string path = ((data_type == DataType::AUDIO)
5600 ? new_audio_source_path (legal_playlist_name, diskstream_channels.n_audio(), chan_n, false, true)
5601 : new_midi_source_path (legal_playlist_name));
5608 source = SourceFactory::createWritable (data_type, *this, path, false, sample_rate());
5611 catch (failed_constructor& err) {
5612 error << string_compose (_("cannot create new file \"%1\" for %2"), path, track.name()) << endmsg;
5616 srcs.push_back (source);
5619 /* tell redirects that care that we are about to use a much larger
5620 * blocksize. this will flush all plugins too, so that they are ready
5621 * to be used for this process.
5624 need_block_size_reset = true;
5625 track.set_block_size (bounce_chunk_size);
5626 _engine.main_thread()->get_buffers ();
5630 latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5632 /* create a set of reasonably-sized buffers */
5633 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
5634 buffers.ensure_buffers(*t, max_proc.get(*t), bounce_chunk_size);
5636 buffers.set_count (max_proc);
5638 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5639 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5640 boost::shared_ptr<MidiSource> ms;
5642 afs->prepare_for_peakfile_writes ();
5643 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5644 Source::Lock lock(ms->mutex());
5645 ms->mark_streaming_write_started(lock);
5649 while (to_do && !itt.cancel) {
5651 this_chunk = min (to_do, bounce_chunk_size);
5653 if (track.export_stuff (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze)) {
5657 start += this_chunk;
5658 to_do -= this_chunk;
5659 itt.progress = (float) (1.0 - ((double) to_do / len));
5661 if (latency_skip >= bounce_chunk_size) {
5662 latency_skip -= bounce_chunk_size;
5666 const samplecnt_t current_chunk = this_chunk - latency_skip;
5669 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
5670 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5671 boost::shared_ptr<MidiSource> ms;
5674 if (afs->write (buffers.get_audio(n).data(latency_skip), current_chunk) != current_chunk) {
5677 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5678 Source::Lock lock(ms->mutex());
5680 const MidiBuffer& buf = buffers.get_midi(0);
5681 for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
5682 Evoral::Event<samplepos_t> ev = *i;
5683 if (!endpoint || for_export) {
5684 ev.set_time(ev.time() - position);
5686 ms->append_event_samples(lock, ev, ms->natural_position());
5693 /* post-roll, pick up delayed processor output */
5694 latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5696 while (latency_skip && !itt.cancel) {
5697 this_chunk = min (latency_skip, bounce_chunk_size);
5698 latency_skip -= this_chunk;
5700 buffers.silence (this_chunk, 0);
5701 track.bounce_process (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze);
5704 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
5705 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5708 if (afs->write (buffers.get_audio(n).data(), this_chunk) != this_chunk) {
5720 xnow = localtime (&now);
5722 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
5723 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5724 boost::shared_ptr<MidiSource> ms;
5727 afs->update_header (position, *xnow, now);
5728 afs->flush_header ();
5729 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5730 Source::Lock lock(ms->mutex());
5731 ms->mark_streaming_write_completed(lock);
5735 /* construct a region to represent the bounced material */
5739 plist.add (Properties::start, 0);
5740 plist.add (Properties::whole_file, true);
5741 plist.add (Properties::length, srcs.front()->length(srcs.front()->natural_position()));
5742 plist.add (Properties::name, region_name_from_path (srcs.front()->name(), true));
5744 result = RegionFactory::create (srcs, plist, true);
5750 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5751 (*src)->mark_for_remove ();
5752 (*src)->drop_references ();
5756 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5757 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5760 afs->done_with_peakfile_writes ();
5764 _bounce_processing_active = false;
5766 if (need_block_size_reset) {
5767 _engine.main_thread()->drop_buffers ();
5768 track.set_block_size (get_block_size());
5771 unblock_processing ();
5777 Session::gain_automation_buffer() const
5779 return ProcessThread::gain_automation_buffer ();
5783 Session::trim_automation_buffer() const
5785 return ProcessThread::trim_automation_buffer ();
5789 Session::send_gain_automation_buffer() const
5791 return ProcessThread::send_gain_automation_buffer ();
5795 Session::scratch_automation_buffer() const
5797 return ProcessThread::scratch_automation_buffer ();
5801 Session::pan_automation_buffer() const
5803 return ProcessThread::pan_automation_buffer ();
5807 Session::get_silent_buffers (ChanCount count)
5809 return ProcessThread::get_silent_buffers (count);
5813 Session::get_scratch_buffers (ChanCount count, bool silence)
5815 return ProcessThread::get_scratch_buffers (count, silence);
5819 Session::get_noinplace_buffers (ChanCount count)
5821 return ProcessThread::get_noinplace_buffers (count);
5825 Session::get_route_buffers (ChanCount count, bool silence)
5827 return ProcessThread::get_route_buffers (count, silence);
5832 Session::get_mix_buffers (ChanCount count)
5834 return ProcessThread::get_mix_buffers (count);
5838 Session::ntracks () const
5841 boost::shared_ptr<RouteList> r = routes.reader ();
5843 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5844 if (boost::dynamic_pointer_cast<Track> (*i)) {
5853 Session::nbusses () const
5856 boost::shared_ptr<RouteList> r = routes.reader ();
5858 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5859 if (boost::dynamic_pointer_cast<Track>(*i) == 0) {
5868 Session::nstripables (bool with_monitor) const
5870 uint32_t rv = routes.reader()->size ();
5871 rv += _vca_manager->vcas ().size ();
5885 Session::plot_process_graph (std::string const& file_name) const {
5886 return _process_graph ? _process_graph->plot (file_name) : false;
5890 Session::add_automation_list(AutomationList *al)
5892 automation_lists[al->id()] = al;
5895 /** @return true if there is at least one record-enabled track, otherwise false */
5897 Session::have_rec_enabled_track () const
5899 return g_atomic_int_get (const_cast<gint*>(&_have_rec_enabled_track)) == 1;
5903 Session::have_rec_disabled_track () const
5905 return g_atomic_int_get (const_cast<gint*>(&_have_rec_disabled_track)) == 1;
5908 /** Update the state of our rec-enabled tracks flag */
5910 Session::update_route_record_state ()
5912 boost::shared_ptr<RouteList> rl = routes.reader ();
5913 RouteList::iterator i = rl->begin();
5914 while (i != rl->end ()) {
5916 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5917 if (tr && tr->rec_enable_control()->get_value()) {
5924 int const old = g_atomic_int_get (&_have_rec_enabled_track);
5926 g_atomic_int_set (&_have_rec_enabled_track, i != rl->end () ? 1 : 0);
5928 if (g_atomic_int_get (&_have_rec_enabled_track) != old) {
5929 RecordStateChanged (); /* EMIT SIGNAL */
5932 for (i = rl->begin(); i != rl->end (); ++i) {
5933 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5934 if (tr && !tr->rec_enable_control()->get_value()) {
5939 g_atomic_int_set (&_have_rec_disabled_track, i != rl->end () ? 1 : 0);
5941 bool record_arm_state_changed = (old != g_atomic_int_get (&_have_rec_enabled_track) );
5943 if (record_status() == Recording && record_arm_state_changed ) {
5944 RecordArmStateChanged ();
5950 Session::listen_position_changed ()
5953 /* skip duing session restore (already taken care of) */
5956 ProcessorChangeBlocker pcb (this);
5957 boost::shared_ptr<RouteList> r = routes.reader ();
5958 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
5959 (*i)->listen_position_changed ();
5964 Session::solo_control_mode_changed ()
5966 if (soloing() || listening()) {
5968 /* We can't use ::clear_all_solo_state() here because during
5969 session loading at program startup, that will queue a call
5970 to rt_clear_all_solo_state() that will not execute until
5971 AFTER solo states have been established (thus throwing away
5972 the session's saved solo state). So just explicitly turn
5975 set_controls (route_list_to_control_list (get_routes(), &Stripable::solo_control), 0.0, Controllable::NoGroup);
5977 clear_all_solo_state (get_routes());
5982 /** Called when a property of one of our route groups changes */
5984 Session::route_group_property_changed (RouteGroup* rg)
5986 RouteGroupPropertyChanged (rg); /* EMIT SIGNAL */
5989 /** Called when a route is added to one of our route groups */
5991 Session::route_added_to_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
5993 RouteAddedToRouteGroup (rg, r);
5996 /** Called when a route is removed from one of our route groups */
5998 Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
6000 update_route_record_state ();
6001 RouteRemovedFromRouteGroup (rg, r); /* EMIT SIGNAL */
6003 if (!rg->has_control_master () && !rg->has_subgroup () && rg->empty()) {
6004 remove_route_group (*rg);
6008 boost::shared_ptr<AudioTrack>
6009 Session::get_nth_audio_track (int nth) const
6011 boost::shared_ptr<RouteList> rl = routes.reader ();
6012 rl->sort (Stripable::Sorter ());
6014 for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
6015 if (!boost::dynamic_pointer_cast<AudioTrack> (*r)) {
6022 return boost::dynamic_pointer_cast<AudioTrack> (*r);
6024 return boost::shared_ptr<AudioTrack> ();
6027 boost::shared_ptr<RouteList>
6028 Session::get_tracks () const
6030 boost::shared_ptr<RouteList> rl = routes.reader ();
6031 boost::shared_ptr<RouteList> tl (new RouteList);
6033 for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
6034 if (boost::dynamic_pointer_cast<Track> (*r)) {
6035 assert (!(*r)->is_auditioner()); // XXX remove me
6042 boost::shared_ptr<RouteList>
6043 Session::get_routes_with_regions_at (samplepos_t const p) const
6045 boost::shared_ptr<RouteList> r = routes.reader ();
6046 boost::shared_ptr<RouteList> rl (new RouteList);
6048 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6049 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
6054 boost::shared_ptr<Playlist> pl = tr->playlist ();
6059 if (pl->has_region_at (p)) {
6068 Session::goto_end ()
6070 if (_session_range_location) {
6071 request_locate (_session_range_location->end(), false);
6073 request_locate (0, false);
6078 Session::goto_start (bool and_roll)
6080 if (_session_range_location) {
6081 request_locate (_session_range_location->start(), and_roll);
6083 request_locate (0, and_roll);
6088 Session::current_start_sample () const
6090 return _session_range_location ? _session_range_location->start() : 0;
6094 Session::current_end_sample () const
6096 return _session_range_location ? _session_range_location->end() : 0;
6100 Session::set_session_range_location (samplepos_t start, samplepos_t end)
6102 _session_range_location = new Location (*this, start, end, _("session"), Location::IsSessionRange, 0);
6103 _locations->add (_session_range_location);
6107 Session::step_edit_status_change (bool yn)
6113 send = (_step_editors == 0);
6118 send = (_step_editors == 1);
6121 if (_step_editors > 0) {
6127 StepEditStatusChange (val);
6133 Session::start_time_changed (samplepos_t old)
6135 /* Update the auto loop range to match the session range
6136 (unless the auto loop range has been changed by the user)
6139 Location* s = _locations->session_range_location ();
6144 Location* l = _locations->auto_loop_location ();
6146 if (l && l->start() == old) {
6147 l->set_start (s->start(), true);
6153 Session::end_time_changed (samplepos_t old)
6155 /* Update the auto loop range to match the session range
6156 (unless the auto loop range has been changed by the user)
6159 Location* s = _locations->session_range_location ();
6164 Location* l = _locations->auto_loop_location ();
6166 if (l && l->end() == old) {
6167 l->set_end (s->end(), true);
6172 std::vector<std::string>
6173 Session::source_search_path (DataType type) const
6177 if (session_dirs.size() == 1) {
6179 case DataType::AUDIO:
6180 sp.push_back (_session_dir->sound_path());
6182 case DataType::MIDI:
6183 sp.push_back (_session_dir->midi_path());
6187 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
6188 SessionDirectory sdir (i->path);
6190 case DataType::AUDIO:
6191 sp.push_back (sdir.sound_path());
6193 case DataType::MIDI:
6194 sp.push_back (sdir.midi_path());
6200 if (type == DataType::AUDIO) {
6201 const string sound_path_2X = _session_dir->sound_path_2X();
6202 if (Glib::file_test (sound_path_2X, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
6203 if (find (sp.begin(), sp.end(), sound_path_2X) == sp.end()) {
6204 sp.push_back (sound_path_2X);
6209 // now check the explicit (possibly user-specified) search path
6212 case DataType::AUDIO:
6213 sp += Searchpath(config.get_audio_search_path ());
6215 case DataType::MIDI:
6216 sp += Searchpath(config.get_midi_search_path ());
6224 Session::ensure_search_path_includes (const string& path, DataType type)
6233 case DataType::AUDIO:
6234 sp += Searchpath(config.get_audio_search_path ());
6236 case DataType::MIDI:
6237 sp += Searchpath (config.get_midi_search_path ());
6241 for (vector<std::string>::iterator i = sp.begin(); i != sp.end(); ++i) {
6242 /* No need to add this new directory if it has the same inode as
6243 an existing one; checking inode rather than name prevents duplicated
6244 directories when we are using symlinks.
6246 On Windows, I think we could just do if (*i == path) here.
6248 if (PBD::equivalent_paths (*i, path)) {
6256 case DataType::AUDIO:
6257 config.set_audio_search_path (sp.to_string());
6259 case DataType::MIDI:
6260 config.set_midi_search_path (sp.to_string());
6266 Session::remove_dir_from_search_path (const string& dir, DataType type)
6271 case DataType::AUDIO:
6272 sp = Searchpath(config.get_audio_search_path ());
6274 case DataType::MIDI:
6275 sp = Searchpath (config.get_midi_search_path ());
6282 case DataType::AUDIO:
6283 config.set_audio_search_path (sp.to_string());
6285 case DataType::MIDI:
6286 config.set_midi_search_path (sp.to_string());
6292 boost::shared_ptr<Speakers>
6293 Session::get_speakers()
6299 Session::unknown_processors () const
6303 boost::shared_ptr<RouteList> r = routes.reader ();
6304 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6305 list<string> t = (*i)->unknown_processors ();
6306 copy (t.begin(), t.end(), back_inserter (p));
6316 Session::set_worst_io_latencies_x (IOChange, void *)
6318 set_worst_io_latencies ();
6322 Session::send_latency_compensation_change ()
6324 /* As a result of Send::set_output_latency()
6325 * or InternalReturn::set_playback_offset ()
6326 * the send's own latency can change (source track
6327 * is aligned with target bus).
6329 * This can only happen be triggered by
6330 * Route::update_signal_latency ()
6331 * when updating the processor latency.
6333 * We need to walk the graph again to take those changes into account
6334 * (we should probably recurse or process the graph in a 2 step process).
6336 ++_send_latency_changes;
6340 Session::update_route_latency (bool playback, bool apply_to_delayline)
6342 /* Note: RouteList is process-graph sorted */
6343 boost::shared_ptr<RouteList> r = routes.reader ();
6346 /* reverse the list so that we work backwards from the last route to run to the first,
6347 * this is not needed, but can help to reduce the iterations for aux-sends.
6349 RouteList* rl = routes.reader().get();
6350 r.reset (new RouteList (*rl));
6351 reverse (r->begin(), r->end());
6354 bool changed = false;
6357 _send_latency_changes = 0;
6358 _worst_route_latency = 0;
6360 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6361 // if (!(*i)->active()) { continue ; } // TODO
6363 if ((*i)->signal_latency () != (l = (*i)->update_signal_latency (apply_to_delayline))) {
6366 _worst_route_latency = std::max (l, _worst_route_latency);
6369 if (_send_latency_changes > 0) {
6370 // only 1 extra iteration is needed (we allow only 1 level of aux-sends)
6371 // BUT.. jack'n'sends'n'bugs
6372 if (++bailout < 5) {
6373 cerr << "restarting Session::update_latency. # of send changes: " << _send_latency_changes << " iteration: " << bailout << endl;
6378 DEBUG_TRACE (DEBUG::Latency, string_compose ("worst signal processing latency: %1 (changed ? %2)\n", _worst_route_latency, (changed ? "yes" : "no")));
6384 Session::update_latency (bool playback)
6386 /* called only from AudioEngine::latency_callback.
6387 * but may indirectly be triggered from
6388 * Session::update_latency_compensation -> _engine.update_latencies
6390 DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Engine latency callback: %1\n", (playback ? "PLAYBACK" : "CAPTURE")));
6392 if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _route_deletion_in_progress) {
6395 if (!_engine.running()) {
6399 /* Note; RouteList is sorted as process-graph */
6400 boost::shared_ptr<RouteList> r = routes.reader ();
6403 /* reverse the list so that we work backwards from the last route to run to the first */
6404 RouteList* rl = routes.reader().get();
6405 r.reset (new RouteList (*rl));
6406 reverse (r->begin(), r->end());
6409 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6410 samplecnt_t latency = (*i)->set_private_port_latencies (playback);
6411 (*i)->set_public_port_latencies (latency, playback);
6415 Glib::Threads::Mutex::Lock lx (_update_latency_lock);
6416 set_worst_output_latency ();
6417 update_route_latency (true, true);
6419 Glib::Threads::Mutex::Lock lx (_update_latency_lock);
6420 set_worst_input_latency ();
6421 update_route_latency (false, false);
6424 DEBUG_TRACE (DEBUG::LatencyCompensation, "Engine latency callback: DONE\n");
6425 LatencyUpdated (); /* EMIT SIGNAL */
6429 Session::set_worst_io_latencies ()
6431 DEBUG_TRACE (DEBUG::LatencyCompensation, "Session::set_worst_io_latencies\n");
6432 Glib::Threads::Mutex::Lock lx (_update_latency_lock);
6433 set_worst_output_latency ();
6434 set_worst_input_latency ();
6438 Session::set_worst_output_latency ()
6440 if (inital_connect_or_deletion_in_progress ()) {
6444 _worst_output_latency = 0;
6446 if (!_engine.running()) {
6450 boost::shared_ptr<RouteList> r = routes.reader ();
6452 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6453 _worst_output_latency = max (_worst_output_latency, (*i)->output()->latency());
6456 _worst_output_latency = max (_worst_output_latency, _click_io->latency());
6458 DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Worst output latency: %1\n", _worst_output_latency));
6462 Session::set_worst_input_latency ()
6464 if (inital_connect_or_deletion_in_progress ()) {
6468 _worst_input_latency = 0;
6470 if (!_engine.running()) {
6474 boost::shared_ptr<RouteList> r = routes.reader ();
6476 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6477 _worst_input_latency = max (_worst_input_latency, (*i)->input()->latency());
6480 DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Worst input latency: %1\n", _worst_input_latency));
6484 Session::update_latency_compensation (bool force_whole_graph)
6486 if (inital_connect_or_deletion_in_progress ()) {
6489 /* this lock is not usually contended, but under certain conditions,
6490 * update_latency_compensation may be called concurrently.
6491 * e.g. drag/drop copy a latent plugin while rolling.
6492 * GUI thread (via route_processors_changed) and
6493 * auto_connect_thread_run may race.
6495 Glib::Threads::Mutex::Lock lx (_update_latency_lock, Glib::Threads::TRY_LOCK);
6497 /* no need to do this twice */
6501 DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("update_latency_compensation %1\n", (force_whole_graph ? "of whole graph" : "")));
6503 bool some_track_latency_changed = update_route_latency (false, false);
6505 if (some_track_latency_changed || force_whole_graph) {
6506 DEBUG_TRACE (DEBUG::LatencyCompensation, "update_latency_compensation: delegate to engine\n");
6507 _engine.update_latencies ();
6508 /* above call will ask the backend up update its latencies, which
6509 * eventually will trigger AudioEngine::latency_callback () and
6510 * call Session::update_latency ()
6513 DEBUG_TRACE (DEBUG::LatencyCompensation, "update_latency_compensation: directly apply to routes\n");
6514 boost::shared_ptr<RouteList> r = routes.reader ();
6515 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6516 (*i)->apply_latency_compensation ();
6519 DEBUG_TRACE (DEBUG::LatencyCompensation, "update_latency_compensation: DONE\n");
6523 Session::session_name_is_legal (const string& path)
6525 char illegal_chars[] = { '/', '\\', ':', ';', '\0' };
6527 for (int i = 0; illegal_chars[i]; ++i) {
6528 if (path.find (illegal_chars[i]) != string::npos) {
6529 return illegal_chars[i];
6537 Session::notify_presentation_info_change ()
6539 if (deletion_in_progress()) {
6543 reassign_track_numbers();
6547 Session::operation_in_progress (GQuark op) const
6549 return (find (_current_trans_quarks.begin(), _current_trans_quarks.end(), op) != _current_trans_quarks.end());
6552 boost::shared_ptr<Port>
6553 Session::ltc_output_port () const
6555 return _ltc_output ? _ltc_output->nth (0) : boost::shared_ptr<Port> ();
6559 Session::reconnect_ltc_output ()
6563 string src = Config->get_ltc_output_port();
6565 _ltc_output->disconnect (this);
6567 if (src != _("None") && !src.empty()) {
6568 _ltc_output->nth (0)->connect (src);
6574 Session::set_range_selection (samplepos_t start, samplepos_t end)
6576 _range_selection = Evoral::Range<samplepos_t> (start, end);
6580 Session::set_object_selection (samplepos_t start, samplepos_t end)
6582 _object_selection = Evoral::Range<samplepos_t> (start, end);
6586 Session::clear_range_selection ()
6588 _range_selection = Evoral::Range<samplepos_t> (-1,-1);
6592 Session::clear_object_selection ()
6594 _object_selection = Evoral::Range<samplepos_t> (-1,-1);
6598 Session::auto_connect_route (boost::shared_ptr<Route> route, bool connect_inputs,
6599 const ChanCount& input_start,
6600 const ChanCount& output_start,
6601 const ChanCount& input_offset,
6602 const ChanCount& output_offset)
6604 Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6605 _auto_connect_queue.push (AutoConnectRequest (route, connect_inputs,
6606 input_start, output_start,
6607 input_offset, output_offset));
6609 auto_connect_thread_wakeup ();
6613 Session::auto_connect_thread_wakeup ()
6615 if (pthread_mutex_trylock (&_auto_connect_mutex) == 0) {
6616 pthread_cond_signal (&_auto_connect_cond);
6617 pthread_mutex_unlock (&_auto_connect_mutex);
6622 Session::queue_latency_recompute ()
6624 g_atomic_int_inc (&_latency_recompute_pending);
6625 auto_connect_thread_wakeup ();
6629 Session::auto_connect (const AutoConnectRequest& ar)
6631 boost::shared_ptr<Route> route = ar.route.lock();
6633 if (!route) { return; }
6635 if (!IO::connecting_legal) {
6639 /* If both inputs and outputs are auto-connected to physical ports,
6640 * use the max of input and output offsets to ensure auto-connected
6641 * port numbers always match up (e.g. the first audio input and the
6642 * first audio output of the route will have the same physical
6643 * port number). Otherwise just use the lowest input or output
6647 const bool in_out_physical =
6648 (Config->get_input_auto_connect() & AutoConnectPhysical)
6649 && (Config->get_output_auto_connect() & AutoConnectPhysical)
6650 && ar.connect_inputs;
6652 const ChanCount in_offset = in_out_physical
6653 ? ChanCount::max(ar.input_offset, ar.output_offset)
6656 const ChanCount out_offset = in_out_physical
6657 ? ChanCount::max(ar.input_offset, ar.output_offset)
6660 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
6661 vector<string> physinputs;
6662 vector<string> physoutputs;
6665 /* for connecting track inputs we only want MIDI ports marked
6669 get_physical_ports (physinputs, physoutputs, *t, MidiPortMusic);
6671 if (!physinputs.empty() && ar.connect_inputs) {
6672 uint32_t nphysical_in = physinputs.size();
6674 for (uint32_t i = ar.input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) {
6677 if (Config->get_input_auto_connect() & AutoConnectPhysical) {
6678 port = physinputs[(in_offset.get(*t) + i) % nphysical_in];
6681 if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) {
6687 if (!physoutputs.empty()) {
6688 uint32_t nphysical_out = physoutputs.size();
6689 for (uint32_t i = ar.output_start.get(*t); i < route->n_outputs().get(*t); ++i) {
6692 if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) {
6693 port = physoutputs[(out_offset.get(*t) + i) % nphysical_out];
6694 } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) {
6695 /* master bus is audio only */
6696 if (_master_out && _master_out->n_inputs().get(*t) > 0) {
6697 port = _master_out->input()->ports().port(*t,
6698 i % _master_out->input()->n_ports().get(*t))->name();
6702 if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) {
6711 Session::auto_connect_thread_start ()
6713 if (g_atomic_int_get (&_ac_thread_active)) {
6717 while (!_auto_connect_queue.empty ()) {
6718 _auto_connect_queue.pop ();
6721 g_atomic_int_set (&_ac_thread_active, 1);
6722 if (pthread_create (&_auto_connect_thread, NULL, auto_connect_thread, this)) {
6723 g_atomic_int_set (&_ac_thread_active, 0);
6728 Session::auto_connect_thread_terminate ()
6730 if (!g_atomic_int_get (&_ac_thread_active)) {
6735 Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6736 while (!_auto_connect_queue.empty ()) {
6737 _auto_connect_queue.pop ();
6741 /* cannot use auto_connect_thread_wakeup() because that is allowed to
6742 * fail to wakeup the thread.
6745 pthread_mutex_lock (&_auto_connect_mutex);
6746 g_atomic_int_set (&_ac_thread_active, 0);
6747 pthread_cond_signal (&_auto_connect_cond);
6748 pthread_mutex_unlock (&_auto_connect_mutex);
6751 pthread_join (_auto_connect_thread, &status);
6755 Session::auto_connect_thread (void *arg)
6757 Session *s = static_cast<Session *>(arg);
6758 s->auto_connect_thread_run ();
6764 Session::auto_connect_thread_run ()
6766 pthread_set_name (X_("autoconnect"));
6767 SessionEvent::create_per_thread_pool (X_("autoconnect"), 1024);
6768 PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("autoconnect"), 1024);
6769 pthread_mutex_lock (&_auto_connect_mutex);
6770 while (g_atomic_int_get (&_ac_thread_active)) {
6772 if (!_auto_connect_queue.empty ()) {
6773 // Why would we need the process lock ??
6774 // A: if ports are added while we're connecting, the backend's iterator may be invalidated:
6775 // graph_order_callback() -> resort_routes() -> direct_feeds_according_to_reality () -> backend::connected_to()
6776 // All ardour-internal backends use a std::vector xxxAudioBackend::find_port()
6777 // We have control over those, but what does jack do?
6778 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6780 Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6781 while (!_auto_connect_queue.empty ()) {
6782 const AutoConnectRequest ar (_auto_connect_queue.front());
6783 _auto_connect_queue.pop ();
6790 if (!actively_recording ()) { // might not be needed,
6791 /* this is only used for updating plugin latencies, the
6792 * graph does not change. so it's safe in general.
6794 * update_latency_compensation ()
6795 * calls DiskWriter::set_capture_offset () which
6796 * modifies the capture-offset, which can be a problem.
6798 while (g_atomic_int_and (&_latency_recompute_pending, 0)) {
6799 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6800 update_latency_compensation ();
6805 // this may call ARDOUR::Port::drop ... jack_port_unregister ()
6806 // jack1 cannot cope with removing ports while processing
6807 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6808 AudioEngine::instance()->clear_pending_port_deletions ();
6811 pthread_cond_wait (&_auto_connect_cond, &_auto_connect_mutex);
6813 pthread_mutex_unlock (&_auto_connect_mutex);
6817 Session::cancel_all_solo ()
6821 get_stripables (sl);
6823 set_controls (stripable_list_to_control_list (sl, &Stripable::solo_control), 0.0, Controllable::NoGroup);
6824 clear_all_solo_state (routes.reader());
6828 Session::maybe_update_tempo_from_midiclock_tempo (float bpm)
6830 if (_tempo_map->n_tempos() == 1) {
6831 TempoSection& ts (_tempo_map->tempo_section_at_sample (0));
6832 if (fabs (ts.note_types_per_minute() - bpm) > (0.01 * ts.note_types_per_minute())) {
6833 const Tempo tempo (bpm, 4.0, bpm);
6834 std::cerr << "new tempo " << bpm << " old " << ts.note_types_per_minute() << std::endl;
6835 _tempo_map->replace_tempo (ts, tempo, 0.0, 0.0, AudioTime);