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