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