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