lots of stuff related to capture alignment. things appear to be working now, but...
[ardour.git] / libs / ardour / session.cc
1 /*
2     Copyright (C) 1999-2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdint.h>
21
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 #include <sstream>
26 #include <fstream>
27 #include <cstdio> /* sprintf(3) ... grrr */
28 #include <cmath>
29 #include <cerrno>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #include <glibmm/thread.h>
34 #include <glibmm/miscutils.h>
35 #include <glibmm/fileutils.h>
36
37 #include <boost/algorithm/string/erase.hpp>
38
39 #include "pbd/error.h"
40 #include "pbd/boost_debug.h"
41 #include "pbd/pathscanner.h"
42 #include "pbd/stl_delete.h"
43 #include "pbd/basename.h"
44 #include "pbd/stacktrace.h"
45 #include "pbd/file_utils.h"
46 #include "pbd/convert.h"
47 #include "pbd/strsplit.h"
48
49 #include "ardour/amp.h"
50 #include "ardour/analyser.h"
51 #include "ardour/audio_buffer.h"
52 #include "ardour/audio_diskstream.h"
53 #include "ardour/audio_port.h"
54 #include "ardour/audio_track.h"
55 #include "ardour/audioengine.h"
56 #include "ardour/audiofilesource.h"
57 #include "ardour/audioplaylist.h"
58 #include "ardour/audioregion.h"
59 #include "ardour/auditioner.h"
60 #include "ardour/buffer_manager.h"
61 #include "ardour/buffer_set.h"
62 #include "ardour/bundle.h"
63 #include "ardour/butler.h"
64 #include "ardour/click.h"
65 #include "ardour/configuration.h"
66 #include "ardour/crossfade.h"
67 #include "ardour/cycle_timer.h"
68 #include "ardour/data_type.h"
69 #include "ardour/debug.h"
70 #include "ardour/filename_extensions.h"
71 #include "ardour/internal_send.h"
72 #include "ardour/io_processor.h"
73 #include "ardour/midi_diskstream.h"
74 #include "ardour/midi_playlist.h"
75 #include "ardour/midi_region.h"
76 #include "ardour/midi_track.h"
77 #include "ardour/midi_ui.h"
78 #include "ardour/named_selection.h"
79 #include "ardour/process_thread.h"
80 #include "ardour/playlist.h"
81 #include "ardour/plugin_insert.h"
82 #include "ardour/port_insert.h"
83 #include "ardour/processor.h"
84 #include "ardour/rc_configuration.h"
85 #include "ardour/recent_sessions.h"
86 #include "ardour/region_factory.h"
87 #include "ardour/return.h"
88 #include "ardour/route_group.h"
89 #include "ardour/send.h"
90 #include "ardour/session.h"
91 #include "ardour/session_directory.h"
92 #include "ardour/session_directory.h"
93 #include "ardour/session_metadata.h"
94 #include "ardour/session_playlists.h"
95 #include "ardour/slave.h"
96 #include "ardour/smf_source.h"
97 #include "ardour/source_factory.h"
98 #include "ardour/tape_file_matcher.h"
99 #include "ardour/tempo.h"
100 #include "ardour/utils.h"
101 #include "ardour/graph.h"
102 #include "ardour/speakers.h"
103 #include "ardour/operations.h"
104
105 #include "midi++/port.h"
106 #include "midi++/mmc.h"
107 #include "midi++/manager.h"
108
109 #include "i18n.h"
110
111 using namespace std;
112 using namespace ARDOUR;
113 using namespace PBD;
114
115 bool Session::_disable_all_loaded_plugins = false;
116
117 PBD::Signal1<void,std::string> Session::Dialog;
118 PBD::Signal0<int> Session::AskAboutPendingState;
119 PBD::Signal2<int, framecnt_t, framecnt_t> Session::AskAboutSampleRateMismatch;
120 PBD::Signal0<void> Session::SendFeedback;
121 PBD::Signal3<int,Session*,std::string,DataType> Session::MissingFile;
122
123 PBD::Signal1<void, framepos_t> Session::StartTimeChanged;
124 PBD::Signal1<void, framepos_t> Session::EndTimeChanged;
125 PBD::Signal0<void> Session::AutoBindingOn;
126 PBD::Signal0<void> Session::AutoBindingOff;
127 PBD::Signal2<void,std::string, std::string> Session::Exported;
128 PBD::Signal1<int,boost::shared_ptr<Playlist> > Session::AskAboutPlaylistDeletion;
129 PBD::Signal0<void> Session::Quit;
130
131 static void clean_up_session_event (SessionEvent* ev) { delete ev; }
132 const SessionEvent::RTeventCallback Session::rt_cleanup (clean_up_session_event);
133
134 Session::Session (AudioEngine &eng,
135                   const string& fullpath,
136                   const string& snapshot_name,
137                   BusProfile* bus_profile,
138                   string mix_template)
139         : _engine (eng)
140         , _target_transport_speed (0.0)
141         , _requested_return_frame (-1)
142         , _session_dir (new SessionDirectory(fullpath))
143         , state_tree (0)
144         , _state_of_the_state (Clean)
145         , _butler (new Butler (*this))
146         , _post_transport_work (0)
147         , _send_timecode_update (false)
148         , _all_route_group (new RouteGroup (*this, "all"))
149         , route_graph (new Graph(*this))
150         , routes (new RouteList)
151         , _total_free_4k_blocks (0)
152         , _bundles (new BundleList)
153         , _bundle_xml_node (0)
154         , _current_trans (0)
155         , _click_io ((IO*) 0)
156         , click_data (0)
157         , click_emphasis_data (0)
158         , main_outs (0)
159         , _metadata (new SessionMetadata())
160         , _have_rec_enabled_track (false)
161         , _suspend_timecode_transmission (0)
162 {
163         _locations = new Locations (*this);
164                 
165         playlists.reset (new SessionPlaylists);
166
167         _all_route_group->set_active (true, this);
168
169         interpolation.add_channel_to (0, 0);
170
171         if (!eng.connected()) {
172                 throw failed_constructor();
173         }
174
175         n_physical_outputs = _engine.n_physical_outputs ();
176         n_physical_inputs =  _engine.n_physical_inputs ();
177
178         first_stage_init (fullpath, snapshot_name);
179
180         _is_new = !Glib::file_test (_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
181
182         if (_is_new) {
183                 if (create (mix_template, bus_profile)) {
184                         destroy ();
185                         throw failed_constructor ();
186                 }
187         }
188
189         if (second_stage_init ()) {
190                 destroy ();
191                 throw failed_constructor ();
192         }
193
194         store_recent_sessions(_name, _path);
195
196         bool was_dirty = dirty();
197
198         _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
199
200         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, false));
201         config.ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, true));
202
203         if (was_dirty) {
204                 DirtyChanged (); /* EMIT SIGNAL */
205         }
206
207         StartTimeChanged.connect_same_thread (*this, boost::bind (&Session::start_time_changed, this, _1));
208         EndTimeChanged.connect_same_thread (*this, boost::bind (&Session::end_time_changed, this, _1));
209
210         _is_new = false;
211 }
212
213 Session::~Session ()
214 {
215         destroy ();
216 }
217
218 void
219 Session::destroy ()
220 {
221         vector<void*> debug_pointers;
222
223         /* if we got to here, leaving pending capture state around
224            is a mistake.
225         */
226
227         remove_pending_capture_state ();
228
229         _state_of_the_state = StateOfTheState (CannotSave|Deletion);
230
231         _engine.remove_session ();
232
233         /* clear history so that no references to objects are held any more */
234
235         _history.clear ();
236
237         /* clear state tree so that no references to objects are held any more */
238
239         delete state_tree;
240
241         /* reset dynamic state version back to default */
242
243         Stateful::loading_state_version = 0;
244
245         _butler->drop_references ();
246         delete _butler;
247         delete midi_control_ui;
248         delete _all_route_group;
249
250         if (click_data != default_click) {
251                 delete [] click_data;
252         }
253
254         if (click_emphasis_data != default_click_emphasis) {
255                 delete [] click_emphasis_data;
256         }
257
258         clear_clicks ();
259
260         /* clear out any pending dead wood from RCU managed objects */
261
262         routes.flush ();
263         _bundles.flush ();
264         
265         AudioDiskstream::free_working_buffers();
266
267         /* tell everyone who is still standing that we're about to die */
268         drop_references ();
269
270         /* tell everyone to drop references and delete objects as we go */
271
272         DEBUG_TRACE (DEBUG::Destruction, "delete named selections\n");
273         named_selections.clear ();
274
275         DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
276         RegionFactory::delete_all_regions ();
277
278         DEBUG_TRACE (DEBUG::Destruction, "delete routes\n");
279
280         /* reset these three references to special routes before we do the usual route delete thing */
281
282         auditioner.reset ();
283         _master_out.reset ();
284         _monitor_out.reset ();
285
286         {
287                 RCUWriter<RouteList> writer (routes);
288                 boost::shared_ptr<RouteList> r = writer.get_copy ();
289
290                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
291                         DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for route %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
292                         (*i)->drop_references ();
293                 }
294
295                 r->clear ();
296                 /* writer goes out of scope and updates master */
297         }
298         routes.flush ();
299
300         DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
301         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
302                 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->path(), i->second.use_count()));
303                 i->second->drop_references ();
304         }
305
306         sources.clear ();
307
308         DEBUG_TRACE (DEBUG::Destruction, "delete route groups\n");
309         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
310                 
311                 delete *i;
312         }
313
314         Crossfade::set_buffer_size (0);
315
316         /* not strictly necessary, but doing it here allows the shared_ptr debugging to work */
317         playlists.reset ();
318
319         delete _locations;
320
321         DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n");
322
323 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
324         boost_debug_list_ptrs ();
325 #endif
326 }
327
328 void
329 Session::set_worst_io_latencies ()
330 {
331         if (_state_of_the_state & InitialConnecting) {
332                 return;
333         }
334
335         _worst_output_latency = 0;
336         _worst_input_latency = 0;
337
338         if (!_engine.connected()) {
339                 return;
340         }
341
342         boost::shared_ptr<RouteList> r = routes.reader ();
343
344         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
345                 _worst_output_latency = max (_worst_output_latency, (*i)->output()->latency());
346                 _worst_input_latency = max (_worst_input_latency, (*i)->input()->latency());
347         }
348         
349         DEBUG_TRACE (DEBUG::Latency, string_compose ("Worst output latency: %1 Worst input latency: %2\n",
350                                                      _worst_output_latency, _worst_input_latency));
351 }
352
353 void
354 Session::when_engine_running ()
355 {
356         string first_physical_output;
357
358         BootMessage (_("Set block size and sample rate"));
359
360         set_block_size (_engine.frames_per_cycle());
361         set_frame_rate (_engine.frame_rate());
362
363         BootMessage (_("Using configuration"));
364
365         boost::function<void (std::string)> ff (boost::bind (&Session::config_changed, this, _1, false));
366         boost::function<void (std::string)> ft (boost::bind (&Session::config_changed, this, _1, true));
367
368         Config->map_parameters (ff);
369         config.map_parameters (ft);
370
371         /* every time we reconnect, recompute worst case output latencies */
372
373         _engine.Running.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies, this));
374
375         if (synced_to_jack()) {
376                 _engine.transport_stop ();
377         }
378
379         if (config.get_jack_time_master()) {
380                 _engine.transport_locate (_transport_frame);
381         }
382
383         _clicking = false;
384
385         try {
386                 XMLNode* child = 0;
387
388                 _click_io.reset (new ClickIO (*this, "click"));
389
390                 if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
391
392                         /* existing state for Click */
393                         int c;
394
395                         if (Stateful::loading_state_version < 3000) {
396                                 c = _click_io->set_state_2X (*child->children().front(), Stateful::loading_state_version, false);
397                         } else {
398                                 c = _click_io->set_state (*child->children().front(), Stateful::loading_state_version);
399                         }
400                                         
401
402                         if (c == 0) {
403                                 _clicking = Config->get_clicking ();
404                                 
405                         } else {
406                                 
407                                 error << _("could not setup Click I/O") << endmsg;
408                                 _clicking = false;
409                         }
410
411
412                 } else {
413
414                         /* default state for Click: dual-mono to first 2 physical outputs */
415
416                         vector<string> outs;
417                         _engine.get_physical_outputs (DataType::AUDIO, outs);
418
419                         for (uint32_t physport = 0; physport < 2; ++physport) {
420                                 if (outs.size() > physport) {
421                                         if (_click_io->add_port (outs[physport], this)) {
422                                                 // relax, even though its an error
423                                         }
424                                 }
425                         }
426                         
427                         if (_click_io->n_ports () > ChanCount::ZERO) {
428                                 _clicking = Config->get_clicking ();
429                         }
430                 }
431         }
432         
433         catch (failed_constructor& err) {
434                 error << _("cannot setup Click I/O") << endmsg;
435         }
436
437         BootMessage (_("Compute I/O Latencies"));
438
439         if (_clicking) {
440                 // XXX HOW TO ALERT UI TO THIS ? DO WE NEED TO?
441         }
442
443         BootMessage (_("Set up standard connections"));
444
445         vector<string> inputs[DataType::num_types];
446         vector<string> outputs[DataType::num_types];
447         for (uint32_t i = 0; i < DataType::num_types; ++i) {
448                 _engine.get_physical_inputs (DataType (DataType::Symbol (i)), inputs[i]);
449                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
450         }
451
452         /* Create a set of Bundle objects that map
453            to the physical I/O currently available.  We create both
454            mono and stereo bundles, so that the common cases of mono
455            and stereo tracks get bundles to put in their mixer strip
456            in / out menus.  There may be a nicer way of achieving that;
457            it doesn't really scale that well to higher channel counts
458         */
459
460         /* mono output bundles */
461
462         for (uint32_t np = 0; np < outputs[DataType::AUDIO].size(); ++np) {
463                 char buf[32];
464                 snprintf (buf, sizeof (buf), _("out %" PRIu32), np+1);
465
466                 boost::shared_ptr<Bundle> c (new Bundle (buf, true));
467                 c->add_channel (_("mono"), DataType::AUDIO);
468                 c->set_port (0, outputs[DataType::AUDIO][np]);
469
470                 add_bundle (c);
471         }
472
473         /* stereo output bundles */
474
475         for (uint32_t np = 0; np < outputs[DataType::AUDIO].size(); np += 2) {
476                 if (np + 1 < outputs[DataType::AUDIO].size()) {
477                         char buf[32];
478                         snprintf (buf, sizeof(buf), _("out %" PRIu32 "+%" PRIu32), np + 1, np + 2);
479                         boost::shared_ptr<Bundle> c (new Bundle (buf, true));
480                         c->add_channel (_("L"), DataType::AUDIO);
481                         c->set_port (0, outputs[DataType::AUDIO][np]);
482                         c->add_channel (_("R"), DataType::AUDIO);
483                         c->set_port (1, outputs[DataType::AUDIO][np + 1]);
484
485                         add_bundle (c);
486                 }
487         }
488
489         /* mono input bundles */
490
491         for (uint32_t np = 0; np < inputs[DataType::AUDIO].size(); ++np) {
492                 char buf[32];
493                 snprintf (buf, sizeof (buf), _("in %" PRIu32), np+1);
494
495                 boost::shared_ptr<Bundle> c (new Bundle (buf, false));
496                 c->add_channel (_("mono"), DataType::AUDIO);
497                 c->set_port (0, inputs[DataType::AUDIO][np]);
498
499                 add_bundle (c);
500         }
501
502         /* stereo input bundles */
503
504         for (uint32_t np = 0; np < inputs[DataType::AUDIO].size(); np += 2) {
505                 if (np + 1 < inputs[DataType::AUDIO].size()) {
506                         char buf[32];
507                         snprintf (buf, sizeof(buf), _("in %" PRIu32 "+%" PRIu32), np + 1, np + 2);
508
509                         boost::shared_ptr<Bundle> c (new Bundle (buf, false));
510                         c->add_channel (_("L"), DataType::AUDIO);
511                         c->set_port (0, inputs[DataType::AUDIO][np]);
512                         c->add_channel (_("R"), DataType::AUDIO);
513                         c->set_port (1, inputs[DataType::AUDIO][np + 1]);
514
515                         add_bundle (c);
516                 }
517         }
518
519         /* MIDI input bundles */
520
521         for (uint32_t np = 0; np < inputs[DataType::MIDI].size(); ++np) {
522                 string n = inputs[DataType::MIDI][np];
523                 boost::erase_first (n, X_("alsa_pcm:"));
524                 
525                 boost::shared_ptr<Bundle> c (new Bundle (n, false));
526                 c->add_channel ("", DataType::MIDI);
527                 c->set_port (0, inputs[DataType::MIDI][np]);
528                 add_bundle (c);
529         }
530                 
531         /* MIDI output bundles */
532
533         for (uint32_t np = 0; np < outputs[DataType::MIDI].size(); ++np) {
534                 string n = outputs[DataType::MIDI][np];
535                 boost::erase_first (n, X_("alsa_pcm:"));
536
537                 boost::shared_ptr<Bundle> c (new Bundle (n, true));
538                 c->add_channel ("", DataType::MIDI);
539                 c->set_port (0, outputs[DataType::MIDI][np]);
540                 add_bundle (c);
541         }
542
543         BootMessage (_("Setup signal flow and plugins"));
544
545         hookup_io ();
546
547         if (_is_new && !no_auto_connect()) {
548
549                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
550                 
551                 /* don't connect the master bus outputs if there is a monitor bus */
552
553                 if (_master_out && Config->get_auto_connect_standard_busses() && !_monitor_out) {
554
555                         /* if requested auto-connect the outputs to the first N physical ports.
556                          */
557
558                         uint32_t limit = _master_out->n_outputs().n_total();
559
560                         for (uint32_t n = 0; n < limit; ++n) {
561                                 Port* p = _master_out->output()->nth (n);
562                                 string connect_to;
563                                 if (outputs[p->type()].size() > n) {
564                                         connect_to = outputs[p->type()][n];
565                                 }
566
567                                 if (!connect_to.empty() && p->connected_to (connect_to) == false) {
568                                         if (_master_out->output()->connect (p, connect_to, this)) {
569                                                 error << string_compose (_("cannot connect master output %1 to %2"), n, connect_to)
570                                                       << endmsg;
571                                                 break;
572                                         }
573                                 }
574                         }
575                 }
576
577                 if (_monitor_out) {
578
579                         /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
580                            are undefined, at best.
581                          */
582
583                         /* control out listens to master bus (but ignores it
584                            under some conditions)
585                         */
586
587                         uint32_t limit = _monitor_out->n_inputs().n_audio();
588
589                         if (_master_out) {
590                                 for (uint32_t n = 0; n < limit; ++n) {
591                                         AudioPort* p = _monitor_out->input()->ports().nth_audio_port (n);
592                                         AudioPort* o = _master_out->output()->ports().nth_audio_port (n);
593
594                                         if (o) {
595                                                 string connect_to = o->name();
596                                                 if (_monitor_out->input()->connect (p, connect_to, this)) {
597                                                         error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
598                                                               << endmsg;
599                                                         break;
600                                                 }
601                                         }
602                                 }
603                         }
604
605                         /* if control out is not connected, connect control out to physical outs
606                         */
607
608                         if (!_monitor_out->output()->connected ()) {
609
610                                 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
611
612                                         boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
613
614                                         if (b) {
615                                                 _monitor_out->output()->connect_ports_to_bundle (b, this);
616                                         } else {
617                                                 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
618                                                                            Config->get_monitor_bus_preferred_bundle())
619                                                         << endmsg;
620                                         }
621
622                                 } else {
623                                         
624                                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
625                                                 uint32_t mod = n_physical_outputs.get (*t);
626                                                 uint32_t limit = _monitor_out->n_outputs().get(*t);
627
628                                                 for (uint32_t n = 0; n < limit; ++n) {
629
630                                                         Port* p = _monitor_out->output()->ports().port(*t, n);
631                                                         string connect_to;
632                                                         if (outputs[*t].size() > (n % mod)) {
633                                                                 connect_to = outputs[*t][n % mod];
634                                                         }
635
636                                                         if (!connect_to.empty()) {
637                                                                 if (_monitor_out->output()->connect (p, connect_to, this)) {
638                                                                         error << string_compose (
639                                                                                         _("cannot connect control output %1 to %2"),
640                                                                                         n, connect_to)
641                                                                                 << endmsg;
642                                                                         break;
643                                                                 }
644                                                         }
645                                                 }
646                                         }
647                                 }
648                         }
649                 }
650         }
651
652         set_worst_io_latencies ();
653
654         _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
655
656         /* hook us up to the engine */
657
658         BootMessage (_("Connect to engine"));
659
660         _engine.set_session (this);
661         _engine.update_total_latencies ();
662 }
663
664 void
665 Session::hookup_io ()
666 {
667         /* stop graph reordering notifications from
668            causing resorts, etc.
669         */
670
671         _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
672
673         if (!auditioner) {
674
675                 /* we delay creating the auditioner till now because
676                    it makes its own connections to ports.
677                 */
678
679                 try {
680                         boost::shared_ptr<Auditioner> a (new Auditioner (*this));
681                         if (a->init()) {
682                                 throw failed_constructor ();
683                         }
684                         a->use_new_diskstream ();
685                         auditioner = a;
686                 }
687
688                 catch (failed_constructor& err) {
689                         warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
690                 }
691         }
692
693         /* load bundles, which we may have postponed earlier on */
694         if (_bundle_xml_node) {
695                 load_bundles (*_bundle_xml_node);
696                 delete _bundle_xml_node;
697         }
698
699         /* Tell all IO objects to connect themselves together */
700
701         IO::enable_connecting ();
702         MIDI::Port::MakeConnections ();
703
704         /* Now reset all panners */
705
706         Delivery::reset_panners ();
707
708         /* Connect tracks to monitor/listen bus if there is one.
709            Note that in an existing session, the internal sends will
710            already exist, but we want the routes to notice that
711            they connect to the control out specifically.
712          */
713
714         if (_monitor_out) {
715                 boost::shared_ptr<RouteList> r = routes.reader ();
716                 for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
717                         
718                         if ((*x)->is_monitor()) {
719                                 
720                                 /* relax */
721                                 
722                         } else if ((*x)->is_master()) {
723                                 
724                                 /* relax */
725                                 
726                         } else {
727
728                                 (*x)->listen_via_monitor ();
729                         }
730                 }
731         }
732
733         /* Anyone who cares about input state, wake up and do something */
734
735         IOConnectionsComplete (); /* EMIT SIGNAL */
736
737         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
738
739         /* now handle the whole enchilada as if it was one
740            graph reorder event.
741         */
742
743         graph_reordered ();
744
745         /* update the full solo state, which can't be
746            correctly determined on a per-route basis, but
747            needs the global overview that only the session
748            has.
749         */
750
751         update_route_solo_state ();
752 }
753
754 void
755 Session::track_playlist_changed (boost::weak_ptr<Track> wp)
756 {
757         boost::shared_ptr<Track> track = wp.lock ();
758         if (!track) {
759                 return;
760         }
761         
762         boost::shared_ptr<Playlist> playlist;
763
764         if ((playlist = track->playlist()) != 0) {
765                 playlist->RegionAdded.connect_same_thread (*this, boost::bind (&Session::playlist_region_added, this, _1));
766                 playlist->RangesMoved.connect_same_thread (*this, boost::bind (&Session::playlist_ranges_moved, this, _1));
767                 playlist->RegionsExtended.connect_same_thread (*this, boost::bind (&Session::playlist_regions_extended, this, _1));
768         }
769 }
770
771 bool
772 Session::record_enabling_legal () const
773 {
774         /* this used to be in here, but survey says.... we don't need to restrict it */
775         // if (record_status() == Recording) {
776         //      return false;
777         // }
778
779         if (Config->get_all_safe()) {
780                 return false;
781         }
782         return true;
783 }
784
785 void
786 Session::set_track_monitor_input_status (bool yn)
787 {
788         boost::shared_ptr<RouteList> rl = routes.reader ();
789         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
790                 boost::shared_ptr<AudioTrack> tr = boost::dynamic_pointer_cast<AudioTrack> (*i);
791                 if (tr && tr->record_enabled ()) {
792                         //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
793                         tr->monitor_input (yn);
794                 }
795         }
796 }
797
798 void
799 Session::reset_input_monitor_state ()
800 {
801         if (transport_rolling()) {
802                 set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
803         } else {
804                 set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring);
805         }
806 }
807
808 void
809 Session::auto_punch_start_changed (Location* location)
810 {
811         replace_event (SessionEvent::PunchIn, location->start());
812
813         if (get_record_enabled() && config.get_punch_in()) {
814                 /* capture start has been changed, so save new pending state */
815                 save_state ("", true);
816         }
817 }
818
819 void
820 Session::auto_punch_end_changed (Location* location)
821 {
822         framepos_t when_to_stop = location->end();
823         // when_to_stop += _worst_output_latency + _worst_input_latency;
824         replace_event (SessionEvent::PunchOut, when_to_stop);
825 }
826
827 void
828 Session::auto_punch_changed (Location* location)
829 {
830         framepos_t when_to_stop = location->end();
831
832         replace_event (SessionEvent::PunchIn, location->start());
833         //when_to_stop += _worst_output_latency + _worst_input_latency;
834         replace_event (SessionEvent::PunchOut, when_to_stop);
835 }
836
837 void
838 Session::auto_loop_changed (Location* location)
839 {
840         replace_event (SessionEvent::AutoLoop, location->end(), location->start());
841
842         if (transport_rolling() && play_loop) {
843
844
845                 // if (_transport_frame > location->end()) {
846
847                 if (_transport_frame < location->start() || _transport_frame > location->end()) {
848                         // relocate to beginning of loop
849                         clear_events (SessionEvent::LocateRoll);
850
851                         request_locate (location->start(), true);
852
853                 }
854                 else if (Config->get_seamless_loop() && !loop_changing) {
855
856                         // schedule a locate-roll to refill the diskstreams at the
857                         // previous loop end
858                         loop_changing = true;
859
860                         if (location->end() > last_loopend) {
861                                 clear_events (SessionEvent::LocateRoll);
862                                 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, last_loopend, last_loopend, 0, true);
863                                 queue_event (ev);
864                         }
865
866                 }
867         }
868
869         last_loopend = location->end();
870 }
871
872 void
873 Session::set_auto_punch_location (Location* location)
874 {
875         Location* existing;
876
877         if ((existing = _locations->auto_punch_location()) != 0 && existing != location) {
878                 punch_connections.drop_connections();
879                 existing->set_auto_punch (false, this);
880                 remove_event (existing->start(), SessionEvent::PunchIn);
881                 clear_events (SessionEvent::PunchOut);
882                 auto_punch_location_changed (0);
883         }
884
885         set_dirty();
886
887         if (location == 0) {
888                 return;
889         }
890
891         if (location->end() <= location->start()) {
892                 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
893                 return;
894         }
895
896         punch_connections.drop_connections ();
897
898         location->start_changed.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_start_changed, this, _1));
899         location->end_changed.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_end_changed, this, _1));
900         location->changed.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_changed, this, _1));
901
902         location->set_auto_punch (true, this);
903
904         auto_punch_changed (location);
905
906         auto_punch_location_changed (location);
907 }
908
909 void
910 Session::set_auto_loop_location (Location* location)
911 {
912         Location* existing;
913
914         if ((existing = _locations->auto_loop_location()) != 0 && existing != location) {
915                 loop_connections.drop_connections ();
916                 existing->set_auto_loop (false, this);
917                 remove_event (existing->end(), SessionEvent::AutoLoop);
918                 auto_loop_location_changed (0);
919         }
920
921         set_dirty();
922
923         if (location == 0) {
924                 return;
925         }
926
927         if (location->end() <= location->start()) {
928                 error << _("Session: you can't use a mark for auto loop") << endmsg;
929                 return;
930         }
931
932         last_loopend = location->end();
933
934         loop_connections.drop_connections ();
935
936         location->start_changed.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, _1));
937         location->end_changed.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, _1));
938         location->changed.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, _1));
939
940         location->set_auto_loop (true, this);
941
942         /* take care of our stuff first */
943
944         auto_loop_changed (location);
945
946         /* now tell everyone else */
947
948         auto_loop_location_changed (location);
949 }
950
951 void
952 Session::locations_added (Location *)
953 {
954         set_dirty ();
955 }
956
957 void
958 Session::locations_changed ()
959 {
960         _locations->apply (*this, &Session::handle_locations_changed);
961 }
962
963 void
964 Session::handle_locations_changed (Locations::LocationList& locations)
965 {
966         Locations::LocationList::iterator i;
967         Location* location;
968         bool set_loop = false;
969         bool set_punch = false;
970
971         for (i = locations.begin(); i != locations.end(); ++i) {
972
973                 location =* i;
974
975                 if (location->is_auto_punch()) {
976                         set_auto_punch_location (location);
977                         set_punch = true;
978                 }
979                 if (location->is_auto_loop()) {
980                         set_auto_loop_location (location);
981                         set_loop = true;
982                 }
983
984                 if (location->is_session_range()) {
985                         _session_range_location = location;
986                 }
987         }
988
989         if (!set_loop) {
990                 set_auto_loop_location (0);
991         }
992         if (!set_punch) {
993                 set_auto_punch_location (0);
994         }
995
996         set_dirty();
997 }
998
999 void
1000 Session::enable_record ()
1001 {
1002         while (1) {
1003                 RecordState rs = (RecordState) g_atomic_int_get (&_record_status);
1004                 
1005                 if (rs == Recording) {
1006                         break;
1007                 }
1008
1009                 if (g_atomic_int_compare_and_exchange (&_record_status, rs, Recording)) {
1010                         
1011                         _last_record_location = _transport_frame;
1012                         MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordStrobe));
1013                         
1014                         if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1015                                 set_track_monitor_input_status (true);
1016                         }
1017                         
1018                         RecordStateChanged ();
1019                         break;
1020                 }
1021         }
1022 }
1023
1024 void
1025 Session::disable_record (bool rt_context, bool force)
1026 {
1027         RecordState rs;
1028
1029         if ((rs = (RecordState) g_atomic_int_get (&_record_status)) != Disabled) {
1030
1031                 if ((!Config->get_latched_record_enable () && !play_loop) || force) {
1032                         g_atomic_int_set (&_record_status, Disabled);
1033                         MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordExit));
1034                 } else {
1035                         if (rs == Recording) {
1036                                 g_atomic_int_set (&_record_status, Enabled);
1037                         }
1038                 }
1039
1040                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1041                         set_track_monitor_input_status (false);
1042                 }
1043
1044                 RecordStateChanged (); /* emit signal */
1045
1046                 if (!rt_context) {
1047                         remove_pending_capture_state ();
1048                 }
1049         }
1050 }
1051
1052 void
1053 Session::step_back_from_record ()
1054 {
1055         if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
1056
1057                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1058                         set_track_monitor_input_status (false);
1059                 }
1060         }
1061 }
1062
1063 void
1064 Session::maybe_enable_record ()
1065 {
1066         if (_step_editors > 0) {
1067                 return;
1068         }
1069
1070         g_atomic_int_set (&_record_status, Enabled);
1071
1072         /* This function is currently called from somewhere other than an RT thread.
1073            This save_state() call therefore doesn't impact anything.  Doing it here
1074            means that we save pending state of which sources the next record will use,
1075            which gives us some chance of recovering from a crash during the record.
1076         */
1077         
1078         save_state ("", true);
1079         
1080         if (_transport_speed) {
1081                 if (!config.get_punch_in()) {
1082                         enable_record ();
1083                 }
1084         } else {
1085                 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordPause));
1086                 RecordStateChanged (); /* EMIT SIGNAL */
1087         }
1088
1089         set_dirty();
1090 }
1091
1092 framepos_t
1093 Session::audible_frame () const
1094 {
1095         framepos_t ret;
1096         framepos_t tf;
1097         framecnt_t offset;
1098
1099         /* the first of these two possible settings for "offset"
1100            mean that the audible frame is stationary until
1101            audio emerges from the latency compensation
1102            "pseudo-pipeline".
1103
1104            the second means that the audible frame is stationary
1105            until audio would emerge from a physical port
1106            in the absence of any plugin latency compensation
1107         */
1108
1109         offset = _worst_output_latency;
1110
1111         if (offset > current_block_size) {
1112                 offset -= current_block_size;
1113         } else {
1114                 /* XXX is this correct? if we have no external
1115                    physical connections and everything is internal
1116                    then surely this is zero? still, how
1117                    likely is that anyway?
1118                 */
1119                 offset = current_block_size;
1120         }
1121
1122         if (synced_to_jack()) {
1123                 tf = _engine.transport_frame();
1124         } else {
1125                 tf = _transport_frame;
1126         }
1127
1128         ret = tf;
1129
1130         if (!non_realtime_work_pending()) {
1131
1132                 /* MOVING */
1133
1134                 /* Check to see if we have passed the first guaranteed
1135                    audible frame past our last start position. if not,
1136                    return that last start point because in terms
1137                    of audible frames, we have not moved yet.
1138
1139                    `Start position' in this context means the time we last
1140                    either started or changed transport direction.
1141                 */
1142
1143                 if (_transport_speed > 0.0f) {
1144
1145                         if (!play_loop || !have_looped) {
1146                                 if (tf < _last_roll_or_reversal_location + offset) {
1147                                         return _last_roll_or_reversal_location;
1148                                 }
1149                         }
1150
1151
1152                         /* forwards */
1153                         ret -= offset;
1154
1155                 } else if (_transport_speed < 0.0f) {
1156
1157                         /* XXX wot? no backward looping? */
1158
1159                         if (tf > _last_roll_or_reversal_location - offset) {
1160                                 return _last_roll_or_reversal_location;
1161                         } else {
1162                                 /* backwards */
1163                                 ret += offset;
1164                         }
1165                 }
1166         }
1167
1168         return ret;
1169 }
1170
1171 void
1172 Session::set_frame_rate (framecnt_t frames_per_second)
1173 {
1174         /** \fn void Session::set_frame_size(framecnt_t)
1175                 the AudioEngine object that calls this guarantees
1176                 that it will not be called while we are also in
1177                 ::process(). Its fine to do things that block
1178                 here.
1179         */
1180
1181         _base_frame_rate = frames_per_second;
1182
1183         sync_time_vars();
1184
1185         Automatable::set_automation_interval (ceil ((double) frames_per_second * (0.001 * Config->get_automation_interval())));
1186
1187         clear_clicks ();
1188
1189         // XXX we need some equivalent to this, somehow
1190         // SndFileSource::setup_standard_crossfades (frames_per_second);
1191
1192         set_dirty();
1193
1194         /* XXX need to reset/reinstantiate all LADSPA plugins */
1195 }
1196
1197 void
1198 Session::set_block_size (pframes_t nframes)
1199 {
1200         /* the AudioEngine guarantees
1201            that it will not be called while we are also in
1202            ::process(). It is therefore fine to do things that block
1203            here.
1204         */
1205
1206         {
1207                 current_block_size = nframes;
1208
1209                 ensure_buffers ();
1210
1211                 boost::shared_ptr<RouteList> r = routes.reader ();
1212
1213                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1214                         (*i)->set_block_size (nframes);
1215                 }
1216
1217                 boost::shared_ptr<RouteList> rl = routes.reader ();
1218                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1219                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1220                         if (tr) {
1221                                 tr->set_block_size (nframes);
1222                         }
1223                 }
1224
1225                 set_worst_io_latencies ();
1226         }
1227 }
1228
1229 struct RouteSorter {
1230     /** @return true to run r1 before r2, otherwise false */
1231     bool operator() (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> r2) {
1232             if (r2->feeds (r1)) {
1233                     /* r1 fed by r2; run r2 early */
1234                     return false;
1235             } else if (r1->feeds (r2)) {
1236                     /* r2 fed by r1; run r1 early */
1237                     return true;
1238             } else {
1239                     if (r1->not_fed ()) {
1240                             if (r2->not_fed ()) {
1241                                     /* no ardour-based connections inbound to either route. just use signal order */
1242                                     return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1243                             } else {
1244                                     /* r2 has connections, r1 does not; run r1 early */
1245                                     return true;
1246                             }
1247                     } else {
1248                             if (r2->not_fed()) {
1249                                     /* r1 has connections, r2 does not; run r2 early */
1250                                     return false;
1251                             } else {
1252                                     /* both r1 and r2 have connections, but not to each other. just use signal order */
1253                                     return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1254                             }
1255                     }
1256             }
1257     }
1258 };
1259
1260 static void
1261 trace_terminal (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> rbase)
1262 {
1263         boost::shared_ptr<Route> r2;
1264
1265         if (r1->feeds (rbase) && rbase->feeds (r1)) {
1266                 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
1267                 return;
1268         }
1269
1270         /* make a copy of the existing list of routes that feed r1 */
1271
1272         Route::FedBy existing (r1->fed_by());
1273                         
1274         /* for each route that feeds r1, recurse, marking it as feeding
1275            rbase as well.
1276         */
1277
1278         for (Route::FedBy::iterator i = existing.begin(); i != existing.end(); ++i) {
1279                 if (!(r2 = i->r.lock ())) {
1280                         /* (*i) went away, ignore it */
1281                         continue;
1282                 }
1283                 
1284                 /* r2 is a route that feeds r1 which somehow feeds base. mark
1285                    base as being fed by r2
1286                 */
1287
1288                 rbase->add_fed_by (r2, i->sends_only);
1289
1290                 if (r2 != rbase) {
1291
1292                         /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
1293                            stop here.
1294                         */
1295
1296                         if (r1->feeds (r2) && r2->feeds (r1)) {
1297                                 continue;
1298                         }
1299
1300                         /* now recurse, so that we can mark base as being fed by
1301                            all routes that feed r2
1302                         */
1303
1304                         trace_terminal (r2, rbase);
1305                 }
1306
1307         }
1308 }
1309
1310 void
1311 Session::resort_routes ()
1312 {
1313         /* don't do anything here with signals emitted
1314            by Routes while we are being destroyed.
1315         */
1316
1317         if (_state_of_the_state & Deletion) {
1318                 return;
1319         }
1320
1321         {
1322                 RCUWriter<RouteList> writer (routes);
1323                 boost::shared_ptr<RouteList> r = writer.get_copy ();
1324                 resort_routes_using (r);
1325                 /* writer goes out of scope and forces update */
1326         }
1327
1328         //route_graph->dump(1);
1329
1330 #ifndef NDEBUG
1331         boost::shared_ptr<RouteList> rl = routes.reader ();
1332         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1333                 DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name()));
1334                 
1335                 const Route::FedBy& fb ((*i)->fed_by());
1336
1337                 for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) {
1338                         boost::shared_ptr<Route> sf = f->r.lock();
1339                         if (sf) {
1340                                 DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only));
1341                         }
1342                 }
1343         }
1344 #endif
1345
1346 }
1347 void
1348 Session::resort_routes_using (boost::shared_ptr<RouteList> r)
1349 {
1350         RouteList::iterator i, j;
1351
1352         for (i = r->begin(); i != r->end(); ++i) {
1353
1354                 (*i)->clear_fed_by ();
1355
1356                 for (j = r->begin(); j != r->end(); ++j) {
1357
1358                         /* although routes can feed themselves, it will
1359                            cause an endless recursive descent if we
1360                            detect it. so don't bother checking for
1361                            self-feeding.
1362                         */
1363
1364                         if (*j == *i) {
1365                                 continue;
1366                         }
1367
1368                         bool via_sends_only;
1369
1370                         if ((*j)->direct_feeds (*i, &via_sends_only)) {
1371                                 (*i)->add_fed_by (*j, via_sends_only);
1372                         }
1373                 }
1374         }
1375
1376         for (i = r->begin(); i != r->end(); ++i) {
1377                 trace_terminal (*i, *i);
1378         }
1379
1380         RouteSorter cmp;
1381         r->sort (cmp);
1382
1383         route_graph->rechain (r);
1384
1385 #ifndef NDEBUG
1386         DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
1387         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1388                 DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 signal order %2\n", 
1389                                                            (*i)->name(), (*i)->order_key ("signal")));
1390         }
1391 #endif
1392
1393 }
1394
1395 /** Find a route name starting with \a base, maybe followed by the
1396  *  lowest \a id.  \a id will always be added if \a definitely_add_number
1397  *  is true on entry; otherwise it will only be added if required
1398  *  to make the name unique.
1399  *
1400  *  Names are constructed like e.g. "Audio 3" for base="Audio" and id=3.
1401  *  The available route name with the lowest ID will be used, and \a id
1402  *  will be set to the ID.
1403  *
1404  *  \return false if a route name could not be found, and \a track_name
1405  *  and \a id do not reflect a free route name.
1406  */
1407 bool
1408 Session::find_route_name (string const & base, uint32_t& id, char* name, size_t name_len, bool definitely_add_number)
1409 {
1410         if (!definitely_add_number && route_by_name (base) == 0) {
1411                 /* juse use the base */
1412                 snprintf (name, name_len, "%s", base.c_str());
1413                 return true;
1414         }
1415                 
1416         do {
1417                 snprintf (name, name_len, "%s %" PRIu32, base.c_str(), id);
1418
1419                 if (route_by_name (name) == 0) {
1420                         return true;
1421                 }
1422
1423                 ++id;
1424
1425         } while (id < (UINT_MAX-1));
1426
1427         return false;
1428 }
1429
1430 /** Count the total ins and outs of all non-hidden routes in the session and return them in in and out */
1431 void
1432 Session::count_existing_route_channels (ChanCount& in, ChanCount& out)
1433 {
1434         in  = ChanCount::ZERO;
1435         out = ChanCount::ZERO;
1436         boost::shared_ptr<RouteList> r = routes.reader ();
1437         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1438                 if (!(*i)->is_hidden()) {
1439                         in  += (*i)->n_inputs();
1440                         out += (*i)->n_outputs();
1441                 }
1442         }
1443 }
1444
1445 /** Caller must not hold process lock
1446  *  @param name_template string to use for the start of the name, or "" to use "Midi".
1447  */
1448 list<boost::shared_ptr<MidiTrack> >
1449 Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template)
1450 {
1451         char track_name[32];
1452         uint32_t track_id = 0;
1453         ChanCount existing_inputs;
1454         ChanCount existing_outputs;
1455         string port;
1456         RouteList new_routes;
1457         list<boost::shared_ptr<MidiTrack> > ret;
1458         uint32_t control_id;
1459
1460         count_existing_route_channels (existing_inputs, existing_outputs);
1461
1462         control_id = ntracks() + nbusses();
1463
1464         bool const use_number = (how_many != 1);
1465         
1466         while (how_many) {
1467                 if (!find_route_name (name_template.empty() ? _("Midi") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
1468                         error << "cannot find name for new midi track" << endmsg;
1469                         goto failed;
1470                 }
1471
1472                 boost::shared_ptr<MidiTrack> track;
1473                 
1474                 try {
1475                         track.reset (new MidiTrack (*this, track_name, Route::Flag (0), mode));
1476
1477                         if (track->init ()) {
1478                                 goto failed;
1479                         }
1480
1481                         track->use_new_diskstream();
1482
1483 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1484                         boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
1485 #endif
1486                         {
1487                                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1488                                 if (track->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
1489                                         error << "cannot configure 1 in/1 out configuration for new midi track" << endmsg;
1490                                         goto failed;
1491                                 }
1492
1493                                 if (track->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
1494                                         error << "cannot configure 1 in/1 out configuration for new midi track" << endmsg;
1495                                         goto failed;
1496                                 }
1497                         }
1498
1499                         auto_connect_route (track.get(), existing_inputs, existing_outputs);
1500
1501                         track->non_realtime_input_change();
1502
1503                         if (route_group) {
1504                                 route_group->add (track);
1505                         }
1506
1507                         track->DiskstreamChanged.connect_same_thread (*this, boost::bind (&Session::resort_routes, this));
1508                         track->set_remote_control_id (control_id);
1509
1510                         new_routes.push_back (track);
1511                         ret.push_back (track);
1512                 }
1513
1514                 catch (failed_constructor &err) {
1515                         error << _("Session: could not create new midi track.") << endmsg;
1516                         goto failed;
1517                 }
1518
1519                 catch (AudioEngine::PortRegistrationFailure& pfe) {
1520
1521                         error << string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with ports if you need this many tracks."), PROGRAM_NAME) << endmsg;
1522                         goto failed;
1523                 }
1524
1525                 --how_many;
1526         }
1527
1528   failed:
1529         if (!new_routes.empty()) {
1530                 add_routes (new_routes, false);
1531                 save_state (_current_snapshot_name);
1532         }
1533
1534         return ret;
1535 }
1536
1537 /** Caller must hold process lock.
1538  *  @param connect_inputs true to connect inputs as well as outputs, false to connect just outputs.
1539  *  @param input_start Where to start from when auto-connecting inputs; e.g. if this is 0, auto-connect starting from input 0.
1540  *  @param output_start As \a input_start, but for outputs.
1541  */
1542 void
1543 Session::auto_connect_route (
1544         Route* route, ChanCount& existing_inputs, ChanCount& existing_outputs, bool connect_inputs, ChanCount input_start, ChanCount output_start
1545         )
1546 {
1547         /* If both inputs and outputs are auto-connected to physical ports,
1548            use the max of input and output offsets to ensure auto-connected
1549            port numbers always match up (e.g. the first audio input and the
1550            first audio output of the route will have the same physical
1551            port number).  Otherwise just use the lowest input or output
1552            offset possible.
1553         */
1554
1555         const bool in_out_physical =
1556                    (Config->get_input_auto_connect() & AutoConnectPhysical)
1557                 && (Config->get_output_auto_connect() & AutoConnectPhysical)
1558                 && connect_inputs;
1559
1560         const ChanCount in_offset = in_out_physical
1561                 ? ChanCount::max(existing_inputs, existing_outputs)
1562                 : existing_inputs;
1563
1564         const ChanCount out_offset = in_out_physical
1565                 ? ChanCount::max(existing_inputs, existing_outputs)
1566                 : existing_outputs;
1567
1568         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1569                 vector<string> physinputs;
1570                 vector<string> physoutputs;
1571
1572                 _engine.get_physical_outputs (*t, physoutputs);
1573                 _engine.get_physical_inputs (*t, physinputs);
1574
1575                 if (!physinputs.empty() && connect_inputs) {
1576                         uint32_t nphysical_in = physinputs.size();
1577                         for (uint32_t i = input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) {
1578                                 string port;
1579
1580                                 if (Config->get_input_auto_connect() & AutoConnectPhysical) {
1581                                         port = physinputs[(in_offset.get(*t) + i) % nphysical_in];
1582                                 }
1583
1584                                 if (!port.empty() && route->input()->connect (
1585                                                 route->input()->ports().port(*t, i), port, this)) {
1586                                         break;
1587                                 }
1588                         }
1589                 }
1590
1591                 if (!physoutputs.empty()) {
1592                         uint32_t nphysical_out = physoutputs.size();
1593                         for (uint32_t i = output_start.get(*t); i < route->n_outputs().get(*t); ++i) {
1594                                 string port;
1595
1596                                 if (Config->get_output_auto_connect() & AutoConnectPhysical) {
1597                                         port = physoutputs[(out_offset.get(*t) + i) % nphysical_out];
1598                                 } else if (Config->get_output_auto_connect() & AutoConnectMaster) {
1599                                         if (_master_out && _master_out->n_inputs().get(*t) > 0) {
1600                                                 port = _master_out->input()->ports().port(*t,
1601                                                                 i % _master_out->input()->n_ports().get(*t))->name();
1602                                         }
1603                                 }
1604
1605                                 if (!port.empty() && route->output()->connect (
1606                                                 route->output()->ports().port(*t, i), port, this)) {
1607                                         break;
1608                                 }
1609                         }
1610                 }
1611         }
1612
1613         existing_inputs += route->n_inputs();
1614         existing_outputs += route->n_outputs();
1615 }
1616
1617 /** Caller must not hold process lock
1618  *  @param name_template string to use for the start of the name, or "" to use "Audio".
1619  */
1620 list< boost::shared_ptr<AudioTrack> >
1621 Session::new_audio_track (
1622         int input_channels, int output_channels, TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template
1623         )
1624 {
1625         char track_name[32];
1626         uint32_t track_id = 0;
1627         ChanCount existing_inputs;
1628         ChanCount existing_outputs;
1629         string port;
1630         RouteList new_routes;
1631         list<boost::shared_ptr<AudioTrack> > ret;
1632         uint32_t control_id;
1633
1634         count_existing_route_channels (existing_inputs, existing_outputs);
1635
1636         control_id = ntracks() + nbusses() + 1;
1637
1638         bool const use_number = (how_many != 1);
1639         
1640         while (how_many) {
1641                 if (!find_route_name (name_template.empty() ? _("Audio") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
1642                         error << "cannot find name for new audio track" << endmsg;
1643                         goto failed;
1644                 }
1645
1646                 boost::shared_ptr<AudioTrack> track;
1647                 
1648                 try {
1649                         track.reset (new AudioTrack (*this, track_name, Route::Flag (0), mode));
1650
1651                         if (track->init ()) {
1652                                 goto failed;
1653                         }
1654
1655                         track->use_new_diskstream();
1656
1657 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1658                         boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
1659 #endif
1660                         {
1661                                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1662
1663                                 if (track->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
1664                                         error << string_compose (
1665                                                 _("cannot configure %1 in/%2 out configuration for new audio track"),
1666                                                 input_channels, output_channels)
1667                                               << endmsg;
1668                                         goto failed;
1669                                 }
1670                                 
1671                                 if (track->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
1672                                         error << string_compose (
1673                                                 _("cannot configure %1 in/%2 out configuration for new audio track"),
1674                                                 input_channels, output_channels)
1675                                               << endmsg;
1676                                         goto failed;
1677                                 }
1678
1679                                 auto_connect_route (track.get(), existing_inputs, existing_outputs);
1680                         }
1681
1682                         if (route_group) {
1683                                 route_group->add (track);
1684                         }
1685
1686                         track->non_realtime_input_change();
1687
1688                         track->DiskstreamChanged.connect_same_thread (*this, boost::bind (&Session::resort_routes, this));
1689                         track->set_remote_control_id (control_id);
1690                         ++control_id;
1691
1692                         new_routes.push_back (track);
1693                         ret.push_back (track);
1694                 }
1695
1696                 catch (failed_constructor &err) {
1697                         error << _("Session: could not create new audio track.") << endmsg;
1698                         goto failed;
1699                 }
1700
1701                 catch (AudioEngine::PortRegistrationFailure& pfe) {
1702
1703                         error << pfe.what() << endmsg;
1704                         goto failed;
1705                 }
1706
1707                 --how_many;
1708         }
1709
1710   failed:
1711         if (!new_routes.empty()) {
1712                 add_routes (new_routes, true);
1713         }
1714
1715         return ret;
1716 }
1717
1718 void
1719 Session::set_remote_control_ids ()
1720 {
1721         RemoteModel m = Config->get_remote_model();
1722         bool emit_signal = false;
1723
1724         boost::shared_ptr<RouteList> r = routes.reader ();
1725
1726         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1727                 if (MixerOrdered == m) {
1728                         int32_t order = (*i)->order_key(N_("signal"));
1729                         (*i)->set_remote_control_id (order+1, false);
1730                         emit_signal = true;
1731                 } else if (EditorOrdered == m) {
1732                         int32_t order = (*i)->order_key(N_("editor"));
1733                         (*i)->set_remote_control_id (order+1, false);
1734                         emit_signal = true;
1735                 } else if (UserOrdered == m) {
1736                         //do nothing ... only changes to remote id's are initiated by user
1737                 }
1738         }
1739
1740         if (emit_signal) {
1741                 Route::RemoteControlIDChange();
1742         }
1743 }
1744
1745 /** Caller must not hold process lock.
1746  *  @param name_template string to use for the start of the name, or "" to use "Bus".
1747  */
1748 RouteList
1749 Session::new_audio_route (int input_channels, int output_channels, RouteGroup* route_group, uint32_t how_many, string name_template)
1750 {
1751         char bus_name[32];
1752         uint32_t bus_id = 0;
1753         ChanCount existing_inputs;
1754         ChanCount existing_outputs;
1755         string port;
1756         RouteList ret;
1757         uint32_t control_id;
1758
1759         count_existing_route_channels (existing_inputs, existing_outputs);
1760
1761         control_id = ntracks() + nbusses() + 1;
1762
1763         bool const use_number = (how_many != 1);
1764         while (how_many) {
1765                 if (!find_route_name (name_template.empty () ? _("Bus") : name_template, ++bus_id, bus_name, sizeof(bus_name), use_number)) {
1766                         error << "cannot find name for new audio bus" << endmsg;
1767                         goto failure;
1768                 }
1769
1770                 try {
1771                         boost::shared_ptr<Route> bus (new Route (*this, bus_name, Route::Flag(0), DataType::AUDIO));
1772
1773                         if (bus->init ()) {
1774                                 goto failure;
1775                         }
1776
1777 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1778                         boost_debug_shared_ptr_mark_interesting (bus.get(), "Route");
1779 #endif
1780                         {
1781                                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1782
1783                                 if (bus->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
1784                                         error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1785                                                                  input_channels, output_channels)
1786                                               << endmsg;
1787                                         goto failure;
1788                                 }
1789                                 
1790                                 
1791                                 if (bus->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
1792                                         error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1793                                                                  input_channels, output_channels)
1794                                               << endmsg;
1795                                         goto failure;
1796                                 }
1797
1798                                 auto_connect_route (bus.get(), existing_inputs, existing_outputs, false);
1799                         }
1800
1801                         if (route_group) {
1802                                 route_group->add (bus);
1803                         }
1804                         bus->set_remote_control_id (control_id);
1805                         ++control_id;
1806
1807                         bus->add_internal_return ();
1808
1809                         ret.push_back (bus);
1810                 }
1811
1812
1813                 catch (failed_constructor &err) {
1814                         error << _("Session: could not create new audio route.") << endmsg;
1815                         goto failure;
1816                 }
1817
1818                 catch (AudioEngine::PortRegistrationFailure& pfe) {
1819                         error << pfe.what() << endmsg;
1820                         goto failure;
1821                 }
1822
1823
1824                 --how_many;
1825         }
1826
1827   failure:
1828         if (!ret.empty()) {
1829                 add_routes (ret, true);
1830         }
1831
1832         return ret;
1833
1834 }
1835
1836 RouteList
1837 Session::new_route_from_template (uint32_t how_many, const std::string& template_path)
1838 {
1839         char name[32];
1840         RouteList ret;
1841         uint32_t control_id;
1842         XMLTree tree;
1843         uint32_t number = 0;
1844
1845         if (!tree.read (template_path.c_str())) {
1846                 return ret;
1847         }
1848
1849         XMLNode* node = tree.root();
1850
1851         control_id = ntracks() + nbusses() + 1;
1852
1853         while (how_many) {
1854
1855                 XMLNode node_copy (*node); // make a copy so we can change the name if we need to
1856
1857                 std::string node_name = IO::name_from_state (*node_copy.children().front());
1858
1859                 /* generate a new name by adding a number to the end of the template name */
1860                 if (!find_route_name (node_name.c_str(), ++number, name, sizeof(name), true)) {
1861                         fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
1862                         /*NOTREACHED*/
1863                 }
1864
1865                 /* set IO children to use the new name */
1866                 XMLNodeList const & children = node_copy.children ();
1867                 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1868                         if ((*i)->name() == IO::state_node_name) {
1869                                 IO::set_name_in_state (**i, name);
1870                         }
1871                 }
1872
1873                 Track::zero_diskstream_id_in_xml (node_copy);
1874
1875                 try {
1876                         boost::shared_ptr<Route> route (XMLRouteFactory (node_copy, 3000));
1877
1878                         if (route == 0) {
1879                                 error << _("Session: cannot create track/bus from template description") << endmsg;
1880                                 goto out;
1881                         }
1882
1883                         if (boost::dynamic_pointer_cast<Track>(route)) {
1884                                 /* force input/output change signals so that the new diskstream
1885                                    picks up the configuration of the route. During session
1886                                    loading this normally happens in a different way.
1887                                 */
1888                                 
1889                                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1890                                 
1891                                 IOChange change (IOChange::Type (IOChange::ConfigurationChanged | IOChange::ConnectionsChanged));
1892                                 change.after = route->input()->n_ports();
1893                                 route->input()->changed (change, this);
1894                                 change.after = route->output()->n_ports();
1895                                 route->output()->changed (change, this);
1896                         }
1897
1898                         route->set_remote_control_id (control_id);
1899                         ++control_id;
1900
1901                         ret.push_back (route);
1902                 }
1903
1904                 catch (failed_constructor &err) {
1905                         error << _("Session: could not create new route from template") << endmsg;
1906                         goto out;
1907                 }
1908
1909                 catch (AudioEngine::PortRegistrationFailure& pfe) {
1910                         error << pfe.what() << endmsg;
1911                         goto out;
1912                 }
1913
1914                 --how_many;
1915         }
1916
1917   out:
1918         if (!ret.empty()) {
1919                 add_routes (ret, true);
1920         }
1921
1922         return ret;
1923 }
1924
1925 void
1926 Session::add_routes (RouteList& new_routes, bool save)
1927 {
1928         {
1929                 RCUWriter<RouteList> writer (routes);
1930                 boost::shared_ptr<RouteList> r = writer.get_copy ();
1931                 r->insert (r->end(), new_routes.begin(), new_routes.end());
1932
1933
1934                 /* if there is no control out and we're not in the middle of loading,
1935                    resort the graph here. if there is a control out, we will resort
1936                    toward the end of this method. if we are in the middle of loading,
1937                    we will resort when done.
1938                 */
1939
1940                 if (!_monitor_out && IO::connecting_legal) {
1941                         resort_routes_using (r);
1942                 }
1943         }
1944
1945         for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
1946
1947                 boost::weak_ptr<Route> wpr (*x);
1948                 boost::shared_ptr<Route> r (*x);
1949
1950                 r->listen_changed.connect_same_thread (*this, boost::bind (&Session::route_listen_changed, this, _1, wpr));
1951                 r->solo_changed.connect_same_thread (*this, boost::bind (&Session::route_solo_changed, this, _1, _2, wpr));
1952                 r->solo_isolated_changed.connect_same_thread (*this, boost::bind (&Session::route_solo_isolated_changed, this, _1, wpr));
1953                 r->mute_changed.connect_same_thread (*this, boost::bind (&Session::route_mute_changed, this, _1));
1954                 r->output()->changed.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies_x, this, _1, _2));
1955                 r->processors_changed.connect_same_thread (*this, boost::bind (&Session::route_processors_changed, this, _1));
1956                 r->order_key_changed.connect_same_thread (*this, boost::bind (&Session::route_order_key_changed, this));
1957
1958                 if (r->is_master()) {
1959                         _master_out = r;
1960                 }
1961
1962                 if (r->is_monitor()) {
1963                         _monitor_out = r;
1964                 }
1965
1966                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (r);
1967                 if (tr) {
1968                         tr->PlaylistChanged.connect_same_thread (*this, boost::bind (&Session::track_playlist_changed, this, boost::weak_ptr<Track> (tr)));
1969                         track_playlist_changed (boost::weak_ptr<Track> (tr));
1970                         tr->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Session::update_have_rec_enabled_track, this));
1971
1972                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (tr);
1973                         if (mt) {
1974                                 mt->StepEditStatusChange.connect_same_thread (*this, boost::bind (&Session::step_edit_status_change, this, _1));
1975                         }
1976                 }
1977         }
1978
1979         if (_monitor_out && IO::connecting_legal) {
1980
1981                 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
1982                         if ((*x)->is_monitor()) {
1983                                 /* relax */
1984                         } else if ((*x)->is_master()) {
1985                                 /* relax */
1986                         } else {
1987                                 (*x)->listen_via_monitor ();
1988                         }
1989                 }
1990
1991                 resort_routes ();
1992         }
1993
1994         set_dirty();
1995
1996         if (save) {
1997                 save_state (_current_snapshot_name);
1998         }
1999
2000         RouteAdded (new_routes); /* EMIT SIGNAL */
2001         Route::RemoteControlIDChange (); /* EMIT SIGNAL */
2002 }
2003
2004 void
2005 Session::globally_set_send_gains_to_zero (boost::shared_ptr<Route> dest)
2006 {
2007         boost::shared_ptr<RouteList> r = routes.reader ();
2008         boost::shared_ptr<Send> s;
2009
2010         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2011                 if ((s = (*i)->internal_send_for (dest)) != 0) {
2012                         s->amp()->gain_control()->set_value (0.0);
2013                 }
2014         }
2015 }
2016
2017 void
2018 Session::globally_set_send_gains_to_unity (boost::shared_ptr<Route> dest)
2019 {
2020         boost::shared_ptr<RouteList> r = routes.reader ();
2021         boost::shared_ptr<Send> s;
2022
2023         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2024                 if ((s = (*i)->internal_send_for (dest)) != 0) {
2025                         s->amp()->gain_control()->set_value (1.0);
2026                 }
2027         }
2028 }
2029
2030 void
2031 Session::globally_set_send_gains_from_track(boost::shared_ptr<Route> dest)
2032 {
2033         boost::shared_ptr<RouteList> r = routes.reader ();
2034         boost::shared_ptr<Send> s;
2035
2036         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2037                 if ((s = (*i)->internal_send_for (dest)) != 0) {
2038                         s->amp()->gain_control()->set_value ((*i)->gain_control()->get_value());
2039                 }
2040         }
2041 }
2042
2043 /** @param include_buses true to add sends to buses and tracks, false for just tracks */
2044 void
2045 Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p, bool include_buses)
2046 {
2047         boost::shared_ptr<RouteList> r = routes.reader ();
2048         boost::shared_ptr<RouteList> t (new RouteList);
2049
2050         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2051                 if (include_buses || boost::dynamic_pointer_cast<Track>(*i)) {
2052                         t->push_back (*i);
2053                 }
2054         }
2055
2056         add_internal_sends (dest, p, t);
2057 }
2058
2059 void
2060 Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::shared_ptr<RouteList> senders)
2061 {
2062         if (dest->is_monitor() || dest->is_master()) {
2063                 return;
2064         }
2065
2066         if (!dest->internal_return()) {
2067                 dest->add_internal_return();
2068         }
2069
2070         for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
2071
2072                 if ((*i)->is_monitor() || (*i)->is_master() || (*i) == dest) {
2073                         continue;
2074                 }
2075
2076                 (*i)->listen_via (dest, p);
2077         }
2078
2079         graph_reordered ();
2080 }
2081
2082 void
2083 Session::remove_route (boost::shared_ptr<Route> route)
2084 {
2085         if (((route == _master_out) || (route == _monitor_out)) && !Config->get_allow_special_bus_removal()) {
2086                 return;
2087         }
2088
2089         route->set_solo (false, this);
2090
2091         {
2092                 RCUWriter<RouteList> writer (routes);
2093                 boost::shared_ptr<RouteList> rs = writer.get_copy ();
2094
2095                 rs->remove (route);
2096
2097                 /* deleting the master out seems like a dumb
2098                    idea, but its more of a UI policy issue
2099                    than our concern.
2100                 */
2101
2102                 if (route == _master_out) {
2103                         _master_out = boost::shared_ptr<Route> ();
2104                 }
2105
2106                 if (route == _monitor_out) {
2107
2108                         /* cancel control outs for all routes */
2109
2110                         for (RouteList::iterator r = rs->begin(); r != rs->end(); ++r) {
2111                                 (*r)->drop_listen (_monitor_out);
2112                         }
2113
2114                         _monitor_out.reset ();
2115                 }
2116
2117                 /* writer goes out of scope, forces route list update */
2118         }
2119
2120         update_route_solo_state ();
2121
2122         // We need to disconnect the route's inputs and outputs
2123
2124         route->input()->disconnect (0);
2125         route->output()->disconnect (0);
2126
2127         /* if the route had internal sends sending to it, remove them */
2128         if (route->internal_return()) {
2129
2130                 boost::shared_ptr<RouteList> r = routes.reader ();
2131                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2132                         boost::shared_ptr<Send> s = (*i)->internal_send_for (route);
2133                         if (s) {
2134                                 (*i)->remove_processor (s);
2135                         }
2136                 }
2137         }       
2138
2139         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
2140         if (mt && mt->step_editing()) {
2141                 if (_step_editors > 0) {
2142                         _step_editors--;
2143                 }
2144         }
2145
2146         update_latency_compensation (false, false);
2147         set_dirty();
2148
2149         /* Re-sort routes to remove the graph's current references to the one that is
2150          * going away, then flush old references out of the graph.
2151          */
2152
2153         resort_routes ();
2154         route_graph->clear_other_chain ();
2155
2156         /* get rid of it from the dead wood collection in the route list manager */
2157
2158         /* XXX i think this is unsafe as it currently stands, but i am not sure. (pd, october 2nd, 2006) */
2159
2160         routes.flush ();
2161
2162         /* try to cause everyone to drop their references */
2163
2164         route->drop_references ();
2165
2166         sync_order_keys (N_("session"));
2167
2168         Route::RemoteControlIDChange(); /* EMIT SIGNAL */
2169
2170         /* save the new state of the world */
2171
2172         if (save_state (_current_snapshot_name)) {
2173                 save_history (_current_snapshot_name);
2174         }
2175 }
2176
2177 void
2178 Session::route_mute_changed (void* /*src*/)
2179 {
2180         set_dirty ();
2181 }
2182
2183 void
2184 Session::route_listen_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
2185 {
2186         boost::shared_ptr<Route> route = wpr.lock();
2187         if (!route) {
2188                 error << string_compose (_("programming error: %1"), X_("invalid route weak ptr passed to route_solo_changed")) << endmsg;
2189                 return;
2190         }
2191
2192         if (route->listening_via_monitor ()) {
2193
2194                 if (Config->get_exclusive_solo()) {
2195                         /* new listen: disable all other listen */
2196                         boost::shared_ptr<RouteList> r = routes.reader ();
2197                         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2198                                 if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden()) {
2199                                         continue;
2200                                 } 
2201                                 (*i)->set_listen (false, this);
2202                         }
2203                 }
2204
2205                 _listen_cnt++;
2206
2207         } else if (_listen_cnt > 0) {
2208
2209                 _listen_cnt--;
2210         }
2211
2212         update_route_solo_state ();
2213 }
2214 void
2215 Session::route_solo_isolated_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
2216 {
2217         boost::shared_ptr<Route> route = wpr.lock ();
2218
2219         if (!route) {
2220                 /* should not happen */
2221                 error << string_compose (_("programming error: %1"), X_("invalid route weak ptr passed to route_solo_changed")) << endmsg;
2222                 return;
2223         }
2224         
2225         bool send_changed = false;
2226
2227         if (route->solo_isolated()) {
2228                 if (_solo_isolated_cnt == 0) {
2229                         send_changed = true;
2230                 }
2231                 _solo_isolated_cnt++;
2232         } else if (_solo_isolated_cnt > 0) {
2233                 _solo_isolated_cnt--;
2234                 if (_solo_isolated_cnt == 0) {
2235                         send_changed = true;
2236                 }
2237         }
2238
2239         if (send_changed) {
2240                 IsolatedChanged (); /* EMIT SIGNAL */
2241         }
2242 }
2243             
2244 void
2245 Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_ptr<Route> wpr)
2246 {
2247         if (!self_solo_change) {
2248                 // session doesn't care about changes to soloed-by-others
2249                 return;
2250         }
2251
2252         if (solo_update_disabled) {
2253                 // We know already
2254                 return;
2255         }
2256
2257         boost::shared_ptr<Route> route = wpr.lock ();
2258
2259         if (!route) {
2260                 /* should not happen */
2261                 error << string_compose (_("programming error: %1"), X_("invalid route weak ptr passed to route_solo_changed")) << endmsg;
2262                 return;
2263         }
2264         
2265         boost::shared_ptr<RouteList> r = routes.reader ();
2266         int32_t delta;
2267
2268         if (route->self_soloed()) {
2269                 delta = 1;
2270         } else {
2271                 delta = -1;
2272         }
2273  
2274         if (delta == 1 && Config->get_exclusive_solo()) {
2275                 /* new solo: disable all other solos */
2276                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2277                         if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden()) {
2278                                 continue;
2279                         } 
2280                         (*i)->set_solo (false, this);
2281                 }
2282         }
2283
2284         solo_update_disabled = true;
2285         
2286         RouteList uninvolved;
2287         
2288         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2289                 bool via_sends_only;
2290                 bool in_signal_flow;
2291
2292                 if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden()) {
2293                         continue;
2294                 } 
2295
2296                 in_signal_flow = false;
2297
2298                 if ((*i)->feeds (route, &via_sends_only)) {
2299                         if (!via_sends_only) {
2300                                 if (!route->soloed_by_others_upstream()) {
2301                                         (*i)->mod_solo_by_others_downstream (delta);
2302                                 }
2303                                 in_signal_flow = true;
2304                         }
2305                 } 
2306                 
2307                 if (route->feeds (*i, &via_sends_only)) {
2308                         (*i)->mod_solo_by_others_upstream (delta);
2309                         in_signal_flow = true;
2310                 }
2311
2312                 if (!in_signal_flow) {
2313                         uninvolved.push_back (*i);
2314                 }
2315         }
2316
2317         solo_update_disabled = false;
2318         update_route_solo_state (r);
2319
2320         /* now notify that the mute state of the routes not involved in the signal
2321            pathway of the just-solo-changed route may have altered.
2322         */
2323
2324         for (RouteList::iterator i = uninvolved.begin(); i != uninvolved.end(); ++i) {
2325                 (*i)->mute_changed (this);
2326         }
2327
2328         SoloChanged (); /* EMIT SIGNAL */
2329         set_dirty();
2330 }
2331
2332 void
2333 Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
2334 {
2335         /* now figure out if anything that matters is soloed (or is "listening")*/
2336
2337         bool something_soloed = false;
2338         uint32_t listeners = 0;
2339         uint32_t isolated = 0;
2340
2341         if (!r) {
2342                 r = routes.reader();
2343         }
2344
2345         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2346                 if (!(*i)->is_master() && !(*i)->is_monitor() && !(*i)->is_hidden() && (*i)->self_soloed()) {
2347                         something_soloed = true;
2348                 }
2349
2350                 if (!(*i)->is_hidden() && (*i)->listening_via_monitor()) {
2351                         if (Config->get_solo_control_is_listen_control()) {
2352                                 listeners++;
2353                         } else {
2354                                 (*i)->set_listen (false, this);
2355                         }
2356                 }
2357
2358                 if ((*i)->solo_isolated()) {
2359                         isolated++;
2360                 }
2361         }
2362
2363         if (something_soloed != _non_soloed_outs_muted) {
2364                 _non_soloed_outs_muted = something_soloed;
2365                 SoloActive (_non_soloed_outs_muted); /* EMIT SIGNAL */
2366         }
2367
2368         _listen_cnt = listeners;
2369
2370         if (isolated != _solo_isolated_cnt) {
2371                 _solo_isolated_cnt = isolated;
2372                 IsolatedChanged (); /* EMIT SIGNAL */
2373         }
2374 }
2375
2376 boost::shared_ptr<RouteList> 
2377 Session::get_routes_with_internal_returns() const
2378 {
2379         boost::shared_ptr<RouteList> r = routes.reader ();
2380         boost::shared_ptr<RouteList> rl (new RouteList);
2381
2382         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2383                 if ((*i)->internal_return ()) {
2384                         rl->push_back (*i);
2385                 }
2386         }
2387         return rl;
2388 }
2389
2390 bool
2391 Session::io_name_is_legal (const std::string& name)
2392 {
2393         boost::shared_ptr<RouteList> r = routes.reader ();
2394         
2395         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2396                 if ((*i)->name() == name) {
2397                         return false;
2398                 }
2399                 
2400                 if ((*i)->has_io_processor_named (name)) {
2401                         return false;
2402                 }
2403         }
2404         
2405         return true;
2406 }
2407
2408 boost::shared_ptr<Route>
2409 Session::route_by_name (string name)
2410 {
2411         boost::shared_ptr<RouteList> r = routes.reader ();
2412
2413         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2414                 if ((*i)->name() == name) {
2415                         return *i;
2416                 }
2417         }
2418
2419         return boost::shared_ptr<Route> ((Route*) 0);
2420 }
2421
2422 boost::shared_ptr<Route>
2423 Session::route_by_id (PBD::ID id)
2424 {
2425         boost::shared_ptr<RouteList> r = routes.reader ();
2426
2427         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2428                 if ((*i)->id() == id) {
2429                         return *i;
2430                 }
2431         }
2432
2433         return boost::shared_ptr<Route> ((Route*) 0);
2434 }
2435
2436 boost::shared_ptr<Route>
2437 Session::route_by_remote_id (uint32_t id)
2438 {
2439         boost::shared_ptr<RouteList> r = routes.reader ();
2440
2441         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2442                 if ((*i)->remote_control_id() == id) {
2443                         return *i;
2444                 }
2445         }
2446
2447         return boost::shared_ptr<Route> ((Route*) 0);
2448 }
2449
2450 void
2451 Session::playlist_region_added (boost::weak_ptr<Region> w)
2452 {
2453         boost::shared_ptr<Region> r = w.lock ();
2454         if (!r) {
2455                 return;
2456         }
2457
2458         /* These are the operations that are currently in progress... */
2459         list<GQuark> curr = _current_trans_quarks;
2460         curr.sort ();
2461
2462         /* ...and these are the operations during which we want to update
2463            the session range location markers.
2464         */
2465         list<GQuark> ops;
2466         ops.push_back (Operations::capture);
2467         ops.push_back (Operations::paste);
2468         ops.push_back (Operations::duplicate_region);
2469         ops.push_back (Operations::insert_file);
2470         ops.push_back (Operations::insert_region);
2471         ops.push_back (Operations::drag_region_brush);
2472         ops.push_back (Operations::region_drag);
2473         ops.push_back (Operations::selection_grab);
2474         ops.push_back (Operations::region_fill);
2475         ops.push_back (Operations::fill_selection);
2476         ops.push_back (Operations::create_region);
2477         ops.sort ();
2478
2479         /* See if any of the current operations match the ones that we want */
2480         list<GQuark> in;
2481         set_intersection (_current_trans_quarks.begin(), _current_trans_quarks.end(), ops.begin(), ops.end(), back_inserter (in));
2482
2483         /* If so, update the session range markers */
2484         if (!in.empty ()) {
2485                 maybe_update_session_range (r->position (), r->last_frame ());
2486         }
2487 }
2488
2489 /** Update the session range markers if a is before the current start or
2490  *  b is after the current end.
2491  */
2492 void
2493 Session::maybe_update_session_range (framepos_t a, framepos_t b)
2494 {
2495         if (_state_of_the_state & Loading) {
2496                 return;
2497         }
2498
2499         if (_session_range_location == 0) {
2500                 
2501                 add_session_range_location (a, b);
2502                 
2503         } else {
2504                 
2505                 if (a < _session_range_location->start()) {
2506                         _session_range_location->set_start (a);
2507                 }
2508                 
2509                 if (b > _session_range_location->end()) {
2510                         _session_range_location->set_end (b);
2511                 }
2512         }
2513 }
2514
2515 void
2516 Session::playlist_ranges_moved (list<Evoral::RangeMove<framepos_t> > const & ranges)
2517 {
2518         for (list<Evoral::RangeMove<framepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
2519                 maybe_update_session_range (i->to, i->to + i->length);
2520         }
2521 }
2522
2523 void
2524 Session::playlist_regions_extended (list<Evoral::Range<framepos_t> > const & ranges)
2525 {
2526         for (list<Evoral::Range<framepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
2527                 maybe_update_session_range (i->from, i->to);
2528         }
2529 }
2530
2531 /* Region management */
2532
2533 boost::shared_ptr<Region>
2534 Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
2535 {
2536         const RegionFactory::RegionMap& regions (RegionFactory::regions());
2537         RegionFactory::RegionMap::const_iterator i;
2538         boost::shared_ptr<Region> region;
2539
2540         Glib::Mutex::Lock lm (region_lock);
2541
2542         for (i = regions.begin(); i != regions.end(); ++i) {
2543
2544                 region = i->second;
2545
2546                 if (region->whole_file()) {
2547
2548                         if (child->source_equivalent (region)) {
2549                                 return region;
2550                         }
2551                 }
2552         }
2553
2554         return boost::shared_ptr<Region> ();
2555 }
2556
2557 int
2558 Session::destroy_sources (list<boost::shared_ptr<Source> > srcs)
2559 {
2560         set<boost::shared_ptr<Region> > relevant_regions;
2561
2562         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ++s) {
2563                 RegionFactory::get_regions_using_source (*s, relevant_regions);
2564         }
2565
2566         cerr << "There are " << relevant_regions.size() << " using " << srcs.size() << " sources" << endl;
2567
2568         for (set<boost::shared_ptr<Region> >::iterator r = relevant_regions.begin(); r != relevant_regions.end(); ) {
2569                 set<boost::shared_ptr<Region> >::iterator tmp;
2570
2571                 tmp = r;
2572                 ++tmp;
2573
2574                 cerr << "Cleanup " << (*r)->name() << " UC = " << (*r).use_count() << endl;
2575
2576                 playlists->destroy_region (*r);
2577                 RegionFactory::map_remove (*r);
2578
2579                 (*r)->drop_sources ();
2580                 (*r)->drop_references ();
2581
2582                 cerr << "\tdone UC = " << (*r).use_count() << endl;
2583
2584                 relevant_regions.erase (r);
2585
2586                 r = tmp;
2587         }
2588
2589         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ) {
2590                 
2591                 {
2592                         Glib::Mutex::Lock ls (source_lock);
2593                         /* remove from the main source list */
2594                         sources.erase ((*s)->id());
2595                 }
2596
2597                 (*s)->mark_for_remove ();
2598                 (*s)->drop_references ();
2599
2600                 s = srcs.erase (s);
2601         }
2602
2603         return 0;
2604 }
2605
2606 int
2607 Session::remove_last_capture ()
2608 {
2609         list<boost::shared_ptr<Source> > srcs;
2610
2611         boost::shared_ptr<RouteList> rl = routes.reader ();
2612         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2613                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2614                 if (!tr) {
2615                         continue;
2616                 }
2617                 
2618                 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
2619
2620                 if (!l.empty()) {
2621                         srcs.insert (srcs.end(), l.begin(), l.end());
2622                         l.clear ();
2623                 }
2624         }
2625
2626         destroy_sources (srcs);
2627
2628         save_state (_current_snapshot_name);
2629
2630         return 0;
2631 }
2632
2633 /* Source Management */
2634
2635 void
2636 Session::add_source (boost::shared_ptr<Source> source)
2637 {
2638         pair<SourceMap::key_type, SourceMap::mapped_type> entry;
2639         pair<SourceMap::iterator,bool> result;
2640
2641         entry.first = source->id();
2642         entry.second = source;
2643
2644         {
2645                 Glib::Mutex::Lock lm (source_lock);
2646                 result = sources.insert (entry);
2647         }
2648
2649         if (result.second) {
2650
2651                 /* yay, new source */
2652
2653                 set_dirty();
2654
2655                 boost::shared_ptr<AudioFileSource> afs;
2656                 
2657                 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
2658                         if (Config->get_auto_analyse_audio()) {
2659                                 Analyser::queue_source_for_analysis (source, false);
2660                         }
2661                 }
2662
2663                 source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
2664         }
2665 }
2666
2667 void
2668 Session::remove_source (boost::weak_ptr<Source> src)
2669 {
2670         if (_state_of_the_state & Deletion) {
2671                 return;
2672         }
2673         
2674         SourceMap::iterator i;
2675         boost::shared_ptr<Source> source = src.lock();
2676
2677         if (!source) {
2678                 return;
2679         }
2680
2681         {
2682                 Glib::Mutex::Lock lm (source_lock);
2683
2684                 if ((i = sources.find (source->id())) != sources.end()) {
2685                         sources.erase (i);
2686                 }
2687         }
2688
2689         if (!_state_of_the_state & InCleanup) {
2690
2691                 /* save state so we don't end up with a session file
2692                    referring to non-existent sources.
2693                 */
2694
2695                 save_state (_current_snapshot_name);
2696         }
2697 }
2698
2699 boost::shared_ptr<Source>
2700 Session::source_by_id (const PBD::ID& id)
2701 {
2702         Glib::Mutex::Lock lm (source_lock);
2703         SourceMap::iterator i;
2704         boost::shared_ptr<Source> source;
2705
2706         if ((i = sources.find (id)) != sources.end()) {
2707                 source = i->second;
2708         }
2709
2710         return source;
2711 }
2712
2713 boost::shared_ptr<Source>
2714 Session::source_by_path_and_channel (const string& path, uint16_t chn)
2715 {
2716         Glib::Mutex::Lock lm (source_lock);
2717
2718         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2719                 boost::shared_ptr<AudioFileSource> afs
2720                         = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
2721
2722                 if (afs && afs->path() == path && chn == afs->channel()) {
2723                         return afs;
2724                 }
2725         }
2726         return boost::shared_ptr<Source>();
2727 }
2728
2729 uint32_t
2730 Session::count_sources_by_origin (const string& path)
2731 {
2732         uint32_t cnt = 0;
2733         Glib::Mutex::Lock lm (source_lock);
2734
2735         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2736                 boost::shared_ptr<FileSource> fs
2737                         = boost::dynamic_pointer_cast<FileSource>(i->second);
2738
2739                 if (fs && fs->origin() == path) {
2740                         ++cnt;
2741                 }
2742         }
2743
2744         return cnt;
2745 }
2746
2747
2748 string
2749 Session::change_source_path_by_name (string path, string oldname, string newname, bool destructive)
2750 {
2751         string look_for;
2752         string old_basename = PBD::basename_nosuffix (oldname);
2753         string new_legalized = legalize_for_path (newname);
2754
2755         /* note: we know (or assume) the old path is already valid */
2756
2757         if (destructive) {
2758
2759                 /* destructive file sources have a name of the form:
2760
2761                     /path/to/Tnnnn-NAME(%[LR])?.wav
2762
2763                     the task here is to replace NAME with the new name.
2764                 */
2765
2766                 string dir;
2767                 string prefix;
2768                 string::size_type dash;
2769
2770                 dir = Glib::path_get_dirname (path);
2771                 path = Glib::path_get_basename (path);
2772
2773                 /* '-' is not a legal character for the NAME part of the path */
2774
2775                 if ((dash = path.find_last_of ('-')) == string::npos) {
2776                         return "";
2777                 }
2778
2779                 prefix = path.substr (0, dash);
2780
2781                 path += prefix;
2782                 path += '-';
2783                 path += new_legalized;
2784                 path += native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
2785                 path = Glib::build_filename (dir, path);
2786
2787         } else {
2788
2789                 /* non-destructive file sources have a name of the form:
2790
2791                     /path/to/NAME-nnnnn(%[LR])?.ext
2792
2793                     the task here is to replace NAME with the new name.
2794                 */
2795
2796                 string dir;
2797                 string suffix;
2798                 string::size_type dash;
2799                 string::size_type postfix;
2800
2801                 dir = Glib::path_get_dirname (path);
2802                 path = Glib::path_get_basename (path);
2803
2804                 /* '-' is not a legal character for the NAME part of the path */
2805
2806                 if ((dash = path.find_last_of ('-')) == string::npos) {
2807                         return "";
2808                 }
2809
2810                 suffix = path.substr (dash+1);
2811
2812                 // Suffix is now everything after the dash. Now we need to eliminate
2813                 // the nnnnn part, which is done by either finding a '%' or a '.'
2814
2815                 postfix = suffix.find_last_of ("%");
2816                 if (postfix == string::npos) {
2817                         postfix = suffix.find_last_of ('.');
2818                 }
2819
2820                 if (postfix != string::npos) {
2821                         suffix = suffix.substr (postfix);
2822                 } else {
2823                         error << "Logic error in Session::change_source_path_by_name(), please report" << endl;
2824                         return "";
2825                 }
2826
2827                 const uint32_t limit = 10000;
2828                 char buf[PATH_MAX+1];
2829
2830                 for (uint32_t cnt = 1; cnt <= limit; ++cnt) {
2831
2832                         snprintf (buf, sizeof(buf), "%s-%u%s", newname.c_str(), cnt, suffix.c_str());
2833
2834                         if (!matching_unsuffixed_filename_exists_in (dir, buf)) {
2835                                 path = Glib::build_filename (dir, buf);
2836                                 break;
2837                         }
2838
2839                         path = "";
2840                 }
2841
2842                 if (path.empty()) {
2843                         fatal << string_compose (_("FATAL ERROR! Could not find a suitable version of %1 for a rename"),
2844                                                  newname) << endl;
2845                         /*NOTREACHED*/
2846                 }
2847         }
2848
2849         return path;
2850 }
2851
2852 /** Return the full path (in some session directory) for a new within-session source.
2853  * \a name must be a session-unique name that does not contain slashes
2854  *         (e.g. as returned by new_*_source_name)
2855  */
2856 string
2857 Session::new_source_path_from_name (DataType type, const string& name)
2858 {
2859         assert(name.find("/") == string::npos);
2860
2861         SessionDirectory sdir(get_best_session_directory_for_new_source());
2862
2863         sys::path p;
2864         if (type == DataType::AUDIO) {
2865                 p = sdir.sound_path();
2866         } else if (type == DataType::MIDI) {
2867                 p = sdir.midi_path();
2868         } else {
2869                 error << "Unknown source type, unable to create file path" << endmsg;
2870                 return "";
2871         }
2872
2873         p /= name;
2874         return p.to_string();
2875 }
2876
2877 string
2878 Session::peak_path (string base) const
2879 {
2880         sys::path peakfile_path(_session_dir->peak_path());
2881         peakfile_path /= base + peakfile_suffix;
2882         return peakfile_path.to_string();
2883 }
2884
2885 /** Return a unique name based on \a base for a new internal audio source */
2886 string
2887 Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t chan, bool destructive)
2888 {
2889         uint32_t cnt;
2890         char buf[PATH_MAX+1];
2891         const uint32_t limit = 10000;
2892         string legalized;
2893         string ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
2894
2895         buf[0] = '\0';
2896         legalized = legalize_for_path (base);
2897
2898         // Find a "version" of the base name that doesn't exist in any of the possible directories.
2899         for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
2900
2901                 vector<space_and_path>::iterator i;
2902                 uint32_t existing = 0;
2903
2904                 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2905
2906                         if (destructive) {
2907
2908                                 if (nchan < 2) {
2909                                         snprintf (buf, sizeof(buf), "T%04d-%s%s",
2910                                                   cnt, legalized.c_str(), ext.c_str());
2911                                 } else if (nchan == 2) {
2912                                         if (chan == 0) {
2913                                                 snprintf (buf, sizeof(buf), "T%04d-%s%%L%s",
2914                                                           cnt, legalized.c_str(), ext.c_str());
2915                                         } else {
2916                                                 snprintf (buf, sizeof(buf), "T%04d-%s%%R%s",
2917                                                           cnt, legalized.c_str(), ext.c_str());
2918                                         }
2919                                 } else if (nchan < 26) {
2920                                         snprintf (buf, sizeof(buf), "T%04d-%s%%%c%s",
2921                                                   cnt, legalized.c_str(), 'a' + chan, ext.c_str());
2922                                 } else {
2923                                         snprintf (buf, sizeof(buf), "T%04d-%s%s",
2924                                                   cnt, legalized.c_str(), ext.c_str());
2925                                 }
2926
2927                         } else {
2928
2929                                 if (nchan < 2) {
2930                                         snprintf (buf, sizeof(buf), "%s-%u%s", legalized.c_str(), cnt, ext.c_str());
2931                                 } else if (nchan == 2) {
2932                                         if (chan == 0) {
2933                                                 snprintf (buf, sizeof(buf), "%s-%u%%L%s", legalized.c_str(), cnt, ext.c_str());
2934                                         } else {
2935                                                 snprintf (buf, sizeof(buf), "%s-%u%%R%s", legalized.c_str(), cnt, ext.c_str());
2936                                         }
2937                                 } else if (nchan < 26) {
2938                                         snprintf (buf, sizeof(buf), "%s-%u%%%c%s", legalized.c_str(), cnt, 'a' + chan, ext.c_str());
2939                                 } else {
2940                                         snprintf (buf, sizeof(buf), "%s-%u%s", legalized.c_str(), cnt, ext.c_str());
2941                                 }
2942                         }
2943
2944                         SessionDirectory sdir((*i).path);
2945
2946                         string spath = sdir.sound_path().to_string();
2947
2948                         /* note that we search *without* the extension so that
2949                            we don't end up both "Audio 1-1.wav" and "Audio 1-1.caf" 
2950                            in the event that this new name is required for
2951                            a file format change.
2952                         */
2953
2954                         if (matching_unsuffixed_filename_exists_in (spath, buf)) {
2955                                 existing++;
2956                                 break;
2957                         }
2958                 }
2959
2960                 if (existing == 0) {
2961                         break;
2962                 }
2963
2964                 if (cnt > limit) {
2965                         error << string_compose(
2966                                         _("There are already %1 recordings for %2, which I consider too many."),
2967                                         limit, base) << endmsg;
2968                         destroy ();
2969                         throw failed_constructor();
2970                 }
2971         }
2972         
2973         return Glib::path_get_basename (buf);
2974 }
2975
2976 /** Create a new within-session audio source */
2977 boost::shared_ptr<AudioFileSource>
2978 Session::create_audio_source_for_session (size_t n_chans, string const & n, uint32_t chan, bool destructive)
2979 {
2980         const string name    = new_audio_source_name (n, n_chans, chan, destructive);
2981         const string path    = new_source_path_from_name(DataType::AUDIO, name);
2982
2983         return boost::dynamic_pointer_cast<AudioFileSource> (
2984                 SourceFactory::createWritable (DataType::AUDIO, *this, path, string(), destructive, frame_rate()));
2985 }
2986
2987 /** Return a unique name based on \a base for a new internal MIDI source */
2988 string
2989 Session::new_midi_source_name (const string& base)
2990 {
2991         uint32_t cnt;
2992         char buf[PATH_MAX+1];
2993         const uint32_t limit = 10000;
2994         string legalized;
2995
2996         buf[0] = '\0';
2997         legalized = legalize_for_path (base);
2998
2999         // Find a "version" of the file name that doesn't exist in any of the possible directories.
3000         for (cnt = 1; cnt <= limit; ++cnt) {
3001
3002                 vector<space_and_path>::iterator i;
3003                 uint32_t existing = 0;
3004
3005                 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3006
3007                         SessionDirectory sdir((*i).path);
3008
3009                         sys::path p = sdir.midi_path();
3010                         p /= legalized;
3011
3012                         snprintf (buf, sizeof(buf), "%s-%u.mid", p.to_string().c_str(), cnt);
3013
3014                         if (sys::exists (buf)) {
3015                                 existing++;
3016                         }
3017                 }
3018
3019                 if (existing == 0) {
3020                         break;
3021                 }
3022
3023                 if (cnt > limit) {
3024                         error << string_compose(
3025                                         _("There are already %1 recordings for %2, which I consider too many."),
3026                                         limit, base) << endmsg;
3027                         destroy ();
3028                         throw failed_constructor();
3029                 }
3030         }
3031
3032         return Glib::path_get_basename(buf);
3033 }
3034
3035
3036 /** Create a new within-session MIDI source */
3037 boost::shared_ptr<MidiSource>
3038 Session::create_midi_source_for_session (Track* track, string const & n)
3039 {
3040         /* try to use the existing write source for the track, to keep numbering sane 
3041          */
3042
3043         if (track) {
3044                 /*MidiTrack* mt = dynamic_cast<Track*> (track);
3045                   assert (mt);
3046                 */
3047
3048                 list<boost::shared_ptr<Source> > l = track->steal_write_sources ();
3049                 
3050                 if (!l.empty()) {
3051                         assert (boost::dynamic_pointer_cast<MidiSource> (l.front()));
3052                         return boost::dynamic_pointer_cast<MidiSource> (l.front());
3053                 }
3054         }
3055
3056         const string name = new_midi_source_name (n);
3057         const string path = new_source_path_from_name (DataType::MIDI, name);
3058
3059         return boost::dynamic_pointer_cast<SMFSource> (
3060                 SourceFactory::createWritable (
3061                         DataType::MIDI, *this, path, string(), false, frame_rate()));
3062 }
3063
3064
3065 void
3066 Session::add_playlist (boost::shared_ptr<Playlist> playlist, bool unused)
3067 {
3068         if (playlist->hidden()) {
3069                 return;
3070         }
3071
3072         playlists->add (playlist);
3073
3074         if (unused) {
3075                 playlist->release();
3076         }
3077
3078         set_dirty();
3079 }
3080
3081 void
3082 Session::remove_playlist (boost::weak_ptr<Playlist> weak_playlist)
3083 {
3084         if (_state_of_the_state & Deletion) {
3085                 return;
3086         }
3087
3088         boost::shared_ptr<Playlist> playlist (weak_playlist.lock());
3089
3090         if (!playlist) {
3091                 return;
3092         }
3093
3094         playlists->remove (playlist);
3095
3096         set_dirty();
3097 }
3098
3099 void
3100 Session::set_audition (boost::shared_ptr<Region> r)
3101 {
3102         pending_audition_region = r;
3103         add_post_transport_work (PostTransportAudition);
3104         _butler->schedule_transport_work ();
3105 }
3106
3107 void
3108 Session::audition_playlist ()
3109 {
3110         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
3111         ev->region.reset ();
3112         queue_event (ev);
3113 }
3114
3115 void
3116 Session::non_realtime_set_audition ()
3117 {
3118         if (!pending_audition_region) {
3119                 auditioner->audition_current_playlist ();
3120         } else {
3121                 auditioner->audition_region (pending_audition_region);
3122                 pending_audition_region.reset ();
3123         }
3124         AuditionActive (true); /* EMIT SIGNAL */
3125 }
3126
3127 void
3128 Session::audition_region (boost::shared_ptr<Region> r)
3129 {
3130         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
3131         ev->region = r;
3132         queue_event (ev);
3133 }
3134
3135 void
3136 Session::cancel_audition ()
3137 {
3138         if (auditioner->auditioning()) {
3139                 auditioner->cancel_audition ();
3140                 AuditionActive (false); /* EMIT SIGNAL */
3141         }
3142 }
3143
3144 bool
3145 Session::RoutePublicOrderSorter::operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b)
3146 {
3147         if (a->is_monitor()) { 
3148                 return true;
3149         }
3150         if (b->is_monitor()) {
3151                 return false;
3152         }
3153         return a->order_key(N_("signal")) < b->order_key(N_("signal"));
3154 }
3155
3156 bool
3157 Session::is_auditioning () const
3158 {
3159         /* can be called before we have an auditioner object */
3160         if (auditioner) {
3161                 return auditioner->auditioning();
3162         } else {
3163                 return false;
3164         }
3165 }
3166
3167 void
3168 Session::graph_reordered ()
3169 {
3170         /* don't do this stuff if we are setting up connections
3171            from a set_state() call or creating new tracks. Ditto for deletion.
3172         */
3173
3174         if (_state_of_the_state & (InitialConnecting|Deletion)) {
3175                 return;
3176         }
3177
3178         /* every track/bus asked for this to be handled but it was deferred because
3179            we were connecting. do it now.
3180         */
3181
3182         request_input_change_handling ();
3183
3184         resort_routes ();
3185
3186         /* force all diskstreams to update their capture offset values to
3187            reflect any changes in latencies within the graph.
3188         */
3189
3190         boost::shared_ptr<RouteList> rl = routes.reader ();
3191         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
3192                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
3193                 if (tr) {
3194                         tr->set_capture_offset ();
3195                 }
3196         }
3197 }
3198
3199 framecnt_t
3200 Session::available_capture_duration ()
3201 {
3202         float sample_bytes_on_disk = 4.0; // keep gcc happy
3203
3204         switch (config.get_native_file_data_format()) {
3205         case FormatFloat:
3206                 sample_bytes_on_disk = 4.0;
3207                 break;
3208
3209         case FormatInt24:
3210                 sample_bytes_on_disk = 3.0;
3211                 break;
3212
3213         case FormatInt16:
3214                 sample_bytes_on_disk = 2.0;
3215                 break;
3216
3217         default:
3218                 /* impossible, but keep some gcc versions happy */
3219                 fatal << string_compose (_("programming error: %1"),
3220                                          X_("illegal native file data format"))
3221                       << endmsg;
3222                 /*NOTREACHED*/
3223         }
3224
3225         double scale = 4096.0 / sample_bytes_on_disk;
3226
3227         if (_total_free_4k_blocks * scale > (double) max_framecnt) {
3228                 return max_framecnt;
3229         }
3230         
3231         return (framecnt_t) floor (_total_free_4k_blocks * scale);
3232 }
3233
3234 void
3235 Session::add_bundle (boost::shared_ptr<Bundle> bundle)
3236 {
3237         {
3238                 RCUWriter<BundleList> writer (_bundles);
3239                 boost::shared_ptr<BundleList> b = writer.get_copy ();
3240                 b->push_back (bundle);
3241         }
3242
3243         BundleAdded (bundle); /* EMIT SIGNAL */
3244
3245         set_dirty();
3246 }
3247
3248 void
3249 Session::remove_bundle (boost::shared_ptr<Bundle> bundle)
3250 {
3251         bool removed = false;
3252
3253         {
3254                 RCUWriter<BundleList> writer (_bundles);
3255                 boost::shared_ptr<BundleList> b = writer.get_copy ();
3256                 BundleList::iterator i = find (b->begin(), b->end(), bundle);
3257
3258                 if (i != b->end()) {
3259                         b->erase (i);
3260                         removed = true;
3261                 }
3262         }
3263
3264         if (removed) {
3265                  BundleRemoved (bundle); /* EMIT SIGNAL */
3266         }
3267
3268         set_dirty();
3269 }
3270
3271 boost::shared_ptr<Bundle>
3272 Session::bundle_by_name (string name) const
3273 {
3274         boost::shared_ptr<BundleList> b = _bundles.reader ();
3275
3276         for (BundleList::const_iterator i = b->begin(); i != b->end(); ++i) {
3277                 if ((*i)->name() == name) {
3278                         return* i;
3279                 }
3280         }
3281
3282         return boost::shared_ptr<Bundle> ();
3283 }
3284
3285 void
3286 Session::tempo_map_changed (const PropertyChange&)
3287 {
3288         clear_clicks ();
3289
3290         playlists->update_after_tempo_map_change ();
3291
3292         _locations->apply (*this, &Session::update_locations_after_tempo_map_change);
3293         
3294         set_dirty ();
3295 }
3296
3297 void
3298 Session::update_locations_after_tempo_map_change (Locations::LocationList& loc)
3299 {
3300         for (Locations::LocationList::iterator i = loc.begin(); i != loc.end(); ++i) {
3301                 (*i)->recompute_frames_from_bbt ();
3302         }
3303 }
3304
3305 /** Ensures that all buffers (scratch, send, silent, etc) are allocated for
3306  * the given count with the current block size.
3307  */
3308 void
3309 Session::ensure_buffers (ChanCount howmany)
3310 {
3311         BufferManager::ensure_buffers (howmany);
3312 }
3313
3314 void
3315 Session::ensure_buffer_set(BufferSet& buffers, const ChanCount& count)
3316 {
3317         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
3318                 buffers.ensure_buffers(*t, count.get(*t), _engine.raw_buffer_size(*t));
3319         }
3320 }
3321
3322 uint32_t
3323 Session::next_insert_id ()
3324 {
3325         /* this doesn't really loop forever. just think about it */
3326
3327         while (true) {
3328                 for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < insert_bitset.size(); ++n) {
3329                         if (!insert_bitset[n]) {
3330                                 insert_bitset[n] = true;
3331                                 return n;
3332
3333                         }
3334                 }
3335
3336                 /* none available, so resize and try again */
3337
3338                 insert_bitset.resize (insert_bitset.size() + 16, false);
3339         }
3340 }
3341
3342 uint32_t
3343 Session::next_send_id ()
3344 {
3345         /* this doesn't really loop forever. just think about it */
3346
3347         while (true) {
3348                 for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < send_bitset.size(); ++n) {
3349                         if (!send_bitset[n]) {
3350                                 send_bitset[n] = true;
3351                                 return n;
3352
3353                         }
3354                 }
3355
3356                 /* none available, so resize and try again */
3357
3358                 send_bitset.resize (send_bitset.size() + 16, false);
3359         }
3360 }
3361
3362 uint32_t
3363 Session::next_return_id ()
3364 {
3365         /* this doesn't really loop forever. just think about it */
3366
3367         while (true) {
3368                 for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < return_bitset.size(); ++n) {
3369                         if (!return_bitset[n]) {
3370                                 return_bitset[n] = true;
3371                                 return n;
3372
3373                         }
3374                 }
3375
3376                 /* none available, so resize and try again */
3377
3378                 return_bitset.resize (return_bitset.size() + 16, false);
3379         }
3380 }
3381
3382 void
3383 Session::mark_send_id (uint32_t id)
3384 {
3385         if (id >= send_bitset.size()) {
3386                 send_bitset.resize (id+16, false);
3387         }
3388         if (send_bitset[id]) {
3389                 warning << string_compose (_("send ID %1 appears to be in use already"), id) << endmsg;
3390         }
3391         send_bitset[id] = true;
3392 }
3393
3394 void
3395 Session::mark_return_id (uint32_t id)
3396 {
3397         if (id >= return_bitset.size()) {
3398                 return_bitset.resize (id+16, false);
3399         }
3400         if (return_bitset[id]) {
3401                 warning << string_compose (_("return ID %1 appears to be in use already"), id) << endmsg;
3402         }
3403         return_bitset[id] = true;
3404 }
3405
3406 void
3407 Session::mark_insert_id (uint32_t id)
3408 {
3409         if (id >= insert_bitset.size()) {
3410                 insert_bitset.resize (id+16, false);
3411         }
3412         if (insert_bitset[id]) {
3413                 warning << string_compose (_("insert ID %1 appears to be in use already"), id) << endmsg;
3414         }
3415         insert_bitset[id] = true;
3416 }
3417
3418 void
3419 Session::unmark_send_id (uint32_t id)
3420 {
3421         if (id < send_bitset.size()) {
3422                 send_bitset[id] = false;
3423         }
3424 }
3425
3426 void
3427 Session::unmark_return_id (uint32_t id)
3428 {
3429         if (id < return_bitset.size()) {
3430                 return_bitset[id] = false;
3431         }
3432 }
3433
3434 void
3435 Session::unmark_insert_id (uint32_t id)
3436 {
3437         if (id < insert_bitset.size()) {
3438                 insert_bitset[id] = false;
3439         }
3440 }
3441
3442
3443 /* Named Selection management */
3444
3445 boost::shared_ptr<NamedSelection>
3446 Session::named_selection_by_name (string name)
3447 {
3448         Glib::Mutex::Lock lm (named_selection_lock);
3449         for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
3450                 if ((*i)->name == name) {
3451                         return *i;
3452                 }
3453         }
3454         return boost::shared_ptr<NamedSelection>();
3455 }
3456
3457 void
3458 Session::add_named_selection (boost::shared_ptr<NamedSelection> named_selection)
3459 {
3460         {
3461                 Glib::Mutex::Lock lm (named_selection_lock);
3462                 named_selections.insert (named_selections.begin(), named_selection);
3463         }
3464
3465         set_dirty();
3466
3467         NamedSelectionAdded (); /* EMIT SIGNAL */
3468 }
3469
3470 void
3471 Session::remove_named_selection (boost::shared_ptr<NamedSelection> named_selection)
3472 {
3473         bool removed = false;
3474
3475         {
3476                 Glib::Mutex::Lock lm (named_selection_lock);
3477
3478                 NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
3479
3480                 if (i != named_selections.end()) {
3481                         named_selections.erase (i);
3482                         set_dirty();
3483                         removed = true;
3484                 }
3485         }
3486
3487         if (removed) {
3488                  NamedSelectionRemoved (); /* EMIT SIGNAL */
3489         }
3490 }
3491
3492 void
3493 Session::reset_native_file_format ()
3494 {
3495         boost::shared_ptr<RouteList> rl = routes.reader ();
3496         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
3497                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
3498                 if (tr) {
3499                         /* don't save state as we do this, there's no point
3500                          */
3501
3502                         _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
3503                         tr->reset_write_sources (false);
3504                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
3505                 }
3506         }
3507 }
3508
3509 bool
3510 Session::route_name_unique (string n) const
3511 {
3512         boost::shared_ptr<RouteList> r = routes.reader ();
3513
3514         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3515                 if ((*i)->name() == n) {
3516                         return false;
3517                 }
3518         }
3519
3520         return true;
3521 }
3522
3523 bool
3524 Session::route_name_internal (string n) const
3525 {
3526         if (auditioner && auditioner->name() == n) {
3527                 return true;
3528         }
3529
3530         if (_click_io && _click_io->name() == n) {
3531                 return true;
3532         }
3533
3534         return false;
3535 }
3536
3537 int
3538 Session::freeze_all (InterThreadInfo& itt)
3539 {
3540         boost::shared_ptr<RouteList> r = routes.reader ();
3541
3542         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3543
3544                 boost::shared_ptr<Track> t;
3545
3546                 if ((t = boost::dynamic_pointer_cast<Track>(*i)) != 0) {
3547                         /* XXX this is wrong because itt.progress will keep returning to zero at the start
3548                            of every track.
3549                         */
3550                         t->freeze_me (itt);
3551                 }
3552         }
3553
3554         return 0;
3555 }
3556
3557 boost::shared_ptr<Region>
3558 Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
3559                           bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs,
3560                           InterThreadInfo& itt, bool enable_processing)
3561 {
3562         boost::shared_ptr<Region> result;
3563         boost::shared_ptr<Playlist> playlist;
3564         boost::shared_ptr<AudioFileSource> fsource;
3565         uint32_t x;
3566         char buf[PATH_MAX+1];
3567         ChanCount diskstream_channels (track.n_channels());
3568         framepos_t position;
3569         framecnt_t this_chunk;
3570         framepos_t to_do;
3571         BufferSet buffers;
3572         SessionDirectory sdir(get_best_session_directory_for_new_source ());
3573         const string sound_dir = sdir.sound_path().to_string();
3574         framepos_t len = end - start;
3575         bool need_block_size_reset = false;
3576         string ext;
3577         ChanCount const max_proc = track.max_processor_streams ();
3578
3579         if (end <= start) {
3580                 error << string_compose (_("Cannot write a range where end <= start (e.g. %1 <= %2)"),
3581                                          end, start) << endmsg;
3582                 return result;
3583         }
3584
3585         const framecnt_t chunk_size = (256 * 1024)/4;
3586
3587         // block all process callback handling
3588
3589         block_processing ();
3590
3591         /* call tree *MUST* hold route_lock */
3592
3593         if ((playlist = track.playlist()) == 0) {
3594                 goto out;
3595         }
3596
3597         /* external redirects will be a problem */
3598
3599         if (track.has_external_redirects()) {
3600                 goto out;
3601         }
3602
3603         ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
3604
3605         for (uint32_t chan_n = 0; chan_n < diskstream_channels.n_audio(); ++chan_n) {
3606
3607                 for (x = 0; x < 99999; ++x) {
3608                         snprintf (buf, sizeof(buf), "%s/%s-%d-bounce-%" PRIu32 "%s", sound_dir.c_str(), playlist->name().c_str(), chan_n, x+1, ext.c_str());
3609                         if (access (buf, F_OK) != 0) {
3610                                 break;
3611                         }
3612                 }
3613
3614                 if (x == 99999) {
3615                         error << string_compose (_("too many bounced versions of playlist \"%1\""), playlist->name()) << endmsg;
3616                         goto out;
3617                 }
3618
3619                 try {
3620                         fsource = boost::dynamic_pointer_cast<AudioFileSource> (
3621                                 SourceFactory::createWritable (DataType::AUDIO, *this, buf, string(), false, frame_rate()));
3622                 }
3623
3624                 catch (failed_constructor& err) {
3625                         error << string_compose (_("cannot create new audio file \"%1\" for %2"), buf, track.name()) << endmsg;
3626                         goto out;
3627                 }
3628
3629                 srcs.push_back (fsource);
3630         }
3631
3632         /* tell redirects that care that we are about to use a much larger blocksize */
3633         
3634         need_block_size_reset = true;
3635         track.set_block_size (chunk_size);
3636
3637         /* XXX need to flush all redirects */
3638
3639         position = start;
3640         to_do = len;
3641
3642         /* create a set of reasonably-sized buffers */
3643         buffers.ensure_buffers (DataType::AUDIO, max_proc.n_audio(), chunk_size);
3644         buffers.set_count (max_proc);
3645
3646         for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
3647                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3648                 if (afs)
3649                         afs->prepare_for_peakfile_writes ();
3650         }
3651
3652         while (to_do && !itt.cancel) {
3653
3654                 this_chunk = min (to_do, chunk_size);
3655
3656                 if (track.export_stuff (buffers, start, this_chunk, enable_processing)) {
3657                         goto out;
3658                 }
3659
3660                 uint32_t n = 0;
3661                 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
3662                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3663
3664                         if (afs) {
3665                                 if (afs->write (buffers.get_audio(n).data(), this_chunk) != this_chunk) {
3666                                         goto out;
3667                                 }
3668                         }
3669                 }
3670
3671                 start += this_chunk;
3672                 to_do -= this_chunk;
3673
3674                 itt.progress = (float) (1.0 - ((double) to_do / len));
3675
3676         }
3677
3678         if (!itt.cancel) {
3679
3680                 time_t now;
3681                 struct tm* xnow;
3682                 time (&now);
3683                 xnow = localtime (&now);
3684
3685                 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3686                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3687
3688                         if (afs) {
3689                                 afs->update_header (position, *xnow, now);
3690                                 afs->flush_header ();
3691                         }
3692                 }
3693
3694                 /* construct a region to represent the bounced material */
3695
3696                 PropertyList plist;
3697                 
3698                 plist.add (Properties::start, 0);
3699                 plist.add (Properties::length, srcs.front()->length(srcs.front()->timeline_position()));
3700                 plist.add (Properties::name, region_name_from_path (srcs.front()->name(), true));
3701                 
3702                 result = RegionFactory::create (srcs, plist);
3703                            
3704         }
3705
3706   out:
3707         if (!result) {
3708                 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
3709                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3710
3711                         if (afs) {
3712                                 afs->mark_for_remove ();
3713                         }
3714
3715                         (*src)->drop_references ();
3716                 }
3717
3718         } else {
3719                 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
3720                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3721
3722                         if (afs)
3723                                 afs->done_with_peakfile_writes ();
3724                 }
3725         }
3726
3727
3728         if (need_block_size_reset) {
3729                 track.set_block_size (get_block_size());
3730         }
3731         
3732         unblock_processing ();
3733
3734         return result;
3735 }
3736
3737 gain_t*
3738 Session::gain_automation_buffer() const
3739 {
3740         return ProcessThread::gain_automation_buffer ();
3741 }
3742
3743 pan_t**
3744 Session::pan_automation_buffer() const
3745 {
3746         return ProcessThread::pan_automation_buffer ();
3747 }
3748
3749 BufferSet&
3750 Session::get_silent_buffers (ChanCount count)
3751 {
3752         return ProcessThread::get_silent_buffers (count);
3753 }
3754
3755 BufferSet&
3756 Session::get_scratch_buffers (ChanCount count)
3757 {
3758         return ProcessThread::get_scratch_buffers (count);
3759 }
3760
3761 BufferSet&
3762 Session::get_mix_buffers (ChanCount count)
3763 {
3764         return ProcessThread::get_mix_buffers (count);
3765 }
3766
3767 uint32_t
3768 Session::ntracks () const
3769 {
3770         uint32_t n = 0;
3771         boost::shared_ptr<RouteList> r = routes.reader ();
3772
3773         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3774                 if (boost::dynamic_pointer_cast<Track> (*i)) {
3775                         ++n;
3776                 }
3777         }
3778
3779         return n;
3780 }
3781
3782 uint32_t
3783 Session::nbusses () const
3784 {
3785         uint32_t n = 0;
3786         boost::shared_ptr<RouteList> r = routes.reader ();
3787
3788         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3789                 if (boost::dynamic_pointer_cast<Track>(*i) == 0) {
3790                         ++n;
3791                 }
3792         }
3793
3794         return n;
3795 }
3796
3797 void
3798 Session::add_automation_list(AutomationList *al)
3799 {
3800         automation_lists[al->id()] = al;
3801 }
3802
3803 void
3804 Session::sync_order_keys (std::string const & base)
3805 {
3806         if (deletion_in_progress()) {
3807                 return;
3808         }
3809
3810         if (!Config->get_sync_all_route_ordering()) {
3811                 /* leave order keys as they are */
3812                 return;
3813         }
3814
3815         boost::shared_ptr<RouteList> r = routes.reader ();
3816
3817         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3818                 (*i)->sync_order_keys (base);
3819         }
3820
3821         Route::SyncOrderKeys (base); // EMIT SIGNAL
3822
3823         /* this might not do anything */
3824
3825         set_remote_control_ids ();
3826 }
3827
3828 /** @return true if there is at least one record-enabled track, otherwise false */
3829 bool
3830 Session::have_rec_enabled_track () const
3831 {
3832         return g_atomic_int_get (&_have_rec_enabled_track) == 1;
3833 }
3834
3835 /** Update the state of our rec-enabled tracks flag */
3836 void
3837 Session::update_have_rec_enabled_track ()
3838 {
3839         boost::shared_ptr<RouteList> rl = routes.reader ();
3840         RouteList::iterator i = rl->begin();
3841         while (i != rl->end ()) {
3842
3843                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
3844                 if (tr && tr->record_enabled ()) {
3845                         break;
3846                 }
3847                 
3848                 ++i;
3849         }
3850
3851         int const old = g_atomic_int_get (&_have_rec_enabled_track);
3852
3853         g_atomic_int_set (&_have_rec_enabled_track, i != rl->end () ? 1 : 0);
3854
3855         if (g_atomic_int_get (&_have_rec_enabled_track) != old) {
3856                 RecordStateChanged (); /* EMIT SIGNAL */
3857         }
3858 }
3859
3860 void
3861 Session::listen_position_changed ()
3862 {
3863         boost::shared_ptr<RouteList> r = routes.reader ();
3864
3865         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3866                 (*i)->listen_position_changed ();
3867         }
3868 }
3869
3870 void
3871 Session::solo_control_mode_changed ()
3872 {
3873         /* cancel all solo or all listen when solo control mode changes */
3874
3875         if (soloing()) {
3876                 set_solo (get_routes(), false);
3877         } else if (listening()) {
3878                 set_listen (get_routes(), false);
3879         }
3880 }
3881
3882 /** Called when anything about any of our route groups changes (membership, state etc.) */
3883 void
3884 Session::route_group_changed ()
3885 {
3886         RouteGroupChanged (); /* EMIT SIGNAL */
3887 }
3888
3889 vector<SyncSource>
3890 Session::get_available_sync_options () const
3891 {
3892         vector<SyncSource> ret;
3893         
3894         ret.push_back (JACK);
3895         ret.push_back (MTC);
3896         ret.push_back (MIDIClock);
3897
3898         return ret;
3899 }
3900
3901 boost::shared_ptr<RouteList>
3902 Session::get_routes_with_regions_at (framepos_t const p) const
3903 {
3904         boost::shared_ptr<RouteList> r = routes.reader ();
3905         boost::shared_ptr<RouteList> rl (new RouteList);
3906
3907         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3908                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
3909                 if (!tr) {
3910                         continue;
3911                 }
3912                 
3913                 boost::shared_ptr<Playlist> pl = tr->playlist ();
3914                 if (!pl) {
3915                         continue;
3916                 }
3917                 
3918                 if (pl->has_region_at (p)) {
3919                         rl->push_back (*i);
3920                 }
3921         }
3922
3923         return rl;
3924 }
3925
3926 void
3927 Session::goto_end ()
3928 {
3929         if (_session_range_location) {
3930                 request_locate (_session_range_location->end(), false);
3931         } else {
3932                 request_locate (0, false);
3933         }
3934 }
3935
3936 void
3937 Session::goto_start ()
3938 {
3939         if (_session_range_location) {
3940                 request_locate (_session_range_location->start(), false);
3941         } else {
3942                 request_locate (0, false);
3943         }
3944 }
3945
3946 framepos_t
3947 Session::current_start_frame () const
3948 {
3949         return _session_range_location ? _session_range_location->start() : 0;
3950 }
3951
3952 framepos_t
3953 Session::current_end_frame () const
3954 {
3955         return _session_range_location ? _session_range_location->end() : 0;
3956 }
3957
3958 void
3959 Session::add_session_range_location (framepos_t start, framepos_t end)
3960 {
3961         _session_range_location = new Location (*this, start, end, _("session"), Location::IsSessionRange);
3962         _locations->add (_session_range_location);
3963 }
3964
3965 /** Called when one of our routes' order keys has changed */
3966 void
3967 Session::route_order_key_changed ()
3968 {
3969         RouteOrderKeyChanged (); /* EMIT SIGNAL */
3970 }
3971
3972 void
3973 Session::step_edit_status_change (bool yn)
3974 {
3975         bool send = false;
3976
3977         bool val = false;
3978         if (yn) {
3979                 send = (_step_editors == 0);
3980                 val = true;
3981
3982                 _step_editors++;
3983         } else {
3984                 send = (_step_editors == 1);
3985                 val = false;
3986
3987                 if (_step_editors > 0) {
3988                         _step_editors--;
3989                 }
3990         }
3991
3992         if (send) {
3993                 StepEditStatusChange (val);
3994         }
3995 }
3996
3997         
3998 void
3999 Session::start_time_changed (framepos_t old)
4000 {
4001         /* Update the auto loop range to match the session range
4002            (unless the auto loop range has been changed by the user)
4003         */
4004
4005         Location* s = _locations->session_range_location ();
4006         if (s == 0) {
4007                 return;
4008         }
4009         
4010         Location* l = _locations->auto_loop_location ();
4011
4012         if (l->start() == old) {
4013                 l->set_start (s->start(), true);
4014         }
4015 }
4016
4017 void
4018 Session::end_time_changed (framepos_t old)
4019 {
4020         /* Update the auto loop range to match the session range
4021            (unless the auto loop range has been changed by the user)
4022         */
4023
4024         Location* s = _locations->session_range_location ();
4025         if (s == 0) {
4026                 return;
4027         }
4028         
4029         Location* l = _locations->auto_loop_location ();
4030
4031         if (l->end() == old) {
4032                 l->set_end (s->end(), true);
4033         }
4034 }
4035
4036 string
4037 Session::source_search_path (DataType type) const
4038 {
4039         string search_path;
4040
4041         if (session_dirs.size() == 1) {
4042                 switch (type) {
4043                 case DataType::AUDIO:
4044                         search_path = _session_dir->sound_path().to_string();
4045                         break;
4046                 case DataType::MIDI:
4047                         search_path = _session_dir->midi_path().to_string();
4048                         break;
4049                 }
4050         } else {
4051                 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4052                         SessionDirectory sdir (i->path);
4053                         if (!search_path.empty()) {
4054                                 search_path += ':';
4055                         } 
4056                         switch (type) {
4057                         case DataType::AUDIO:
4058                                 search_path += sdir.sound_path().to_string();
4059                                 break;
4060                         case DataType::MIDI:
4061                                 search_path += sdir.midi_path().to_string();
4062                                 break;
4063                         }
4064                 }
4065         }
4066                 
4067         /* now add user-specified locations
4068          */
4069
4070         vector<string> dirs;
4071
4072         switch (type) {
4073         case DataType::AUDIO:
4074                 split (config.get_audio_search_path (), dirs, ':');
4075                 break;
4076         case DataType::MIDI:
4077                 split (config.get_midi_search_path (), dirs, ':');
4078                 break;
4079         }
4080
4081         for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
4082                 search_path += ':';
4083                 search_path += *i;
4084                         
4085         }
4086         
4087         return search_path;
4088 }
4089
4090 void
4091 Session::ensure_search_path_includes (const string& path, DataType type)
4092 {
4093         string search_path;
4094         vector<string> dirs;
4095
4096         if (path == ".") {
4097                 return;
4098         }
4099
4100         switch (type) {
4101         case DataType::AUDIO:
4102                 search_path = config.get_audio_search_path ();
4103                 break;
4104         case DataType::MIDI:
4105                 search_path = config.get_midi_search_path ();
4106                 break;
4107         }
4108
4109         split (search_path, dirs, ':');
4110
4111         for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
4112                 if (*i == path) {
4113                         return;
4114                 }
4115         }
4116
4117         if (!search_path.empty()) {
4118                 search_path += ':';
4119         }
4120
4121         search_path += path;
4122         
4123         switch (type) {
4124         case DataType::AUDIO:
4125                 config.set_audio_search_path (search_path);
4126                 break;
4127         case DataType::MIDI:
4128                 config.set_midi_search_path (search_path);
4129                 break;
4130         }
4131 }
4132
4133 boost::shared_ptr<Speakers>
4134 Session::get_speakers() 
4135 {
4136         return _speakers;
4137 }
4138
4139 list<string>
4140 Session::unknown_processors () const
4141 {
4142         list<string> p;
4143
4144         boost::shared_ptr<RouteList> r = routes.reader ();
4145         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4146                 list<string> t = (*i)->unknown_processors ();
4147                 copy (t.begin(), t.end(), back_inserter (p));
4148         }
4149
4150         p.sort ();
4151         p.unique ();
4152
4153         return p;
4154 }
4155
4156 #ifdef HAVE_JACK_NEW_LATENCY
4157 void
4158 Session::update_latency (bool playback)
4159 {
4160         DEBUG_TRACE (DEBUG::Latency, "JACK latency callback\n");
4161
4162         boost::shared_ptr<RouteList> r = routes.reader ();
4163
4164         if (playback) {
4165                 /* reverse the list so that we work backwards from the last route to run to the first */
4166                 reverse (r->begin(), r->end());
4167         }
4168
4169         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4170                 DEBUG_TRACE (DEBUG::Latency, string_compose ("------------- Working on latency for %1\n", (*i)->name()));
4171                 (*i)->set_latency_ranges (playback);
4172                 DEBUG_TRACE (DEBUG::Latency, string_compose ("------------- Done working on latency for %1\n\n", (*i)->name()));
4173         }
4174 }
4175 #endif