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