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