move reset (deletion) of click_io and ltc_output objects until after we are disconnec...
[ardour.git] / libs / ardour / session.cc
1 /*
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>
14  *
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.
19  *
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.
24  *
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.
28  */
29
30 #include <stdint.h>
31
32 #include <algorithm>
33 #include <string>
34 #include <vector>
35 #include <sstream>
36 #include <cstdio> /* sprintf(3) ... grrr */
37 #include <cmath>
38 #include <cerrno>
39 #include <unistd.h>
40 #include <limits.h>
41
42 #include <glibmm/threads.h>
43 #include <glibmm/miscutils.h>
44 #include <glibmm/fileutils.h>
45
46 #include <boost/algorithm/string/erase.hpp>
47
48 #include "pbd/basename.h"
49 #include "pbd/convert.h"
50 #include "pbd/error.h"
51 #include "pbd/file_utils.h"
52 #include "pbd/i18n.h"
53 #include "pbd/md5.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"
61
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"
126
127 #include "midi++/port.h"
128 #include "midi++/mmc.h"
129
130 #include "LuaBridge/LuaBridge.h"
131
132 #include <glibmm/checksum.h>
133
134 namespace ARDOUR {
135 class MidiSource;
136 class Processor;
137 class Speakers;
138 }
139
140 using namespace std;
141 using namespace ARDOUR;
142 using namespace PBD;
143
144 bool Session::_disable_all_loaded_plugins = false;
145 bool Session::_bypass_all_loaded_plugins = false;
146 guint Session::_name_id_counter = 0;
147
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;
154
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;
163
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;
168
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,
174                   string mix_template)
175         : _playlists (new SessionPlaylists)
176         , _engine (eng)
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)
185         , _seek_counter (0)
186         , _session_range_location (0)
187         , _session_range_is_free (true)
188         , _silent (false)
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)
205         , _listening (false)
206         , _listen_cnt (0)
207         , _solo_isolated_cnt (0)
208         , _writable (false)
209         , _was_seamless (Config->get_seamless_loop ())
210         , _under_nsm_control (false)
211         , _xrun_count (0)
212         , transport_master_tracking_state (Stopped)
213         , master_wait_end (0)
214         , post_export_sync (false)
215         , post_export_position (0)
216         , _exporting (false)
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)
223         , _is_new (true)
224         , _send_qf_mtc (false)
225         , _pframes_since_last_mtc (0)
226         , play_loop (false)
227         , loop_changing (false)
228         , last_loopend (0)
229         , _session_dir (new SessionDirectory (fullpath))
230         , _current_snapshot_name (snapshot_name)
231         , state_tree (0)
232         , state_was_pending (false)
233         , _state_of_the_state (StateOfTheState (CannotSave | InitialConnecting | Loading))
234         , _suspend_save (0)
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))
242         , _n_lua_scripts (0)
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)
252         , step_speed (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)
261         , ltc_encoder (0)
262         , ltc_enc_buf(0)
263         , ltc_buf_off (0)
264         , ltc_buf_len (0)
265         , ltc_speed (0)
266         , ltc_enc_byte (0)
267         , ltc_enc_pos (0)
268         , ltc_enc_cnt (0)
269         , ltc_enc_off (0)
270         , restarting (false)
271         , ltc_prev_cycle (0)
272         , ltc_timecode_offset (0)
273         , ltc_timecode_negative_offset (false)
274         , midi_control_ui (0)
275         , _tempo_map (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)
288         , _playback_load (0)
289         , _capture_load (0)
290         , _bundles (new BundleList)
291         , _bundle_xml_node (0)
292         , _current_trans (0)
293         , _clicking (false)
294         , _click_rec_only (false)
295         , click_data (0)
296         , click_emphasis_data (0)
297         , click_length (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)
306         , main_outs (0)
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)
312         , _step_editors (0)
313         , _suspend_timecode_transmission (0)
314         ,  _speakers (new Speakers)
315         , _ignore_route_processor_changes (0)
316         , midi_clock (0)
317         , _scene_changer (0)
318         , _midi_ports (0)
319         , _mmc (0)
320         , _vca_manager (new VCAManager (*this))
321         , _selection (new CoreSelection (*this))
322         , _global_locate_pending (false)
323 {
324         created_with = string_compose ("%1 %2", PROGRAM_NAME, revision);
325
326         pthread_mutex_init (&_rt_emit_mutex, 0);
327         pthread_cond_init (&_rt_emit_cond, 0);
328
329         pthread_mutex_init (&_auto_connect_mutex, 0);
330         pthread_cond_init (&_auto_connect_cond, 0);
331
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
334
335         pre_engine_init (fullpath); // sets _is_new
336
337         setup_lua ();
338
339         assert (AudioEngine::instance()->running());
340         immediately_post_engine ();
341
342         if (_is_new) {
343
344                 Stateful::loading_state_version = CURRENT_SESSION_FILE_VERSION;
345
346                 if (create (mix_template, bus_profile)) {
347                         destroy ();
348                         throw SessionException (_("Session initialization failed"));
349                 }
350
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.
355                  *
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
359                  * of a template.
360                  */
361
362                 if (!mix_template.empty()) {
363                         try {
364                                 if (load_state (_current_snapshot_name)) {
365                                         throw SessionException (_("Failed to load template/snapshot state"));
366                                 }
367                         } catch (PBD::unknown_enumeration& e) {
368                                 throw SessionException (_("Failed to parse template/snapshot state"));
369                         }
370                         store_recent_templates (mix_template);
371                 }
372
373                 /* load default session properties - if any */
374                 config.load_state();
375
376         } else {
377
378                 if (load_state (_current_snapshot_name)) {
379                         throw SessionException (_("Failed to load state"));
380                 }
381         }
382
383         int err = post_engine_init ();
384
385         if (err) {
386                 destroy ();
387                 switch (err) {
388                         case -1:
389                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Failed to create background threads.")));
390                                 break;
391                         case -2:
392                         case -3:
393                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid TempoMap in session-file.")));
394                                 break;
395                         case -4:
396                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid or corrupt session state.")));
397                                 break;
398                         case -5:
399                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Port registration failed.")));
400                                 break;
401                         default:
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")));
403                                 break;
404                 }
405         }
406
407         store_recent_sessions (_name, _path);
408
409         bool was_dirty = dirty();
410         unset_dirty ();
411
412         PresentationInfo::Change.connect_same_thread (*this, boost::bind (&Session::notify_presentation_info_change, this));
413
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));
416
417         if (was_dirty) {
418                 DirtyChanged (); /* EMIT SIGNAL */
419         }
420
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));
423
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));
426
427         emit_thread_start ();
428         auto_connect_thread_start ();
429
430         /* hook us up to the engine since we are now completely constructed */
431
432         BootMessage (_("Connect to engine"));
433
434         _engine.set_session (this);
435         _engine.reset_timebase ();
436
437         ensure_subdirs (); // archived or zipped sessions may lack peaks/ analysis/ etc
438
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).
443                  */
444                 _is_new = true;
445         }
446
447         session_loaded ();
448         _is_new = false;
449
450         BootMessage (_("Session loading complete"));
451 }
452
453 Session::~Session ()
454 {
455 #ifdef PT_TIMING
456         ST.dump ("ST.dump");
457 #endif
458         destroy ();
459 }
460
461 unsigned int
462 Session::next_name_id ()
463 {
464         return g_atomic_int_add (&_name_id_counter, 1);
465 }
466
467 unsigned int
468 Session::name_id_counter ()
469 {
470         return g_atomic_int_get (&_name_id_counter);
471 }
472
473 void
474 Session::init_name_id_counter (guint n)
475 {
476         g_atomic_int_set (&_name_id_counter, n);
477 }
478
479 int
480 Session::immediately_post_engine ()
481 {
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.
485          */
486
487         _rt_tasklist.reset (new RTTaskList ());
488
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.
492                 */
493                 _process_graph.reset (new Graph (*this));
494         }
495
496         /* Restart transport FSM */
497
498         _transport_fsm->start ();
499
500         /* every time we reconnect, do stuff ... */
501
502         _engine.Running.connect_same_thread (*this, boost::bind (&Session::engine_running, this));
503
504         if (synced_to_engine()) {
505                 _engine.transport_stop ();
506         }
507
508         if (config.get_jack_time_master()) {
509                 _engine.transport_locate (_transport_sample);
510         }
511
512         try {
513                 BootMessage (_("Set up LTC"));
514                 setup_ltc ();
515                 BootMessage (_("Set up Click"));
516                 setup_click ();
517                 BootMessage (_("Set up standard connections"));
518                 setup_bundles ();
519         }
520
521         catch (failed_constructor& err) {
522                 return -1;
523         }
524
525         /* TODO, connect in different thread. (PortRegisteredOrUnregistered may be in RT context)
526          * can we do that? */
527          _engine.PortRegisteredOrUnregistered.connect_same_thread (*this, boost::bind (&Session::setup_bundles, this));
528
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());
533
534         return 0;
535 }
536
537 void
538 Session::destroy ()
539 {
540         vector<void*> debug_pointers;
541
542         /* if we got to here, leaving pending capture state around
543            is a mistake.
544         */
545
546         remove_pending_capture_state ();
547
548         Analyser::flush ();
549
550         _state_of_the_state = StateOfTheState (CannotSave | Deletion);
551
552         {
553                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
554                 ltc_tx_cleanup();
555         }
556
557         /* disconnect from any and all signals that we are connected to */
558
559         Port::PortSignalDrop (); /* EMIT SIGNAL */
560         drop_connections ();
561
562         /* shutdown control surface protocols while we still have ports
563          * and the engine to move data to any devices.
564          */
565         ControlProtocolManager::instance().drop_protocols ();
566
567         /* stop auto dis/connecting */
568         auto_connect_thread_terminate ();
569
570         MIDI::Name::MidiPatchManager::instance().remove_search_path(session_directory().midi_patch_path());
571
572         _engine.remove_session ();
573
574         /* deregister all ports - there will be no process or any other
575          * callbacks from the engine any more.
576          */
577
578         Port::PortDrop (); /* EMIT SIGNAL */
579
580         /* remove I/O objects that we (the session) own */
581         _click_io.reset ();
582         _ltc_output.reset ();
583
584         {
585                 Glib::Threads::Mutex::Lock lm (controllables_lock);
586                 for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
587                         (*i)->DropReferences (); /* EMIT SIGNAL */
588                 }
589                 controllables.clear ();
590         }
591
592         /* clear history so that no references to objects are held any more */
593
594         _history.clear ();
595
596         /* clear state tree so that no references to objects are held any more */
597
598         delete state_tree;
599         state_tree = 0;
600
601         {
602                 /* unregister all lua functions, drop held references (if any) */
603                 Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
604                 (*_lua_cleanup)();
605                 lua.do_command ("Session = nil");
606                 delete _lua_run;
607                 delete _lua_add;
608                 delete _lua_del;
609                 delete _lua_list;
610                 delete _lua_save;
611                 delete _lua_load;
612                 delete _lua_cleanup;
613                 lua.collect_garbage ();
614         }
615
616         /* reset dynamic state version back to default */
617         Stateful::loading_state_version = 0;
618
619         _butler->drop_references ();
620         delete _butler;
621         _butler = 0;
622
623         delete _all_route_group;
624
625         DEBUG_TRACE (DEBUG::Destruction, "delete route groups\n");
626         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
627                 delete *i;
628         }
629
630         if (click_data != default_click) {
631                 delete [] click_data;
632         }
633
634         if (click_emphasis_data != default_click_emphasis) {
635                 delete [] click_emphasis_data;
636         }
637
638         clear_clicks ();
639
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.
644          */
645         if (auditioner) {
646                 auditioner->drop_references ();
647         }
648         auditioner.reset ();
649
650         /* drop references to routes held by the monitoring section
651          * specifically _monitor_out aux/listen references */
652         remove_monitor_section();
653
654         /* clear out any pending dead wood from RCU managed objects */
655
656         routes.flush ();
657         _bundles.flush ();
658
659         DiskReader::free_working_buffers();
660
661         /* tell everyone who is still standing that we're about to die */
662         drop_references ();
663
664         /* tell everyone to drop references and delete objects as we go */
665
666         DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
667         RegionFactory::delete_all_regions ();
668
669         /* Do this early so that VCAs no longer hold references to routes */
670
671         DEBUG_TRACE (DEBUG::Destruction, "delete vcas\n");
672         delete _vca_manager;
673
674         DEBUG_TRACE (DEBUG::Destruction, "delete routes\n");
675
676         /* reset these three references to special routes before we do the usual route delete thing */
677
678         _master_out.reset ();
679         _monitor_out.reset ();
680
681         {
682                 RCUWriter<RouteList> writer (routes);
683                 boost::shared_ptr<RouteList> r = writer.get_copy ();
684
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 ();
688                 }
689
690                 r->clear ();
691                 /* writer goes out of scope and updates master */
692         }
693         routes.flush ();
694
695         {
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 ();
701                 }
702
703                 sources.clear ();
704         }
705
706         /* not strictly necessary, but doing it here allows the shared_ptr debugging to work */
707         _playlists.reset ();
708
709         emit_thread_terminate ();
710
711         pthread_cond_destroy (&_rt_emit_cond);
712         pthread_mutex_destroy (&_rt_emit_mutex);
713
714         pthread_cond_destroy (&_auto_connect_cond);
715         pthread_mutex_destroy (&_auto_connect_mutex);
716
717         delete _scene_changer; _scene_changer = 0;
718         delete midi_control_ui; midi_control_ui = 0;
719
720         delete _mmc; _mmc = 0;
721         delete _midi_ports; _midi_ports = 0;
722         delete _locations; _locations = 0;
723
724         delete midi_clock;
725         delete _tempo_map;
726
727         /* clear event queue, the session is gone, nobody is interested in
728          * those anymore, but they do leak memory if not removed
729          */
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 ();
735                 bool remove = true;
736                 bool del = true;
737                 switch (ev->type) {
738                         case SessionEvent::AutoLoop:
739                         case SessionEvent::Skip:
740                         case SessionEvent::PunchIn:
741                         case SessionEvent::PunchOut:
742                         case SessionEvent::RangeStop:
743                         case SessionEvent::RangeLocate:
744                                 remove = false;
745                                 del = false;
746                                 break;
747                         case SessionEvent::RealTimeOperation:
748                                 process_rtop (ev);
749                                 del = false;
750                         default:
751                                 break;
752                 }
753                 if (remove) {
754                         del = del && !_remove_event (ev);
755                 }
756                 if (del) {
757                         delete ev;
758                 }
759         }
760
761         {
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 ();
767         }
768
769         DEBUG_TRACE (DEBUG::Destruction, "delete selection\n");
770         delete _selection;
771         _selection = 0;
772
773         _transport_fsm->stop ();
774
775         DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n");
776
777 #ifndef NDEBUG
778         Controllable::dump_registry ();
779 #endif
780
781         BOOST_SHOW_POINTERS ();
782 }
783
784 void
785 Session::setup_ltc ()
786 {
787         XMLNode* child = 0;
788
789         _ltc_output.reset (new IO (*this, X_("LTC Out"), IO::Output));
790
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);
793         } else {
794                 {
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 ();
799                 }
800         }
801
802         /* fix up names of LTC ports because we don't want the normal
803          * IO style of NAME/TYPE-{in,out}N
804          */
805
806         _ltc_output->nth (0)->set_name (X_("LTC-out"));
807 }
808
809 void
810 Session::setup_click ()
811 {
812         _clicking = false;
813
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));
816
817         _click_io.reset (new ClickIO (*this, X_("Click")));
818         _click_gain.reset (new Amp (*this, _("Fader"), gain_control, true));
819         _click_gain->activate ();
820         if (state_tree) {
821                 setup_click_state (state_tree->root());
822         } else {
823                 setup_click_state (0);
824         }
825 }
826
827 void
828 Session::setup_click_state (const XMLNode* node)
829 {
830         const XMLNode* child = 0;
831
832         if (node && (child = find_named_node (*node, "Click")) != 0) {
833
834                 /* existing state for Click */
835                 int c = 0;
836
837                 if (Stateful::loading_state_version < 3000) {
838                         c = _click_io->set_state_2X (*child->children().front(), Stateful::loading_state_version, false);
839                 } else {
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) {
843                                 ++i;
844                                 if (i != children.end()) {
845                                         c = _click_gain->set_state (**i, Stateful::loading_state_version);
846                                 }
847                         }
848                 }
849
850                 if (c == 0) {
851                         _clicking = Config->get_clicking ();
852
853                 } else {
854
855                         error << _("could not setup Click I/O") << endmsg;
856                         _clicking = false;
857                 }
858
859
860         } else {
861
862                 /* default state for Click: dual-mono to first 2 physical outputs */
863
864                 vector<string> outs;
865                 _engine.get_physical_outputs (DataType::AUDIO, outs);
866
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
871                                 }
872                         }
873                 }
874
875                 if (_click_io->n_ports () > ChanCount::ZERO) {
876                         _clicking = Config->get_clicking ();
877                 }
878         }
879 }
880
881 void
882 Session::get_physical_ports (vector<string>& inputs, vector<string>& outputs, DataType type,
883                              MidiPortFlags include, MidiPortFlags exclude)
884 {
885         _engine.get_physical_inputs (type, inputs, include, exclude);
886         _engine.get_physical_outputs (type, outputs, include, exclude);
887 }
888
889
890 void
891 Session::auto_connect_master_bus ()
892 {
893         if (!_master_out || !Config->get_auto_connect_standard_busses() || _monitor_out) {
894                 return;
895         }
896
897         /* if requested auto-connect the outputs to the first N physical ports.
898          */
899
900         uint32_t limit = _master_out->n_outputs().n_total();
901         vector<string> outputs[DataType::num_types];
902
903         for (uint32_t i = 0; i < DataType::num_types; ++i) {
904                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
905         }
906
907         for (uint32_t n = 0; n < limit; ++n) {
908                 boost::shared_ptr<Port> p = _master_out->output()->nth (n);
909                 string connect_to;
910                 if (outputs[p->type()].size() > n) {
911                         connect_to = outputs[p->type()][n];
912                 }
913
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)
917                                       << endmsg;
918                                 break;
919                         }
920                 }
921         }
922 }
923
924 void
925 Session::remove_monitor_section ()
926 {
927         if (!_monitor_out) {
928                 return;
929         }
930
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;
934                 return;
935         }
936
937         /* force reversion to Solo-In-Place */
938         Config->set_solo_control_is_listen_control (false);
939
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)
943         */
944         cancel_audition ();
945
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.
949                  */
950
951                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
952
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.
956                 */
957
958
959                 boost::shared_ptr<RouteList> r = routes.reader ();
960                 ProcessorChangeBlocker  pcb (this, false);
961
962                 for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
963
964                         if ((*x)->is_monitor()) {
965                                 /* relax */
966                         } else if ((*x)->is_master()) {
967                                 /* relax */
968                         } else {
969                                 (*x)->remove_aux_or_listen (_monitor_out);
970                         }
971                 }
972         }
973
974         remove_route (_monitor_out);
975         if (deletion_in_progress ()) {
976                 return;
977         }
978
979         auto_connect_master_bus ();
980
981         if (auditioner) {
982                 auditioner->connect ();
983         }
984
985         MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
986 }
987
988 void
989 Session::add_monitor_section ()
990 {
991         RouteList rl;
992
993         if (!_engine.running()) {
994                 error << _("Cannot create monitor section while the engine is offline.") << endmsg;
995                 return;
996         }
997
998         if (_monitor_out || !_master_out) {
999                 return;
1000         }
1001
1002         boost::shared_ptr<Route> r (new Route (*this, _("Monitor"), PresentationInfo::MonitorOut, DataType::AUDIO));
1003
1004         if (r->init ()) {
1005                 return;
1006         }
1007
1008         BOOST_MARK_ROUTE(r);
1009
1010         try {
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);
1014         } catch (...) {
1015                 error << _("Cannot create monitor section. 'Monitor' Port name is not unique.") << endmsg;
1016                 return;
1017         }
1018
1019         rl.push_back (r);
1020         add_routes (rl, false, false, false, 0);
1021
1022         assert (_monitor_out);
1023
1024         /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
1025            are undefined, at best.
1026         */
1027
1028         uint32_t limit = _monitor_out->n_inputs().n_audio();
1029
1030         if (_master_out) {
1031
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
1037                  * this, i think.
1038                  */
1039
1040                 _master_out->output()->disconnect (this);
1041
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);
1045
1046                         if (o) {
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)
1050                                               << endmsg;
1051                                         break;
1052                                 }
1053                         }
1054                 }
1055         }
1056
1057         /* if monitor section is not connected, connect it to physical outs
1058          */
1059
1060         if ((Config->get_auto_connect_standard_busses () || Profile->get_mixbus ()) && !_monitor_out->output()->connected ()) {
1061
1062                 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1063
1064                         boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1065
1066                         if (b) {
1067                                 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1068                         } else {
1069                                 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1070                                                            Config->get_monitor_bus_preferred_bundle())
1071                                         << endmsg;
1072                         }
1073
1074                 } else {
1075
1076                         /* Monitor bus is audio only */
1077
1078                         vector<string> outputs[DataType::num_types];
1079
1080                         for (uint32_t i = 0; i < DataType::num_types; ++i) {
1081                                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1082                         }
1083
1084                         uint32_t mod = outputs[DataType::AUDIO].size();
1085                         uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1086
1087                         if (mod != 0) {
1088
1089                                 for (uint32_t n = 0; n < limit; ++n) {
1090
1091                                         boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1092                                         string connect_to;
1093                                         if (outputs[DataType::AUDIO].size() > (n % mod)) {
1094                                                 connect_to = outputs[DataType::AUDIO][n % mod];
1095                                         }
1096
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"),
1101                                                                 n, connect_to)
1102                                                               << endmsg;
1103                                                         break;
1104                                                 }
1105                                         }
1106                                 }
1107                         }
1108                 }
1109         }
1110
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.
1113          */
1114
1115         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1116
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.
1120         */
1121
1122
1123         boost::shared_ptr<RouteList> rls = routes.reader ();
1124
1125         ProcessorChangeBlocker  pcb (this, false /* XXX */);
1126
1127         for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1128
1129                 if ((*x)->is_monitor()) {
1130                         /* relax */
1131                 } else if ((*x)->is_master()) {
1132                         /* relax */
1133                 } else {
1134                         (*x)->enable_monitor_send ();
1135                 }
1136         }
1137
1138         if (auditioner) {
1139                 auditioner->connect ();
1140         }
1141
1142         MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
1143 }
1144
1145 void
1146 Session::reset_monitor_section ()
1147 {
1148         /* Process lock should be held by the caller.*/
1149
1150         if (!_monitor_out) {
1151                 return;
1152         }
1153
1154         uint32_t limit = _master_out->n_outputs().n_audio();
1155
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
1161          * this, i think.
1162          */
1163
1164         _master_out->output()->disconnect (this);
1165         _monitor_out->output()->disconnect (this);
1166
1167         // monitor section follow master bus - except midi
1168         ChanCount mon_chn (_master_out->output()->n_ports());
1169         mon_chn.set_midi (0);
1170
1171         _monitor_out->input()->ensure_io (mon_chn, false, this);
1172         _monitor_out->output()->ensure_io (mon_chn, false, this);
1173
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);
1177
1178                 if (o) {
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)
1182                                       << endmsg;
1183                                 break;
1184                         }
1185                 }
1186         }
1187
1188         /* connect monitor section to physical outs
1189          */
1190
1191         if (Config->get_auto_connect_standard_busses()) {
1192
1193                 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1194
1195                         boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1196
1197                         if (b) {
1198                                 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1199                         } else {
1200                                 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1201                                                            Config->get_monitor_bus_preferred_bundle())
1202                                         << endmsg;
1203                         }
1204
1205                 } else {
1206
1207                         /* Monitor bus is audio only */
1208
1209                         vector<string> outputs[DataType::num_types];
1210
1211                         for (uint32_t i = 0; i < DataType::num_types; ++i) {
1212                                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1213                         }
1214
1215                         uint32_t mod = outputs[DataType::AUDIO].size();
1216                         uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1217
1218                         if (mod != 0) {
1219
1220                                 for (uint32_t n = 0; n < limit; ++n) {
1221
1222                                         boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1223                                         string connect_to;
1224                                         if (outputs[DataType::AUDIO].size() > (n % mod)) {
1225                                                 connect_to = outputs[DataType::AUDIO][n % mod];
1226                                         }
1227
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"),
1232                                                                 n, connect_to)
1233                                                               << endmsg;
1234                                                         break;
1235                                                 }
1236                                         }
1237                                 }
1238                         }
1239                 }
1240         }
1241
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.
1245         */
1246
1247
1248         boost::shared_ptr<RouteList> rls = routes.reader ();
1249
1250         ProcessorChangeBlocker pcb (this, false);
1251
1252         for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1253
1254                 if ((*x)->is_monitor()) {
1255                         /* relax */
1256                 } else if ((*x)->is_master()) {
1257                         /* relax */
1258                 } else {
1259                         (*x)->enable_monitor_send ();
1260                 }
1261         }
1262 }
1263
1264 int
1265 Session::add_master_bus (ChanCount const& count)
1266 {
1267         if (master_out ()) {
1268                 return -1;
1269         }
1270
1271         RouteList rl;
1272
1273         boost::shared_ptr<Route> r (new Route (*this, _("Master"), PresentationInfo::MasterOut, DataType::AUDIO));
1274         if (r->init ()) {
1275                 return -1;
1276         }
1277
1278         BOOST_MARK_ROUTE(r);
1279
1280         {
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);
1284         }
1285
1286         rl.push_back (r);
1287         add_routes (rl, false, false, false, PresentationInfo::max_order);
1288         return 0;
1289 }
1290
1291 void
1292 Session::hookup_io ()
1293 {
1294         /* stop graph reordering notifications from
1295            causing resorts, etc.
1296         */
1297
1298         _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
1299
1300         if (!auditioner) {
1301
1302                 /* we delay creating the auditioner till now because
1303                    it makes its own connections to ports.
1304                 */
1305
1306                 try {
1307                         boost::shared_ptr<Auditioner> a (new Auditioner (*this));
1308                         if (a->init()) {
1309                                 throw failed_constructor ();
1310                         }
1311                         auditioner = a;
1312                 }
1313
1314                 catch (failed_constructor& err) {
1315                         warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
1316                 }
1317         }
1318
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;
1323         }
1324
1325         /* Tell all IO objects to connect themselves together */
1326
1327         IO::enable_connecting ();
1328
1329         /* Now tell all "floating" ports to connect to whatever
1330            they should be connected to.
1331         */
1332
1333         AudioEngine::instance()->reconnect_ports ();
1334         TransportMasterManager::instance().reconnect_ports ();
1335
1336         /* Anyone who cares about input state, wake up and do something */
1337
1338         IOConnectionsComplete (); /* EMIT SIGNAL */
1339
1340         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
1341
1342         /* now handle the whole enchilada as if it was one
1343            graph reorder event.
1344         */
1345
1346         graph_reordered ();
1347
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
1351            has.
1352         */
1353
1354         update_route_solo_state ();
1355 }
1356
1357 void
1358 Session::track_playlist_changed (boost::weak_ptr<Track> wp)
1359 {
1360         boost::shared_ptr<Track> track = wp.lock ();
1361         if (!track) {
1362                 return;
1363         }
1364
1365         boost::shared_ptr<Playlist> playlist;
1366
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));
1371         }
1372 }
1373
1374 bool
1375 Session::record_enabling_legal () const
1376 {
1377         if (Config->get_all_safe()) {
1378                 return false;
1379         }
1380         return true;
1381 }
1382
1383 void
1384 Session::set_track_monitor_input_status (bool yn)
1385 {
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);
1392                 }
1393         }
1394 }
1395
1396 void
1397 Session::auto_punch_start_changed (Location* location)
1398 {
1399         replace_event (SessionEvent::PunchIn, location->start());
1400
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);
1404         }
1405 }
1406
1407 void
1408 Session::auto_punch_end_changed (Location* location)
1409 {
1410         replace_event (SessionEvent::PunchOut, location->end());
1411 }
1412
1413 void
1414 Session::auto_punch_changed (Location* location)
1415 {
1416         auto_punch_start_changed (location);
1417         auto_punch_end_changed (location);
1418 }
1419
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.
1423  */
1424 void
1425 Session::auto_loop_declick_range (Location* loc, samplepos_t & pos, samplepos_t & length)
1426 {
1427         pos = max (loc->start(), loc->end() - 64);
1428         length = loc->end() - pos;
1429 }
1430
1431 void
1432 Session::auto_loop_changed (Location* location)
1433 {
1434         replace_event (SessionEvent::AutoLoop, location->end(), location->start());
1435         samplepos_t dcp;
1436         samplecnt_t dcl;
1437         auto_loop_declick_range (location, dcp, dcl);
1438
1439         bool rolling = transport_rolling ();
1440
1441         if (rolling && play_loop) {
1442
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);
1447
1448                 }
1449         } else {
1450                 clear_events (SessionEvent::AutoLoop);
1451         }
1452
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.
1455          */
1456
1457         samplepos_t pos;
1458
1459         if (!rolling && select_playhead_priority_target (pos)) {
1460                 if (pos == location->start()) {
1461                         request_locate (pos);
1462                 }
1463         }
1464
1465         last_loopend = location->end();
1466         set_dirty ();
1467 }
1468
1469 void
1470 Session::set_auto_punch_location (Location* location)
1471 {
1472         Location* existing;
1473
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);
1480         }
1481
1482         set_dirty();
1483
1484         if (location == 0) {
1485                 return;
1486         }
1487
1488         if (location->end() <= location->start()) {
1489                 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1490                 return;
1491         }
1492
1493         punch_connections.drop_connections ();
1494
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));
1498
1499         location->set_auto_punch (true, this);
1500
1501         auto_punch_changed (location);
1502
1503         auto_punch_location_changed (location);
1504 }
1505
1506 void
1507 Session::set_session_extents (samplepos_t start, samplepos_t end)
1508 {
1509         Location* existing;
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);
1513         }
1514
1515         if (end <= start) {
1516                 error << _("Session: you can't use that location for session start/end)") << endmsg;
1517                 return;
1518         }
1519
1520         existing->set( start, end );
1521
1522         set_dirty();
1523 }
1524
1525 void
1526 Session::set_auto_loop_location (Location* location)
1527 {
1528         Location* existing;
1529
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);
1534                 samplepos_t dcp;
1535                 samplecnt_t dcl;
1536                 auto_loop_declick_range (existing, dcp, dcl);
1537                 auto_loop_location_changed (0);
1538         }
1539
1540         set_dirty();
1541
1542         if (location == 0) {
1543                 return;
1544         }
1545
1546         if (location->end() <= location->start()) {
1547                 error << _("You cannot use this location for auto-loop because it has zero or negative length") << endmsg;
1548                 return;
1549         }
1550
1551         last_loopend = location->end();
1552
1553         loop_connections.drop_connections ();
1554
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));
1559
1560         location->set_auto_loop (true, this);
1561
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);
1569                         }
1570                 }
1571         }
1572
1573         /* take care of our stuff first */
1574
1575         auto_loop_changed (location);
1576
1577         /* now tell everyone else */
1578
1579         auto_loop_location_changed (location);
1580 }
1581
1582 void
1583 Session::update_marks (Location*)
1584 {
1585         set_dirty ();
1586 }
1587
1588 void
1589 Session::update_skips (Location* loc, bool consolidate)
1590 {
1591         if (_ignore_skips_updates) {
1592                 return;
1593         }
1594
1595         Locations::LocationList skips;
1596
1597         if (consolidate) {
1598                 PBD::Unwinder<bool> uw (_ignore_skips_updates, true);
1599                 consolidate_skips (loc);
1600         }
1601
1602         sync_locations_to_skips ();
1603
1604         set_dirty ();
1605 }
1606
1607 void
1608 Session::consolidate_skips (Location* loc)
1609 {
1610         Locations::LocationList all_locations = _locations->list ();
1611
1612         for (Locations::LocationList::iterator l = all_locations.begin(); l != all_locations.end(); ) {
1613
1614                 if (!(*l)->is_skip ()) {
1615                         ++l;
1616                         continue;
1617                 }
1618
1619                 /* don't test against self */
1620
1621                 if (*l == loc) {
1622                         ++l;
1623                         continue;
1624                 }
1625
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);
1638                                 break;
1639
1640                         case Evoral::OverlapNone:
1641                                 ++l;
1642                                 break;
1643                 }
1644         }
1645 }
1646
1647 void
1648 Session::sync_locations_to_skips ()
1649 {
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.
1652          */
1653         clear_events (SessionEvent::Skip, boost::bind (&Session::_sync_locations_to_skips, this));
1654 }
1655
1656 void
1657 Session::_sync_locations_to_skips ()
1658 {
1659         /* called as a callback after existing Skip events have been cleared from a realtime audioengine thread */
1660
1661         Locations::LocationList const & locs (_locations->list());
1662
1663         for (Locations::LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1664
1665                 Location* location = *i;
1666
1667                 if (location->is_skip() && location->is_skipping()) {
1668                         SessionEvent* ev = new SessionEvent (SessionEvent::Skip, SessionEvent::Add, location->start(), location->end(), 1.0);
1669                         queue_event (ev);
1670                 }
1671         }
1672 }
1673
1674
1675 void
1676 Session::location_added (Location *location)
1677 {
1678         if (location->is_auto_punch()) {
1679                 set_auto_punch_location (location);
1680         }
1681
1682         if (location->is_auto_loop()) {
1683                 set_auto_loop_location (location);
1684         }
1685
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.
1689                          */
1690                 _session_range_location = location;
1691         }
1692
1693         if (location->is_mark()) {
1694                 /* listen for per-location signals that require us to do any * global updates for marks */
1695
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));
1701         }
1702
1703         if (location->is_range_marker()) {
1704                 /* listen for per-location signals that require us to do any * global updates for marks */
1705
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));
1711         }
1712
1713         if (location->is_skip()) {
1714                 /* listen for per-location signals that require us to update skip-locate events */
1715
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));
1721
1722                 update_skips (location, true);
1723         }
1724
1725         set_dirty ();
1726 }
1727
1728 void
1729 Session::location_removed (Location *location)
1730 {
1731         if (location->is_auto_loop()) {
1732                 set_auto_loop_location (0);
1733                 if (!play_loop) {
1734                         set_track_loop (false);
1735                 }
1736                 unset_play_loop ();
1737         }
1738
1739         if (location->is_auto_punch()) {
1740                 set_auto_punch_location (0);
1741         }
1742
1743         if (location->is_session_range()) {
1744                 /* this is never supposed to happen */
1745                 error << _("programming error: session range removed!") << endl;
1746         }
1747
1748         if (location->is_skip()) {
1749
1750                 update_skips (location, false);
1751         }
1752
1753         set_dirty ();
1754 }
1755
1756 void
1757 Session::locations_changed ()
1758 {
1759         _locations->apply (*this, &Session::_locations_changed);
1760 }
1761
1762 void
1763 Session::_locations_changed (const Locations::LocationList& locations)
1764 {
1765         /* There was some mass-change in the Locations object.
1766          *
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.
1769          */
1770
1771         {
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);
1775                 }
1776         }
1777
1778         update_skips (NULL, false);
1779 }
1780
1781 void
1782 Session::enable_record ()
1783 {
1784         if (_transport_speed != 0.0 && _transport_speed != 1.0) {
1785                 /* no recording at anything except normal speed */
1786                 return;
1787         }
1788
1789         while (1) {
1790                 RecordState rs = (RecordState) g_atomic_int_get (&_record_status);
1791
1792                 if (rs == Recording) {
1793                         break;
1794                 }
1795
1796                 if (g_atomic_int_compare_and_exchange (&_record_status, rs, Recording)) {
1797
1798                         _last_record_location = _transport_sample;
1799                         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordStrobe));
1800
1801                         if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1802                                 set_track_monitor_input_status (true);
1803                         }
1804
1805                         RecordStateChanged ();
1806                         break;
1807                 }
1808         }
1809 }
1810
1811 void
1812 Session::set_all_tracks_record_enabled (bool enable )
1813 {
1814         boost::shared_ptr<RouteList> rl = routes.reader();
1815         set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), enable, Controllable::NoGroup);
1816 }
1817
1818 void
1819 Session::disable_record (bool rt_context, bool force)
1820 {
1821         RecordState rs;
1822
1823         if ((rs = (RecordState) g_atomic_int_get (&_record_status)) != Disabled) {
1824
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));
1828                 } else {
1829                         if (rs == Recording) {
1830                                 g_atomic_int_set (&_record_status, Enabled);
1831                         }
1832                 }
1833
1834                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1835                         set_track_monitor_input_status (false);
1836                 }
1837
1838                 RecordStateChanged (); /* emit signal */
1839         }
1840 }
1841
1842 void
1843 Session::step_back_from_record ()
1844 {
1845         if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
1846
1847                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1848                         set_track_monitor_input_status (false);
1849                 }
1850
1851                 RecordStateChanged (); /* emit signal */
1852         }
1853 }
1854
1855 void
1856 Session::maybe_enable_record (bool rt_context)
1857 {
1858         if (_step_editors > 0) {
1859                 return;
1860         }
1861
1862         g_atomic_int_set (&_record_status, Enabled);
1863
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.
1869          */
1870
1871         if (!rt_context) {
1872                 save_state ("", true);
1873         }
1874
1875         if (_transport_speed) {
1876                 if (!config.get_punch_in()) {
1877                         enable_record ();
1878                 }
1879         } else {
1880                 send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordPause));
1881                 RecordStateChanged (); /* EMIT SIGNAL */
1882         }
1883
1884         set_dirty();
1885 }
1886
1887 samplepos_t
1888 Session::audible_sample (bool* latent_locate) const
1889 {
1890         if (latent_locate) {
1891                 *latent_locate = false;
1892         }
1893
1894         samplepos_t ret;
1895
1896         if (synced_to_engine()) {
1897                 /* Note: this is basically just sync-to-JACK */
1898                 ret = _engine.transport_sample();
1899         } else {
1900                 ret = _transport_sample;
1901         }
1902
1903         assert (ret >= 0);
1904
1905         if (!transport_rolling()) {
1906                 return ret;
1907         }
1908
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.
1916                          */
1917                         Location *location = _locations->auto_loop_location();
1918                         sampleoffset_t lo = location->start() - ret;
1919                         if (lo > 0) {
1920                                 ret = location->end () - lo;
1921                                 if (latent_locate) {
1922                                         *latent_locate = true;
1923                                 }
1924                         }
1925                 }
1926         } else if (_transport_speed < 0.0f) {
1927                 /* XXX wot? no backward looping? */
1928         }
1929 #endif
1930
1931         return std::max ((samplepos_t)0, ret);
1932 }
1933
1934 samplecnt_t
1935 Session::preroll_samples (samplepos_t pos) const
1936 {
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;
1942         }
1943         if (pr < 0) {
1944                 return 0;
1945         }
1946         return pr * sample_rate();
1947 }
1948
1949 void
1950 Session::set_sample_rate (samplecnt_t frames_per_second)
1951 {
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
1956                 here.
1957         */
1958
1959         if (_base_sample_rate == 0) {
1960                 _base_sample_rate = frames_per_second;
1961         }
1962         else if (_base_sample_rate != frames_per_second && frames_per_second != _nominal_sample_rate) {
1963                 NotifyAboutSampleRateMismatch (_base_sample_rate, frames_per_second);
1964         }
1965         _nominal_sample_rate = frames_per_second;
1966
1967         sync_time_vars();
1968
1969         clear_clicks ();
1970         reset_write_sources (false);
1971
1972         // XXX we need some equivalent to this, somehow
1973         // SndFileSource::setup_standard_crossfades (frames_per_second);
1974
1975         set_dirty();
1976
1977         /* XXX need to reset/reinstantiate all LADSPA plugins */
1978 }
1979
1980 void
1981 Session::set_block_size (pframes_t nframes)
1982 {
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
1986            here.
1987         */
1988
1989         {
1990                 current_block_size = nframes;
1991
1992                 ensure_buffers ();
1993
1994                 boost::shared_ptr<RouteList> r = routes.reader ();
1995
1996                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1997                         (*i)->set_block_size (nframes);
1998                 }
1999
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);
2003                         if (tr) {
2004                                 tr->set_block_size (nframes);
2005                         }
2006                 }
2007
2008                 set_worst_io_latencies ();
2009         }
2010 }
2011
2012
2013 static void
2014 trace_terminal (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> rbase, bool sends_only)
2015 {
2016         boost::shared_ptr<Route> r2;
2017
2018         if (r1->feeds (rbase) && rbase->feeds (r1)) {
2019                 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
2020                 return;
2021         }
2022
2023         /* make a copy of the existing list of routes that feed r1 */
2024
2025         Route::FedBy existing (r1->fed_by());
2026
2027         /* for each route that feeds r1, recurse, marking it as feeding
2028            rbase as well.
2029         */
2030
2031         for (Route::FedBy::iterator i = existing.begin(); i != existing.end(); ++i) {
2032                 if (!(r2 = i->r.lock ())) {
2033                         /* (*i) went away, ignore it */
2034                         continue;
2035                 }
2036
2037                 /* r2 is a route that feeds r1 which somehow feeds base. mark
2038                    base as being fed by r2
2039                 */
2040
2041                 rbase->add_fed_by (r2, i->sends_only || sends_only);
2042
2043                 if (r2 != rbase) {
2044
2045                         /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
2046                            stop here.
2047                         */
2048
2049                         if (r1->feeds (r2) && r2->feeds (r1)) {
2050                                 continue;
2051                         }
2052
2053                         /* now recurse, so that we can mark base as being fed by
2054                            all routes that feed r2
2055                         */
2056
2057                         trace_terminal (r2, rbase, i->sends_only || sends_only);
2058                 }
2059
2060         }
2061 }
2062
2063 void
2064 Session::resort_routes ()
2065 {
2066         /* don't do anything here with signals emitted
2067            by Routes during initial setup or while we
2068            are being destroyed.
2069         */
2070
2071         if (inital_connect_or_deletion_in_progress ()) {
2072                 return;
2073         }
2074
2075         if (_route_deletion_in_progress) {
2076                 return;
2077         }
2078
2079         {
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 */
2084         }
2085
2086 #ifndef NDEBUG
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()));
2091
2092                         const Route::FedBy& fb ((*i)->fed_by());
2093
2094                         for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) {
2095                                 boost::shared_ptr<Route> sf = f->r.lock();
2096                                 if (sf) {
2097                                         DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only));
2098                                 }
2099                         }
2100                 }
2101         }
2102 #endif
2103
2104 }
2105
2106 /** This is called whenever we need to rebuild the graph of how we will process
2107  *  routes.
2108  *  @param r List of routes, in any order.
2109  */
2110
2111 void
2112 Session::resort_routes_using (boost::shared_ptr<RouteList> r)
2113 {
2114         /* We are going to build a directed graph of our routes;
2115            this is where the edges of that graph are put.
2116         */
2117
2118         GraphEdges edges;
2119
2120         /* Go through all routes doing two things:
2121          *
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.
2125          *
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.
2129          */
2130
2131         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2132
2133                 /* Clear out the route's list of direct or indirect feeds */
2134                 (*i)->clear_fed_by ();
2135
2136                 for (RouteList::iterator j = r->begin(); j != r->end(); ++j) {
2137
2138                         bool via_sends_only;
2139
2140                         /* See if this *j feeds *i according to the current state of the JACK
2141                            connections and internal sends.
2142                         */
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);
2148                         }
2149                 }
2150         }
2151
2152         /* Attempt a topological sort of the route graph */
2153         boost::shared_ptr<RouteList> sorted_routes = topological_sort (r, edges);
2154
2155         if (sorted_routes) {
2156                 /* We got a satisfactory topological sort, so there is no feedback;
2157                    use this new graph.
2158
2159                    Note: the process graph rechain does not require a
2160                    topologically-sorted list, but hey ho.
2161                 */
2162                 if (_process_graph) {
2163                         _process_graph->rechain (sorted_routes, edges);
2164                 }
2165
2166                 _current_route_graph = edges;
2167
2168                 /* Complete the building of the routes' lists of what directly
2169                    or indirectly feeds them.
2170                 */
2171                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2172                         trace_terminal (*i, *i, false);
2173                 }
2174
2175                 *r = *sorted_routes;
2176
2177 #ifndef NDEBUG
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()));
2181                 }
2182 #endif
2183
2184                 SuccessfulGraphSort (); /* EMIT SIGNAL */
2185
2186         } else {
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
2193                    as it was before.
2194                 */
2195
2196                 FeedbackDetected (); /* EMIT SIGNAL */
2197         }
2198
2199 }
2200
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.
2205  *
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.
2209  *
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.
2212  */
2213 bool
2214 Session::find_route_name (string const & base, uint32_t& id, string& name, bool definitely_add_number)
2215 {
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.
2219         */
2220
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.
2225                         */
2226                         if (!reserved->second || route_by_name (reserved->first)) {
2227                                 definitely_add_number = true;
2228                                 if (id < 1) {
2229                                         id = 1;
2230                                 }
2231                         }
2232                         break;
2233                 }
2234         }
2235
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"
2238          */
2239         if (!definitely_add_number && route_by_name (base) == 0 && (route_by_name (string_compose("%1 1", base)) == 0)) {
2240                 /* just use the base */
2241                 name = base;
2242                 return true;
2243         }
2244
2245         do {
2246                 name = string_compose ("%1 %2", base, id);
2247
2248                 if (route_by_name (name) == 0) {
2249                         return true;
2250                 }
2251
2252                 ++id;
2253
2254         } while (id < (UINT_MAX-1));
2255
2256         return false;
2257 }
2258
2259 /** Count the total ins and outs of all non-hidden tracks in the session and return them in in and out */
2260 void
2261 Session::count_existing_track_channels (ChanCount& in, ChanCount& out)
2262 {
2263         in  = ChanCount::ZERO;
2264         out = ChanCount::ZERO;
2265
2266         boost::shared_ptr<RouteList> r = routes.reader ();
2267
2268         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2269                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2270                 if (!tr) {
2271                         continue;
2272                 }
2273                 assert (!tr->is_auditioner()); // XXX remove me
2274                 in  += tr->n_inputs();
2275                 out += tr->n_outputs();
2276         }
2277 }
2278
2279 string
2280 Session::default_track_name_pattern (DataType t)
2281 {
2282         switch (t) {
2283         case DataType::AUDIO:
2284                 return _("Audio ");
2285                 break;
2286
2287         case DataType::MIDI:
2288                 return _("MIDI ");
2289         }
2290
2291         return "";
2292 }
2293
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
2297  */
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,
2303                          TrackMode mode)
2304 {
2305         string track_name;
2306         uint32_t track_id = 0;
2307         string port;
2308         RouteList new_routes;
2309         list<boost::shared_ptr<MidiTrack> > ret;
2310
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);
2313
2314         while (how_many) {
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;
2317                         goto failed;
2318                 }
2319
2320                 boost::shared_ptr<MidiTrack> track;
2321
2322                 try {
2323                         track.reset (new MidiTrack (*this, track_name, mode));
2324
2325                         if (track->init ()) {
2326                                 goto failed;
2327                         }
2328
2329                         if (strict_io) {
2330                                 track->set_strict_io (true);
2331                         }
2332
2333                         BOOST_MARK_TRACK (track);
2334
2335                         {
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;
2339                                         goto failed;
2340                                 }
2341
2342                                 if (track->output()->ensure_io (output, false, this)) {
2343                                         error << "cannot configure " << output << " out configuration for new midi track" << endmsg;
2344                                         goto failed;
2345                                 }
2346                         }
2347
2348                         if (route_group) {
2349                                 route_group->add (track);
2350                         }
2351
2352                         new_routes.push_back (track);
2353                         ret.push_back (track);
2354                 }
2355
2356                 catch (failed_constructor &err) {
2357                         error << _("Session: could not create new midi track.") << endmsg;
2358                         goto failed;
2359                 }
2360
2361                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2362
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;
2364                         goto failed;
2365                 }
2366
2367                 --how_many;
2368         }
2369
2370         failed:
2371         if (!new_routes.empty()) {
2372                 StateProtector sp (this);
2373                 add_routes (new_routes, true, true, false, order);
2374
2375                 if (instrument) {
2376                         for (RouteList::iterator r = new_routes.begin(); r != new_routes.end(); ++r) {
2377                                 PluginPtr plugin = instrument->load (*this);
2378                                 if (!plugin) {
2379                                         warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2380                                         continue;
2381                                 }
2382                                 if (pset) {
2383                                         plugin->load_preset (*pset);
2384                                 }
2385                                 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2386                                 if (strict_io) {
2387                                         pi->set_strict_io (true);
2388                                 }
2389
2390                                 (*r)->add_processor (pi, PreFader);
2391
2392                                 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2393                                         (*r)->move_instrument_down (false);
2394                                 }
2395                         }
2396                 }
2397         }
2398
2399         return ret;
2400 }
2401
2402 RouteList
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)
2406 {
2407         string bus_name;
2408         uint32_t bus_id = 0;
2409         string port;
2410         RouteList ret;
2411
2412         bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Midi Bus");
2413
2414         while (how_many) {
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;
2417                         goto failure;
2418                 }
2419
2420                 try {
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
2422
2423                         if (bus->init ()) {
2424                                 goto failure;
2425                         }
2426
2427                         if (strict_io) {
2428                                 bus->set_strict_io (true);
2429                         }
2430
2431                         BOOST_MARK_ROUTE(bus);
2432
2433                         {
2434                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2435
2436                                 if (bus->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2437                                         error << _("cannot configure new midi bus input") << endmsg;
2438                                         goto failure;
2439                                 }
2440
2441
2442                                 if (bus->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2443                                         error << _("cannot configure new midi bus output") << endmsg;
2444                                         goto failure;
2445                                 }
2446                         }
2447
2448                         if (route_group) {
2449                                 route_group->add (bus);
2450                         }
2451
2452                         bus->add_internal_return ();
2453                         ret.push_back (bus);
2454                 }
2455
2456                 catch (failed_constructor &err) {
2457                         error << _("Session: could not create new audio route.") << endmsg;
2458                         goto failure;
2459                 }
2460
2461                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2462                         error << pfe.what() << endmsg;
2463                         goto failure;
2464                 }
2465
2466
2467                 --how_many;
2468         }
2469
2470         failure:
2471         if (!ret.empty()) {
2472                 StateProtector sp (this);
2473                 add_routes (ret, false, false, false, order);
2474
2475                 if (instrument) {
2476                         for (RouteList::iterator r = ret.begin(); r != ret.end(); ++r) {
2477                                 PluginPtr plugin = instrument->load (*this);
2478                                 if (!plugin) {
2479                                         warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2480                                         continue;
2481                                 }
2482                                 if (pset) {
2483                                         plugin->load_preset (*pset);
2484                                 }
2485                                 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2486                                 if (strict_io) {
2487                                         pi->set_strict_io (true);
2488                                 }
2489
2490                                 (*r)->add_processor (pi, PreFader);
2491
2492                                 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2493                                         (*r)->move_instrument_down (false);
2494                                 }
2495                         }
2496                 }
2497         }
2498
2499         return ret;
2500
2501 }
2502
2503
2504 void
2505 Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr<Route> wmt)
2506 {
2507         boost::shared_ptr<Route> midi_track (wmt.lock());
2508
2509         if (!midi_track) {
2510                 return;
2511         }
2512
2513         if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) {
2514
2515                 if (change.after.n_audio() <= change.before.n_audio()) {
2516                         return;
2517                 }
2518
2519                 /* new audio ports: make sure the audio goes somewhere useful,
2520                  * unless the user has no-auto-connect selected.
2521                  *
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
2524                  * outputs here.
2525                  */
2526                 auto_connect_route (midi_track, false, ChanCount(), change.before);
2527         }
2528 }
2529
2530 bool
2531 Session::ensure_stripable_sort_order ()
2532 {
2533         StripableList sl;
2534         get_stripables (sl);
2535         sl.sort (Stripable::Sorter ());
2536
2537         bool change = false;
2538         PresentationInfo::order_t order = 0;
2539
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 ()) {
2544                         continue;
2545                 }
2546                 if (order != s->presentation_info().order()) {
2547                         s->set_presentation_order (order);
2548                         change = true;
2549                 }
2550                 ++order;
2551         }
2552         return change;
2553 }
2554
2555 void
2556 Session::ensure_route_presentation_info_gap (PresentationInfo::order_t first_new_order, uint32_t how_many)
2557 {
2558         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("ensure order gap starting at %1 for %2\n", first_new_order, how_many));
2559
2560         if (first_new_order == PresentationInfo::max_order) {
2561                 /* adding at end, no worries */
2562                 return;
2563         }
2564
2565         /* create a gap in the presentation info to accomodate @param how_many
2566          * new objects.
2567          */
2568         StripableList sl;
2569         get_stripables (sl);
2570
2571         for (StripableList::iterator si = sl.begin(); si != sl.end(); ++si) {
2572                 boost::shared_ptr<Stripable> s (*si);
2573
2574                 if (s->presentation_info().special (false)) {
2575                         continue;
2576                 }
2577
2578                 if (!s->presentation_info().order_set()) {
2579                         continue;
2580                 }
2581
2582                 if (s->presentation_info().order () >= first_new_order) {
2583                         s->set_presentation_order (s->presentation_info().order () + how_many);
2584                 }
2585         }
2586 }
2587
2588 /** Caller must not hold process lock
2589  *  @param name_template string to use for the start of the name, or "" to use "Audio".
2590  */
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,
2594                           TrackMode mode)
2595 {
2596         string track_name;
2597         uint32_t track_id = 0;
2598         string port;
2599         RouteList new_routes;
2600         list<boost::shared_ptr<AudioTrack> > ret;
2601
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);
2604
2605         while (how_many) {
2606
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;
2609                         goto failed;
2610                 }
2611
2612                 boost::shared_ptr<AudioTrack> track;
2613
2614                 try {
2615                         track.reset (new AudioTrack (*this, track_name, mode));
2616
2617                         if (track->init ()) {
2618                                 goto failed;
2619                         }
2620
2621                         if (Profile->get_mixbus ()) {
2622                                 track->set_strict_io (true);
2623                         }
2624
2625                         BOOST_MARK_TRACK (track);
2626
2627                         {
2628                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2629
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)
2634                                               << endmsg;
2635                                         goto failed;
2636                                 }
2637
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)
2642                                               << endmsg;
2643                                         goto failed;
2644                                 }
2645                         }
2646
2647                         if (route_group) {
2648                                 route_group->add (track);
2649                         }
2650
2651                         new_routes.push_back (track);
2652                         ret.push_back (track);
2653                 }
2654
2655                 catch (failed_constructor &err) {
2656                         error << _("Session: could not create new audio track.") << endmsg;
2657                         goto failed;
2658                 }
2659
2660                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2661
2662                         error << pfe.what() << endmsg;
2663                         goto failed;
2664                 }
2665
2666                 --how_many;
2667         }
2668
2669         failed:
2670         if (!new_routes.empty()) {
2671                 StateProtector sp (this);
2672                 add_routes (new_routes, true, true, false, order);
2673         }
2674
2675         return ret;
2676 }
2677
2678 /** Caller must not hold process lock.
2679  *  @param name_template string to use for the start of the name, or "" to use "Bus".
2680  */
2681 RouteList
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)
2684 {
2685         string bus_name;
2686         uint32_t bus_id = 0;
2687         string port;
2688         RouteList ret;
2689
2690         bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Bus");
2691
2692         while (how_many) {
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;
2695                         goto failure;
2696                 }
2697
2698                 try {
2699                         boost::shared_ptr<Route> bus (new Route (*this, bus_name, flags, DataType::AUDIO));
2700
2701                         if (bus->init ()) {
2702                                 goto failure;
2703                         }
2704
2705                         if (Profile->get_mixbus ()) {
2706                                 bus->set_strict_io (true);
2707                         }
2708
2709                         BOOST_MARK_ROUTE(bus);
2710
2711                         {
2712                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2713
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)
2717                                               << endmsg;
2718                                         goto failure;
2719                                 }
2720
2721
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)
2725                                               << endmsg;
2726                                         goto failure;
2727                                 }
2728                         }
2729
2730                         if (route_group) {
2731                                 route_group->add (bus);
2732                         }
2733
2734                         bus->add_internal_return ();
2735                         ret.push_back (bus);
2736                 }
2737
2738                 catch (failed_constructor &err) {
2739                         error << _("Session: could not create new audio route.") << endmsg;
2740                         goto failure;
2741                 }
2742
2743                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2744                         error << pfe.what() << endmsg;
2745                         goto failure;
2746                 }
2747
2748
2749                 --how_many;
2750         }
2751
2752         failure:
2753         if (!ret.empty()) {
2754                 StateProtector sp (this);
2755
2756                 if (flags == PresentationInfo::FoldbackBus) {
2757                         add_routes (ret, false, false, true, order); // no autoconnect
2758                 } else {
2759                         add_routes (ret, false, true, true, order); // autoconnect // outputs only
2760                 }
2761         }
2762
2763         return ret;
2764
2765 }
2766
2767 RouteList
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)
2770 {
2771         XMLTree tree;
2772
2773         if (!tree.read (template_path.c_str())) {
2774                 return RouteList();
2775         }
2776
2777         return new_route_from_template (how_many, insert_at, *tree.root(), name_base, pd);
2778 }
2779
2780 RouteList
2781 Session::new_route_from_template (uint32_t how_many, PresentationInfo::order_t insert_at, XMLNode& node, const std::string& name_base, PlaylistDisposition pd)
2782 {
2783         RouteList ret;
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
2787            values by Stateful.
2788         */
2789         Stateful::ForceIDRegeneration force_ids;
2790         IO::disable_connecting ();
2791
2792         while (how_many) {
2793
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.
2796                  */
2797
2798                 XMLNode node_copy (node);
2799                 std::vector<boost::shared_ptr<Playlist> > shared_playlists;
2800
2801                 try {
2802                         string name;
2803
2804                         if (!name_base.empty()) {
2805
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.
2809                                  */
2810
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*/
2814                                 }
2815
2816                         } else {
2817
2818                                 string const route_name  = node_copy.property(X_("name"))->value ();
2819
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*/
2824                                 }
2825                         }
2826
2827                         /* figure out the appropriate playlist setup. The track
2828                          * (if the Route we're creating is a track) will find
2829                          * playlists via ID.
2830                          */
2831
2832                         if (pd == CopyPlaylist) {
2833
2834                                 PBD::ID playlist_id;
2835
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());
2841                                 }
2842
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());
2848                                 }
2849
2850                         } else if (pd == SharePlaylist) {
2851                                 PBD::ID playlist_id;
2852
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);
2856                                 }
2857
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);
2861                                 }
2862
2863                         } else { /* NewPlaylist */
2864
2865                                 PBD::ID pid;
2866
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());
2870                                 }
2871
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());
2875                                 }
2876                         }
2877
2878                         /* Fix up new name in the XML node */
2879
2880                         Route::set_name_in_state (node_copy, name);
2881
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.
2892                                                  */
2893                                                 XMLProperty const * target = (*i)->property (X_("target"));
2894                                                 if (!target) {
2895                                                         (*i)->set_property ("type", "dangling-aux-send");
2896                                                         continue;
2897                                                 }
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");
2901                                                         continue;
2902                                                 }
2903                                         }
2904                                         if (role && role->value() == X_("Listen")) {
2905                                                 (*i)->remove_property (X_("bitslot"));
2906                                         }
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")) {
2919                                                                 continue;
2920                                                         }
2921                                                         (*j)->remove_property (X_("name"));
2922                                                         (*j)->set_property ("name", name);
2923                                                 }
2924                                         }
2925                                         else if (type && type->value() == X_("intreturn")) {
2926                                                 (*i)->remove_property (X_("bitslot"));
2927                                                 (*i)->set_property ("ignore-bitslot", "1");
2928                                         }
2929                                         else if (type && type->value() == X_("return")) {
2930                                                 // Return::set_state() generates a new one
2931                                                 (*i)->remove_property (X_("bitslot"));
2932                                         }
2933                                         else if (type && type->value() == X_("port")) {
2934                                                 IOProcessor::prepare_for_reset (**i, name);
2935                                         }
2936                                 }
2937                         }
2938
2939                         /* new routes start off unsoloed to avoid issues related to
2940                            upstream / downstream buses.
2941                         */
2942                         node_copy.remove_node_and_delete (X_("Controllable"), X_("name"), X_("solo"));
2943
2944                         boost::shared_ptr<Route> route (XMLRouteFactory (node_copy, 3000));
2945
2946                         if (route == 0) {
2947                                 error << _("Session: cannot create track/bus from template description") << endmsg;
2948                                 goto out;
2949                         }
2950
2951                         /* Fix up sharing of playlists with the new Route/Track */
2952
2953                         for (vector<boost::shared_ptr<Playlist> >::iterator sp = shared_playlists.begin(); sp != shared_playlists.end(); ++sp) {
2954                                 (*sp)->share_with (route->id());
2955                         }
2956
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.
2961                                 */
2962
2963                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2964
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);
2970                         }
2971
2972                         ret.push_back (route);
2973                 }
2974
2975                 catch (failed_constructor &err) {
2976                         error << _("Session: could not create new route from template") << endmsg;
2977                         goto out;
2978                 }
2979
2980                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2981                         error << pfe.what() << endmsg;
2982                         goto out;
2983                 }
2984
2985                 catch (...) {
2986                         IO::enable_connecting ();
2987                         throw;
2988                 }
2989
2990                 --how_many;
2991         }
2992
2993         out:
2994         if (!ret.empty()) {
2995                 StateProtector sp (this);
2996
2997                 add_routes (ret, true, true, false, insert_at);
2998         }
2999
3000         IO::enable_connecting ();
3001
3002         return ret;
3003 }
3004
3005 void
3006 Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, bool save, PresentationInfo::order_t order)
3007 {
3008         try {
3009                 PBD::Unwinder<bool> aip (_adding_routes_in_progress, true);
3010                 add_routes_inner (new_routes, input_auto_connect, output_auto_connect, order);
3011
3012         } catch (...) {
3013                 error << _("Adding new tracks/busses failed") << endmsg;
3014         }
3015
3016         graph_reordered ();
3017
3018         set_dirty();
3019
3020         if (save) {
3021                 save_state (_current_snapshot_name);
3022         }
3023
3024         update_route_record_state ();
3025
3026         RouteAdded (new_routes); /* EMIT SIGNAL */
3027 }
3028
3029 void
3030 Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, PresentationInfo::order_t order)
3031 {
3032         ChanCount existing_inputs;
3033         ChanCount existing_outputs;
3034         uint32_t n_routes;
3035         uint32_t added = 0;
3036
3037         count_existing_track_channels (existing_inputs, existing_outputs);
3038
3039         {
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());
3044
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.
3049                  */
3050
3051                 if (!_monitor_out && IO::connecting_legal) {
3052                         resort_routes_using (r);
3053                 }
3054         }
3055
3056         /* monitor is not part of the order */
3057         if (_monitor_out) {
3058                 assert (n_routes > 0);
3059                 --n_routes;
3060         }
3061
3062         {
3063                 PresentationInfo::ChangeSuspender cs;
3064                 ensure_route_presentation_info_gap (order, new_routes.size());
3065
3066                 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x, ++added) {
3067
3068                         boost::weak_ptr<Route> wpr (*x);
3069                         boost::shared_ptr<Route> r (*x);
3070
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));
3074
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));
3078
3079                         if (r->is_master()) {
3080                                 _master_out = r;
3081                         }
3082
3083                         if (r->is_monitor()) {
3084                                 _monitor_out = r;
3085                         }
3086
3087                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (r);
3088                         if (tr) {
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));
3092
3093                                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (tr);
3094                                 if (mt) {
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)));
3098                                 }
3099                         }
3100
3101                         if (!r->presentation_info().special (false)) {
3102
3103                                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("checking PI state for %1\n", r->name()));
3104
3105                                 /* presentation info order may already have been set from XML */
3106
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));
3112                                         } else {
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));
3115                                         }
3116                                 } else {
3117                                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order already set to %1\n", r->presentation_info().order()));
3118                                 }
3119                         }
3120
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)"
3124                          */
3125                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("added route %1, group order %2 type %3 (summary: %4)\n",
3126                                                                        r->name(),
3127                                                                        r->presentation_info().order(),
3128                                                                        enum_2_string (r->presentation_info().flags()),
3129                                                                        r->presentation_info()));
3130 #endif
3131
3132
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();
3137                         }
3138
3139                         ARDOUR::GUIIdle ();
3140                 }
3141                 ensure_stripable_sort_order ();
3142         }
3143
3144         if (_monitor_out && IO::connecting_legal) {
3145                 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
3146
3147                 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
3148                         if ((*x)->is_monitor()) {
3149                                 /* relax */
3150                         } else if ((*x)->is_master()) {
3151                                 /* relax */
3152                         } else {
3153                                 (*x)->enable_monitor_send ();
3154                         }
3155                 }
3156         }
3157
3158         reassign_track_numbers ();
3159 }
3160
3161 void
3162 Session::globally_set_send_gains_to_zero (boost::shared_ptr<Route> dest)
3163 {
3164         boost::shared_ptr<RouteList> r = routes.reader ();
3165         boost::shared_ptr<Send> s;
3166
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);
3170                 }
3171         }
3172 }
3173
3174 void
3175 Session::globally_set_send_gains_to_unity (boost::shared_ptr<Route> dest)
3176 {
3177         boost::shared_ptr<RouteList> r = routes.reader ();
3178         boost::shared_ptr<Send> s;
3179
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);
3183                 }
3184         }
3185 }
3186
3187 void
3188 Session::globally_set_send_gains_from_track(boost::shared_ptr<Route> dest)
3189 {
3190         boost::shared_ptr<RouteList> r = routes.reader ();
3191         boost::shared_ptr<Send> s;
3192
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);
3196                 }
3197         }
3198 }
3199
3200 /** @param include_buses true to add sends to buses and tracks, false for just tracks */
3201 void
3202 Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p, bool include_buses)
3203 {
3204         boost::shared_ptr<RouteList> r = routes.reader ();
3205         boost::shared_ptr<RouteList> t (new RouteList);
3206
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)) {
3210                         t->push_back (*i);
3211                 }
3212         }
3213
3214         add_internal_sends (dest, p, t);
3215 }
3216
3217 void
3218 Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::shared_ptr<RouteList> senders)
3219 {
3220         for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
3221                 add_internal_send (dest, (*i)->before_processor_for_placement (p), *i);
3222         }
3223 }
3224
3225 void
3226 Session::add_internal_send (boost::shared_ptr<Route> dest, int index, boost::shared_ptr<Route> sender)
3227 {
3228         add_internal_send (dest, sender->before_processor_for_index (index), sender);
3229 }
3230
3231 void
3232 Session::add_internal_send (boost::shared_ptr<Route> dest, boost::shared_ptr<Processor> before, boost::shared_ptr<Route> sender)
3233 {
3234         if (sender->is_monitor() || sender->is_master() || sender == dest || dest->is_monitor() || dest->is_master()) {
3235                 return;
3236         }
3237
3238         if (!dest->internal_return()) {
3239                 dest->add_internal_return ();
3240         }
3241
3242         sender->add_aux_send (dest, before);
3243
3244         graph_reordered ();
3245 }
3246
3247 void
3248 Session::remove_routes (boost::shared_ptr<RouteList> routes_to_remove)
3249 {
3250         bool mute_changed = false;
3251         bool send_selected = false;
3252
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 ();
3257
3258                 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3259
3260                         if (_selection->selected (*iter)) {
3261                                 send_selected = true;
3262                         }
3263
3264                         if (*iter == _master_out) {
3265                                 continue;
3266                         }
3267
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);
3271                         }
3272
3273                         if ((*iter)->mute_control()->muted ()) {
3274                                 mute_changed = true;
3275                         }
3276
3277                         rs->remove (*iter);
3278
3279                         /* deleting the master out seems like a dumb
3280                            idea, but its more of a UI policy issue
3281                            than our concern.
3282                         */
3283
3284                         if (*iter == _master_out) {
3285                                 _master_out = boost::shared_ptr<Route> ();
3286                         }
3287
3288                         if (*iter == _monitor_out) {
3289                                 _monitor_out.reset ();
3290                         }
3291
3292                         // We need to disconnect the route's inputs and outputs
3293
3294                         (*iter)->input()->disconnect (0);
3295                         (*iter)->output()->disconnect (0);
3296
3297                         /* if the route had internal sends sending to it, remove them */
3298
3299                         if (!deletion_in_progress () && (*iter)->internal_return()) {
3300
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);
3304                                         if (s) {
3305                                                 (*i)->remove_processor (s);
3306                                         }
3307                                 }
3308                         }
3309
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);
3315                         }
3316
3317                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*iter);
3318                         if (mt && mt->step_editing()) {
3319                                 if (_step_editors > 0) {
3320                                         _step_editors--;
3321                                 }
3322                         }
3323                 }
3324
3325                 /* writer goes out of scope, forces route list update */
3326
3327         } // end of RCU Writer scope
3328
3329         if (mute_changed) {
3330                 MuteChanged (); /* EMIT SIGNAL */
3331         }
3332
3333         update_route_solo_state ();
3334         update_latency_compensation ();
3335         set_dirty();
3336
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.
3339          */
3340
3341         routes.flush (); // maybe unsafe, see below.
3342         resort_routes ();
3343
3344         if (_process_graph && !deletion_in_progress() && _engine.running()) {
3345                 _process_graph->clear_other_chain ();
3346         }
3347
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) */
3350
3351         routes.flush ();
3352
3353         /* remove these routes from the selection if appropriate, and signal
3354          * the change *before* we call DropReferences for them.
3355          */
3356
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());
3360                 }
3361                 PropertyChange pc;
3362                 pc.add (Properties::selected);
3363                 PresentationInfo::Change (pc);
3364         }
3365
3366         /* try to cause everyone to drop their references
3367          * and unregister ports from the backend
3368          */
3369
3370         for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3371                 (*iter)->drop_references ();
3372         }
3373
3374         if (deletion_in_progress()) {
3375                 return;
3376         }
3377
3378         PropertyChange pc;
3379         pc.add (Properties::order);
3380         PresentationInfo::Change (pc);
3381
3382         /* save the new state of the world */
3383
3384         if (save_state (_current_snapshot_name)) {
3385                 save_history (_current_snapshot_name);
3386         }
3387
3388         update_route_record_state ();
3389 }
3390
3391 void
3392 Session::remove_route (boost::shared_ptr<Route> route)
3393 {
3394         boost::shared_ptr<RouteList> rl (new RouteList);
3395         rl->push_back (route);
3396         remove_routes (rl);
3397 }
3398
3399 void
3400 Session::route_mute_changed ()
3401 {
3402         MuteChanged (); /* EMIT SIGNAL */
3403         set_dirty ();
3404 }
3405
3406 void
3407 Session::route_listen_changed (Controllable::GroupControlDisposition group_override, boost::weak_ptr<Route> wpr)
3408 {
3409         boost::shared_ptr<Route> route (wpr.lock());
3410
3411         if (!route) {
3412                 return;
3413         }
3414
3415         assert (Config->get_solo_control_is_listen_control());
3416
3417         if (route->solo_control()->soloed_by_self_or_masters()) {
3418
3419                 if (Config->get_exclusive_solo()) {
3420
3421                         RouteGroup* rg = route->route_group ();
3422                         const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3423
3424                         boost::shared_ptr<RouteList> r = routes.reader ();
3425
3426                         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3427                                 if ((*i) == route) {
3428                                         /* already changed */
3429                                         continue;
3430                                 }
3431
3432                                 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3433                                         /* route does not get solo propagated to it */
3434                                         continue;
3435                                 }
3436
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
3441                                          * here
3442                                          */
3443                                         continue;
3444                                 }
3445                                 (*i)->solo_control()->set_value (0.0, Controllable::NoGroup);
3446                         }
3447                 }
3448
3449                 _listen_cnt++;
3450
3451         } else if (_listen_cnt > 0) {
3452
3453                 _listen_cnt--;
3454         }
3455 }
3456
3457 void
3458 Session::route_solo_isolated_changed (boost::weak_ptr<Route> wpr)
3459 {
3460         boost::shared_ptr<Route> route (wpr.lock());
3461
3462         if (!route) {
3463                 return;
3464         }
3465
3466         bool send_changed = false;
3467
3468         if (route->solo_isolate_control()->solo_isolated()) {
3469                 if (_solo_isolated_cnt == 0) {
3470                         send_changed = true;
3471                 }
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;
3477                 }
3478         }
3479
3480         if (send_changed) {
3481                 IsolatedChanged (); /* EMIT SIGNAL */
3482         }
3483 }
3484
3485 void
3486 Session::route_solo_changed (bool self_solo_changed, Controllable::GroupControlDisposition group_override,  boost::weak_ptr<Route> wpr)
3487 {
3488         DEBUG_TRACE (DEBUG::Solo, string_compose ("route solo change, self = %1, update\n", self_solo_changed));
3489
3490         boost::shared_ptr<Route> route (wpr.lock());
3491
3492         if (!route) {
3493                 return;
3494         }
3495
3496         if (Config->get_solo_control_is_listen_control()) {
3497                 route_listen_changed (group_override, wpr);
3498                 return;
3499         }
3500
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()));
3502
3503         if (route->solo_control()->transitioned_into_solo() == 0) {
3504                 /* route solo changed by upstream/downstream or clear all solo state; not interesting
3505                    to Session.
3506                 */
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()));
3508                 return;
3509         }
3510
3511         boost::shared_ptr<RouteList> r = routes.reader ();
3512         int32_t delta = route->solo_control()->transitioned_into_solo ();
3513
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.
3518          *
3519          * The first argument to the signal that this method is connected to is the
3520          * GroupControlDisposition value that was used to change solo.
3521          *
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
3527          * belongs to.
3528          */
3529
3530         RouteGroup* rg = route->route_group ();
3531         const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3532
3533         DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate to session, group accounted for ? %1\n", group_already_accounted_for));
3534
3535         if (delta == 1 && Config->get_exclusive_solo()) {
3536
3537                 /* new solo: disable all other solos, but not the group if its solo-enabled */
3538
3539                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3540
3541                         if ((*i) == route) {
3542                                 /* already changed */
3543                                 continue;
3544                         }
3545
3546                         if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3547                                 /* route does not get solo propagated to it */
3548                                 continue;
3549                         }
3550
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
3555                                  * here
3556                                  */
3557                                 continue;
3558                         }
3559
3560                         (*i)->solo_control()->set_value (0.0, group_override);
3561                 }
3562         }
3563
3564         DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate solo change, delta = %1\n", delta));
3565
3566         RouteList uninvolved;
3567
3568         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1\n", route->name()));
3569
3570         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3571                 bool via_sends_only;
3572                 bool in_signal_flow;
3573
3574                 if ((*i) == route) {
3575                         /* already changed */
3576                         continue;
3577                 }
3578
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(),
3582                                                                   (*i)->can_solo()));
3583                         continue;
3584                 }
3585
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
3590                          * here
3591                          */
3592                         continue;
3593                 }
3594
3595                 in_signal_flow = false;
3596
3597                 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed from %1\n", (*i)->name()));
3598
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);
3604                                 } else {
3605                                         DEBUG_TRACE (DEBUG::Solo, "\talready soloed by others upstream\n");
3606                                 }
3607                         } else {
3608                                 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tthere is a send-only feed from %1\n", (*i)->name()));
3609                         }
3610                         in_signal_flow = true;
3611                 } else {
3612                         DEBUG_TRACE (DEBUG::Solo, string_compose ("\tno feed from %1\n", (*i)->name()));
3613                 }
3614
3615                 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed to %1\n", (*i)->name()));
3616
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
3621                            sends are involved.
3622                         */
3623                         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 feeds %2 via sends only %3 sboD %4 sboU %5\n",
3624                                                                   route->name(),
3625                                                                   (*i)->name(),
3626                                                                   via_sends_only,
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);
3633                         } else {
3634                                 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tfeed to %1 ignored, sends-only\n", (*i)->name()));
3635                         }
3636                         in_signal_flow = true;
3637                 } else {
3638                         DEBUG_TRACE (DEBUG::Solo, string_compose("\tno feed to %1\n", (*i)->name()) );
3639                 }
3640
3641                 if (!in_signal_flow) {
3642                         uninvolved.push_back (*i);
3643                 }
3644         }
3645
3646         DEBUG_TRACE (DEBUG::Solo, "propagation complete\n");
3647
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.
3650         */
3651
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
3657                  */
3658         }
3659 }
3660
3661 void
3662 Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
3663 {
3664         /* now figure out if anything that matters is soloed (or is "listening")*/
3665
3666         bool something_soloed = false;
3667         bool something_listening = false;
3668         uint32_t listeners = 0;
3669         uint32_t isolated = 0;
3670
3671         if (!r) {
3672                 r = routes.reader();
3673         }
3674
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()) {
3679                                         listeners++;
3680                                         something_listening = true;
3681                                 }
3682                         } else {
3683                                 (*i)->set_listen (false);
3684                                 if ((*i)->can_solo() && (*i)->solo_control()->soloed_by_self_or_masters()) {
3685                                         something_soloed = true;
3686                                 }
3687                         }
3688                 }
3689
3690                 if ((*i)->solo_isolate_control()->solo_isolated()) {
3691                         isolated++;
3692                 }
3693         }
3694
3695         if (something_soloed != _non_soloed_outs_muted) {
3696                 _non_soloed_outs_muted = something_soloed;
3697                 SoloActive (_non_soloed_outs_muted); /* EMIT SIGNAL */
3698         }
3699
3700         if (something_listening != _listening) {
3701                 _listening = something_listening;
3702                 SoloActive (_listening);
3703         }
3704
3705         _listen_cnt = listeners;
3706
3707         if (isolated != _solo_isolated_cnt) {
3708                 _solo_isolated_cnt = isolated;
3709                 IsolatedChanged (); /* EMIT SIGNAL */
3710         }
3711
3712         DEBUG_TRACE (DEBUG::Solo, string_compose ("solo state updated by session, soloed? %1 listeners %2 isolated %3\n",
3713                                                   something_soloed, listeners, isolated));
3714
3715
3716         SoloChanged (); /* EMIT SIGNAL */
3717         set_dirty();
3718 }
3719
3720 bool
3721 Session::muted () const
3722 {
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.
3726         bool muted = false;
3727         StripableList all;
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()) {
3732                         continue;
3733                 }
3734                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(*i);
3735                 if (r && !r->active()) {
3736                         continue;
3737                 }
3738                 boost::shared_ptr<MuteControl> mc = (*i)->mute_control();
3739                 if (mc && mc->muted ()) {
3740                         muted = true;
3741                         break;
3742                 }
3743         }
3744         return muted;
3745 }
3746
3747 std::vector<boost::weak_ptr<AutomationControl> >
3748 Session::cancel_all_mute ()
3749 {
3750         StripableList all;
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()) {
3757                         continue;
3758                 }
3759                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*i);
3760                 if (r && !r->active()) {
3761                         continue;
3762                 }
3763                 boost::shared_ptr<AutomationControl> ac = (*i)->mute_control();
3764                 if (ac && ac->get_value () > 0) {
3765                         cl->push_back (ac);
3766                         muted.push_back (boost::weak_ptr<AutomationControl>(ac));
3767                 }
3768         }
3769         if (!cl->empty ()) {
3770                 set_controls (cl, 0.0, PBD::Controllable::UseGroup);
3771         }
3772         return muted;
3773 }
3774
3775 void
3776 Session::get_stripables (StripableList& sl, PresentationInfo::Flag fl) const
3777 {
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) {
3781                         sl.push_back (*it);
3782                 }
3783         }
3784
3785         if (fl & PresentationInfo::VCA) {
3786                 VCAList v = _vca_manager->vcas ();
3787                 sl.insert (sl.end(), v.begin(), v.end());
3788         }
3789 }
3790
3791 StripableList
3792 Session::get_stripables () const
3793 {
3794         PresentationInfo::Flag fl = PresentationInfo::AllStripables;
3795         StripableList rv;
3796         Session::get_stripables (rv, fl);
3797         rv.sort (Stripable::Sorter ());
3798         return rv;
3799 }
3800
3801 RouteList
3802 Session::get_routelist (bool mixer_order, PresentationInfo::Flag fl) const
3803 {
3804         boost::shared_ptr<RouteList> r = routes.reader ();
3805         RouteList rv;
3806         for (RouteList::iterator it = r->begin(); it != r->end(); ++it) {
3807                 if ((*it)->presentation_info ().flags () & fl) {
3808                         rv.push_back (*it);
3809                 }
3810         }
3811         rv.sort (Stripable::Sorter (mixer_order));
3812         return rv;
3813 }
3814
3815 boost::shared_ptr<RouteList>
3816 Session::get_routes_with_internal_returns() const
3817 {
3818         boost::shared_ptr<RouteList> r = routes.reader ();
3819         boost::shared_ptr<RouteList> rl (new RouteList);
3820
3821         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3822                 if ((*i)->internal_return ()) {
3823                         rl->push_back (*i);
3824                 }
3825         }
3826         return rl;
3827 }
3828
3829 bool
3830 Session::io_name_is_legal (const std::string& name) const
3831 {
3832         boost::shared_ptr<RouteList> r = routes.reader ();
3833
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;
3839                         }
3840                         /* all other instances of a reserved name are not allowed */
3841                         return false;
3842                 }
3843         }
3844
3845         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3846                 if ((*i)->name() == name) {
3847                         return false;
3848                 }
3849
3850                 if ((*i)->has_io_processor_named (name)) {
3851                         return false;
3852                 }
3853         }
3854
3855         return true;
3856 }
3857
3858 void
3859 Session::set_exclusive_input_active (boost::shared_ptr<RouteList> rl, bool onoff, bool flip_others)
3860 {
3861         RouteList rl2;
3862         vector<string> connections;
3863
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.
3866          */
3867
3868         if (flip_others == false && rl->size() == 1) {
3869                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (rl->front());
3870                 if (mt) {
3871                         mt->set_input_active (onoff);
3872                         return;
3873                 }
3874         }
3875
3876         for (RouteList::iterator rt = rl->begin(); rt != rl->end(); ++rt) {
3877
3878                 PortSet& ps ((*rt)->input()->ports());
3879
3880                 for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
3881                         p->get_connections (connections);
3882                 }
3883
3884                 for (vector<string>::iterator s = connections.begin(); s != connections.end(); ++s) {
3885                         routes_using_input_from (*s, rl2);
3886                 }
3887
3888                 /* scan all relevant routes to see if others are on or off */
3889
3890                 bool others_are_already_on = false;
3891
3892                 for (RouteList::iterator r = rl2.begin(); r != rl2.end(); ++r) {
3893
3894                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
3895
3896                         if (!mt) {
3897                                 continue;
3898                         }
3899
3900                         if ((*r) != (*rt)) {
3901                                 if (mt->input_active()) {
3902                                         others_are_already_on = true;
3903                                 }
3904                         } else {
3905                                 /* this one needs changing */
3906                                 mt->set_input_active (onoff);
3907                         }
3908                 }
3909
3910                 if (flip_others) {
3911
3912                         /* globally reverse other routes */
3913
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);
3917                                         if (mt) {
3918                                                 mt->set_input_active (!others_are_already_on);
3919                                         }
3920                                 }
3921                         }
3922                 }
3923         }
3924 }
3925
3926 void
3927 Session::routes_using_input_from (const string& str, RouteList& rl)
3928 {
3929         boost::shared_ptr<RouteList> r = routes.reader();
3930
3931         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3932                 if ((*i)->input()->connected_to (str)) {
3933                         rl.push_back (*i);
3934                 }
3935         }
3936 }
3937
3938 boost::shared_ptr<Route>
3939 Session::route_by_name (string name) const
3940 {
3941         boost::shared_ptr<RouteList> r = routes.reader ();
3942
3943         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3944                 if ((*i)->name() == name) {
3945                         return *i;
3946                 }
3947         }
3948
3949         return boost::shared_ptr<Route> ((Route*) 0);
3950 }
3951
3952 boost::shared_ptr<Route>
3953 Session::route_by_id (PBD::ID id) const
3954 {
3955         boost::shared_ptr<RouteList> r = routes.reader ();
3956
3957         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3958                 if ((*i)->id() == id) {
3959                         return *i;
3960                 }
3961         }
3962
3963         return boost::shared_ptr<Route> ((Route*) 0);
3964 }
3965
3966
3967 boost::shared_ptr<Stripable>
3968 Session::stripable_by_id (PBD::ID id) const
3969 {
3970         StripableList sl;
3971         get_stripables (sl);
3972
3973         for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
3974                 if ((*s)->id() == id) {
3975                         return *s;
3976                 }
3977         }
3978
3979         return boost::shared_ptr<Stripable>();
3980 }
3981
3982 boost::shared_ptr<Processor>
3983 Session::processor_by_id (PBD::ID id) const
3984 {
3985         boost::shared_ptr<RouteList> r = routes.reader ();
3986
3987         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3988                 boost::shared_ptr<Processor> p = (*i)->Route::processor_by_id (id);
3989                 if (p) {
3990                         return p;
3991                 }
3992         }
3993
3994         return boost::shared_ptr<Processor> ();
3995 }
3996
3997 boost::shared_ptr<Route>
3998 Session::get_remote_nth_route (PresentationInfo::order_t n) const
3999 {
4000         return boost::dynamic_pointer_cast<Route> (get_remote_nth_stripable (n, PresentationInfo::Route));
4001 }
4002
4003 boost::shared_ptr<Stripable>
4004 Session::get_remote_nth_stripable (PresentationInfo::order_t n, PresentationInfo::Flag flags) const
4005 {
4006         StripableList sl;
4007         PresentationInfo::order_t match_cnt = 0;
4008
4009         get_stripables (sl);
4010         sl.sort (Stripable::Sorter());
4011
4012         for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
4013
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.
4019
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.
4024                         */
4025
4026                         if (! (flags & PresentationInfo::Hidden)) {
4027                                 continue;
4028                         }
4029                 }
4030
4031                 if ((*s)->presentation_info().flag_match (flags)) {
4032                         if (match_cnt++ == n) {
4033                                 return *s;
4034                         }
4035                 }
4036         }
4037
4038         /* there is no nth stripable that matches the given flags */
4039         return boost::shared_ptr<Stripable>();
4040 }
4041
4042 boost::shared_ptr<Route>
4043 Session::route_by_selected_count (uint32_t id) const
4044 {
4045         RouteList r (*(routes.reader ()));
4046         r.sort (Stripable::Sorter());
4047
4048         RouteList::iterator i;
4049
4050         for (i = r.begin(); i != r.end(); ++i) {
4051                 if ((*i)->is_selected()) {
4052                         if (id == 0) {
4053                                 return *i;
4054                         }
4055                         --id;
4056                 }
4057         }
4058
4059         return boost::shared_ptr<Route> ();
4060 }
4061
4062 void
4063 Session::reassign_track_numbers ()
4064 {
4065         int64_t tn = 0;
4066         int64_t bn = 0;
4067         RouteList r (*(routes.reader ()));
4068         r.sort (Stripable::Sorter());
4069
4070         StateProtector sp (this);
4071
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);
4076                 }
4077                 else if (!(*i)->is_master() && !(*i)->is_monitor()) {
4078                         (*i)->set_track_number(--bn);
4079                 }
4080         }
4081         const uint32_t decimals = ceilf (log10f (tn + 1));
4082         const bool decimals_changed = _track_number_decimals != decimals;
4083         _track_number_decimals = decimals;
4084
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);
4088                         if (t) {
4089                                 t->resync_track_name();
4090                         }
4091                 }
4092                 // trigger GUI re-layout
4093                 config.ParameterChanged("track-name-number");
4094         }
4095
4096 #ifndef NDEBUG
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()));
4101                 }
4102         }
4103 #endif /* NDEBUG */
4104
4105 }
4106
4107 void
4108 Session::playlist_region_added (boost::weak_ptr<Region> w)
4109 {
4110         boost::shared_ptr<Region> r = w.lock ();
4111         if (!r) {
4112                 return;
4113         }
4114
4115         /* These are the operations that are currently in progress... */
4116         list<GQuark> curr = _current_trans_quarks;
4117         curr.sort ();
4118
4119         /* ...and these are the operations during which we want to update
4120            the session range location markers.
4121         */
4122         list<GQuark> ops;
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);
4136         ops.sort ();
4137
4138         /* See if any of the current operations match the ones that we want */
4139         list<GQuark> in;
4140         set_intersection (_current_trans_quarks.begin(), _current_trans_quarks.end(), ops.begin(), ops.end(), back_inserter (in));
4141
4142         /* If so, update the session range markers */
4143         if (!in.empty ()) {
4144                 maybe_update_session_range (r->position (), r->last_sample ());
4145         }
4146 }
4147
4148 /** Update the session range markers if a is before the current start or
4149  *  b is after the current end.
4150  */
4151 void
4152 Session::maybe_update_session_range (samplepos_t a, samplepos_t b)
4153 {
4154         if (loading ()) {
4155                 return;
4156         }
4157
4158         samplepos_t session_end_marker_shift_samples = session_end_shift * _nominal_sample_rate;
4159
4160         if (_session_range_location == 0) {
4161
4162                 set_session_range_location (a, b + session_end_marker_shift_samples);
4163
4164         } else {
4165
4166                 if (_session_range_is_free && (a < _session_range_location->start())) {
4167                         _session_range_location->set_start (a);
4168                 }
4169
4170                 if (_session_range_is_free && (b > _session_range_location->end())) {
4171                         _session_range_location->set_end (b);
4172                 }
4173         }
4174 }
4175
4176 void
4177 Session::set_session_range_is_free (bool yn)
4178 {
4179         _session_range_is_free = yn;
4180 }
4181
4182 void
4183 Session::playlist_ranges_moved (list<Evoral::RangeMove<samplepos_t> > const & ranges)
4184 {
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);
4187         }
4188 }
4189
4190 void
4191 Session::playlist_regions_extended (list<Evoral::Range<samplepos_t> > const & ranges)
4192 {
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);
4195         }
4196 }
4197
4198 /* Region management */
4199
4200 boost::shared_ptr<Region>
4201 Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
4202 {
4203         const RegionFactory::RegionMap& regions (RegionFactory::regions());
4204         RegionFactory::RegionMap::const_iterator i;
4205         boost::shared_ptr<Region> region;
4206
4207         Glib::Threads::Mutex::Lock lm (region_lock);
4208
4209         for (i = regions.begin(); i != regions.end(); ++i) {
4210
4211                 region = i->second;
4212
4213                 if (region->whole_file()) {
4214
4215                         if (child->source_equivalent (region)) {
4216                                 return region;
4217                         }
4218                 }
4219         }
4220
4221         return boost::shared_ptr<Region> ();
4222 }
4223
4224 int
4225 Session::destroy_sources (list<boost::shared_ptr<Source> > srcs)
4226 {
4227         set<boost::shared_ptr<Region> > relevant_regions;
4228
4229         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ++s) {
4230                 RegionFactory::get_regions_using_source (*s, relevant_regions);
4231         }
4232
4233         for (set<boost::shared_ptr<Region> >::iterator r = relevant_regions.begin(); r != relevant_regions.end(); ) {
4234                 set<boost::shared_ptr<Region> >::iterator tmp;
4235
4236                 tmp = r;
4237                 ++tmp;
4238
4239                 _playlists->destroy_region (*r);
4240                 RegionFactory::map_remove (*r);
4241
4242                 (*r)->drop_sources ();
4243                 (*r)->drop_references ();
4244
4245                 relevant_regions.erase (r);
4246
4247                 r = tmp;
4248         }
4249
4250         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ) {
4251
4252                 {
4253                         Glib::Threads::Mutex::Lock ls (source_lock);
4254                         /* remove from the main source list */
4255                         sources.erase ((*s)->id());
4256                 }
4257
4258                 (*s)->mark_for_remove ();
4259                 (*s)->drop_references ();
4260                 SourceRemoved(*s);
4261
4262                 s = srcs.erase (s);
4263         }
4264
4265         return 0;
4266 }
4267
4268 int
4269 Session::remove_last_capture ()
4270 {
4271         list<boost::shared_ptr<Source> > srcs;
4272
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);
4276                 if (!tr) {
4277                         continue;
4278                 }
4279
4280                 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4281
4282                 if (!l.empty()) {
4283                         srcs.insert (srcs.end(), l.begin(), l.end());
4284                         l.clear ();
4285                 }
4286         }
4287
4288         destroy_sources (srcs);
4289
4290         save_state (_current_snapshot_name);
4291
4292         return 0;
4293 }
4294
4295 void
4296 Session::get_last_capture_sources (std::list<boost::shared_ptr<Source> >& srcs)
4297 {
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);
4301                 if (!tr) {
4302                         continue;
4303                 }
4304
4305                 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4306
4307                 if (!l.empty()) {
4308                         srcs.insert (srcs.end(), l.begin(), l.end());
4309                         l.clear ();
4310                 }
4311         }
4312 }
4313
4314 /* Source Management */
4315
4316 void
4317 Session::add_source (boost::shared_ptr<Source> source)
4318 {
4319         pair<SourceMap::key_type, SourceMap::mapped_type> entry;
4320         pair<SourceMap::iterator,bool> result;
4321
4322         entry.first = source->id();
4323         entry.second = source;
4324
4325         {
4326                 Glib::Threads::Mutex::Lock lm (source_lock);
4327                 result = sources.insert (entry);
4328         }
4329
4330         if (result.second) {
4331
4332                 /* yay, new source */
4333
4334                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
4335
4336                 if (fs) {
4337                         if (!fs->within_session()) {
4338                                 ensure_search_path_includes (Glib::path_get_dirname (fs->path()), fs->type());
4339                         }
4340                 }
4341
4342                 set_dirty();
4343
4344                 boost::shared_ptr<AudioFileSource> afs;
4345
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);
4349                         }
4350                 }
4351
4352                 source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
4353
4354                 SourceAdded(source);
4355         }
4356 }
4357
4358 void
4359 Session::remove_source (boost::weak_ptr<Source> src)
4360 {
4361         if (deletion_in_progress ()) {
4362                 return;
4363         }
4364
4365         SourceMap::iterator i;
4366         boost::shared_ptr<Source> source = src.lock();
4367
4368         if (!source) {
4369                 return;
4370         }
4371
4372         {
4373                 Glib::Threads::Mutex::Lock lm (source_lock);
4374
4375                 if ((i = sources.find (source->id())) != sources.end()) {
4376                         sources.erase (i);
4377                         SourceRemoved(source);
4378                 }
4379         }
4380
4381         if (!in_cleanup () && !loading ()) {
4382
4383                 /* save state so we don't end up with a session file
4384                  * referring to non-existent sources.
4385                  */
4386
4387                 save_state (_current_snapshot_name);
4388         }
4389 }
4390
4391 boost::shared_ptr<Source>
4392 Session::source_by_id (const PBD::ID& id)
4393 {
4394         Glib::Threads::Mutex::Lock lm (source_lock);
4395         SourceMap::iterator i;
4396         boost::shared_ptr<Source> source;
4397
4398         if ((i = sources.find (id)) != sources.end()) {
4399                 source = i->second;
4400         }
4401
4402         return source;
4403 }
4404
4405 boost::shared_ptr<AudioFileSource>
4406 Session::audio_source_by_path_and_channel (const string& path, uint16_t chn) const
4407 {
4408         /* Restricted to audio files because only audio sources have channel
4409            as a property.
4410         */
4411
4412         Glib::Threads::Mutex::Lock lm (source_lock);
4413
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);
4417
4418                 if (afs && afs->path() == path && chn == afs->channel()) {
4419                         return afs;
4420                 }
4421         }
4422
4423         return boost::shared_ptr<AudioFileSource>();
4424 }
4425
4426 boost::shared_ptr<MidiSource>
4427 Session::midi_source_by_path (const std::string& path, bool need_source_lock) const
4428 {
4429         /* Restricted to MIDI files because audio sources require a channel
4430            for unique identification, in addition to a path.
4431         */
4432
4433         Glib::Threads::Mutex::Lock lm (source_lock, Glib::Threads::NOT_LOCK);
4434         if (need_source_lock) {
4435                 lm.acquire ();
4436         }
4437
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);
4443
4444                 if (ms && fs && fs->path() == path) {
4445                         return ms;
4446                 }
4447         }
4448
4449         return boost::shared_ptr<MidiSource>();
4450 }
4451
4452 uint32_t
4453 Session::count_sources_by_origin (const string& path)
4454 {
4455         uint32_t cnt = 0;
4456         Glib::Threads::Mutex::Lock lm (source_lock);
4457
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);
4461
4462                 if (fs && fs->origin() == path) {
4463                         ++cnt;
4464                 }
4465         }
4466
4467         return cnt;
4468 }
4469
4470 static string
4471 peak_file_helper (const string& peak_path, const string& file_path, const string& file_base, bool hash) {
4472         if (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);
4475         } else {
4476                 return Glib::build_filename (peak_path, file_base + peakfile_suffix);
4477         }
4478 }
4479
4480 string
4481 Session::construct_peak_filepath (const string& filepath, const bool in_session, const bool old_peak_name) const
4482 {
4483         string interchange_dir_string = string (interchange_dir_name) + G_DIR_SEPARATOR;
4484
4485         if (Glib::path_is_absolute (filepath)) {
4486
4487                 /* rip the session dir from the audiofile source */
4488
4489                 string session_path;
4490                 bool in_another_session = true;
4491
4492                 if (filepath.find (interchange_dir_string) != string::npos) {
4493
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 */
4498
4499                         /* see if it is within our session */
4500
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;
4504                                         break;
4505                                 }
4506                         }
4507                 } else {
4508                         in_another_session = false;
4509                 }
4510
4511
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);
4515                 }
4516         }
4517
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
4522          */
4523         std::string filename = Glib::path_get_basename (filepath);
4524         std::string path;
4525
4526         /* 2) if the file is outside our session dir:
4527          * (imported but not copied) add the path for check-summming */
4528         if (!in_session) {
4529                 path = Glib::path_get_dirname (filepath);
4530         }
4531
4532         return peak_file_helper (_session_dir->peak_path(), path, Glib::path_get_basename (filepath), !old_peak_name);
4533 }
4534
4535 string
4536 Session::new_audio_source_path_for_embedded (const std::string& path)
4537 {
4538         /* embedded source:
4539          *
4540          * we know that the filename is already unique because it exists
4541          * out in the filesystem.
4542          *
4543          * However, when we bring it into the session, we could get a
4544          * collision.
4545          *
4546          * Eg. two embedded files:
4547          *
4548          *          /foo/bar/baz.wav
4549          *          /frob/nic/baz.wav
4550          *
4551          * When merged into session, these collide.
4552          *
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.
4556          *
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.
4560          */
4561
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);
4565
4566         if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
4567
4568                 MD5 md5;
4569
4570                 md5.digestString (path.c_str());
4571                 md5.writeToString ();
4572                 base = md5.digestChars;
4573
4574                 string ext = get_suffix (path);
4575
4576                 if (!ext.empty()) {
4577                         base += '.';
4578                         base += ext;
4579                 }
4580
4581                 newpath = Glib::build_filename (sdir.sound_path(), base);
4582
4583                 /* if this collides, we're screwed */
4584
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;
4587                         return string();
4588                 }
4589
4590         }
4591
4592         return newpath;
4593 }
4594
4595 /** Return true if there are no audio file sources that use @param name as
4596  * the filename component of their path.
4597  *
4598  * Return false otherwise.
4599  *
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.
4602  *
4603  * If/when Ardour supports native files in non-mono formats, the logic here
4604  * will need to be revisited.
4605  */
4606 bool
4607 Session::audio_source_name_is_unique (const string& name)
4608 {
4609         std::vector<string> sdirs = source_search_path (DataType::AUDIO);
4610         vector<space_and_path>::iterator i;
4611         uint32_t existing = 0;
4612
4613         for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4614
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.
4619                 */
4620
4621                 const string spath = *i;
4622
4623                 if (matching_unsuffixed_filename_exists_in (spath, name)) {
4624                         existing++;
4625                         break;
4626                 }
4627
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.
4635                  */
4636
4637
4638                 string possible_path = Glib::build_filename (spath, name);
4639
4640                 if (audio_source_by_path_and_channel (possible_path, 0)) {
4641                         existing++;
4642                         break;
4643                 }
4644         }
4645
4646         return (existing == 0);
4647 }
4648
4649 string
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)
4651 {
4652         ostringstream sstr;
4653         const string ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
4654
4655         sstr << legalized_base;
4656
4657         if (take_required || related_exists) {
4658                 sstr << '-';
4659                 sstr << cnt;
4660         }
4661
4662         if (nchan == 2) {
4663                 if (chan == 0) {
4664                         sstr << "%L";
4665                 } else {
4666                         sstr << "%R";
4667                 }
4668         } else if (nchan > 2) {
4669                 if (nchan < 26) {
4670                         sstr << '%';
4671                         sstr << 'a' + chan;
4672                 } else {
4673                         /* XXX what? more than 26 channels! */
4674                         sstr << '%';
4675                         sstr << chan+1;
4676                 }
4677         }
4678
4679         sstr << ext;
4680
4681         return sstr.str();
4682 }
4683
4684 /** Return a unique name based on \a base for a new internal audio source */
4685 string
4686 Session::new_audio_source_path (const string& base, uint32_t nchan, uint32_t chan, bool destructive, bool take_required)
4687 {
4688         uint32_t cnt;
4689         string possible_name;
4690         const uint32_t limit = 9999; // arbitrary limit on number of files with the same basic name
4691         string legalized;
4692         bool some_related_source_name_exists = false;
4693
4694         legalized = legalize_for_path (base);
4695
4696         // Find a "version" of the base name that doesn't exist in any of the possible directories.
4697
4698         for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
4699
4700                 possible_name = format_audio_source_name (legalized, nchan, chan, destructive, take_required, cnt, some_related_source_name_exists);
4701
4702                 if (audio_source_name_is_unique (possible_name)) {
4703                         break;
4704                 }
4705
4706                 some_related_source_name_exists = true;
4707
4708                 if (cnt > limit) {
4709                         error << string_compose(
4710                                         _("There are already %1 recordings for %2, which I consider too many."),
4711                                         limit, base) << endmsg;
4712                         destroy ();
4713                         throw failed_constructor();
4714                 }
4715         }
4716
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
4719          * audio source.
4720          */
4721
4722         SessionDirectory sdir (get_best_session_directory_for_new_audio());
4723
4724         std::string s = Glib::build_filename (sdir.sound_path(), possible_name);
4725
4726         return s;
4727 }
4728
4729 /** Return a unique name based on `base` for a new internal MIDI source */
4730 string
4731 Session::new_midi_source_path (const string& base, bool need_lock)
4732 {
4733         string possible_path;
4734         string possible_name;
4735
4736         possible_name = legalize_for_path (base);
4737
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);
4740
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
4745          *
4746          * -> reverse the array, check main session folder last and use that as location
4747          *    for MIDI files.
4748          */
4749         std::reverse(sdirs.begin(), sdirs.end());
4750
4751         while (true) {
4752                 possible_name = bump_name_once (possible_name, '-');
4753
4754                 vector<space_and_path>::iterator i;
4755                 uint32_t existing = 0;
4756
4757                 for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4758
4759                         possible_path = Glib::build_filename (*i, possible_name + ".mid");
4760
4761                         if (Glib::file_test (possible_path, Glib::FILE_TEST_EXISTS)) {
4762                                 existing++;
4763                         }
4764
4765                         if (midi_source_by_path (possible_path, need_lock)) {
4766                                 existing++;
4767                         }
4768                 }
4769
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;
4774                         destroy ();
4775                         return 0;
4776                 }
4777
4778                 if (existing == 0) {
4779                         break;
4780                 }
4781         }
4782
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.
4785         */
4786
4787         return possible_path;
4788 }
4789
4790
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)
4794 {
4795         const string path = new_audio_source_path (base, n_chans, chan, destructive, true);
4796
4797         if (!path.empty()) {
4798                 return boost::dynamic_pointer_cast<AudioFileSource> (
4799                         SourceFactory::createWritable (DataType::AUDIO, *this, path, destructive, sample_rate(), true, true));
4800         } else {
4801                 throw failed_constructor ();
4802         }
4803 }
4804
4805 /** Create a new within-session MIDI source */
4806 boost::shared_ptr<MidiSource>
4807 Session::create_midi_source_for_session (string const & basic_name)
4808 {
4809         const string path = new_midi_source_path (basic_name);
4810
4811         if (!path.empty()) {
4812                 return boost::dynamic_pointer_cast<SMFSource> (
4813                         SourceFactory::createWritable (
4814                                 DataType::MIDI, *this, path, false, sample_rate()));
4815         } else {
4816                 throw failed_constructor ();
4817         }
4818 }
4819
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)
4823 {
4824         /* the caller passes in the track the source will be used in,
4825            so that we can keep the numbering sane.
4826
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.
4830
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?
4835
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
4838            use "Foo-N+1" here.
4839
4840            If that attempted rename fails, we get "Foo-N+2.mid" anyway.
4841         */
4842
4843         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (track);
4844         assert (mt);
4845         std::string name = track->steal_write_source_name ();
4846
4847         if (name.empty()) {
4848                 return boost::shared_ptr<MidiSource>();
4849         }
4850
4851         /* MIDI files are small, just put them in the first location of the
4852            session source search path.
4853         */
4854
4855         const string path = Glib::build_filename (source_search_path (DataType::MIDI).front(), name);
4856
4857         return boost::dynamic_pointer_cast<SMFSource> (
4858                 SourceFactory::createWritable (
4859                         DataType::MIDI, *this, path, false, sample_rate()));
4860 }
4861
4862 bool
4863 Session::playlist_is_active (boost::shared_ptr<Playlist> playlist)
4864 {
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 ) {
4868                         return true;
4869                 }
4870         }
4871         return false;
4872 }
4873
4874 void
4875 Session::add_playlist (boost::shared_ptr<Playlist> playlist, bool unused)
4876 {
4877         if (playlist->hidden()) {
4878                 return;
4879         }
4880
4881         _playlists->add (playlist);
4882
4883         if (unused) {
4884                 playlist->release();
4885         }
4886
4887         set_dirty();
4888 }
4889
4890 void
4891 Session::remove_playlist (boost::weak_ptr<Playlist> weak_playlist)
4892 {
4893         if (deletion_in_progress ()) {
4894                 return;
4895         }
4896
4897         boost::shared_ptr<Playlist> playlist (weak_playlist.lock());
4898
4899         if (!playlist) {
4900                 return;
4901         }
4902
4903         _playlists->remove (playlist);
4904
4905         set_dirty();
4906 }
4907
4908 void
4909 Session::set_audition (boost::shared_ptr<Region> r)
4910 {
4911         pending_audition_region = r;
4912         add_post_transport_work (PostTransportAudition);
4913         _butler->schedule_transport_work ();
4914 }
4915
4916 void
4917 Session::audition_playlist ()
4918 {
4919         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
4920         ev->region.reset ();
4921         queue_event (ev);
4922 }
4923
4924
4925 void
4926 Session::register_lua_function (
4927                 const std::string& name,
4928                 const std::string& script,
4929                 const LuaScriptParamList& args
4930                 )
4931 {
4932         Glib::Threads::Mutex::Lock lm (lua_lock);
4933
4934         lua_State* L = lua.getState();
4935
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;
4941         }
4942         (*_lua_add)(name, bytecode, tbl_arg); // throws luabridge::LuaException
4943         lm.release();
4944
4945         LuaScriptsChanged (); /* EMIT SIGNAL */
4946         set_dirty();
4947 }
4948
4949 void
4950 Session::unregister_lua_function (const std::string& name)
4951 {
4952         Glib::Threads::Mutex::Lock lm (lua_lock);
4953         (*_lua_del)(name); // throws luabridge::LuaException
4954         lua.collect_garbage ();
4955         lm.release();
4956
4957         LuaScriptsChanged (); /* EMIT SIGNAL */
4958         set_dirty();
4959 }
4960
4961 std::vector<std::string>
4962 Session::registered_lua_functions ()
4963 {
4964         Glib::Threads::Mutex::Lock lm (lua_lock);
4965         std::vector<std::string> rv;
4966
4967         try {
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> ());
4972                 }
4973         } catch (...) { }
4974         return rv;
4975 }
4976
4977 #ifndef NDEBUG
4978 static void _lua_print (std::string s) {
4979         std::cout << "SessionLua: " << s << "\n";
4980 }
4981 #endif
4982
4983 void
4984 Session::try_run_lua (pframes_t nframes)
4985 {
4986         if (_n_lua_scripts == 0) return;
4987         Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
4988         if (tm.locked ()) {
4989                 try { (*_lua_run)(nframes); } catch (...) { }
4990                 lua.collect_garbage_step ();
4991         }
4992 }
4993
4994 void
4995 Session::setup_lua ()
4996 {
4997 #ifndef NDEBUG
4998         lua.Print.connect (&_lua_print);
4999 #endif
5000         lua.sandbox (true);
5001         lua.do_command (
5002                         "function ArdourSession ()"
5003                         "  local self = { scripts = {}, instances = {} }"
5004                         ""
5005                         "  local remove = function (n)"
5006                         "   self.scripts[n] = nil"
5007                         "   self.instances[n] = nil"
5008                         "   Session:scripts_changed()" // call back
5009                         "  end"
5010                         ""
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
5020                         "  end"
5021                         ""
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)"
5027                         "  end"
5028                         ""
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)"
5034                         "       remove (n)"
5035                         "      end"
5036                         "   end"
5037                         "   collectgarbage()"
5038                         "  end"
5039                         ""
5040                         "  local cleanup = function ()"
5041                         "   self.scripts = nil"
5042                         "   self.instances = nil"
5043                         "  end"
5044                         ""
5045                         "  local list = function ()"
5046                         "   local rv = {}"
5047                         "   for n, _ in pairs (self.scripts) do"
5048                         "     rv[n] = true"
5049                         "   end"
5050                         "   return rv"
5051                         "  end"
5052                         ""
5053                         "  local function basic_serialize (o)"
5054                         "    if type(o) == \"number\" then"
5055                         "     return tostring(o)"
5056                         "    else"
5057                         "     return string.format(\"%q\", o)"
5058                         "    end"
5059                         "  end"
5060                         ""
5061                         "  local function serialize (name, value)"
5062                         "   local rv = name .. ' = '"
5063                         "   collectgarbage()"
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"
5067                         "    rv = rv .. '{} '"
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 :(
5072                         "    end"
5073                         "    return rv;"
5074                         "   elseif type(value) == \"function\" then"
5075                         "     return rv .. string.format(\"%q\", string.dump(value, true))"
5076                         "   else"
5077                         "    error('cannot save a ' .. type(value))"
5078                         "   end"
5079                         "  end"
5080                         ""
5081                         ""
5082                         "  local save = function ()"
5083                         "   return (serialize('scripts', self.scripts))"
5084                         "  end"
5085                         ""
5086                         "  local restore = function (state)"
5087                         "   self.scripts = {}"
5088                         "   load (state)()"
5089                         "   for n, s in pairs (scripts) do"
5090                         "    addinternal (n, load(s['f']), s['a'])"
5091                         "   end"
5092                         "  end"
5093                         ""
5094                         " return { run = run, add = add, remove = remove,"
5095                   "          list = list, restore = restore, save = save, cleanup = cleanup}"
5096                         " end"
5097                         " "
5098                         " sess = ArdourSession ()"
5099                         " ArdourSession = nil"
5100                         " "
5101                         "function ardour () end"
5102                         );
5103
5104         lua_State* L = lua.getState();
5105
5106         try {
5107                 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
5108                 lua.do_command ("sess = nil"); // hide it.
5109                 lua.do_command ("collectgarbage()");
5110
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 ())
5121                         << endmsg;
5122                 abort(); /*NOTREACHED*/
5123         } catch (...) {
5124                 fatal << string_compose (_("programming error: %1"),
5125                                 X_("Failed to setup session Lua interpreter"))
5126                         << endmsg;
5127                 abort(); /*NOTREACHED*/
5128         }
5129
5130         lua_mlock (L, 1);
5131         LuaBindings::stddef (L);
5132         LuaBindings::common (L);
5133         LuaBindings::dsp (L);
5134         lua_mlock (L, 0);
5135         luabridge::push <Session *> (L, this);
5136         lua_setglobal (L, "Session");
5137 }
5138
5139 void
5140 Session::scripts_changed ()
5141 {
5142         assert (!lua_lock.trylock()); // must hold lua_lock
5143
5144         try {
5145                 luabridge::LuaRef list ((*_lua_list)());
5146                 int cnt = 0;
5147                 for (luabridge::Iterator i (list); !i.isNil (); ++i) {
5148                         if (!i.key ().isString ()) { assert(0); continue; }
5149                         ++cnt;
5150                 }
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 ())
5155                         << endmsg;
5156                 abort(); /*NOTREACHED*/
5157         } catch (...) {
5158                 fatal << string_compose (_("programming error: %1"),
5159                                 X_("Indexing Lua Session Scripts failed."))
5160                         << endmsg;
5161                 abort(); /*NOTREACHED*/
5162         }
5163 }
5164
5165 void
5166 Session::non_realtime_set_audition ()
5167 {
5168         assert (pending_audition_region);
5169         auditioner->audition_region (pending_audition_region);
5170         pending_audition_region.reset ();
5171         AuditionActive (true); /* EMIT SIGNAL */
5172 }
5173
5174 void
5175 Session::audition_region (boost::shared_ptr<Region> r)
5176 {
5177         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
5178         ev->region = r;
5179         queue_event (ev);
5180 }
5181
5182 void
5183 Session::cancel_audition ()
5184 {
5185         if (!auditioner) {
5186                 return;
5187         }
5188         if (auditioner->auditioning()) {
5189                 auditioner->cancel_audition ();
5190                 AuditionActive (false); /* EMIT SIGNAL */
5191         }
5192 }
5193
5194 bool
5195 Session::is_auditioning () const
5196 {
5197         /* can be called before we have an auditioner object */
5198         if (auditioner) {
5199                 return auditioner->auditioning();
5200         } else {
5201                 return false;
5202         }
5203 }
5204
5205 void
5206 Session::graph_reordered ()
5207 {
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.
5210         */
5211
5212         if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _reconnecting_routes_in_progress || _route_deletion_in_progress) {
5213                 return;
5214         }
5215
5216         resort_routes ();
5217
5218         /* force all diskstreams to update their capture offset values to
5219          * reflect any changes in latencies within the graph.
5220          */
5221         update_latency_compensation (true);
5222 }
5223
5224 /** @return Number of samples that there is disk space available to write,
5225  *  if known.
5226  */
5227 boost::optional<samplecnt_t>
5228 Session::available_capture_duration ()
5229 {
5230         Glib::Threads::Mutex::Lock lm (space_lock);
5231
5232         if (_total_free_4k_blocks_uncertain) {
5233                 return boost::optional<samplecnt_t> ();
5234         }
5235
5236         float sample_bytes_on_disk = 4.0; // keep gcc happy
5237
5238         switch (config.get_native_file_data_format()) {
5239         case FormatFloat:
5240                 sample_bytes_on_disk = 4.0;
5241                 break;
5242
5243         case FormatInt24:
5244                 sample_bytes_on_disk = 3.0;
5245                 break;
5246
5247         case FormatInt16:
5248                 sample_bytes_on_disk = 2.0;
5249                 break;
5250
5251         default:
5252                 /* impossible, but keep some gcc versions happy */
5253                 fatal << string_compose (_("programming error: %1"),
5254                                          X_("illegal native file data format"))
5255                       << endmsg;
5256                 abort(); /*NOTREACHED*/
5257         }
5258
5259         double scale = 4096.0 / sample_bytes_on_disk;
5260
5261         if (_total_free_4k_blocks * scale > (double) max_samplecnt) {
5262                 return max_samplecnt;
5263         }
5264
5265         return (samplecnt_t) floor (_total_free_4k_blocks * scale);
5266 }
5267
5268 void
5269 Session::tempo_map_changed (const PropertyChange&)
5270 {
5271         clear_clicks ();
5272
5273         _playlists->update_after_tempo_map_change ();
5274
5275         _locations->apply (*this, &Session::update_locations_after_tempo_map_change);
5276
5277         set_dirty ();
5278 }
5279
5280 void
5281 Session::update_locations_after_tempo_map_change (const Locations::LocationList& loc)
5282 {
5283         for (Locations::LocationList::const_iterator i = loc.begin(); i != loc.end(); ++i) {
5284                 (*i)->recompute_samples_from_beat ();
5285         }
5286 }
5287
5288 /** Ensures that all buffers (scratch, send, silent, etc) are allocated for
5289  * the given count with the current block size.
5290  */
5291 void
5292 Session::ensure_buffers (ChanCount howmany)
5293 {
5294         BufferManager::ensure_buffers (howmany, bounce_processing() ? bounce_chunk_size : 0);
5295 }
5296
5297 void
5298 Session::ensure_buffer_set(BufferSet& buffers, const ChanCount& count)
5299 {
5300         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
5301                 buffers.ensure_buffers(*t, count.get(*t), _engine.raw_buffer_size(*t));
5302         }
5303 }
5304
5305 uint32_t
5306 Session::next_insert_id ()
5307 {
5308         /* this doesn't really loop forever. just think about it */
5309
5310         while (true) {
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;
5314                                 return n;
5315
5316                         }
5317                 }
5318
5319                 /* none available, so resize and try again */
5320
5321                 insert_bitset.resize (insert_bitset.size() + 16, false);
5322         }
5323 }
5324
5325 uint32_t
5326 Session::next_send_id ()
5327 {
5328         /* this doesn't really loop forever. just think about it */
5329
5330         while (true) {
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;
5334                                 return n;
5335
5336                         }
5337                 }
5338
5339                 /* none available, so resize and try again */
5340
5341                 send_bitset.resize (send_bitset.size() + 16, false);
5342         }
5343 }
5344
5345 uint32_t
5346 Session::next_aux_send_id ()
5347 {
5348         /* this doesn't really loop forever. just think about it */
5349
5350         while (true) {
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;
5354                                 return n;
5355
5356                         }
5357                 }
5358
5359                 /* none available, so resize and try again */
5360
5361                 aux_send_bitset.resize (aux_send_bitset.size() + 16, false);
5362         }
5363 }
5364
5365 uint32_t
5366 Session::next_return_id ()
5367 {
5368         /* this doesn't really loop forever. just think about it */
5369
5370         while (true) {
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;
5374                                 return n;
5375
5376                         }
5377                 }
5378
5379                 /* none available, so resize and try again */
5380
5381                 return_bitset.resize (return_bitset.size() + 16, false);
5382         }
5383 }
5384
5385 void
5386 Session::mark_send_id (uint32_t id)
5387 {
5388         if (id >= send_bitset.size()) {
5389                 send_bitset.resize (id+16, false);
5390         }
5391         if (send_bitset[id]) {
5392                 warning << string_compose (_("send ID %1 appears to be in use already"), id) << endmsg;
5393         }
5394         send_bitset[id] = true;
5395 }
5396
5397 void
5398 Session::mark_aux_send_id (uint32_t id)
5399 {
5400         if (id >= aux_send_bitset.size()) {
5401                 aux_send_bitset.resize (id+16, false);
5402         }
5403         if (aux_send_bitset[id]) {
5404                 warning << string_compose (_("aux send ID %1 appears to be in use already"), id) << endmsg;
5405         }
5406         aux_send_bitset[id] = true;
5407 }
5408
5409 void
5410 Session::mark_return_id (uint32_t id)
5411 {
5412         if (id >= return_bitset.size()) {
5413                 return_bitset.resize (id+16, false);
5414         }
5415         if (return_bitset[id]) {
5416                 warning << string_compose (_("return ID %1 appears to be in use already"), id) << endmsg;
5417         }
5418         return_bitset[id] = true;
5419 }
5420
5421 void
5422 Session::mark_insert_id (uint32_t id)
5423 {
5424         if (id >= insert_bitset.size()) {
5425                 insert_bitset.resize (id+16, false);
5426         }
5427         if (insert_bitset[id]) {
5428                 warning << string_compose (_("insert ID %1 appears to be in use already"), id) << endmsg;
5429         }
5430         insert_bitset[id] = true;
5431 }
5432
5433 void
5434 Session::unmark_send_id (uint32_t id)
5435 {
5436         if (id < send_bitset.size()) {
5437                 send_bitset[id] = false;
5438         }
5439 }
5440
5441 void
5442 Session::unmark_aux_send_id (uint32_t id)
5443 {
5444         if (id < aux_send_bitset.size()) {
5445                 aux_send_bitset[id] = false;
5446         }
5447 }
5448
5449 void
5450 Session::unmark_return_id (uint32_t id)
5451 {
5452         if (deletion_in_progress ()) {
5453                 return;
5454         }
5455         if (id < return_bitset.size()) {
5456                 return_bitset[id] = false;
5457         }
5458 }
5459
5460 void
5461 Session::unmark_insert_id (uint32_t id)
5462 {
5463         if (id < insert_bitset.size()) {
5464                 insert_bitset[id] = false;
5465         }
5466 }
5467
5468 void
5469 Session::reset_native_file_format ()
5470 {
5471         boost::shared_ptr<RouteList> rl = routes.reader ();
5472
5473         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
5474                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5475                 if (tr) {
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);
5480                 }
5481         }
5482 }
5483
5484 bool
5485 Session::route_name_unique (string n) const
5486 {
5487         boost::shared_ptr<RouteList> r = routes.reader ();
5488
5489         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5490                 if ((*i)->name() == n) {
5491                         return false;
5492                 }
5493         }
5494
5495         return true;
5496 }
5497
5498 bool
5499 Session::route_name_internal (string n) const
5500 {
5501         if (auditioner && auditioner->name() == n) {
5502                 return true;
5503         }
5504
5505         if (_click_io && _click_io->name() == n) {
5506                 return true;
5507         }
5508
5509         return false;
5510 }
5511
5512 int
5513 Session::freeze_all (InterThreadInfo& itt)
5514 {
5515         boost::shared_ptr<RouteList> r = routes.reader ();
5516
5517         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
5518
5519                 boost::shared_ptr<Track> t;
5520
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
5523                            of every track.
5524                         */
5525                         t->freeze_me (itt);
5526                 }
5527         }
5528
5529         return 0;
5530 }
5531
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)
5538 {
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;
5545         samplepos_t to_do;
5546         samplepos_t latency_skip;
5547         BufferSet buffers;
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;
5553
5554         DataType data_type = track.data_type();
5555
5556         if (end <= start) {
5557                 error << string_compose (_("Cannot write a range where end <= start (e.g. %1 <= %2)"),
5558                                          end, start) << endmsg;
5559                 return result;
5560         }
5561
5562         diskstream_channels = track.bounce_get_output_streams (diskstream_channels, endpoint,
5563                         include_endpoint, for_export, for_freeze);
5564
5565         if (data_type == DataType::MIDI && endpoint && !for_export && !for_freeze && diskstream_channels.n(DataType::AUDIO) > 0) {
5566                 data_type = DataType::AUDIO;
5567         }
5568
5569         if (diskstream_channels.n(data_type) < 1) {
5570                 error << _("Cannot write a range with no data.") << endmsg;
5571                 return result;
5572         }
5573
5574         // block all process callback handling
5575
5576         block_processing ();
5577
5578         {
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());
5584         }
5585
5586         _bounce_processing_active = true;
5587
5588         /* call tree *MUST* hold route_lock */
5589
5590         if ((playlist = track.playlist()) == 0) {
5591                 goto out;
5592         }
5593
5594         legal_playlist_name = "(bounce)" + legalize_for_path (playlist->name());
5595
5596         for (uint32_t chan_n = 0; chan_n < diskstream_channels.n(data_type); ++chan_n) {
5597
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));
5602
5603                 if (path.empty()) {
5604                         goto out;
5605                 }
5606
5607                 try {
5608                         source = SourceFactory::createWritable (data_type, *this, path, false, sample_rate());
5609                 }
5610
5611                 catch (failed_constructor& err) {
5612                         error << string_compose (_("cannot create new file \"%1\" for %2"), path, track.name()) << endmsg;
5613                         goto out;
5614                 }
5615
5616                 srcs.push_back (source);
5617         }
5618
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.
5622          */
5623
5624         need_block_size_reset = true;
5625         track.set_block_size (bounce_chunk_size);
5626         _engine.main_thread()->get_buffers ();
5627
5628         position = start;
5629         to_do = len;
5630         latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5631
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);
5635         }
5636         buffers.set_count (max_proc);
5637
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;
5641                 if (afs) {
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);
5646                 }
5647         }
5648
5649         while (to_do && !itt.cancel) {
5650
5651                 this_chunk = min (to_do, bounce_chunk_size);
5652
5653                 if (track.export_stuff (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze)) {
5654                         goto out;
5655                 }
5656
5657                 start += this_chunk;
5658                 to_do -= this_chunk;
5659                 itt.progress = (float) (1.0 - ((double) to_do / len));
5660
5661                 if (latency_skip >= bounce_chunk_size) {
5662                         latency_skip -= bounce_chunk_size;
5663                         continue;
5664                 }
5665
5666                 const samplecnt_t current_chunk = this_chunk - latency_skip;
5667
5668                 uint32_t n = 0;
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;
5672
5673                         if (afs) {
5674                                 if (afs->write (buffers.get_audio(n).data(latency_skip), current_chunk) != current_chunk) {
5675                                         goto out;
5676                                 }
5677                         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5678                                 Source::Lock lock(ms->mutex());
5679
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);
5685                                         }
5686                                         ms->append_event_samples(lock, ev, ms->natural_position());
5687                                 }
5688                         }
5689                 }
5690                 latency_skip = 0;
5691         }
5692
5693         /* post-roll, pick up delayed processor output */
5694         latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5695
5696         while (latency_skip && !itt.cancel) {
5697                 this_chunk = min (latency_skip, bounce_chunk_size);
5698                 latency_skip -= this_chunk;
5699
5700                 buffers.silence (this_chunk, 0);
5701                 track.bounce_process (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze);
5702
5703                 uint32_t n = 0;
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);
5706
5707                         if (afs) {
5708                                 if (afs->write (buffers.get_audio(n).data(), this_chunk) != this_chunk) {
5709                                         goto out;
5710                                 }
5711                         }
5712                 }
5713         }
5714
5715         if (!itt.cancel) {
5716
5717                 time_t now;
5718                 struct tm* xnow;
5719                 time (&now);
5720                 xnow = localtime (&now);
5721
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;
5725
5726                         if (afs) {
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);
5732                         }
5733                 }
5734
5735                 /* construct a region to represent the bounced material */
5736
5737                 PropertyList plist;
5738
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));
5743
5744                 result = RegionFactory::create (srcs, plist, true);
5745
5746         }
5747
5748         out:
5749         if (!result) {
5750                 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5751                         (*src)->mark_for_remove ();
5752                         (*src)->drop_references ();
5753                 }
5754
5755         } else {
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);
5758
5759                         if (afs)
5760                                 afs->done_with_peakfile_writes ();
5761                 }
5762         }
5763
5764         _bounce_processing_active = false;
5765
5766         if (need_block_size_reset) {
5767                 _engine.main_thread()->drop_buffers ();
5768                 track.set_block_size (get_block_size());
5769         }
5770
5771         unblock_processing ();
5772
5773         return result;
5774 }
5775
5776 gain_t*
5777 Session::gain_automation_buffer() const
5778 {
5779         return ProcessThread::gain_automation_buffer ();
5780 }
5781
5782 gain_t*
5783 Session::trim_automation_buffer() const
5784 {
5785         return ProcessThread::trim_automation_buffer ();
5786 }
5787
5788 gain_t*
5789 Session::send_gain_automation_buffer() const
5790 {
5791         return ProcessThread::send_gain_automation_buffer ();
5792 }
5793
5794 gain_t*
5795 Session::scratch_automation_buffer() const
5796 {
5797         return ProcessThread::scratch_automation_buffer ();
5798 }
5799
5800 pan_t**
5801 Session::pan_automation_buffer() const
5802 {
5803         return ProcessThread::pan_automation_buffer ();
5804 }
5805
5806 BufferSet&
5807 Session::get_silent_buffers (ChanCount count)
5808 {
5809         return ProcessThread::get_silent_buffers (count);
5810 }
5811
5812 BufferSet&
5813 Session::get_scratch_buffers (ChanCount count, bool silence)
5814 {
5815         return ProcessThread::get_scratch_buffers (count, silence);
5816 }
5817
5818 BufferSet&
5819 Session::get_noinplace_buffers (ChanCount count)
5820 {
5821         return ProcessThread::get_noinplace_buffers (count);
5822 }
5823
5824 BufferSet&
5825 Session::get_route_buffers (ChanCount count, bool silence)
5826 {
5827         return ProcessThread::get_route_buffers (count, silence);
5828 }
5829
5830
5831 BufferSet&
5832 Session::get_mix_buffers (ChanCount count)
5833 {
5834         return ProcessThread::get_mix_buffers (count);
5835 }
5836
5837 uint32_t
5838 Session::ntracks () const
5839 {
5840         uint32_t n = 0;
5841         boost::shared_ptr<RouteList> r = routes.reader ();
5842
5843         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5844                 if (boost::dynamic_pointer_cast<Track> (*i)) {
5845                         ++n;
5846                 }
5847         }
5848
5849         return n;
5850 }
5851
5852 uint32_t
5853 Session::nbusses () const
5854 {
5855         uint32_t n = 0;
5856         boost::shared_ptr<RouteList> r = routes.reader ();
5857
5858         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5859                 if (boost::dynamic_pointer_cast<Track>(*i) == 0) {
5860                         ++n;
5861                 }
5862         }
5863
5864         return n;
5865 }
5866
5867 uint32_t
5868 Session::nstripables (bool with_monitor) const
5869 {
5870         uint32_t rv = routes.reader()->size ();
5871         rv += _vca_manager->vcas ().size ();
5872
5873         if (with_monitor) {
5874                 return rv;
5875         }
5876
5877         if (_monitor_out) {
5878                 assert (rv > 0);
5879                 --rv;
5880         }
5881         return rv;
5882 }
5883
5884 bool
5885 Session::plot_process_graph (std::string const& file_name) const {
5886         return _process_graph ? _process_graph->plot (file_name) : false;
5887 }
5888
5889 void
5890 Session::add_automation_list(AutomationList *al)
5891 {
5892         automation_lists[al->id()] = al;
5893 }
5894
5895 /** @return true if there is at least one record-enabled track, otherwise false */
5896 bool
5897 Session::have_rec_enabled_track () const
5898 {
5899         return g_atomic_int_get (const_cast<gint*>(&_have_rec_enabled_track)) == 1;
5900 }
5901
5902 bool
5903 Session::have_rec_disabled_track () const
5904 {
5905         return g_atomic_int_get (const_cast<gint*>(&_have_rec_disabled_track)) == 1;
5906 }
5907
5908 /** Update the state of our rec-enabled tracks flag */
5909 void
5910 Session::update_route_record_state ()
5911 {
5912         boost::shared_ptr<RouteList> rl = routes.reader ();
5913         RouteList::iterator i = rl->begin();
5914         while (i != rl->end ()) {
5915
5916                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5917                                     if (tr && tr->rec_enable_control()->get_value()) {
5918                         break;
5919                 }
5920
5921                 ++i;
5922         }
5923
5924         int const old = g_atomic_int_get (&_have_rec_enabled_track);
5925
5926         g_atomic_int_set (&_have_rec_enabled_track, i != rl->end () ? 1 : 0);
5927
5928         if (g_atomic_int_get (&_have_rec_enabled_track) != old) {
5929                 RecordStateChanged (); /* EMIT SIGNAL */
5930         }
5931
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()) {
5935                         break;
5936                 }
5937         }
5938
5939         g_atomic_int_set (&_have_rec_disabled_track, i != rl->end () ? 1 : 0);
5940
5941         bool record_arm_state_changed = (old != g_atomic_int_get (&_have_rec_enabled_track) );
5942
5943         if (record_status() == Recording && record_arm_state_changed ) {
5944                 RecordArmStateChanged ();
5945         }
5946
5947 }
5948
5949 void
5950 Session::listen_position_changed ()
5951 {
5952         if (loading ()) {
5953                 /* skip duing session restore (already taken care of) */
5954                 return;
5955         }
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 ();
5960         }
5961 }
5962
5963 void
5964 Session::solo_control_mode_changed ()
5965 {
5966         if (soloing() || listening()) {
5967                 if (loading()) {
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
5973                            them all off.
5974                         */
5975                         set_controls (route_list_to_control_list (get_routes(), &Stripable::solo_control), 0.0, Controllable::NoGroup);
5976                 } else {
5977                         clear_all_solo_state (get_routes());
5978                 }
5979         }
5980 }
5981
5982 /** Called when a property of one of our route groups changes */
5983 void
5984 Session::route_group_property_changed (RouteGroup* rg)
5985 {
5986         RouteGroupPropertyChanged (rg); /* EMIT SIGNAL */
5987 }
5988
5989 /** Called when a route is added to one of our route groups */
5990 void
5991 Session::route_added_to_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
5992 {
5993         RouteAddedToRouteGroup (rg, r);
5994 }
5995
5996 /** Called when a route is removed from one of our route groups */
5997 void
5998 Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
5999 {
6000         update_route_record_state ();
6001         RouteRemovedFromRouteGroup (rg, r); /* EMIT SIGNAL */
6002
6003         if (!rg->has_control_master () && !rg->has_subgroup () && rg->empty()) {
6004                 remove_route_group (*rg);
6005         }
6006 }
6007
6008 boost::shared_ptr<AudioTrack>
6009 Session::get_nth_audio_track (int nth) const
6010 {
6011         boost::shared_ptr<RouteList> rl = routes.reader ();
6012         rl->sort (Stripable::Sorter ());
6013
6014         for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
6015                 if (!boost::dynamic_pointer_cast<AudioTrack> (*r)) {
6016                         continue;
6017                 }
6018
6019                 if (--nth > 0) {
6020                         continue;
6021                 }
6022                 return boost::dynamic_pointer_cast<AudioTrack> (*r);
6023         }
6024         return boost::shared_ptr<AudioTrack> ();
6025 }
6026
6027 boost::shared_ptr<RouteList>
6028 Session::get_tracks () const
6029 {
6030         boost::shared_ptr<RouteList> rl = routes.reader ();
6031         boost::shared_ptr<RouteList> tl (new RouteList);
6032
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
6036                         tl->push_back (*r);
6037                 }
6038         }
6039         return tl;
6040 }
6041
6042 boost::shared_ptr<RouteList>
6043 Session::get_routes_with_regions_at (samplepos_t const p) const
6044 {
6045         boost::shared_ptr<RouteList> r = routes.reader ();
6046         boost::shared_ptr<RouteList> rl (new RouteList);
6047
6048         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6049                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
6050                 if (!tr) {
6051                         continue;
6052                 }
6053
6054                 boost::shared_ptr<Playlist> pl = tr->playlist ();
6055                 if (!pl) {
6056                         continue;
6057                 }
6058
6059                 if (pl->has_region_at (p)) {
6060                         rl->push_back (*i);
6061                 }
6062         }
6063
6064         return rl;
6065 }
6066
6067 void
6068 Session::goto_end ()
6069 {
6070         if (_session_range_location) {
6071                 request_locate (_session_range_location->end(), false);
6072         } else {
6073                 request_locate (0, false);
6074         }
6075 }
6076
6077 void
6078 Session::goto_start (bool and_roll)
6079 {
6080         if (_session_range_location) {
6081                 request_locate (_session_range_location->start(), and_roll);
6082         } else {
6083                 request_locate (0, and_roll);
6084         }
6085 }
6086
6087 samplepos_t
6088 Session::current_start_sample () const
6089 {
6090         return _session_range_location ? _session_range_location->start() : 0;
6091 }
6092
6093 samplepos_t
6094 Session::current_end_sample () const
6095 {
6096         return _session_range_location ? _session_range_location->end() : 0;
6097 }
6098
6099 void
6100 Session::set_session_range_location (samplepos_t start, samplepos_t end)
6101 {
6102         _session_range_location = new Location (*this, start, end, _("session"), Location::IsSessionRange, 0);
6103         _locations->add (_session_range_location);
6104 }
6105
6106 void
6107 Session::step_edit_status_change (bool yn)
6108 {
6109         bool send = false;
6110
6111         bool val = false;
6112         if (yn) {
6113                 send = (_step_editors == 0);
6114                 val = true;
6115
6116                 _step_editors++;
6117         } else {
6118                 send = (_step_editors == 1);
6119                 val = false;
6120
6121                 if (_step_editors > 0) {
6122                         _step_editors--;
6123                 }
6124         }
6125
6126         if (send) {
6127                 StepEditStatusChange (val);
6128         }
6129 }
6130
6131
6132 void
6133 Session::start_time_changed (samplepos_t old)
6134 {
6135         /* Update the auto loop range to match the session range
6136            (unless the auto loop range has been changed by the user)
6137         */
6138
6139         Location* s = _locations->session_range_location ();
6140         if (s == 0) {
6141                 return;
6142         }
6143
6144         Location* l = _locations->auto_loop_location ();
6145
6146         if (l && l->start() == old) {
6147                 l->set_start (s->start(), true);
6148         }
6149         set_dirty ();
6150 }
6151
6152 void
6153 Session::end_time_changed (samplepos_t old)
6154 {
6155         /* Update the auto loop range to match the session range
6156            (unless the auto loop range has been changed by the user)
6157         */
6158
6159         Location* s = _locations->session_range_location ();
6160         if (s == 0) {
6161                 return;
6162         }
6163
6164         Location* l = _locations->auto_loop_location ();
6165
6166         if (l && l->end() == old) {
6167                 l->set_end (s->end(), true);
6168         }
6169         set_dirty ();
6170 }
6171
6172 std::vector<std::string>
6173 Session::source_search_path (DataType type) const
6174 {
6175         Searchpath sp;
6176
6177         if (session_dirs.size() == 1) {
6178                 switch (type) {
6179                 case DataType::AUDIO:
6180                         sp.push_back (_session_dir->sound_path());
6181                         break;
6182                 case DataType::MIDI:
6183                         sp.push_back (_session_dir->midi_path());
6184                         break;
6185                 }
6186         } else {
6187                 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
6188                         SessionDirectory sdir (i->path);
6189                         switch (type) {
6190                         case DataType::AUDIO:
6191                                 sp.push_back (sdir.sound_path());
6192                                 break;
6193                         case DataType::MIDI:
6194                                 sp.push_back (sdir.midi_path());
6195                                 break;
6196                         }
6197                 }
6198         }
6199
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);
6205                         }
6206                 }
6207         }
6208
6209         // now check the explicit (possibly user-specified) search path
6210
6211         switch (type) {
6212         case DataType::AUDIO:
6213                 sp += Searchpath(config.get_audio_search_path ());
6214                 break;
6215         case DataType::MIDI:
6216                 sp += Searchpath(config.get_midi_search_path ());
6217                 break;
6218         }
6219
6220         return sp;
6221 }
6222
6223 void
6224 Session::ensure_search_path_includes (const string& path, DataType type)
6225 {
6226         Searchpath sp;
6227
6228         if (path == ".") {
6229                 return;
6230         }
6231
6232         switch (type) {
6233         case DataType::AUDIO:
6234                 sp += Searchpath(config.get_audio_search_path ());
6235                 break;
6236         case DataType::MIDI:
6237                 sp += Searchpath (config.get_midi_search_path ());
6238                 break;
6239         }
6240
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.
6245
6246                    On Windows, I think we could just do if (*i == path) here.
6247                 */
6248                 if (PBD::equivalent_paths (*i, path)) {
6249                         return;
6250                 }
6251         }
6252
6253         sp += path;
6254
6255         switch (type) {
6256         case DataType::AUDIO:
6257                 config.set_audio_search_path (sp.to_string());
6258                 break;
6259         case DataType::MIDI:
6260                 config.set_midi_search_path (sp.to_string());
6261                 break;
6262         }
6263 }
6264
6265 void
6266 Session::remove_dir_from_search_path (const string& dir, DataType type)
6267 {
6268         Searchpath sp;
6269
6270         switch (type) {
6271         case DataType::AUDIO:
6272                 sp = Searchpath(config.get_audio_search_path ());
6273                 break;
6274         case DataType::MIDI:
6275                 sp = Searchpath (config.get_midi_search_path ());
6276                 break;
6277         }
6278
6279         sp -= dir;
6280
6281         switch (type) {
6282         case DataType::AUDIO:
6283                 config.set_audio_search_path (sp.to_string());
6284                 break;
6285         case DataType::MIDI:
6286                 config.set_midi_search_path (sp.to_string());
6287                 break;
6288         }
6289
6290 }
6291
6292 boost::shared_ptr<Speakers>
6293 Session::get_speakers()
6294 {
6295         return _speakers;
6296 }
6297
6298 list<string>
6299 Session::unknown_processors () const
6300 {
6301         list<string> p;
6302
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));
6307         }
6308
6309         p.sort ();
6310         p.unique ();
6311
6312         return p;
6313 }
6314
6315 void
6316 Session::set_worst_io_latencies_x (IOChange, void *)
6317 {
6318                 set_worst_io_latencies ();
6319 }
6320
6321 void
6322 Session::send_latency_compensation_change ()
6323 {
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).
6328          *
6329          * This can only happen be triggered by
6330          * Route::update_signal_latency ()
6331          * when updating the processor latency.
6332          *
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).
6335          */
6336         ++_send_latency_changes;
6337 }
6338
6339 bool
6340 Session::update_route_latency (bool playback, bool apply_to_delayline)
6341 {
6342         /* Note: RouteList is process-graph sorted */
6343         boost::shared_ptr<RouteList> r = routes.reader ();
6344
6345         if (playback) {
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.
6348                  */
6349                 RouteList* rl = routes.reader().get();
6350                 r.reset (new RouteList (*rl));
6351                 reverse (r->begin(), r->end());
6352         }
6353
6354         bool changed = false;
6355         int bailout = 0;
6356 restart:
6357         _send_latency_changes = 0;
6358         _worst_route_latency = 0;
6359
6360         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6361                 // if (!(*i)->active()) { continue ; } // TODO
6362                 samplecnt_t l;
6363                 if ((*i)->signal_latency () != (l = (*i)->update_signal_latency (apply_to_delayline))) {
6364                         changed = true;
6365                 }
6366                 _worst_route_latency = std::max (l, _worst_route_latency);
6367         }
6368
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;
6374                         goto restart;
6375                 }
6376         }
6377
6378         DEBUG_TRACE (DEBUG::Latency, string_compose ("worst signal processing latency: %1 (changed ? %2)\n", _worst_route_latency, (changed ? "yes" : "no")));
6379
6380         return changed;
6381 }
6382
6383 void
6384 Session::update_latency (bool playback)
6385 {
6386         /* called only from AudioEngine::latency_callback.
6387          * but may indirectly be triggered from
6388          * Session::update_latency_compensation -> _engine.update_latencies
6389          */
6390         DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Engine latency callback: %1\n", (playback ? "PLAYBACK" : "CAPTURE")));
6391
6392         if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _route_deletion_in_progress) {
6393                 return;
6394         }
6395         if (!_engine.running()) {
6396                 return;
6397         }
6398
6399         /* Note; RouteList is sorted as process-graph */
6400         boost::shared_ptr<RouteList> r = routes.reader ();
6401
6402         if (playback) {
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());
6407         }
6408
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);
6412         }
6413
6414         if (playback) {
6415                 Glib::Threads::Mutex::Lock lx (_update_latency_lock);
6416                 set_worst_output_latency ();
6417                 update_route_latency (true, true);
6418         } else {
6419                 Glib::Threads::Mutex::Lock lx (_update_latency_lock);
6420                 set_worst_input_latency ();
6421                 update_route_latency (false, false);
6422         }
6423
6424         DEBUG_TRACE (DEBUG::LatencyCompensation, "Engine latency callback: DONE\n");
6425         LatencyUpdated (); /* EMIT SIGNAL */
6426 }
6427
6428 void
6429 Session::set_worst_io_latencies ()
6430 {
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 ();
6435 }
6436
6437 void
6438 Session::set_worst_output_latency ()
6439 {
6440         if (inital_connect_or_deletion_in_progress ()) {
6441                 return;
6442         }
6443
6444         _worst_output_latency = 0;
6445
6446         if (!_engine.running()) {
6447                 return;
6448         }
6449
6450         boost::shared_ptr<RouteList> r = routes.reader ();
6451
6452         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6453                 _worst_output_latency = max (_worst_output_latency, (*i)->output()->latency());
6454         }
6455
6456         _worst_output_latency = max (_worst_output_latency, _click_io->latency());
6457
6458         DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Worst output latency: %1\n", _worst_output_latency));
6459 }
6460
6461 void
6462 Session::set_worst_input_latency ()
6463 {
6464         if (inital_connect_or_deletion_in_progress ()) {
6465                 return;
6466         }
6467
6468         _worst_input_latency = 0;
6469
6470         if (!_engine.running()) {
6471                 return;
6472         }
6473
6474         boost::shared_ptr<RouteList> r = routes.reader ();
6475
6476         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6477                 _worst_input_latency = max (_worst_input_latency, (*i)->input()->latency());
6478         }
6479
6480         DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("Worst input latency: %1\n", _worst_input_latency));
6481 }
6482
6483 void
6484 Session::update_latency_compensation (bool force_whole_graph)
6485 {
6486         if (inital_connect_or_deletion_in_progress ()) {
6487                 return;
6488         }
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.
6494          */
6495         Glib::Threads::Mutex::Lock lx (_update_latency_lock, Glib::Threads::TRY_LOCK);
6496         if (!lx.locked()) {
6497                 /* no need to do this twice */
6498                 return;
6499         }
6500
6501         DEBUG_TRACE (DEBUG::LatencyCompensation, string_compose ("update_latency_compensation %1\n", (force_whole_graph ? "of whole graph" : "")));
6502
6503         bool some_track_latency_changed = update_route_latency (false, false);
6504
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 ()
6511                  */
6512         } else {
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 ();
6517                 }
6518         }
6519         DEBUG_TRACE (DEBUG::LatencyCompensation, "update_latency_compensation: DONE\n");
6520 }
6521
6522 char
6523 Session::session_name_is_legal (const string& path)
6524 {
6525         char illegal_chars[] = { '/', '\\', ':', ';', '\0' };
6526
6527         for (int i = 0; illegal_chars[i]; ++i) {
6528                 if (path.find (illegal_chars[i]) != string::npos) {
6529                         return illegal_chars[i];
6530                 }
6531         }
6532
6533         return 0;
6534 }
6535
6536 void
6537 Session::notify_presentation_info_change ()
6538 {
6539         if (deletion_in_progress()) {
6540                 return;
6541         }
6542
6543         reassign_track_numbers();
6544 }
6545
6546 bool
6547 Session::operation_in_progress (GQuark op) const
6548 {
6549         return (find (_current_trans_quarks.begin(), _current_trans_quarks.end(), op) != _current_trans_quarks.end());
6550 }
6551
6552 boost::shared_ptr<Port>
6553 Session::ltc_output_port () const
6554 {
6555         return _ltc_output ? _ltc_output->nth (0) : boost::shared_ptr<Port> ();
6556 }
6557
6558 void
6559 Session::reconnect_ltc_output ()
6560 {
6561         if (_ltc_output) {
6562
6563                 string src = Config->get_ltc_output_port();
6564
6565                 _ltc_output->disconnect (this);
6566
6567                 if (src != _("None") && !src.empty())  {
6568                         _ltc_output->nth (0)->connect (src);
6569                 }
6570         }
6571 }
6572
6573 void
6574 Session::set_range_selection (samplepos_t start, samplepos_t end)
6575 {
6576         _range_selection = Evoral::Range<samplepos_t> (start, end);
6577 }
6578
6579 void
6580 Session::set_object_selection (samplepos_t start, samplepos_t end)
6581 {
6582         _object_selection = Evoral::Range<samplepos_t> (start, end);
6583 }
6584
6585 void
6586 Session::clear_range_selection ()
6587 {
6588         _range_selection = Evoral::Range<samplepos_t> (-1,-1);
6589 }
6590
6591 void
6592 Session::clear_object_selection ()
6593 {
6594         _object_selection = Evoral::Range<samplepos_t> (-1,-1);
6595 }
6596
6597 void
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)
6603 {
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));
6608
6609         auto_connect_thread_wakeup ();
6610 }
6611
6612 void
6613 Session::auto_connect_thread_wakeup ()
6614 {
6615         if (pthread_mutex_trylock (&_auto_connect_mutex) == 0) {
6616                 pthread_cond_signal (&_auto_connect_cond);
6617                 pthread_mutex_unlock (&_auto_connect_mutex);
6618         }
6619 }
6620
6621 void
6622 Session::queue_latency_recompute ()
6623 {
6624         g_atomic_int_inc (&_latency_recompute_pending);
6625         auto_connect_thread_wakeup ();
6626 }
6627
6628 void
6629 Session::auto_connect (const AutoConnectRequest& ar)
6630 {
6631         boost::shared_ptr<Route> route = ar.route.lock();
6632
6633         if (!route) { return; }
6634
6635         if (!IO::connecting_legal) {
6636                 return;
6637         }
6638
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
6644          * offset possible.
6645          */
6646
6647         const bool in_out_physical =
6648                 (Config->get_input_auto_connect() & AutoConnectPhysical)
6649                 && (Config->get_output_auto_connect() & AutoConnectPhysical)
6650                 && ar.connect_inputs;
6651
6652         const ChanCount in_offset = in_out_physical
6653                 ? ChanCount::max(ar.input_offset, ar.output_offset)
6654                 : ar.input_offset;
6655
6656         const ChanCount out_offset = in_out_physical
6657                 ? ChanCount::max(ar.input_offset, ar.output_offset)
6658                 : ar.output_offset;
6659
6660         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
6661                 vector<string> physinputs;
6662                 vector<string> physoutputs;
6663
6664
6665                 /* for connecting track inputs we only want MIDI ports marked
6666                  * for "music".
6667                  */
6668
6669                 get_physical_ports (physinputs, physoutputs, *t, MidiPortMusic);
6670
6671                 if (!physinputs.empty() && ar.connect_inputs) {
6672                         uint32_t nphysical_in = physinputs.size();
6673
6674                         for (uint32_t i = ar.input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) {
6675                                 string port;
6676
6677                                 if (Config->get_input_auto_connect() & AutoConnectPhysical) {
6678                                         port = physinputs[(in_offset.get(*t) + i) % nphysical_in];
6679                                 }
6680
6681                                 if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) {
6682                                         break;
6683                                 }
6684                         }
6685                 }
6686
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) {
6690                                 string port;
6691
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();
6699                                         }
6700                                 }
6701
6702                                 if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) {
6703                                         break;
6704                                 }
6705                         }
6706                 }
6707         }
6708 }
6709
6710 void
6711 Session::auto_connect_thread_start ()
6712 {
6713         if (g_atomic_int_get (&_ac_thread_active)) {
6714                 return;
6715         }
6716
6717         while (!_auto_connect_queue.empty ()) {
6718                 _auto_connect_queue.pop ();
6719         }
6720
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);
6724         }
6725 }
6726
6727 void
6728 Session::auto_connect_thread_terminate ()
6729 {
6730         if (!g_atomic_int_get (&_ac_thread_active)) {
6731                 return;
6732         }
6733
6734         {
6735                 Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6736                 while (!_auto_connect_queue.empty ()) {
6737                         _auto_connect_queue.pop ();
6738                 }
6739         }
6740
6741         /* cannot use auto_connect_thread_wakeup() because that is allowed to
6742          * fail to wakeup the thread.
6743          */
6744
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);
6749
6750         void *status;
6751         pthread_join (_auto_connect_thread, &status);
6752 }
6753
6754 void *
6755 Session::auto_connect_thread (void *arg)
6756 {
6757         Session *s = static_cast<Session *>(arg);
6758         s->auto_connect_thread_run ();
6759         pthread_exit (0);
6760         return 0;
6761 }
6762
6763 void
6764 Session::auto_connect_thread_run ()
6765 {
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)) {
6771
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 ());
6779
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 ();
6784                                 lx.release ();
6785                                 auto_connect (ar);
6786                                 lx.acquire ();
6787                         }
6788                 }
6789
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.
6793                          * BUT..
6794                          * update_latency_compensation ()
6795                          * calls DiskWriter::set_capture_offset () which
6796                          * modifies the capture-offset, which can be a problem.
6797                          */
6798                         while (g_atomic_int_and (&_latency_recompute_pending, 0)) {
6799                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6800                                 update_latency_compensation ();
6801                         }
6802                 }
6803
6804                 {
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 ();
6809                 }
6810
6811                 pthread_cond_wait (&_auto_connect_cond, &_auto_connect_mutex);
6812         }
6813         pthread_mutex_unlock (&_auto_connect_mutex);
6814 }
6815
6816 void
6817 Session::cancel_all_solo ()
6818 {
6819         StripableList sl;
6820
6821         get_stripables (sl);
6822
6823         set_controls (stripable_list_to_control_list (sl, &Stripable::solo_control), 0.0, Controllable::NoGroup);
6824         clear_all_solo_state (routes.reader());
6825 }
6826
6827 void
6828 Session::maybe_update_tempo_from_midiclock_tempo (float bpm)
6829 {
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);
6836                 }
6837         }
6838 }