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