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