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