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