2 Copyright (C) 1999-2004 Paul Davis
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.
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.
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.
26 #include <cstdio> /* sprintf(3) ... grrr */
32 #include <sigc++/bind.h>
33 #include <sigc++/retype.h>
35 #include <glibmm/thread.h>
36 #include <glibmm/miscutils.h>
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>
44 #include <ardour/audioengine.h>
45 #include <ardour/configuration.h>
46 #include <ardour/session.h>
47 #include <ardour/audio_diskstream.h>
48 #include <ardour/utils.h>
49 #include <ardour/audioplaylist.h>
50 #include <ardour/audioregion.h>
51 #include <ardour/audiofilesource.h>
52 #include <ardour/destructive_filesource.h>
53 #include <ardour/auditioner.h>
54 #include <ardour/recent_sessions.h>
55 #include <ardour/redirect.h>
56 #include <ardour/send.h>
57 #include <ardour/insert.h>
58 #include <ardour/connection.h>
59 #include <ardour/slave.h>
60 #include <ardour/tempo.h>
61 #include <ardour/audio_track.h>
62 #include <ardour/cycle_timer.h>
63 #include <ardour/named_selection.h>
64 #include <ardour/crossfade.h>
65 #include <ardour/playlist.h>
66 #include <ardour/click.h>
67 #include <ardour/data_type.h>
68 #include <ardour/source_factory.h>
71 #include <ardour/osc.h>
77 using namespace ARDOUR;
79 using boost::shared_ptr;
81 const char* Session::_template_suffix = X_(".template");
82 const char* Session::_statefile_suffix = X_(".ardour");
83 const char* Session::_pending_suffix = X_(".pending");
84 const char* Session::sound_dir_name = X_("sounds");
85 const char* Session::tape_dir_name = X_("tapes");
86 const char* Session::peak_dir_name = X_("peaks");
87 const char* Session::dead_sound_dir_name = X_("dead_sounds");
89 Session::compute_peak_t Session::compute_peak = 0;
90 Session::apply_gain_to_buffer_t Session::apply_gain_to_buffer = 0;
91 Session::mix_buffers_with_gain_t Session::mix_buffers_with_gain = 0;
92 Session::mix_buffers_no_gain_t Session::mix_buffers_no_gain = 0;
94 sigc::signal<int> Session::AskAboutPendingState;
95 sigc::signal<void> Session::SendFeedback;
97 sigc::signal<void> Session::SMPTEOffsetChanged;
98 sigc::signal<void> Session::SMPTETypeChanged;
99 sigc::signal<void> Session::PullupChanged;
100 sigc::signal<void> Session::StartTimeChanged;
101 sigc::signal<void> Session::EndTimeChanged;
104 Session::find_session (string str, string& path, string& snapshot, bool& isnew)
107 char buf[PATH_MAX+1];
111 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
112 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
118 /* check to see if it exists, and what it is */
120 if (stat (str.c_str(), &statbuf)) {
121 if (errno == ENOENT) {
124 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
132 /* it exists, so it must either be the name
133 of the directory, or the name of the statefile
137 if (S_ISDIR (statbuf.st_mode)) {
139 string::size_type slash = str.find_last_of ('/');
141 if (slash == string::npos) {
143 /* a subdirectory of cwd, so statefile should be ... */
149 tmp += _statefile_suffix;
153 if (stat (tmp.c_str(), &statbuf)) {
154 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
164 /* some directory someplace in the filesystem.
165 the snapshot name is the directory name
170 snapshot = str.substr (slash+1);
174 } else if (S_ISREG (statbuf.st_mode)) {
176 string::size_type slash = str.find_last_of ('/');
177 string::size_type suffix;
179 /* remove the suffix */
181 if (slash != string::npos) {
182 snapshot = str.substr (slash+1);
187 suffix = snapshot.find (_statefile_suffix);
189 if (suffix == string::npos) {
190 error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
196 snapshot = snapshot.substr (0, suffix);
198 if (slash == string::npos) {
200 /* we must be in the directory where the
201 statefile lives. get it using cwd().
204 char cwd[PATH_MAX+1];
206 if (getcwd (cwd, sizeof (cwd)) == 0) {
207 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
216 /* full path to the statefile */
218 path = str.substr (0, slash);
223 /* what type of file is it? */
224 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
230 /* its the name of a new directory. get the name
234 string::size_type slash = str.find_last_of ('/');
236 if (slash == string::npos) {
238 /* no slash, just use the name, but clean it up */
240 path = legalize_for_path (str);
246 snapshot = str.substr (slash+1);
253 Session::Session (AudioEngine &eng,
255 string snapshot_name,
256 string* mix_template)
259 _mmc_port (default_mmc_port),
260 _mtc_port (default_mtc_port),
261 _midi_port (default_midi_port),
262 pending_events (2048),
263 midi_requests (128), // the size of this should match the midi request pool size
264 diskstreams (new DiskstreamList),
265 routes (new RouteList),
266 auditioner ((Auditioner*) 0),
272 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << " (1)" << endl;
274 n_physical_outputs = _engine.n_physical_outputs();
275 n_physical_inputs = _engine.n_physical_inputs();
277 first_stage_init (fullpath, snapshot_name);
279 if (create (new_session, mix_template, _engine.frame_rate() * 60 * 5)) {
280 cerr << "create failed\n";
281 throw failed_constructor ();
284 if (second_stage_init (new_session)) {
285 cerr << "2nd state failed\n";
286 throw failed_constructor ();
289 store_recent_sessions(_name, _path);
291 bool was_dirty = dirty();
293 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
295 Config->ParameterChanged.connect (mem_fun (*this, &Session::handle_configuration_change));
298 DirtyChanged (); /* EMIT SIGNAL */
302 Session::Session (AudioEngine &eng,
304 string snapshot_name,
305 AutoConnectOption input_ac,
306 AutoConnectOption output_ac,
307 uint32_t control_out_channels,
308 uint32_t master_out_channels,
309 uint32_t requested_physical_in,
310 uint32_t requested_physical_out,
311 jack_nframes_t initial_length)
314 _mmc_port (default_mmc_port),
315 _mtc_port (default_mtc_port),
316 _midi_port (default_midi_port),
317 pending_events (2048),
319 diskstreams (new DiskstreamList),
320 routes (new RouteList),
326 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << " (2)" << endl;
328 n_physical_outputs = max (requested_physical_out, _engine.n_physical_outputs());
329 n_physical_inputs = max (requested_physical_in, _engine.n_physical_inputs());
331 first_stage_init (fullpath, snapshot_name);
333 if (create (new_session, 0, initial_length)) {
334 throw failed_constructor ();
337 if (control_out_channels) {
338 shared_ptr<Route> r (new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut));
345 if (master_out_channels) {
346 shared_ptr<Route> r (new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut));
352 /* prohibit auto-connect to master, because there isn't one */
353 output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
356 input_auto_connect = input_ac;
357 output_auto_connect = output_ac;
359 if (second_stage_init (new_session)) {
360 throw failed_constructor ();
363 store_recent_sessions(_name, _path);
365 bool was_dirty = dirty ();
367 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
370 DirtyChanged (); /* EMIT SIGNAL */
376 /* if we got to here, leaving pending capture state around
380 remove_pending_capture_state ();
382 _state_of_the_state = StateOfTheState (CannotSave|Deletion);
383 _engine.remove_session ();
385 GoingAway (); /* EMIT SIGNAL */
391 /* clear history so that no references to objects are held any more */
395 /* clear state tree so that no references to objects are held any more */
401 terminate_butler_thread ();
402 terminate_midi_thread ();
404 if (click_data && click_data != default_click) {
405 delete [] click_data;
408 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
409 delete [] click_emphasis_data;
414 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
418 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
422 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
426 AudioDiskstream::free_working_buffers();
428 #undef TRACK_DESTRUCTION
429 #ifdef TRACK_DESTRUCTION
430 cerr << "delete named selections\n";
431 #endif /* TRACK_DESTRUCTION */
432 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ) {
433 NamedSelectionList::iterator tmp;
442 #ifdef TRACK_DESTRUCTION
443 cerr << "delete playlists\n";
444 #endif /* TRACK_DESTRUCTION */
445 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ) {
446 PlaylistList::iterator tmp;
456 #ifdef TRACK_DESTRUCTION
457 cerr << "delete audio regions\n";
458 #endif /* TRACK_DESTRUCTION */
460 for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
461 i->second->drop_references ();
464 audio_regions.clear ();
466 #ifdef TRACK_DESTRUCTION
467 cerr << "delete routes\n";
468 #endif /* TRACK_DESTRUCTION */
470 RCUWriter<RouteList> writer (routes);
471 boost::shared_ptr<RouteList> r = writer.get_copy ();
472 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
473 (*i)->drop_references ();
476 /* writer goes out of scope and updates master */
481 #ifdef TRACK_DESTRUCTION
482 cerr << "delete diskstreams\n";
483 #endif /* TRACK_DESTRUCTION */
485 RCUWriter<DiskstreamList> dwriter (diskstreams);
486 boost::shared_ptr<DiskstreamList> dsl = dwriter.get_copy();
487 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
488 (*i)->drop_references ();
492 diskstreams.flush ();
494 #ifdef TRACK_DESTRUCTION
495 cerr << "delete audio sources\n";
496 #endif /* TRACK_DESTRUCTION */
497 for (AudioSourceList::iterator i = audio_sources.begin(); i != audio_sources.end(); ) {
498 AudioSourceList::iterator tmp;
503 i->second->drop_references ();
508 audio_sources.clear ();
510 #ifdef TRACK_DESTRUCTION
511 cerr << "delete mix groups\n";
512 #endif /* TRACK_DESTRUCTION */
513 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ) {
514 list<RouteGroup*>::iterator tmp;
524 #ifdef TRACK_DESTRUCTION
525 cerr << "delete edit groups\n";
526 #endif /* TRACK_DESTRUCTION */
527 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ) {
528 list<RouteGroup*>::iterator tmp;
538 #ifdef TRACK_DESTRUCTION
539 cerr << "delete connections\n";
540 #endif /* TRACK_DESTRUCTION */
541 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ) {
542 ConnectionList::iterator tmp;
552 if (butler_mixdown_buffer) {
553 delete [] butler_mixdown_buffer;
556 if (butler_gain_buffer) {
557 delete [] butler_gain_buffer;
560 Crossfade::set_buffer_size (0);
568 Session::set_worst_io_latencies ()
570 _worst_output_latency = 0;
571 _worst_input_latency = 0;
573 if (!_engine.connected()) {
577 boost::shared_ptr<RouteList> r = routes.reader ();
579 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
580 _worst_output_latency = max (_worst_output_latency, (*i)->output_latency());
581 _worst_input_latency = max (_worst_input_latency, (*i)->input_latency());
586 Session::when_engine_running ()
588 string first_physical_output;
590 /* we don't want to run execute this again */
592 first_time_running.disconnect ();
594 set_block_size (_engine.frames_per_cycle());
595 set_frame_rate (_engine.frame_rate());
597 /* every time we reconnect, recompute worst case output latencies */
599 _engine.Running.connect (mem_fun (*this, &Session::set_worst_io_latencies));
601 if (synced_to_jack()) {
602 _engine.transport_stop ();
605 if (Config->get_jack_time_master()) {
606 _engine.transport_locate (_transport_frame);
614 _click_io.reset (new ClickIO (*this, "click", 0, 0, -1, -1));
616 if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
618 /* existing state for Click */
620 if (_click_io->set_state (*child->children().front()) == 0) {
622 _clicking = click_requested;
626 error << _("could not setup Click I/O") << endmsg;
632 /* default state for Click */
634 first_physical_output = _engine.get_nth_physical_output (0);
636 if (first_physical_output.length()) {
637 if (_click_io->add_output_port (first_physical_output, this)) {
638 // relax, even though its an error
640 _clicking = click_requested;
646 catch (failed_constructor& err) {
647 error << _("cannot setup Click I/O") << endmsg;
650 set_worst_io_latencies ();
653 ControlChanged (Clicking); /* EMIT SIGNAL */
656 if (auditioner == 0) {
658 /* we delay creating the auditioner till now because
659 it makes its own connections to ports named
660 in the ARDOUR_RC config file. the engine has
661 to be running for this to work.
665 auditioner.reset (new Auditioner (*this));
668 catch (failed_constructor& err) {
669 warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
673 /* Create a set of Connection objects that map
674 to the physical outputs currently available
679 for (uint32_t np = 0; np < n_physical_outputs; ++np) {
681 snprintf (buf, sizeof (buf), _("out %" PRIu32), np+1);
683 Connection* c = new OutputConnection (buf, true);
686 c->add_connection (0, _engine.get_nth_physical_output (np));
691 for (uint32_t np = 0; np < n_physical_inputs; ++np) {
693 snprintf (buf, sizeof (buf), _("in %" PRIu32), np+1);
695 Connection* c = new InputConnection (buf, true);
698 c->add_connection (0, _engine.get_nth_physical_input (np));
705 for (uint32_t np = 0; np < n_physical_outputs; np +=2) {
707 snprintf (buf, sizeof (buf), _("out %" PRIu32 "+%" PRIu32), np+1, np+2);
709 Connection* c = new OutputConnection (buf, true);
713 c->add_connection (0, _engine.get_nth_physical_output (np));
714 c->add_connection (1, _engine.get_nth_physical_output (np+1));
719 for (uint32_t np = 0; np < n_physical_inputs; np +=2) {
721 snprintf (buf, sizeof (buf), _("in %" PRIu32 "+%" PRIu32), np+1, np+2);
723 Connection* c = new InputConnection (buf, true);
727 c->add_connection (0, _engine.get_nth_physical_input (np));
728 c->add_connection (1, _engine.get_nth_physical_input (np+1));
737 /* create master/control ports */
742 /* force the master to ignore any later call to this */
744 if (_master_out->pending_state_node) {
745 _master_out->ports_became_legal();
748 /* no panner resets till we are through */
750 _master_out->defer_pan_reset ();
752 while ((int) _master_out->n_inputs() < _master_out->input_maximum()) {
753 if (_master_out->add_input_port ("", this)) {
754 error << _("cannot setup master inputs")
760 while ((int) _master_out->n_outputs() < _master_out->output_maximum()) {
761 if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
762 error << _("cannot setup master outputs")
769 _master_out->allow_pan_reset ();
773 Connection* c = new OutputConnection (_("Master Out"), true);
775 for (uint32_t n = 0; n < _master_out->n_inputs (); ++n) {
777 c->add_connection ((int) n, _master_out->input(n)->name());
784 /* catch up on send+insert cnts */
788 for (list<PortInsert*>::iterator i = _port_inserts.begin(); i != _port_inserts.end(); ++i) {
791 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
792 if (id > insert_cnt) {
800 for (list<Send*>::iterator i = _sends.begin(); i != _sends.end(); ++i) {
803 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
810 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
812 /* hook us up to the engine */
814 _engine.set_session (this);
819 osc->set_session (*this);
822 _state_of_the_state = Clean;
824 DirtyChanged (); /* EMIT SIGNAL */
828 Session::hookup_io ()
830 /* stop graph reordering notifications from
831 causing resorts, etc.
834 _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
836 /* Tell all IO objects to create their ports */
843 while ((int) _control_out->n_inputs() < _control_out->input_maximum()) {
844 if (_control_out->add_input_port ("", this)) {
845 error << _("cannot setup control inputs")
851 while ((int) _control_out->n_outputs() < _control_out->output_maximum()) {
852 if (_control_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
853 error << _("cannot set up master outputs")
861 /* Tell all IO objects to connect themselves together */
863 IO::enable_connecting ();
865 /* Now reset all panners */
867 IO::reset_panners ();
869 /* Anyone who cares about input state, wake up and do something */
871 IOConnectionsComplete (); /* EMIT SIGNAL */
873 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
875 /* now handle the whole enchilada as if it was one
881 /* update mixer solo state */
887 Session::playlist_length_changed (Playlist* pl)
889 /* we can't just increase end_location->end() if pl->get_maximum_extent()
890 if larger. if the playlist used to be the longest playlist,
891 and its now shorter, we have to decrease end_location->end(). hence,
892 we have to iterate over all diskstreams and check the
893 playlists currently in use.
899 Session::diskstream_playlist_changed (boost::shared_ptr<Diskstream> dstream)
903 if ((playlist = dstream->playlist()) != 0) {
904 playlist->LengthChanged.connect (sigc::bind (mem_fun (this, &Session::playlist_length_changed), playlist));
907 /* see comment in playlist_length_changed () */
912 Session::record_enabling_legal () const
914 /* this used to be in here, but survey says.... we don't need to restrict it */
915 // if (record_status() == Recording) {
926 Session::set_auto_play (bool yn)
928 if (auto_play != yn) {
931 ControlChanged (AutoPlay);
936 Session::set_auto_return (bool yn)
938 if (auto_return != yn) {
941 ControlChanged (AutoReturn);
946 Session::set_crossfades_active (bool yn)
948 if (crossfades_active != yn) {
949 crossfades_active = yn;
951 ControlChanged (CrossFadesActive);
956 Session::set_do_not_record_plugins (bool yn)
958 if (do_not_record_plugins != yn) {
959 do_not_record_plugins = yn;
961 ControlChanged (RecordingPlugins);
966 Session::set_auto_input (bool yn)
968 if (auto_input != yn) {
971 if (Config->get_use_hardware_monitoring() && transport_rolling()) {
972 /* auto-input only makes a difference if we're rolling */
974 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
976 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
977 if ((*i)->record_enabled ()) {
978 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
979 (*i)->monitor_input (!auto_input);
985 ControlChanged (AutoInput);
990 Session::reset_input_monitor_state ()
992 if (transport_rolling()) {
994 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
996 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
997 if ((*i)->record_enabled ()) {
998 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
999 (*i)->monitor_input (Config->get_use_hardware_monitoring() && !auto_input);
1003 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1005 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1006 if ((*i)->record_enabled ()) {
1007 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
1008 (*i)->monitor_input (Config->get_use_hardware_monitoring());
1016 Session::set_input_auto_connect (bool yn)
1019 input_auto_connect = AutoConnectOption (input_auto_connect|AutoConnectPhysical);
1021 input_auto_connect = AutoConnectOption (input_auto_connect|~AutoConnectPhysical);
1027 Session::get_input_auto_connect () const
1029 return (input_auto_connect & AutoConnectPhysical);
1033 Session::set_output_auto_connect (AutoConnectOption aco)
1035 output_auto_connect = aco;
1040 Session::auto_punch_start_changed (Location* location)
1042 replace_event (Event::PunchIn, location->start());
1044 if (get_record_enabled() && get_punch_in()) {
1045 /* capture start has been changed, so save new pending state */
1046 save_state ("", true);
1051 Session::auto_punch_end_changed (Location* location)
1053 jack_nframes_t when_to_stop = location->end();
1054 // when_to_stop += _worst_output_latency + _worst_input_latency;
1055 replace_event (Event::PunchOut, when_to_stop);
1059 Session::auto_punch_changed (Location* location)
1061 jack_nframes_t when_to_stop = location->end();
1063 replace_event (Event::PunchIn, location->start());
1064 //when_to_stop += _worst_output_latency + _worst_input_latency;
1065 replace_event (Event::PunchOut, when_to_stop);
1069 Session::auto_loop_changed (Location* location)
1071 replace_event (Event::AutoLoop, location->end(), location->start());
1073 if (transport_rolling() && get_auto_loop()) {
1075 //if (_transport_frame < location->start() || _transport_frame > location->end()) {
1077 if (_transport_frame > location->end()) {
1078 // relocate to beginning of loop
1079 clear_events (Event::LocateRoll);
1081 request_locate (location->start(), true);
1084 else if (seamless_loop && !loop_changing) {
1086 // schedule a locate-roll to refill the diskstreams at the
1087 // previous loop end
1088 loop_changing = true;
1090 if (location->end() > last_loopend) {
1091 clear_events (Event::LocateRoll);
1092 Event *ev = new Event (Event::LocateRoll, Event::Add, last_loopend, last_loopend, 0, true);
1099 last_loopend = location->end();
1104 Session::set_auto_punch_location (Location* location)
1108 if ((existing = _locations.auto_punch_location()) != 0 && existing != location) {
1109 auto_punch_start_changed_connection.disconnect();
1110 auto_punch_end_changed_connection.disconnect();
1111 auto_punch_changed_connection.disconnect();
1112 existing->set_auto_punch (false, this);
1113 remove_event (existing->start(), Event::PunchIn);
1114 clear_events (Event::PunchOut);
1115 auto_punch_location_changed (0);
1120 if (location == 0) {
1124 if (location->end() <= location->start()) {
1125 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1129 auto_punch_start_changed_connection.disconnect();
1130 auto_punch_end_changed_connection.disconnect();
1131 auto_punch_changed_connection.disconnect();
1133 auto_punch_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_punch_start_changed));
1134 auto_punch_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_punch_end_changed));
1135 auto_punch_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_punch_changed));
1137 location->set_auto_punch (true, this);
1138 auto_punch_location_changed (location);
1142 Session::set_punch_in (bool yn)
1144 if (punch_in == yn) {
1150 if ((location = _locations.auto_punch_location()) != 0) {
1151 if ((punch_in = yn) == true) {
1152 replace_event (Event::PunchIn, location->start());
1154 remove_event (location->start(), Event::PunchIn);
1159 ControlChanged (PunchIn); /* EMIT SIGNAL */
1163 Session::set_punch_out (bool yn)
1165 if (punch_out == yn) {
1171 if ((location = _locations.auto_punch_location()) != 0) {
1172 if ((punch_out = yn) == true) {
1173 replace_event (Event::PunchOut, location->end());
1175 clear_events (Event::PunchOut);
1180 ControlChanged (PunchOut); /* EMIT SIGNAL */
1184 Session::set_auto_loop_location (Location* location)
1188 if ((existing = _locations.auto_loop_location()) != 0 && existing != location) {
1189 auto_loop_start_changed_connection.disconnect();
1190 auto_loop_end_changed_connection.disconnect();
1191 auto_loop_changed_connection.disconnect();
1192 existing->set_auto_loop (false, this);
1193 remove_event (existing->end(), Event::AutoLoop);
1194 auto_loop_location_changed (0);
1199 if (location == 0) {
1203 if (location->end() <= location->start()) {
1204 error << _("Session: you can't use a mark for auto loop") << endmsg;
1208 last_loopend = location->end();
1210 auto_loop_start_changed_connection.disconnect();
1211 auto_loop_end_changed_connection.disconnect();
1212 auto_loop_changed_connection.disconnect();
1214 auto_loop_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1215 auto_loop_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1216 auto_loop_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_loop_changed));
1218 location->set_auto_loop (true, this);
1219 auto_loop_location_changed (location);
1223 Session::locations_added (Location* ignored)
1229 Session::locations_changed ()
1231 _locations.apply (*this, &Session::handle_locations_changed);
1235 Session::handle_locations_changed (Locations::LocationList& locations)
1237 Locations::LocationList::iterator i;
1239 bool set_loop = false;
1240 bool set_punch = false;
1242 for (i = locations.begin(); i != locations.end(); ++i) {
1246 if (location->is_auto_punch()) {
1247 set_auto_punch_location (location);
1250 if (location->is_auto_loop()) {
1251 set_auto_loop_location (location);
1258 set_auto_loop_location (0);
1261 set_auto_punch_location (0);
1268 Session::enable_record ()
1270 /* XXX really atomic compare+swap here */
1271 if (g_atomic_int_get (&_record_status) != Recording) {
1272 g_atomic_int_set (&_record_status, Recording);
1273 _last_record_location = _transport_frame;
1274 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordStrobe);
1276 if (Config->get_use_hardware_monitoring() && auto_input) {
1277 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1278 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1279 if ((*i)->record_enabled ()) {
1280 (*i)->monitor_input (true);
1285 RecordStateChanged ();
1290 Session::disable_record (bool rt_context, bool force)
1294 if ((rs = (RecordState) g_atomic_int_get (&_record_status)) != Disabled) {
1296 if (!Config->get_latched_record_enable () || force) {
1297 g_atomic_int_set (&_record_status, Disabled);
1299 if (rs == Recording) {
1300 g_atomic_int_set (&_record_status, Enabled);
1304 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordExit);
1306 if (Config->get_use_hardware_monitoring() && auto_input) {
1307 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1309 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1310 if ((*i)->record_enabled ()) {
1311 (*i)->monitor_input (false);
1316 RecordStateChanged (); /* emit signal */
1319 remove_pending_capture_state ();
1325 Session::step_back_from_record ()
1327 g_atomic_int_set (&_record_status, Enabled);
1329 if (Config->get_use_hardware_monitoring()) {
1330 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1332 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1333 if (auto_input && (*i)->record_enabled ()) {
1334 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
1335 (*i)->monitor_input (false);
1342 Session::maybe_enable_record ()
1344 g_atomic_int_set (&_record_status, Enabled);
1346 /* XXX this save should really happen in another thread. its needed so that
1347 pending capture state can be recovered if we crash.
1350 save_state ("", true);
1352 if (_transport_speed) {
1357 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordPause);
1358 RecordStateChanged (); /* EMIT SIGNAL */
1365 Session::audible_frame () const
1368 jack_nframes_t offset;
1371 /* the first of these two possible settings for "offset"
1372 mean that the audible frame is stationary until
1373 audio emerges from the latency compensation
1376 the second means that the audible frame is stationary
1377 until audio would emerge from a physical port
1378 in the absence of any plugin latency compensation
1381 offset = _worst_output_latency;
1383 if (offset > current_block_size) {
1384 offset -= current_block_size;
1386 /* XXX is this correct? if we have no external
1387 physical connections and everything is internal
1388 then surely this is zero? still, how
1389 likely is that anyway?
1391 offset = current_block_size;
1394 if (synced_to_jack()) {
1395 tf = _engine.transport_frame();
1397 tf = _transport_frame;
1400 if (_transport_speed == 0) {
1410 if (!non_realtime_work_pending()) {
1414 /* take latency into account */
1423 Session::set_frame_rate (jack_nframes_t frames_per_second)
1425 /** \fn void Session::set_frame_size(jack_nframes_t)
1426 the AudioEngine object that calls this guarantees
1427 that it will not be called while we are also in
1428 ::process(). Its fine to do things that block
1432 _base_frame_rate = frames_per_second;
1436 Route::set_automation_interval ((jack_nframes_t) ceil ((double) frames_per_second * 0.25));
1438 // XXX we need some equivalent to this, somehow
1439 // DestructiveFileSource::setup_standard_crossfades (frames_per_second);
1443 /* XXX need to reset/reinstantiate all LADSPA plugins */
1447 Session::set_block_size (jack_nframes_t nframes)
1449 /* the AudioEngine guarantees
1450 that it will not be called while we are also in
1451 ::process(). It is therefore fine to do things that block
1456 vector<Sample*>::iterator i;
1459 current_block_size = nframes;
1461 for (np = 0, i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i, ++np) {
1465 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
1469 _passthru_buffers.clear ();
1470 _silent_buffers.clear ();
1472 ensure_passthru_buffers (np);
1474 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
1478 #ifdef NO_POSIX_MEMALIGN
1479 buf = (Sample *) malloc(current_block_size * sizeof(Sample));
1481 posix_memalign((void **)&buf,16,current_block_size * 4);
1485 memset (*i, 0, sizeof (Sample) * current_block_size);
1489 if (_gain_automation_buffer) {
1490 delete [] _gain_automation_buffer;
1492 _gain_automation_buffer = new gain_t[nframes];
1494 allocate_pan_automation_buffers (nframes, _npan_buffers, true);
1496 boost::shared_ptr<RouteList> r = routes.reader ();
1498 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1499 (*i)->set_block_size (nframes);
1502 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1503 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1504 (*i)->set_block_size (nframes);
1507 set_worst_io_latencies ();
1512 Session::set_default_fade (float steepness, float fade_msecs)
1515 jack_nframes_t fade_frames;
1517 /* Don't allow fade of less 1 frame */
1519 if (fade_msecs < (1000.0 * (1.0/_current_frame_rate))) {
1526 fade_frames = (jack_nframes_t) floor (fade_msecs * _current_frame_rate * 0.001);
1530 default_fade_msecs = fade_msecs;
1531 default_fade_steepness = steepness;
1534 // jlc, WTF is this!
1535 Glib::RWLock::ReaderLock lm (route_lock);
1536 AudioRegion::set_default_fade (steepness, fade_frames);
1541 /* XXX have to do this at some point */
1542 /* foreach region using default fade, reset, then
1543 refill_all_diskstream_buffers ();
1548 struct RouteSorter {
1549 bool operator() (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> r2) {
1550 if (r1->fed_by.find (r2) != r1->fed_by.end()) {
1552 } else if (r2->fed_by.find (r1) != r2->fed_by.end()) {
1555 if (r1->fed_by.empty()) {
1556 if (r2->fed_by.empty()) {
1557 /* no ardour-based connections inbound to either route. just use signal order */
1558 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1560 /* r2 has connections, r1 does not; run r1 early */
1564 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1571 trace_terminal (shared_ptr<Route> r1, shared_ptr<Route> rbase)
1573 shared_ptr<Route> r2;
1575 if ((r1->fed_by.find (rbase) != r1->fed_by.end()) && (rbase->fed_by.find (r1) != rbase->fed_by.end())) {
1576 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
1580 /* make a copy of the existing list of routes that feed r1 */
1582 set<shared_ptr<Route> > existing = r1->fed_by;
1584 /* for each route that feeds r1, recurse, marking it as feeding
1588 for (set<shared_ptr<Route> >::iterator i = existing.begin(); i != existing.end(); ++i) {
1591 /* r2 is a route that feeds r1 which somehow feeds base. mark
1592 base as being fed by r2
1595 rbase->fed_by.insert (r2);
1599 /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
1603 if ((r1->fed_by.find (r2) != r1->fed_by.end()) && (r2->fed_by.find (r1) != r2->fed_by.end())) {
1607 /* now recurse, so that we can mark base as being fed by
1608 all routes that feed r2
1611 trace_terminal (r2, rbase);
1618 Session::resort_routes ()
1620 /* don't do anything here with signals emitted
1621 by Routes while we are being destroyed.
1624 if (_state_of_the_state & Deletion) {
1631 RCUWriter<RouteList> writer (routes);
1632 shared_ptr<RouteList> r = writer.get_copy ();
1633 resort_routes_using (r);
1634 /* writer goes out of scope and forces update */
1639 Session::resort_routes_using (shared_ptr<RouteList> r)
1641 RouteList::iterator i, j;
1643 for (i = r->begin(); i != r->end(); ++i) {
1645 (*i)->fed_by.clear ();
1647 for (j = r->begin(); j != r->end(); ++j) {
1649 /* although routes can feed themselves, it will
1650 cause an endless recursive descent if we
1651 detect it. so don't bother checking for
1659 if ((*j)->feeds (*i)) {
1660 (*i)->fed_by.insert (*j);
1665 for (i = r->begin(); i != r->end(); ++i) {
1666 trace_terminal (*i, *i);
1673 cerr << "finished route resort\n";
1675 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1676 cerr << " " << (*i)->name() << " signal order = " << (*i)->order_key ("signal") << endl;
1683 list<boost::shared_ptr<AudioTrack> >
1684 Session::new_audio_track (int input_channels, int output_channels, TrackMode mode, uint32_t how_many)
1686 char track_name[32];
1687 uint32_t track_id = 0;
1689 uint32_t channels_used = 0;
1691 RouteList new_routes;
1692 list<boost::shared_ptr<AudioTrack> > ret;
1694 /* count existing audio tracks */
1697 shared_ptr<RouteList> r = routes.reader ();
1699 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1700 if (dynamic_cast<AudioTrack*>((*i).get()) != 0) {
1701 if (!(*i)->hidden()) {
1703 channels_used += (*i)->n_inputs();
1709 vector<string> physinputs;
1710 vector<string> physoutputs;
1711 uint32_t nphysical_in;
1712 uint32_t nphysical_out;
1714 _engine.get_physical_outputs (physoutputs);
1715 _engine.get_physical_inputs (physinputs);
1719 /* check for duplicate route names, since we might have pre-existing
1720 routes with this name (e.g. create Audio1, Audio2, delete Audio1,
1721 save, close,restart,add new route - first named route is now
1729 snprintf (track_name, sizeof(track_name), "Audio %" PRIu32, track_id);
1731 if (route_by_name (track_name) == 0) {
1735 } while (track_id < (UINT_MAX-1));
1737 if (input_auto_connect & AutoConnectPhysical) {
1738 nphysical_in = min (n_physical_inputs, (uint32_t) physinputs.size());
1743 if (output_auto_connect & AutoConnectPhysical) {
1744 nphysical_out = min (n_physical_outputs, (uint32_t) physinputs.size());
1750 shared_ptr<AudioTrack> track (new AudioTrack (*this, track_name, Route::Flag (0), mode));
1752 if (track->ensure_io (input_channels, output_channels, false, this)) {
1753 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1754 input_channels, output_channels)
1759 for (uint32_t x = 0; x < track->n_inputs() && x < nphysical_in; ++x) {
1763 if (input_auto_connect & AutoConnectPhysical) {
1764 port = physinputs[(channels_used+x)%nphysical_in];
1767 if (port.length() && track->connect_input (track->input (x), port, this)) {
1773 for (uint32_t x = 0; x < track->n_outputs(); ++x) {
1777 if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
1778 port = physoutputs[(channels_used+x)%nphysical_out];
1779 } else if (output_auto_connect & AutoConnectMaster) {
1781 port = _master_out->input (x%_master_out->n_inputs())->name();
1785 if (port.length() && track->connect_output (track->output (x), port, this)) {
1790 channels_used += track->n_inputs ();
1793 vector<string> cports;
1794 uint32_t ni = _control_out->n_inputs();
1796 for (n = 0; n < ni; ++n) {
1797 cports.push_back (_control_out->input(n)->name());
1800 track->set_control_outs (cports);
1803 track->DiskstreamChanged.connect (mem_fun (this, &Session::resort_routes));
1804 track->set_remote_control_id (ntracks());
1806 new_routes.push_back (track);
1807 ret.push_back (track);
1810 catch (failed_constructor &err) {
1811 error << _("Session: could not create new audio track.") << endmsg;
1812 // XXX should we delete the tracks already created?
1820 if (!new_routes.empty()) {
1821 add_routes (new_routes, false);
1822 save_state (_current_snapshot_name);
1829 Session::new_audio_route (int input_channels, int output_channels, uint32_t how_many)
1832 uint32_t bus_id = 1;
1837 /* count existing audio busses */
1840 shared_ptr<RouteList> r = routes.reader ();
1842 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1843 if (dynamic_cast<AudioTrack*>((*i).get()) == 0) {
1844 if (!(*i)->hidden()) {
1851 vector<string> physinputs;
1852 vector<string> physoutputs;
1854 _engine.get_physical_outputs (physoutputs);
1855 _engine.get_physical_inputs (physinputs);
1862 snprintf (bus_name, sizeof(bus_name), "Bus %" PRIu32, bus_id);
1864 if (route_by_name (bus_name) == 0) {
1868 } while (bus_id < (UINT_MAX-1));
1871 shared_ptr<Route> bus (new Route (*this, bus_name, -1, -1, -1, -1, Route::Flag(0), DataType::AUDIO));
1873 if (bus->ensure_io (input_channels, output_channels, false, this)) {
1874 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1875 input_channels, output_channels)
1879 for (uint32_t x = 0; x < bus->n_inputs(); ++x) {
1883 if (input_auto_connect & AutoConnectPhysical) {
1884 port = physinputs[((n+x)%n_physical_inputs)];
1887 if (port.length() && bus->connect_input (bus->input (x), port, this)) {
1892 for (uint32_t x = 0; x < bus->n_outputs(); ++x) {
1896 if (output_auto_connect & AutoConnectPhysical) {
1897 port = physoutputs[((n+x)%n_physical_outputs)];
1898 } else if (output_auto_connect & AutoConnectMaster) {
1900 port = _master_out->input (x%_master_out->n_inputs())->name();
1904 if (port.length() && bus->connect_output (bus->output (x), port, this)) {
1910 vector<string> cports;
1911 uint32_t ni = _control_out->n_inputs();
1913 for (uint32_t n = 0; n < ni; ++n) {
1914 cports.push_back (_control_out->input(n)->name());
1916 bus->set_control_outs (cports);
1919 ret.push_back (bus);
1923 catch (failed_constructor &err) {
1924 error << _("Session: could not create new audio route.") << endmsg;
1933 add_routes (ret, false);
1934 save_state (_current_snapshot_name);
1942 Session::add_routes (RouteList& new_routes, bool save)
1945 RCUWriter<RouteList> writer (routes);
1946 shared_ptr<RouteList> r = writer.get_copy ();
1947 r->insert (r->end(), new_routes.begin(), new_routes.end());
1948 resort_routes_using (r);
1951 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
1952 (*x)->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), (*x)));
1953 (*x)->mute_changed.connect (mem_fun (*this, &Session::route_mute_changed));
1954 (*x)->output_changed.connect (mem_fun (*this, &Session::set_worst_io_latencies_x));
1955 (*x)->redirects_changed.connect (mem_fun (*this, &Session::update_latency_compensation_proxy));
1957 if ((*x)->master()) {
1961 if ((*x)->control()) {
1962 _control_out = (*x);
1969 save_state (_current_snapshot_name);
1972 RouteAdded (new_routes); /* EMIT SIGNAL */
1976 Session::add_diskstream (boost::shared_ptr<Diskstream> dstream)
1978 /* need to do this in case we're rolling at the time, to prevent false underruns */
1979 dstream->do_refill_with_alloc();
1982 RCUWriter<DiskstreamList> writer (diskstreams);
1983 boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
1984 ds->push_back (dstream);
1987 dstream->set_block_size (current_block_size);
1989 dstream->PlaylistChanged.connect (sigc::bind (mem_fun (*this, &Session::diskstream_playlist_changed), dstream));
1990 /* this will connect to future changes, and check the current length */
1991 diskstream_playlist_changed (dstream);
1993 dstream->prepare ();
1997 Session::remove_route (shared_ptr<Route> route)
2000 RCUWriter<RouteList> writer (routes);
2001 shared_ptr<RouteList> rs = writer.get_copy ();
2004 /* deleting the master out seems like a dumb
2005 idea, but its more of a UI policy issue
2009 if (route == _master_out) {
2010 _master_out = shared_ptr<Route> ((Route*) 0);
2013 if (route == _control_out) {
2014 _control_out = shared_ptr<Route> ((Route*) 0);
2016 /* cancel control outs for all routes */
2018 vector<string> empty;
2020 for (RouteList::iterator r = rs->begin(); r != rs->end(); ++r) {
2021 (*r)->set_control_outs (empty);
2025 update_route_solo_state ();
2027 /* writer goes out of scope, forces route list update */
2030 // FIXME: audio specific
2032 boost::shared_ptr<AudioDiskstream> ds;
2034 if ((at = dynamic_cast<AudioTrack*>(route.get())) != 0) {
2035 ds = at->audio_diskstream();
2041 RCUWriter<DiskstreamList> dsl (diskstreams);
2042 boost::shared_ptr<DiskstreamList> d = dsl.get_copy();
2047 find_current_end ();
2049 update_latency_compensation (false, false);
2052 /* XXX should we disconnect from the Route's signals ? */
2054 save_state (_current_snapshot_name);
2056 /* try to cause everyone to drop their references */
2058 route->drop_references ();
2062 Session::route_mute_changed (void* src)
2068 Session::route_solo_changed (void* src, shared_ptr<Route> route)
2070 if (solo_update_disabled) {
2077 is_track = (dynamic_cast<AudioTrack*>(route.get()) != 0);
2079 shared_ptr<RouteList> r = routes.reader ();
2081 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2083 /* soloing a track mutes all other tracks, soloing a bus mutes all other busses */
2087 /* don't mess with busses */
2089 if (dynamic_cast<AudioTrack*>((*i).get()) == 0) {
2095 /* don't mess with tracks */
2097 if (dynamic_cast<AudioTrack*>((*i).get()) != 0) {
2102 if ((*i) != route &&
2103 ((*i)->mix_group () == 0 ||
2104 (*i)->mix_group () != route->mix_group () ||
2105 !route->mix_group ()->is_active())) {
2107 if ((*i)->soloed()) {
2109 /* if its already soloed, and solo latching is enabled,
2110 then leave it as it is.
2113 if (_solo_latched) {
2120 solo_update_disabled = true;
2121 (*i)->set_solo (false, src);
2122 solo_update_disabled = false;
2126 bool something_soloed = false;
2127 bool same_thing_soloed = false;
2128 bool signal = false;
2130 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2131 if ((*i)->soloed()) {
2132 something_soloed = true;
2133 if (dynamic_cast<AudioTrack*>((*i).get())) {
2135 same_thing_soloed = true;
2140 same_thing_soloed = true;
2148 if (something_soloed != currently_soloing) {
2150 currently_soloing = something_soloed;
2153 modify_solo_mute (is_track, same_thing_soloed);
2156 SoloActive (currently_soloing);
2163 Session::set_solo_latched (bool yn)
2165 if (yn != _solo_latched) {
2168 ControlChanged (SoloLatch);
2173 Session::update_route_solo_state ()
2176 bool is_track = false;
2177 bool signal = false;
2179 /* caller must hold RouteLock */
2181 /* this is where we actually implement solo by changing
2182 the solo mute setting of each track.
2185 shared_ptr<RouteList> r = routes.reader ();
2187 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2188 if ((*i)->soloed()) {
2190 if (dynamic_cast<AudioTrack*>((*i).get())) {
2197 if (mute != currently_soloing) {
2199 currently_soloing = mute;
2202 if (!is_track && !mute) {
2204 /* nothing is soloed */
2206 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2207 (*i)->set_solo_mute (false);
2217 modify_solo_mute (is_track, mute);
2220 SoloActive (currently_soloing);
2225 Session::modify_solo_mute (bool is_track, bool mute)
2227 shared_ptr<RouteList> r = routes.reader ();
2229 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2233 /* only alter track solo mute */
2235 if (dynamic_cast<AudioTrack*>((*i).get())) {
2236 if ((*i)->soloed()) {
2237 (*i)->set_solo_mute (!mute);
2239 (*i)->set_solo_mute (mute);
2245 /* only alter bus solo mute */
2247 if (!dynamic_cast<AudioTrack*>((*i).get())) {
2249 if ((*i)->soloed()) {
2251 (*i)->set_solo_mute (false);
2255 /* don't mute master or control outs
2256 in response to another bus solo
2259 if ((*i) != _master_out &&
2260 (*i) != _control_out) {
2261 (*i)->set_solo_mute (mute);
2272 Session::catch_up_on_solo ()
2274 /* this is called after set_state() to catch the full solo
2275 state, which can't be correctly determined on a per-route
2276 basis, but needs the global overview that only the session
2279 update_route_solo_state();
2283 Session::route_by_name (string name)
2285 shared_ptr<RouteList> r = routes.reader ();
2287 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2288 if ((*i)->name() == name) {
2293 return shared_ptr<Route> ((Route*) 0);
2297 Session::route_by_id (PBD::ID id)
2299 shared_ptr<RouteList> r = routes.reader ();
2301 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2302 if ((*i)->id() == id) {
2307 return shared_ptr<Route> ((Route*) 0);
2311 Session::route_by_remote_id (uint32_t id)
2313 shared_ptr<RouteList> r = routes.reader ();
2315 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2316 if ((*i)->remote_control_id() == id) {
2321 return shared_ptr<Route> ((Route*) 0);
2325 Session::find_current_end ()
2327 if (_state_of_the_state & Loading) {
2331 jack_nframes_t max = get_maximum_extent ();
2333 if (max > end_location->end()) {
2334 end_location->set_end (max);
2336 DurationChanged(); /* EMIT SIGNAL */
2341 Session::get_maximum_extent () const
2343 jack_nframes_t max = 0;
2346 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
2348 for (DiskstreamList::const_iterator i = dsl->begin(); i != dsl->end(); ++i) {
2349 Playlist* pl = (*i)->playlist();
2350 if ((me = pl->get_maximum_extent()) > max) {
2358 boost::shared_ptr<Diskstream>
2359 Session::diskstream_by_name (string name)
2361 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
2363 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
2364 if ((*i)->name() == name) {
2369 return boost::shared_ptr<Diskstream>((Diskstream*) 0);
2372 boost::shared_ptr<Diskstream>
2373 Session::diskstream_by_id (const PBD::ID& id)
2375 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
2377 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
2378 if ((*i)->id() == id) {
2383 return boost::shared_ptr<Diskstream>((Diskstream*) 0);
2386 /* AudioRegion management */
2389 Session::new_region_name (string old)
2391 string::size_type last_period;
2393 string::size_type len = old.length() + 64;
2396 if ((last_period = old.find_last_of ('.')) == string::npos) {
2398 /* no period present - add one explicitly */
2401 last_period = old.length() - 1;
2406 number = atoi (old.substr (last_period+1).c_str());
2410 while (number < (UINT_MAX-1)) {
2412 AudioRegionList::const_iterator i;
2417 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
2420 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2421 if (i->second->name() == sbuf) {
2426 if (i == audio_regions.end()) {
2431 if (number != (UINT_MAX-1)) {
2435 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
2440 Session::region_name (string& result, string base, bool newlevel) const
2447 Glib::Mutex::Lock lm (region_lock);
2449 snprintf (buf, sizeof (buf), "%d", (int)audio_regions.size() + 1);
2457 /* XXX this is going to be slow. optimize me later */
2462 string::size_type pos;
2464 pos = base.find_last_of ('.');
2466 /* pos may be npos, but then we just use entire base */
2468 subbase = base.substr (0, pos);
2472 bool name_taken = true;
2475 Glib::Mutex::Lock lm (region_lock);
2477 for (int n = 1; n < 5000; ++n) {
2480 snprintf (buf, sizeof (buf), ".%d", n);
2485 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2486 if (i->second->name() == result) {
2499 fatal << string_compose(_("too many regions with names like %1"), base) << endmsg;
2507 Session::add_region (boost::shared_ptr<Region> region)
2509 boost::shared_ptr<AudioRegion> ar;
2510 boost::shared_ptr<AudioRegion> oar;
2514 Glib::Mutex::Lock lm (region_lock);
2516 if ((ar = boost::dynamic_pointer_cast<AudioRegion> (region)) != 0) {
2518 AudioRegionList::iterator x;
2520 for (x = audio_regions.begin(); x != audio_regions.end(); ++x) {
2522 oar = boost::dynamic_pointer_cast<AudioRegion> (x->second);
2524 if (ar->region_list_equivalent (oar)) {
2529 if (x == audio_regions.end()) {
2531 pair<AudioRegionList::key_type,AudioRegionList::mapped_type> entry;
2533 entry.first = region->id();
2536 pair<AudioRegionList::iterator,bool> x = audio_regions.insert (entry);
2548 fatal << _("programming error: ")
2549 << X_("unknown region type passed to Session::add_region()")
2556 /* mark dirty because something has changed even if we didn't
2557 add the region to the region list.
2563 region->GoingAway.connect (sigc::bind (mem_fun (*this, &Session::remove_region), region));
2564 region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), region));
2565 AudioRegionAdded (ar); /* EMIT SIGNAL */
2570 Session::region_changed (Change what_changed, boost::shared_ptr<Region> region)
2572 if (what_changed & Region::HiddenChanged) {
2573 /* relay hidden changes */
2574 RegionHiddenChange (region);
2579 Session::region_renamed (boost::shared_ptr<Region> region)
2581 add_region (region);
2585 Session::remove_region (boost::shared_ptr<Region> region)
2587 AudioRegionList::iterator i;
2588 boost::shared_ptr<AudioRegion> ar;
2589 bool removed = false;
2592 Glib::Mutex::Lock lm (region_lock);
2594 if ((ar = boost::dynamic_pointer_cast<AudioRegion> (region)) != 0) {
2595 if ((i = audio_regions.find (region->id())) != audio_regions.end()) {
2596 audio_regions.erase (i);
2602 fatal << _("programming error: ")
2603 << X_("unknown region type passed to Session::remove_region()")
2609 /* mark dirty because something has changed even if we didn't
2610 remove the region from the region list.
2616 AudioRegionRemoved(ar); /* EMIT SIGNAL */
2620 boost::shared_ptr<AudioRegion>
2621 Session::find_whole_file_parent (boost::shared_ptr<AudioRegion> child)
2623 AudioRegionList::iterator i;
2624 boost::shared_ptr<AudioRegion> region;
2625 Glib::Mutex::Lock lm (region_lock);
2627 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2631 if (region->whole_file()) {
2633 if (child->source_equivalent (region)) {
2639 return boost::shared_ptr<AudioRegion> ((AudioRegion*) 0);
2643 Session::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
2645 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i)
2646 (*i)->get_region_list_equivalent_regions (region, result);
2650 Session::destroy_region (boost::shared_ptr<Region> region)
2652 boost::shared_ptr<AudioRegion> aregion;
2654 if ((aregion = boost::dynamic_pointer_cast<AudioRegion> (region)) == 0) {
2658 if (aregion->playlist()) {
2659 aregion->playlist()->destroy_region (region);
2662 vector<boost::shared_ptr<Source> > srcs;
2664 for (uint32_t n = 0; n < aregion->n_channels(); ++n) {
2665 srcs.push_back (aregion->source (n));
2668 for (vector<boost::shared_ptr<Source> >::iterator i = srcs.begin(); i != srcs.end(); ++i) {
2670 if ((*i).use_count() == 1) {
2671 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*i);
2674 (afs)->mark_for_remove ();
2677 (*i)->drop_references ();
2685 Session::destroy_regions (list<boost::shared_ptr<Region> > regions)
2687 for (list<boost::shared_ptr<Region> >::iterator i = regions.begin(); i != regions.end(); ++i) {
2688 destroy_region (*i);
2694 Session::remove_last_capture ()
2696 list<boost::shared_ptr<Region> > r;
2698 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
2700 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
2701 list<boost::shared_ptr<Region> >& l = (*i)->last_capture_regions();
2704 r.insert (r.end(), l.begin(), l.end());
2709 destroy_regions (r);
2714 Session::remove_region_from_region_list (boost::shared_ptr<Region> r)
2720 /* Source Management */
2723 Session::add_source (boost::shared_ptr<Source> source)
2725 boost::shared_ptr<AudioFileSource> afs;
2727 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
2729 pair<AudioSourceList::key_type, AudioSourceList::mapped_type> entry;
2730 pair<AudioSourceList::iterator,bool> result;
2732 entry.first = source->id();
2736 Glib::Mutex::Lock lm (audio_source_lock);
2737 result = audio_sources.insert (entry);
2740 if (!result.second) {
2741 cerr << "\tNOT inserted ? " << result.second << endl;
2744 source->GoingAway.connect (sigc::bind (mem_fun (this, &Session::remove_source), boost::weak_ptr<Source> (source)));
2747 SourceAdded (source); /* EMIT SIGNAL */
2749 cerr << "\tNOT AUDIO FILE\n";
2754 Session::remove_source (boost::weak_ptr<Source> src)
2756 AudioSourceList::iterator i;
2757 boost::shared_ptr<Source> source = src.lock();
2760 cerr << "removing a source DEAD\n";
2762 cerr << "removing a source " << source->name () << endl;
2765 Glib::Mutex::Lock lm (audio_source_lock);
2767 if ((i = audio_sources.find (source->id())) != audio_sources.end()) {
2768 audio_sources.erase (i);
2772 if (!_state_of_the_state & InCleanup) {
2774 /* save state so we don't end up with a session file
2775 referring to non-existent sources.
2778 save_state (_current_snapshot_name);
2781 SourceRemoved(source); /* EMIT SIGNAL */
2785 boost::shared_ptr<Source>
2786 Session::source_by_id (const PBD::ID& id)
2788 Glib::Mutex::Lock lm (audio_source_lock);
2789 AudioSourceList::iterator i;
2790 boost::shared_ptr<Source> source;
2792 if ((i = audio_sources.find (id)) != audio_sources.end()) {
2796 /* XXX search MIDI or other searches here */
2802 Session::peak_path_from_audio_path (string audio_path)
2804 /* XXX hardly bombproof! fix me */
2808 res = Glib::path_get_dirname (audio_path);
2809 res = Glib::path_get_dirname (res);
2811 res += peak_dir_name;
2813 res += PBD::basename_nosuffix (audio_path);
2820 Session::change_audio_path_by_name (string path, string oldname, string newname, bool destructive)
2823 string old_basename = PBD::basename_nosuffix (oldname);
2824 string new_legalized = legalize_for_path (newname);
2826 /* note: we know (or assume) the old path is already valid */
2830 /* destructive file sources have a name of the form:
2832 /path/to/Tnnnn-NAME(%[LR])?.wav
2834 the task here is to replace NAME with the new name.
2837 /* find last slash */
2841 string::size_type slash;
2842 string::size_type dash;
2844 if ((slash = path.find_last_of ('/')) == string::npos) {
2848 dir = path.substr (0, slash+1);
2850 /* '-' is not a legal character for the NAME part of the path */
2852 if ((dash = path.find_last_of ('-')) == string::npos) {
2856 prefix = path.substr (slash+1, dash-(slash+1));
2861 path += new_legalized;
2862 path += ".wav"; /* XXX gag me with a spoon */
2866 /* non-destructive file sources have a name of the form:
2868 /path/to/NAME-nnnnn(%[LR])?.wav
2870 the task here is to replace NAME with the new name.
2875 string::size_type slash;
2876 string::size_type dash;
2877 string::size_type postfix;
2879 /* find last slash */
2881 if ((slash = path.find_last_of ('/')) == string::npos) {
2885 dir = path.substr (0, slash+1);
2887 /* '-' is not a legal character for the NAME part of the path */
2889 if ((dash = path.find_last_of ('-')) == string::npos) {
2893 suffix = path.substr (dash+1);
2895 // Suffix is now everything after the dash. Now we need to eliminate
2896 // the nnnnn part, which is done by either finding a '%' or a '.'
2898 postfix = suffix.find_last_of ("%");
2899 if (postfix == string::npos) {
2900 postfix = suffix.find_last_of ('.');
2903 if (postfix != string::npos) {
2904 suffix = suffix.substr (postfix);
2906 error << "Logic error in Session::change_audio_path_by_name(), please report to the developers" << endl;
2910 const uint32_t limit = 10000;
2911 char buf[PATH_MAX+1];
2913 for (uint32_t cnt = 1; cnt <= limit; ++cnt) {
2915 snprintf (buf, sizeof(buf), "%s%s-%u%s", dir.c_str(), newname.c_str(), cnt, suffix.c_str());
2917 if (access (buf, F_OK) != 0) {
2925 error << "FATAL ERROR! Could not find a " << endl;
2934 Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool destructive)
2938 char buf[PATH_MAX+1];
2939 const uint32_t limit = 10000;
2943 legalized = legalize_for_path (name);
2945 /* find a "version" of the file name that doesn't exist in
2946 any of the possible directories.
2949 for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
2951 vector<space_and_path>::iterator i;
2952 uint32_t existing = 0;
2954 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2959 spath += tape_dir_name;
2961 spath += sound_dir_name;
2966 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2967 } else if (nchan == 2) {
2969 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%L.wav", spath.c_str(), cnt, legalized.c_str());
2971 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%R.wav", spath.c_str(), cnt, legalized.c_str());
2973 } else if (nchan < 26) {
2974 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%%c.wav", spath.c_str(), cnt, legalized.c_str(), 'a' + chan);
2976 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2984 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2985 } else if (nchan == 2) {
2987 snprintf (buf, sizeof(buf), "%s-%u%%L.wav", spath.c_str(), cnt);
2989 snprintf (buf, sizeof(buf), "%s-%u%%R.wav", spath.c_str(), cnt);
2991 } else if (nchan < 26) {
2992 snprintf (buf, sizeof(buf), "%s-%u%%%c.wav", spath.c_str(), cnt, 'a' + chan);
2994 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2998 if (access (buf, F_OK) == 0) {
3003 if (existing == 0) {
3008 error << string_compose(_("There are already %1 recordings for %2, which I consider too many."), limit, name) << endmsg;
3009 throw failed_constructor();
3013 /* we now have a unique name for the file, but figure out where to
3020 spath = tape_dir ();
3022 spath = discover_best_sound_dir ();
3025 string::size_type pos = foo.find_last_of ('/');
3027 if (pos == string::npos) {
3030 spath += foo.substr (pos + 1);
3036 boost::shared_ptr<AudioFileSource>
3037 Session::create_audio_source_for_session (AudioDiskstream& ds, uint32_t chan, bool destructive)
3039 string spath = audio_path_from_name (ds.name(), ds.n_channels(), chan, destructive);
3040 return boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (spath, destructive, frame_rate()));
3043 /* Playlist management */
3046 Session::playlist_by_name (string name)
3048 Glib::Mutex::Lock lm (playlist_lock);
3049 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3050 if ((*i)->name() == name) {
3054 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
3055 if ((*i)->name() == name) {
3063 Session::add_playlist (Playlist* playlist)
3065 if (playlist->hidden()) {
3070 Glib::Mutex::Lock lm (playlist_lock);
3071 if (find (playlists.begin(), playlists.end(), playlist) == playlists.end()) {
3072 playlists.insert (playlists.begin(), playlist);
3074 playlist->InUse.connect (mem_fun (*this, &Session::track_playlist));
3075 playlist->GoingAway.connect (sigc::bind (mem_fun (*this, &Session::remove_playlist), playlist));
3081 PlaylistAdded (playlist); /* EMIT SIGNAL */
3085 Session::track_playlist (Playlist* pl, bool inuse)
3087 PlaylistList::iterator x;
3090 Glib::Mutex::Lock lm (playlist_lock);
3093 //cerr << "shifting playlist to unused: " << pl->name() << endl;
3095 unused_playlists.insert (pl);
3097 if ((x = playlists.find (pl)) != playlists.end()) {
3098 playlists.erase (x);
3103 //cerr << "shifting playlist to used: " << pl->name() << endl;
3105 playlists.insert (pl);
3107 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
3108 unused_playlists.erase (x);
3115 Session::remove_playlist (Playlist* playlist)
3117 if (_state_of_the_state & Deletion) {
3122 Glib::Mutex::Lock lm (playlist_lock);
3123 // cerr << "removing playlist: " << playlist->name() << endl;
3125 PlaylistList::iterator i;
3127 i = find (playlists.begin(), playlists.end(), playlist);
3129 if (i != playlists.end()) {
3130 playlists.erase (i);
3133 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
3134 if (i != unused_playlists.end()) {
3135 unused_playlists.erase (i);
3142 PlaylistRemoved (playlist); /* EMIT SIGNAL */
3146 Session::set_audition (boost::shared_ptr<Region> r)
3148 pending_audition_region = r;
3149 post_transport_work = PostTransportWork (post_transport_work | PostTransportAudition);
3150 schedule_butler_transport_work ();
3154 Session::audition_playlist ()
3156 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3157 ev->region.reset ();
3162 Session::non_realtime_set_audition ()
3164 if (!pending_audition_region) {
3165 auditioner->audition_current_playlist ();
3167 auditioner->audition_region (pending_audition_region);
3168 pending_audition_region.reset ();
3170 AuditionActive (true); /* EMIT SIGNAL */
3174 Session::audition_region (boost::shared_ptr<Region> r)
3176 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3182 Session::cancel_audition ()
3184 if (auditioner->active()) {
3185 auditioner->cancel_audition ();
3186 AuditionActive (false); /* EMIT SIGNAL */
3191 Session::RoutePublicOrderSorter::operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b)
3193 return a->order_key(N_("signal")) < b->order_key(N_("signal"));
3197 Session::remove_empty_sounds ()
3200 PathScanner scanner;
3205 vector<string *>* possible_audiofiles = scanner (dir, "\\.wav$", false, true);
3207 for (vector<string *>::iterator i = possible_audiofiles->begin(); i != possible_audiofiles->end(); ++i) {
3209 if (AudioFileSource::is_empty (*(*i))) {
3211 unlink ((*i)->c_str());
3213 string peak_path = peak_path_from_audio_path (**i);
3214 unlink (peak_path.c_str());
3220 delete possible_audiofiles;
3224 Session::is_auditioning () const
3226 /* can be called before we have an auditioner object */
3228 return auditioner->active();
3235 Session::set_all_solo (bool yn)
3237 shared_ptr<RouteList> r = routes.reader ();
3239 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3240 if (!(*i)->hidden()) {
3241 (*i)->set_solo (yn, this);
3249 Session::set_all_mute (bool yn)
3251 shared_ptr<RouteList> r = routes.reader ();
3253 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3254 if (!(*i)->hidden()) {
3255 (*i)->set_mute (yn, this);
3263 Session::n_diskstreams () const
3267 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
3269 for (DiskstreamList::const_iterator i = dsl->begin(); i != dsl->end(); ++i) {
3270 if (!(*i)->hidden()) {
3278 Session::graph_reordered ()
3280 /* don't do this stuff if we are setting up connections
3281 from a set_state() call.
3284 if (_state_of_the_state & InitialConnecting) {
3290 /* force all diskstreams to update their capture offset values to
3291 reflect any changes in latencies within the graph.
3294 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
3296 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
3297 (*i)->set_capture_offset ();
3302 Session::record_disenable_all ()
3304 record_enable_change_all (false);
3308 Session::record_enable_all ()
3310 record_enable_change_all (true);
3314 Session::record_enable_change_all (bool yn)
3316 shared_ptr<RouteList> r = routes.reader ();
3318 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3321 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
3322 at->set_record_enable (yn, this);
3326 /* since we don't keep rec-enable state, don't mark session dirty */
3330 Session::add_redirect (Redirect* redirect)
3334 PortInsert* port_insert;
3335 PluginInsert* plugin_insert;
3337 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3338 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3339 _port_inserts.insert (_port_inserts.begin(), port_insert);
3340 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3341 _plugin_inserts.insert (_plugin_inserts.begin(), plugin_insert);
3343 fatal << _("programming error: unknown type of Insert created!") << endmsg;
3346 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3347 _sends.insert (_sends.begin(), send);
3349 fatal << _("programming error: unknown type of Redirect created!") << endmsg;
3353 redirect->GoingAway.connect (sigc::bind (mem_fun (*this, &Session::remove_redirect), redirect));
3359 Session::remove_redirect (Redirect* redirect)
3363 PortInsert* port_insert;
3364 PluginInsert* plugin_insert;
3366 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3367 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3368 _port_inserts.remove (port_insert);
3369 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3370 _plugin_inserts.remove (plugin_insert);
3372 fatal << _("programming error: unknown type of Insert deleted!") << endmsg;
3375 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3376 _sends.remove (send);
3378 fatal << _("programming error: unknown type of Redirect deleted!") << endmsg;
3386 Session::available_capture_duration ()
3388 const double scale = 4096.0 / sizeof (Sample);
3390 if (_total_free_4k_blocks * scale > (double) max_frames) {
3394 return (jack_nframes_t) floor (_total_free_4k_blocks * scale);
3398 Session::add_connection (ARDOUR::Connection* connection)
3401 Glib::Mutex::Lock guard (connection_lock);
3402 _connections.push_back (connection);
3405 ConnectionAdded (connection); /* EMIT SIGNAL */
3411 Session::remove_connection (ARDOUR::Connection* connection)
3413 bool removed = false;
3416 Glib::Mutex::Lock guard (connection_lock);
3417 ConnectionList::iterator i = find (_connections.begin(), _connections.end(), connection);
3419 if (i != _connections.end()) {
3420 _connections.erase (i);
3426 ConnectionRemoved (connection); /* EMIT SIGNAL */
3432 ARDOUR::Connection *
3433 Session::connection_by_name (string name) const
3435 Glib::Mutex::Lock lm (connection_lock);
3437 for (ConnectionList::const_iterator i = _connections.begin(); i != _connections.end(); ++i) {
3438 if ((*i)->name() == name) {
3447 Session::set_edit_mode (EditMode mode)
3452 Glib::Mutex::Lock lm (playlist_lock);
3454 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3455 (*i)->set_edit_mode (mode);
3460 ControlChanged (EditingMode); /* EMIT SIGNAL */
3464 Session::tempo_map_changed (Change ignored)
3471 Session::ensure_passthru_buffers (uint32_t howmany)
3473 while (howmany > _passthru_buffers.size()) {
3475 #ifdef NO_POSIX_MEMALIGN
3476 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3478 posix_memalign((void **)&p,16,current_block_size * 4);
3480 _passthru_buffers.push_back (p);
3484 #ifdef NO_POSIX_MEMALIGN
3485 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3487 posix_memalign((void **)&p,16,current_block_size * 4);
3489 memset (p, 0, sizeof (Sample) * current_block_size);
3490 _silent_buffers.push_back (p);
3494 #ifdef NO_POSIX_MEMALIGN
3495 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3497 posix_memalign((void **)&p,16,current_block_size * 4);
3499 memset (p, 0, sizeof (Sample) * current_block_size);
3500 _send_buffers.push_back (p);
3503 allocate_pan_automation_buffers (current_block_size, howmany, false);
3507 Session::next_send_name ()
3510 snprintf (buf, sizeof (buf), "send %" PRIu32, ++send_cnt);
3515 Session::next_insert_name ()
3518 snprintf (buf, sizeof (buf), "insert %" PRIu32, ++insert_cnt);
3522 /* Named Selection management */
3525 Session::named_selection_by_name (string name)
3527 Glib::Mutex::Lock lm (named_selection_lock);
3528 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
3529 if ((*i)->name == name) {
3537 Session::add_named_selection (NamedSelection* named_selection)
3540 Glib::Mutex::Lock lm (named_selection_lock);
3541 named_selections.insert (named_selections.begin(), named_selection);
3546 NamedSelectionAdded (); /* EMIT SIGNAL */
3550 Session::remove_named_selection (NamedSelection* named_selection)
3552 bool removed = false;
3555 Glib::Mutex::Lock lm (named_selection_lock);
3557 NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
3559 if (i != named_selections.end()) {
3561 named_selections.erase (i);
3568 NamedSelectionRemoved (); /* EMIT SIGNAL */
3573 Session::reset_native_file_format ()
3575 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
3577 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
3578 (*i)->reset_write_sources (false);
3583 Session::route_name_unique (string n) const
3585 shared_ptr<RouteList> r = routes.reader ();
3587 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3588 if ((*i)->name() == n) {
3597 Session::cleanup_audio_file_source (boost::shared_ptr<AudioFileSource> fs)
3599 return fs->move_to_trash (dead_sound_dir_name);
3603 Session::n_playlists () const
3605 Glib::Mutex::Lock lm (playlist_lock);
3606 return playlists.size();
3610 Session::set_solo_model (SoloModel sm)
3612 if (sm != _solo_model) {
3614 ControlChanged (SoloingModel);
3620 Session::allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howmany, bool force)
3622 if (!force && howmany <= _npan_buffers) {
3626 if (_pan_automation_buffer) {
3628 for (uint32_t i = 0; i < _npan_buffers; ++i) {
3629 delete [] _pan_automation_buffer[i];
3632 delete [] _pan_automation_buffer;
3635 _pan_automation_buffer = new pan_t*[howmany];
3637 for (uint32_t i = 0; i < howmany; ++i) {
3638 _pan_automation_buffer[i] = new pan_t[nframes];
3641 _npan_buffers = howmany;
3645 Session::freeze (InterThreadInfo& itt)
3647 shared_ptr<RouteList> r = routes.reader ();
3649 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3653 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
3654 /* XXX this is wrong because itt.progress will keep returning to zero at the start
3665 Session::write_one_audio_track (AudioTrack& track, jack_nframes_t start, jack_nframes_t len,
3666 bool overwrite, vector<boost::shared_ptr<AudioSource> >& srcs, InterThreadInfo& itt)
3670 boost::shared_ptr<AudioFileSource> fsource;
3672 char buf[PATH_MAX+1];
3675 jack_nframes_t position;
3676 jack_nframes_t this_chunk;
3677 jack_nframes_t to_do;
3678 vector<Sample*> buffers;
3680 // any bigger than this seems to cause stack overflows in called functions
3681 const jack_nframes_t chunk_size = (128 * 1024)/4;
3683 g_atomic_int_set (&processing_prohibited, 1);
3685 /* call tree *MUST* hold route_lock */
3687 if ((playlist = track.diskstream()->playlist()) == 0) {
3691 /* external redirects will be a problem */
3693 if (track.has_external_redirects()) {
3697 nchans = track.audio_diskstream()->n_channels();
3699 dir = discover_best_sound_dir ();
3701 for (uint32_t chan_n=0; chan_n < nchans; ++chan_n) {
3703 for (x = 0; x < 99999; ++x) {
3704 snprintf (buf, sizeof(buf), "%s/%s-%d-bounce-%" PRIu32 ".wav", dir.c_str(), playlist->name().c_str(), chan_n, x+1);
3705 if (access (buf, F_OK) != 0) {
3711 error << string_compose (_("too many bounced versions of playlist \"%1\""), playlist->name()) << endmsg;
3716 fsource = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (buf, false, frame_rate()));
3719 catch (failed_constructor& err) {
3720 error << string_compose (_("cannot create new audio file \"%1\" for %2"), buf, track.name()) << endmsg;
3724 srcs.push_back (fsource);
3727 /* XXX need to flush all redirects */
3732 /* create a set of reasonably-sized buffers */
3734 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
3736 #ifdef NO_POSIX_MEMALIGN
3737 b = (Sample *) malloc(chunk_size * sizeof(Sample));
3739 posix_memalign((void **)&b,16,chunk_size * 4);
3741 buffers.push_back (b);
3744 while (to_do && !itt.cancel) {
3746 this_chunk = min (to_do, chunk_size);
3748 if (track.export_stuff (buffers, nchans, start, this_chunk)) {
3753 for (vector<boost::shared_ptr<AudioSource> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
3754 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3757 if (afs->write (buffers[n], this_chunk) != this_chunk) {
3763 start += this_chunk;
3764 to_do -= this_chunk;
3766 itt.progress = (float) (1.0 - ((double) to_do / len));
3775 xnow = localtime (&now);
3777 for (vector<boost::shared_ptr<AudioSource> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3778 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3781 afs->update_header (position, *xnow, now);
3785 /* build peakfile for new source */
3787 for (vector<boost::shared_ptr<AudioSource> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3788 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3790 afs->build_peaks ();
3799 for (vector<boost::shared_ptr<AudioSource> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
3800 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
3803 afs->mark_for_remove ();
3806 (*src)->drop_references ();
3810 for (vector<Sample*>::iterator i = buffers.begin(); i != buffers.end(); ++i) {
3814 g_atomic_int_set (&processing_prohibited, 0);
3822 Session::get_silent_buffers (uint32_t howmany)
3824 for (uint32_t i = 0; i < howmany; ++i) {
3825 memset (_silent_buffers[i], 0, sizeof (Sample) * current_block_size);
3827 return _silent_buffers;
3831 Session::ntracks () const
3834 shared_ptr<RouteList> r = routes.reader ();
3836 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3837 if (dynamic_cast<AudioTrack*> ((*i).get())) {
3846 Session::nbusses () const
3849 shared_ptr<RouteList> r = routes.reader ();
3851 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
3852 if (dynamic_cast<AudioTrack*> ((*i).get()) == 0) {
3861 Session::set_layer_model (LayerModel lm)
3863 if (lm != layer_model) {
3866 ControlChanged (LayeringModel);
3871 Session::set_xfade_model (CrossfadeModel xm)
3873 if (xm != xfade_model) {
3876 ControlChanged (CrossfadingModel);
3881 Session::handle_configuration_change (const char* parameter)
3883 if (!strcmp (parameter, "use-video-sync")) {
3884 if (_transport_speed == 0.0f) {
3885 waiting_for_sync_offset = true;
3891 Session::add_curve(Curve *curve)
3893 curves[curve->id()] = curve;
3897 Session::add_automation_list(AutomationList *al)
3899 automation_lists[al->id()] = al;