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 <pbd/error.h>
36 #include <pbd/lockmonitor.h>
37 #include <pbd/pathscanner.h>
38 #include <pbd/stl_delete.h>
39 #include <pbd/basename.h>
40 #include <pbd/dirname.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/configuration.h>
44 #include <ardour/session.h>
45 #include <ardour/diskstream.h>
46 #include <ardour/utils.h>
47 #include <ardour/audioplaylist.h>
48 #include <ardour/audioregion.h>
49 #include <ardour/source.h>
50 #include <ardour/filesource.h>
51 #include <ardour/destructive_filesource.h>
52 #include <ardour/sndfilesource.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/osc.h>
72 using namespace ARDOUR;
73 //using namespace sigc;
75 const char* Session::_template_suffix = X_(".template");
76 const char* Session::_statefile_suffix = X_(".ardour");
77 const char* Session::_pending_suffix = X_(".pending");
78 const char* Session::sound_dir_name = X_("sounds");
79 const char* Session::tape_dir_name = X_("tapes");
80 const char* Session::peak_dir_name = X_("peaks");
81 const char* Session::dead_sound_dir_name = X_("dead_sounds");
83 Session::compute_peak_t Session::compute_peak = 0;
84 Session::apply_gain_to_buffer_t Session::apply_gain_to_buffer = 0;
85 Session::mix_buffers_with_gain_t Session::mix_buffers_with_gain = 0;
86 Session::mix_buffers_no_gain_t Session::mix_buffers_no_gain = 0;
88 sigc::signal<int> Session::AskAboutPendingState;
91 Session::find_session (string str, string& path, string& snapshot, bool& isnew)
98 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
99 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
105 /* check to see if it exists, and what it is */
107 if (stat (str.c_str(), &statbuf)) {
108 if (errno == ENOENT) {
111 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
119 /* it exists, so it must either be the name
120 of the directory, or the name of the statefile
124 if (S_ISDIR (statbuf.st_mode)) {
126 string::size_type slash = str.find_last_of ('/');
128 if (slash == string::npos) {
130 /* a subdirectory of cwd, so statefile should be ... */
136 tmp += _statefile_suffix;
140 if (stat (tmp.c_str(), &statbuf)) {
141 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
151 /* some directory someplace in the filesystem.
152 the snapshot name is the directory name
157 snapshot = str.substr (slash+1);
161 } else if (S_ISREG (statbuf.st_mode)) {
163 string::size_type slash = str.find_last_of ('/');
164 string::size_type suffix;
166 /* remove the suffix */
168 if (slash != string::npos) {
169 snapshot = str.substr (slash+1);
174 suffix = snapshot.find (_statefile_suffix);
176 if (suffix == string::npos) {
177 error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
183 snapshot = snapshot.substr (0, suffix);
185 if (slash == string::npos) {
187 /* we must be in the directory where the
188 statefile lives. get it using cwd().
191 char cwd[PATH_MAX+1];
193 if (getcwd (cwd, sizeof (cwd)) == 0) {
194 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
203 /* full path to the statefile */
205 path = str.substr (0, slash);
210 /* what type of file is it? */
211 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
217 /* its the name of a new directory. get the name
221 string::size_type slash = str.find_last_of ('/');
223 if (slash == string::npos) {
225 /* no slash, just use the name, but clean it up */
227 path = legalize_for_path (str);
233 snapshot = str.substr (slash+1);
240 Session::Session (AudioEngine &eng,
242 string snapshot_name,
243 string* mix_template)
246 _mmc_port (default_mmc_port),
247 _mtc_port (default_mtc_port),
248 _midi_port (default_midi_port),
249 pending_events (2048),
250 midi_requests (128), // the size of this should match the midi request pool size
255 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
257 n_physical_outputs = _engine.n_physical_outputs();
258 n_physical_inputs = _engine.n_physical_inputs();
260 first_stage_init (fullpath, snapshot_name);
262 if (create (new_session, mix_template, _engine.frame_rate() * 60 * 5)) {
263 throw failed_constructor ();
266 if (second_stage_init (new_session)) {
267 throw failed_constructor ();
270 store_recent_sessions(_name, _path);
272 bool was_dirty = dirty();
274 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
277 DirtyChanged (); /* EMIT SIGNAL */
281 Session::Session (AudioEngine &eng,
283 string snapshot_name,
284 AutoConnectOption input_ac,
285 AutoConnectOption output_ac,
286 uint32_t control_out_channels,
287 uint32_t master_out_channels,
288 uint32_t requested_physical_in,
289 uint32_t requested_physical_out,
290 jack_nframes_t initial_length)
293 _mmc_port (default_mmc_port),
294 _mtc_port (default_mtc_port),
295 _midi_port (default_midi_port),
296 pending_events (2048),
303 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
305 n_physical_outputs = max (requested_physical_out, _engine.n_physical_outputs());
306 n_physical_inputs = max (requested_physical_in, _engine.n_physical_inputs());
308 first_stage_init (fullpath, snapshot_name);
310 if (create (new_session, 0, initial_length)) {
311 throw failed_constructor ();
314 if (control_out_channels) {
316 r = new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut);
321 if (master_out_channels) {
323 r = new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut);
327 /* prohibit auto-connect to master, because there isn't one */
328 output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
331 input_auto_connect = input_ac;
332 output_auto_connect = output_ac;
334 if (second_stage_init (new_session)) {
335 throw failed_constructor ();
338 store_recent_sessions(_name, _path);
340 bool was_dirty = dirty ();
342 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
345 DirtyChanged (); /* EMIT SIGNAL */
351 /* if we got to here, leaving pending capture state around
355 remove_pending_capture_state ();
357 _state_of_the_state = StateOfTheState (CannotSave|Deletion);
358 _engine.remove_session ();
360 going_away (); /* EMIT SIGNAL */
362 terminate_butler_thread ();
363 terminate_midi_thread ();
365 if (click_data && click_data != default_click) {
366 delete [] click_data;
369 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
370 delete [] click_emphasis_data;
384 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
388 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
392 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
396 for (map<RunContext,char*>::iterator i = _conversion_buffers.begin(); i != _conversion_buffers.end(); ++i) {
397 delete [] (i->second);
400 #undef TRACK_DESTRUCTION
401 #ifdef TRACK_DESTRUCTION
402 cerr << "delete named selections\n";
403 #endif /* TRACK_DESTRUCTION */
404 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ) {
405 NamedSelectionList::iterator tmp;
414 #ifdef TRACK_DESTRUCTION
415 cerr << "delete playlists\n";
416 #endif /* TRACK_DESTRUCTION */
417 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ) {
418 PlaylistList::iterator tmp;
428 #ifdef TRACK_DESTRUCTION
429 cerr << "delete audio regions\n";
430 #endif /* TRACK_DESTRUCTION */
431 for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ) {
432 AudioRegionList::iterator tmp;
442 #ifdef TRACK_DESTRUCTION
443 cerr << "delete routes\n";
444 #endif /* TRACK_DESTRUCTION */
445 for (RouteList::iterator i = routes.begin(); i != routes.end(); ) {
446 RouteList::iterator tmp;
453 #ifdef TRACK_DESTRUCTION
454 cerr << "delete diskstreams\n";
455 #endif /* TRACK_DESTRUCTION */
456 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ) {
457 DiskStreamList::iterator tmp;
467 #ifdef TRACK_DESTRUCTION
468 cerr << "delete sources\n";
469 #endif /* TRACK_DESTRUCTION */
470 for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
471 SourceList::iterator tmp;
481 #ifdef TRACK_DESTRUCTION
482 cerr << "delete mix groups\n";
483 #endif /* TRACK_DESTRUCTION */
484 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ) {
485 list<RouteGroup*>::iterator tmp;
495 #ifdef TRACK_DESTRUCTION
496 cerr << "delete edit groups\n";
497 #endif /* TRACK_DESTRUCTION */
498 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ) {
499 list<RouteGroup*>::iterator tmp;
509 #ifdef TRACK_DESTRUCTION
510 cerr << "delete connections\n";
511 #endif /* TRACK_DESTRUCTION */
512 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ) {
513 ConnectionList::iterator tmp;
523 if (butler_mixdown_buffer) {
524 delete [] butler_mixdown_buffer;
527 if (butler_gain_buffer) {
528 delete [] butler_gain_buffer;
531 Crossfade::set_buffer_size (0);
543 Session::set_worst_io_latencies (bool take_lock)
545 _worst_output_latency = 0;
546 _worst_input_latency = 0;
548 if (!_engine.connected()) {
553 route_lock.read_lock ();
556 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
557 _worst_output_latency = max (_worst_output_latency, (*i)->output_latency());
558 _worst_input_latency = max (_worst_input_latency, (*i)->input_latency());
562 route_lock.unlock ();
567 Session::when_engine_running ()
569 string first_physical_output;
571 /* we don't want to run execute this again */
573 first_time_running.disconnect ();
575 set_block_size (_engine.frames_per_cycle());
576 set_frame_rate (_engine.frame_rate());
578 /* every time we reconnect, recompute worst case output latencies */
580 _engine.Running.connect (sigc::bind (mem_fun (*this, &Session::set_worst_io_latencies), true));
582 if (synced_to_jack()) {
583 _engine.transport_stop ();
586 if (Config->get_jack_time_master()) {
587 _engine.transport_locate (_transport_frame);
595 _click_io = new ClickIO (*this, "click", 0, 0, -1, -1);
597 if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
599 /* existing state for Click */
601 if (_click_io->set_state (*child->children().front()) == 0) {
603 _clicking = click_requested;
607 error << _("could not setup Click I/O") << endmsg;
613 /* default state for Click */
615 first_physical_output = _engine.get_nth_physical_output (0);
617 if (first_physical_output.length()) {
618 if (_click_io->add_output_port (first_physical_output, this)) {
619 // relax, even though its an error
621 _clicking = click_requested;
627 catch (failed_constructor& err) {
628 error << _("cannot setup Click I/O") << endmsg;
631 set_worst_io_latencies (true);
634 ControlChanged (Clicking); /* EMIT SIGNAL */
637 if (auditioner == 0) {
639 /* we delay creating the auditioner till now because
640 it makes its own connections to ports named
641 in the ARDOUR_RC config file. the engine has
642 to be running for this to work.
646 auditioner = new Auditioner (*this);
649 catch (failed_constructor& err) {
650 warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
654 /* Create a set of Connection objects that map
655 to the physical outputs currently available
660 for (uint32_t np = 0; np < n_physical_outputs; ++np) {
662 snprintf (buf, sizeof (buf), _("out %" PRIu32), np+1);
664 Connection* c = new OutputConnection (buf, true);
667 c->add_connection (0, _engine.get_nth_physical_output (np));
672 for (uint32_t np = 0; np < n_physical_inputs; ++np) {
674 snprintf (buf, sizeof (buf), _("in %" PRIu32), np+1);
676 Connection* c = new InputConnection (buf, true);
679 c->add_connection (0, _engine.get_nth_physical_input (np));
686 for (uint32_t np = 0; np < n_physical_outputs; np +=2) {
688 snprintf (buf, sizeof (buf), _("out %" PRIu32 "+%" PRIu32), np+1, np+2);
690 Connection* c = new OutputConnection (buf, true);
694 c->add_connection (0, _engine.get_nth_physical_output (np));
695 c->add_connection (1, _engine.get_nth_physical_output (np+1));
700 for (uint32_t np = 0; np < n_physical_inputs; np +=2) {
702 snprintf (buf, sizeof (buf), _("in %" PRIu32 "+%" PRIu32), np+1, np+2);
704 Connection* c = new InputConnection (buf, true);
708 c->add_connection (0, _engine.get_nth_physical_input (np));
709 c->add_connection (1, _engine.get_nth_physical_input (np+1));
718 /* create master/control ports */
723 /* force the master to ignore any later call to this */
725 if (_master_out->pending_state_node) {
726 _master_out->ports_became_legal();
729 /* no panner resets till we are through */
731 _master_out->defer_pan_reset ();
733 while ((int) _master_out->n_inputs() < _master_out->input_maximum()) {
734 if (_master_out->add_input_port ("", this)) {
735 error << _("cannot setup master inputs")
741 while ((int) _master_out->n_outputs() < _master_out->output_maximum()) {
742 if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
743 error << _("cannot setup master outputs")
750 _master_out->allow_pan_reset ();
754 Connection* c = new OutputConnection (_("Master Out"), true);
756 for (uint32_t n = 0; n < _master_out->n_inputs (); ++n) {
758 c->add_connection ((int) n, _master_out->input(n)->name());
765 /* catch up on send+insert cnts */
769 for (list<PortInsert*>::iterator i = _port_inserts.begin(); i != _port_inserts.end(); ++i) {
772 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
773 if (id > insert_cnt) {
781 for (list<Send*>::iterator i = _sends.begin(); i != _sends.end(); ++i) {
784 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
791 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
793 /* hook us up to the engine */
795 _engine.set_session (this);
799 osc->set_session (*this);
801 _state_of_the_state = Clean;
803 DirtyChanged (); /* EMIT SIGNAL */
807 Session::hookup_io ()
809 /* stop graph reordering notifications from
810 causing resorts, etc.
813 _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
815 /* Tell all IO objects to create their ports */
822 while ((int) _control_out->n_inputs() < _control_out->input_maximum()) {
823 if (_control_out->add_input_port ("", this)) {
824 error << _("cannot setup control inputs")
830 while ((int) _control_out->n_outputs() < _control_out->output_maximum()) {
831 if (_control_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
832 error << _("cannot set up master outputs")
840 /* Tell all IO objects to connect themselves together */
842 IO::enable_connecting ();
844 /* Now reset all panners */
846 IO::reset_panners ();
848 /* Anyone who cares about input state, wake up and do something */
850 IOConnectionsComplete (); /* EMIT SIGNAL */
852 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
854 /* now handle the whole enchilada as if it was one
860 /* update mixer solo state */
866 Session::playlist_length_changed (Playlist* pl)
868 /* we can't just increase end_location->end() if pl->get_maximum_extent()
869 if larger. if the playlist used to be the longest playlist,
870 and its now shorter, we have to decrease end_location->end(). hence,
871 we have to iterate over all diskstreams and check the
872 playlists currently in use.
878 Session::diskstream_playlist_changed (DiskStream* dstream)
882 if ((playlist = dstream->playlist()) != 0) {
883 playlist->LengthChanged.connect (sigc::bind (mem_fun (this, &Session::playlist_length_changed), playlist));
886 /* see comment in playlist_length_changed () */
891 Session::record_enabling_legal () const
893 /* this used to be in here, but survey says.... we don't need to restrict it */
894 // if (record_status() == Recording) {
905 Session::set_auto_play (bool yn)
907 if (auto_play != yn) {
910 ControlChanged (AutoPlay);
915 Session::set_auto_return (bool yn)
917 if (auto_return != yn) {
920 ControlChanged (AutoReturn);
925 Session::set_crossfades_active (bool yn)
927 if (crossfades_active != yn) {
928 crossfades_active = yn;
930 ControlChanged (CrossFadesActive);
935 Session::set_do_not_record_plugins (bool yn)
937 if (do_not_record_plugins != yn) {
938 do_not_record_plugins = yn;
940 ControlChanged (RecordingPlugins);
945 Session::set_auto_input (bool yn)
947 if (auto_input != yn) {
950 if (Config->get_use_hardware_monitoring() && transport_rolling()) {
951 /* auto-input only makes a difference if we're rolling */
953 /* Even though this can called from RT context we are using
954 a non-tentative rwlock here, because the action must occur.
955 The rarity and short potential lock duration makes this "OK"
957 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
958 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
959 if ((*i)->record_enabled ()) {
960 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
961 (*i)->monitor_input (!auto_input);
967 ControlChanged (AutoInput);
972 Session::reset_input_monitor_state ()
974 if (transport_rolling()) {
975 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
976 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
977 if ((*i)->record_enabled ()) {
978 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
979 (*i)->monitor_input (Config->get_use_hardware_monitoring() && !auto_input);
983 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
984 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
985 if ((*i)->record_enabled ()) {
986 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
987 (*i)->monitor_input (Config->get_use_hardware_monitoring());
995 Session::set_input_auto_connect (bool yn)
998 input_auto_connect = AutoConnectOption (input_auto_connect|AutoConnectPhysical);
1000 input_auto_connect = AutoConnectOption (input_auto_connect|~AutoConnectPhysical);
1006 Session::get_input_auto_connect () const
1008 return (input_auto_connect & AutoConnectPhysical);
1012 Session::set_output_auto_connect (AutoConnectOption aco)
1014 output_auto_connect = aco;
1019 Session::auto_punch_start_changed (Location* location)
1021 replace_event (Event::PunchIn, location->start());
1023 if (get_record_enabled() && get_punch_in()) {
1024 /* capture start has been changed, so save new pending state */
1025 save_state ("", true);
1030 Session::auto_punch_end_changed (Location* location)
1032 jack_nframes_t when_to_stop = location->end();
1033 // when_to_stop += _worst_output_latency + _worst_input_latency;
1034 replace_event (Event::PunchOut, when_to_stop);
1038 Session::auto_punch_changed (Location* location)
1040 jack_nframes_t when_to_stop = location->end();
1042 replace_event (Event::PunchIn, location->start());
1043 //when_to_stop += _worst_output_latency + _worst_input_latency;
1044 replace_event (Event::PunchOut, when_to_stop);
1048 Session::auto_loop_changed (Location* location)
1050 replace_event (Event::AutoLoop, location->end(), location->start());
1052 if (transport_rolling() && get_auto_loop()) {
1054 //if (_transport_frame < location->start() || _transport_frame > location->end()) {
1056 if (_transport_frame > location->end()) {
1057 // relocate to beginning of loop
1058 clear_events (Event::LocateRoll);
1060 request_locate (location->start(), true);
1063 else if (seamless_loop && !loop_changing) {
1065 // schedule a locate-roll to refill the diskstreams at the
1066 // previous loop end
1067 loop_changing = true;
1069 if (location->end() > last_loopend) {
1070 clear_events (Event::LocateRoll);
1071 Event *ev = new Event (Event::LocateRoll, Event::Add, last_loopend, last_loopend, 0, true);
1078 last_loopend = location->end();
1083 Session::set_auto_punch_location (Location* location)
1087 if ((existing = _locations.auto_punch_location()) != 0 && existing != location) {
1088 auto_punch_start_changed_connection.disconnect();
1089 auto_punch_end_changed_connection.disconnect();
1090 auto_punch_changed_connection.disconnect();
1091 existing->set_auto_punch (false, this);
1092 remove_event (existing->start(), Event::PunchIn);
1093 clear_events (Event::PunchOut);
1094 auto_punch_location_changed (0);
1099 if (location == 0) {
1103 if (location->end() <= location->start()) {
1104 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1108 auto_punch_start_changed_connection.disconnect();
1109 auto_punch_end_changed_connection.disconnect();
1110 auto_punch_changed_connection.disconnect();
1112 auto_punch_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_punch_start_changed));
1113 auto_punch_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_punch_end_changed));
1114 auto_punch_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_punch_changed));
1116 location->set_auto_punch (true, this);
1117 auto_punch_location_changed (location);
1121 Session::set_punch_in (bool yn)
1123 if (punch_in == yn) {
1129 if ((location = _locations.auto_punch_location()) != 0) {
1130 if ((punch_in = yn) == true) {
1131 replace_event (Event::PunchIn, location->start());
1133 remove_event (location->start(), Event::PunchIn);
1138 ControlChanged (PunchIn); /* EMIT SIGNAL */
1142 Session::set_punch_out (bool yn)
1144 if (punch_out == yn) {
1150 if ((location = _locations.auto_punch_location()) != 0) {
1151 if ((punch_out = yn) == true) {
1152 replace_event (Event::PunchOut, location->end());
1154 clear_events (Event::PunchOut);
1159 ControlChanged (PunchOut); /* EMIT SIGNAL */
1163 Session::set_auto_loop_location (Location* location)
1167 if ((existing = _locations.auto_loop_location()) != 0 && existing != location) {
1168 auto_loop_start_changed_connection.disconnect();
1169 auto_loop_end_changed_connection.disconnect();
1170 auto_loop_changed_connection.disconnect();
1171 existing->set_auto_loop (false, this);
1172 remove_event (existing->end(), Event::AutoLoop);
1173 auto_loop_location_changed (0);
1178 if (location == 0) {
1182 if (location->end() <= location->start()) {
1183 error << _("Session: you can't use a mark for auto loop") << endmsg;
1187 last_loopend = location->end();
1189 auto_loop_start_changed_connection.disconnect();
1190 auto_loop_end_changed_connection.disconnect();
1191 auto_loop_changed_connection.disconnect();
1193 auto_loop_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1194 auto_loop_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1195 auto_loop_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_loop_changed));
1197 location->set_auto_loop (true, this);
1198 auto_loop_location_changed (location);
1202 Session::locations_added (Location* ignored)
1208 Session::locations_changed ()
1210 _locations.apply (*this, &Session::handle_locations_changed);
1214 Session::handle_locations_changed (Locations::LocationList& locations)
1216 Locations::LocationList::iterator i;
1218 bool set_loop = false;
1219 bool set_punch = false;
1221 for (i = locations.begin(); i != locations.end(); ++i) {
1225 if (location->is_auto_punch()) {
1226 set_auto_punch_location (location);
1229 if (location->is_auto_loop()) {
1230 set_auto_loop_location (location);
1237 set_auto_loop_location (0);
1240 set_auto_punch_location (0);
1247 Session::enable_record ()
1249 /* XXX really atomic compare+swap here */
1250 if (atomic_read (&_record_status) != Recording) {
1251 atomic_set (&_record_status, Recording);
1252 _last_record_location = _transport_frame;
1253 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordStrobe);
1255 if (Config->get_use_hardware_monitoring() && auto_input) {
1256 /* Even though this can be called from RT context we are using
1257 a non-tentative rwlock here, because the action must occur.
1258 The rarity and short potential lock duration makes this "OK"
1260 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1262 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1263 if ((*i)->record_enabled ()) {
1264 (*i)->monitor_input (true);
1269 RecordStateChanged ();
1274 Session::disable_record (bool rt_context, bool force)
1278 if ((rs = (RecordState) atomic_read (&_record_status)) != Disabled) {
1280 if (!Config->get_latched_record_enable () || force) {
1281 atomic_set (&_record_status, Disabled);
1283 if (rs == Recording) {
1284 atomic_set (&_record_status, Enabled);
1288 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordExit);
1290 if (Config->get_use_hardware_monitoring() && auto_input) {
1291 /* Even though this can be called from RT context we are using
1292 a non-tentative rwlock here, because the action must occur.
1293 The rarity and short potential lock duration makes this "OK"
1295 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1297 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1298 if ((*i)->record_enabled ()) {
1299 (*i)->monitor_input (false);
1304 RecordStateChanged (); /* emit signal */
1307 remove_pending_capture_state ();
1313 Session::step_back_from_record ()
1315 atomic_set (&_record_status, Enabled);
1317 if (Config->get_use_hardware_monitoring()) {
1318 /* Even though this can be called from RT context we are using
1319 a non-tentative rwlock here, because the action must occur.
1320 The rarity and short potential lock duration makes this "OK"
1322 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1324 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1325 if (auto_input && (*i)->record_enabled ()) {
1326 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
1327 (*i)->monitor_input (false);
1334 Session::maybe_enable_record ()
1336 atomic_set (&_record_status, Enabled);
1338 /* XXX this save should really happen in another thread. its needed so that
1339 pending capture state can be recovered if we crash.
1342 save_state ("", true);
1344 if (_transport_speed) {
1349 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordPause);
1350 RecordStateChanged (); /* EMIT SIGNAL */
1357 Session::audible_frame () const
1360 jack_nframes_t offset;
1363 /* the first of these two possible settings for "offset"
1364 mean that the audible frame is stationary until
1365 audio emerges from the latency compensation
1368 the second means that the audible frame is stationary
1369 until audio would emerge from a physical port
1370 in the absence of any plugin latency compensation
1373 offset = _worst_output_latency;
1375 if (offset > current_block_size) {
1376 offset -= current_block_size;
1378 /* XXX is this correct? if we have no external
1379 physical connections and everything is internal
1380 then surely this is zero? still, how
1381 likely is that anyway?
1383 offset = current_block_size;
1386 if (synced_to_jack()) {
1387 tf = _engine.transport_frame();
1389 tf = _transport_frame;
1392 if (_transport_speed == 0) {
1402 if (!non_realtime_work_pending()) {
1406 /* take latency into account */
1415 Session::set_frame_rate (jack_nframes_t frames_per_second)
1417 /** \fn void Session::set_frame_size(jack_nframes_t)
1418 the AudioEngine object that calls this guarantees
1419 that it will not be called while we are also in
1420 ::process(). Its fine to do things that block
1424 _current_frame_rate = frames_per_second;
1425 _frames_per_smpte_frame = (double) _current_frame_rate / (double) smpte_frames_per_second;
1427 Route::set_automation_interval ((jack_nframes_t) ceil ((double) frames_per_second * 0.25));
1431 /* XXX need to reset/reinstantiate all LADSPA plugins */
1435 Session::set_block_size (jack_nframes_t nframes)
1437 /* the AudioEngine guarantees
1438 that it will not be called while we are also in
1439 ::process(). It is therefore fine to do things that block
1444 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1445 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1446 vector<Sample*>::iterator i;
1449 current_block_size = nframes;
1451 for (np = 0, i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i, ++np) {
1455 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
1459 _passthru_buffers.clear ();
1460 _silent_buffers.clear ();
1462 ensure_passthru_buffers (np);
1464 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
1468 #ifdef NO_POSIX_MEMALIGN
1469 buf = (Sample *) malloc(current_block_size * sizeof(Sample));
1471 posix_memalign((void **)&buf,16,current_block_size * 4);
1475 memset (*i, 0, sizeof (Sample) * current_block_size);
1479 if (_gain_automation_buffer) {
1480 delete [] _gain_automation_buffer;
1482 _gain_automation_buffer = new gain_t[nframes];
1484 allocate_pan_automation_buffers (nframes, _npan_buffers, true);
1486 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1487 (*i)->set_block_size (nframes);
1490 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1491 (*i)->set_block_size (nframes);
1494 set_worst_io_latencies (false);
1499 Session::set_default_fade (float steepness, float fade_msecs)
1502 jack_nframes_t fade_frames;
1504 /* Don't allow fade of less 1 frame */
1506 if (fade_msecs < (1000.0 * (1.0/_current_frame_rate))) {
1513 fade_frames = (jack_nframes_t) floor (fade_msecs * _current_frame_rate * 0.001);
1517 default_fade_msecs = fade_msecs;
1518 default_fade_steepness = steepness;
1521 // jlc, WTF is this!
1522 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1523 AudioRegion::set_default_fade (steepness, fade_frames);
1528 /* XXX have to do this at some point */
1529 /* foreach region using default fade, reset, then
1530 refill_all_diskstream_buffers ();
1535 struct RouteSorter {
1536 bool operator() (Route* r1, Route* r2) {
1537 if (r1->fed_by.find (r2) != r1->fed_by.end()) {
1539 } else if (r2->fed_by.find (r1) != r2->fed_by.end()) {
1542 if (r1->fed_by.empty()) {
1543 if (r2->fed_by.empty()) {
1544 /* no ardour-based connections inbound to either route. just use signal order */
1545 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1547 /* r2 has connections, r1 does not; run r1 early */
1551 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1558 trace_terminal (Route* r1, Route* rbase)
1562 if ((r1->fed_by.find (rbase) != r1->fed_by.end()) && (rbase->fed_by.find (r1) != rbase->fed_by.end())) {
1563 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
1567 /* make a copy of the existing list of routes that feed r1 */
1569 set<Route *> existing = r1->fed_by;
1571 /* for each route that feeds r1, recurse, marking it as feeding
1575 for (set<Route *>::iterator i = existing.begin(); i != existing.end(); ++i) {
1578 /* r2 is a route that feeds r1 which somehow feeds base. mark
1579 base as being fed by r2
1582 rbase->fed_by.insert (r2);
1586 /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
1590 if ((r1->fed_by.find (r2) != r1->fed_by.end()) && (r2->fed_by.find (r1) != r2->fed_by.end())) {
1594 /* now recurse, so that we can mark base as being fed by
1595 all routes that feed r2
1598 trace_terminal (r2, rbase);
1605 Session::resort_routes (void* src)
1607 /* don't do anything here with signals emitted
1608 by Routes while we are being destroyed.
1611 if (_state_of_the_state & Deletion) {
1615 /* Caller MUST hold the route_lock */
1617 RouteList::iterator i, j;
1619 for (i = routes.begin(); i != routes.end(); ++i) {
1621 (*i)->fed_by.clear ();
1623 for (j = routes.begin(); j != routes.end(); ++j) {
1625 /* although routes can feed themselves, it will
1626 cause an endless recursive descent if we
1627 detect it. so don't bother checking for
1635 if ((*j)->feeds (*i)) {
1636 (*i)->fed_by.insert (*j);
1641 for (i = routes.begin(); i != routes.end(); ++i) {
1642 trace_terminal (*i, *i);
1649 cerr << "finished route resort\n";
1651 for (i = routes.begin(); i != routes.end(); ++i) {
1652 cerr << " " << (*i)->name() << " signal order = " << (*i)->order_key ("signal") << endl;
1660 Session::new_audio_track (int input_channels, int output_channels, TrackMode mode)
1663 char track_name[32];
1665 uint32_t channels_used = 0;
1667 uint32_t nphysical_in;
1668 uint32_t nphysical_out;
1670 /* count existing audio tracks */
1673 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1674 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1675 if (dynamic_cast<AudioTrack*>(*i) != 0) {
1676 if (!(*i)->hidden()) {
1678 channels_used += (*i)->n_inputs();
1684 /* check for duplicate route names, since we might have pre-existing
1685 routes with this name (e.g. create Audio1, Audio2, delete Audio1,
1686 save, close,restart,add new route - first named route is now
1691 snprintf (track_name, sizeof(track_name), "Audio %" PRIu32, n+1);
1692 if (route_by_name (track_name) == 0) {
1697 } while (n < (UINT_MAX-1));
1699 if (input_auto_connect & AutoConnectPhysical) {
1700 nphysical_in = n_physical_inputs;
1705 if (output_auto_connect & AutoConnectPhysical) {
1706 nphysical_out = n_physical_outputs;
1712 track = new AudioTrack (*this, track_name, Route::Flag (0), mode);
1714 if (track->ensure_io (input_channels, output_channels, false, this)) {
1715 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1716 input_channels, output_channels)
1721 for (uint32_t x = 0; x < track->n_inputs() && x < nphysical_in; ++x) {
1725 if (input_auto_connect & AutoConnectPhysical) {
1726 port = _engine.get_nth_physical_input ((channels_used+x)%nphysical_in);
1729 if (port.length() && track->connect_input (track->input (x), port, this)) {
1735 for (uint32_t x = 0; x < track->n_outputs(); ++x) {
1739 if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
1740 port = _engine.get_nth_physical_output ((channels_used+x)%nphysical_out);
1741 } else if (output_auto_connect & AutoConnectMaster) {
1743 port = _master_out->input (x%_master_out->n_inputs())->name();
1747 if (port.length() && track->connect_output (track->output (x), port, this)) {
1753 vector<string> cports;
1754 uint32_t ni = _control_out->n_inputs();
1756 for (n = 0; n < ni; ++n) {
1757 cports.push_back (_control_out->input(n)->name());
1760 track->set_control_outs (cports);
1763 track->diskstream_changed.connect (mem_fun (this, &Session::resort_routes));
1767 track->set_remote_control_id (ntracks());
1770 catch (failed_constructor &err) {
1771 error << _("Session: could not create new audio track.") << endmsg;
1779 Session::new_audio_route (int input_channels, int output_channels)
1786 /* count existing audio busses */
1789 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1790 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1791 if (dynamic_cast<AudioTrack*>(*i) == 0) {
1792 if (!(*i)->hidden()) {
1800 snprintf (bus_name, sizeof(bus_name), "Bus %" PRIu32, n+1);
1801 if (route_by_name (bus_name) == 0) {
1806 } while (n < (UINT_MAX-1));
1809 bus = new Route (*this, bus_name, -1, -1, -1, -1);
1811 if (bus->ensure_io (input_channels, output_channels, false, this)) {
1812 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1813 input_channels, output_channels)
1817 for (uint32_t x = 0; x < bus->n_inputs(); ++x) {
1821 if (input_auto_connect & AutoConnectPhysical) {
1822 port = _engine.get_nth_physical_input ((n+x)%n_physical_inputs);
1825 if (port.length() && bus->connect_input (bus->input (x), port, this)) {
1830 for (uint32_t x = 0; x < bus->n_outputs(); ++x) {
1834 if (output_auto_connect & AutoConnectPhysical) {
1835 port = _engine.get_nth_physical_input ((n+x)%n_physical_outputs);
1836 } else if (output_auto_connect & AutoConnectMaster) {
1838 port = _master_out->input (x%_master_out->n_inputs())->name();
1842 if (port.length() && bus->connect_output (bus->output (x), port, this)) {
1848 vector<string> cports;
1849 uint32_t ni = _control_out->n_inputs();
1851 for (uint32_t n = 0; n < ni; ++n) {
1852 cports.push_back (_control_out->input(n)->name());
1854 bus->set_control_outs (cports);
1860 catch (failed_constructor &err) {
1861 error << _("Session: could not create new route.") << endmsg;
1869 Session::add_route (Route* route)
1872 RWLockMonitor lm (route_lock, true, __LINE__, __FILE__);
1873 routes.push_front (route);
1877 route->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), route));
1878 route->mute_changed.connect (mem_fun (*this, &Session::route_mute_changed));
1879 route->output_changed.connect (mem_fun (*this, &Session::set_worst_io_latencies_x));
1880 route->redirects_changed.connect (mem_fun (*this, &Session::update_latency_compensation_proxy));
1882 if (route->master()) {
1883 _master_out = route;
1886 if (route->control()) {
1887 _control_out = route;
1890 AudioTrack* at = dynamic_cast<AudioTrack*>(route);
1891 if (at && at->mode() == Destructive) {
1892 destructive_index++;
1896 save_state (_current_snapshot_name);
1898 RouteAdded (route); /* EMIT SIGNAL */
1902 Session::add_diskstream (DiskStream* dstream)
1904 /* need to do this in case we're rolling at the time, to prevent false underruns */
1905 dstream->do_refill(0, 0, 0);
1908 RWLockMonitor lm (diskstream_lock, true, __LINE__, __FILE__);
1909 diskstreams.push_back (dstream);
1912 /* take a reference to the diskstream, preventing it from
1913 ever being deleted until the session itself goes away,
1914 or chooses to remove it for its own purposes.
1918 dstream->set_block_size (current_block_size);
1920 dstream->PlaylistChanged.connect (sigc::bind (mem_fun (*this, &Session::diskstream_playlist_changed), dstream));
1921 /* this will connect to future changes, and check the current length */
1922 diskstream_playlist_changed (dstream);
1924 dstream->prepare ();
1927 save_state (_current_snapshot_name);
1929 DiskStreamAdded (dstream); /* EMIT SIGNAL */
1933 Session::remove_route (Route& route)
1936 RWLockMonitor lm (route_lock, true, __LINE__, __FILE__);
1937 routes.remove (&route);
1939 /* deleting the master out seems like a dumb
1940 idea, but its more of a UI policy issue
1944 if (&route == _master_out) {
1948 if (&route == _control_out) {
1951 /* cancel control outs for all routes */
1953 vector<string> empty;
1955 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1956 (*r)->set_control_outs (empty);
1960 update_route_solo_state ();
1964 RWLockMonitor lm (diskstream_lock, true, __LINE__, __FILE__);
1968 if ((at = dynamic_cast<AudioTrack*>(&route)) != 0) {
1969 diskstreams.remove (&at->disk_stream());
1970 at->disk_stream().unref ();
1973 find_current_end ();
1976 update_latency_compensation (false, false);
1979 /* XXX should we disconnect from the Route's signals ? */
1981 save_state (_current_snapshot_name);
1987 Session::route_mute_changed (void* src)
1993 Session::route_solo_changed (void* src, Route* route)
1995 if (solo_update_disabled) {
2000 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2003 is_track = (dynamic_cast<AudioTrack*>(route) != 0);
2005 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2007 /* soloing a track mutes all other tracks, soloing a bus mutes all other busses */
2011 /* don't mess with busses */
2013 if (dynamic_cast<AudioTrack*>(*i) == 0) {
2019 /* don't mess with tracks */
2021 if (dynamic_cast<AudioTrack*>(*i) != 0) {
2026 if ((*i) != route &&
2027 ((*i)->mix_group () == 0 ||
2028 (*i)->mix_group () != route->mix_group () ||
2029 !route->mix_group ()->is_active())) {
2031 if ((*i)->soloed()) {
2033 /* if its already soloed, and solo latching is enabled,
2034 then leave it as it is.
2037 if (_solo_latched) {
2044 solo_update_disabled = true;
2045 (*i)->set_solo (false, src);
2046 solo_update_disabled = false;
2050 bool something_soloed = false;
2051 bool same_thing_soloed = false;
2052 bool signal = false;
2054 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2055 if ((*i)->soloed()) {
2056 something_soloed = true;
2057 if (dynamic_cast<AudioTrack*>(*i)) {
2059 same_thing_soloed = true;
2064 same_thing_soloed = true;
2072 if (something_soloed != currently_soloing) {
2074 currently_soloing = something_soloed;
2077 modify_solo_mute (is_track, same_thing_soloed);
2080 SoloActive (currently_soloing);
2087 Session::set_solo_latched (bool yn)
2089 if (yn != _solo_latched) {
2092 ControlChanged (SoloLatch);
2097 Session::update_route_solo_state ()
2100 bool is_track = false;
2101 bool signal = false;
2103 /* caller must hold RouteLock */
2105 /* this is where we actually implement solo by changing
2106 the solo mute setting of each track.
2109 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2110 if ((*i)->soloed()) {
2112 if (dynamic_cast<AudioTrack*>(*i)) {
2119 if (mute != currently_soloing) {
2121 currently_soloing = mute;
2124 if (!is_track && !mute) {
2126 /* nothing is soloed */
2128 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2129 (*i)->set_solo_mute (false);
2139 modify_solo_mute (is_track, mute);
2142 SoloActive (currently_soloing);
2147 Session::modify_solo_mute (bool is_track, bool mute)
2149 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2153 /* only alter track solo mute */
2155 if (dynamic_cast<AudioTrack*>(*i)) {
2156 if ((*i)->soloed()) {
2157 (*i)->set_solo_mute (!mute);
2159 (*i)->set_solo_mute (mute);
2165 /* only alter bus solo mute */
2167 if (!dynamic_cast<AudioTrack*>(*i)) {
2169 if ((*i)->soloed()) {
2171 (*i)->set_solo_mute (false);
2175 /* don't mute master or control outs
2176 in response to another bus solo
2179 if ((*i) != _master_out &&
2180 (*i) != _control_out) {
2181 (*i)->set_solo_mute (mute);
2192 Session::catch_up_on_solo ()
2194 /* this is called after set_state() to catch the full solo
2195 state, which can't be correctly determined on a per-route
2196 basis, but needs the global overview that only the session
2199 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2200 update_route_solo_state();
2204 Session::route_by_name (string name)
2206 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2208 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2209 if ((*i)->name() == name) {
2218 Session::route_by_remote_id (uint32_t id)
2220 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2222 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2223 if ((*i)->remote_control_id() == id) {
2232 Session::find_current_end ()
2234 if (_state_of_the_state & Loading) {
2238 jack_nframes_t max = get_maximum_extent ();
2240 if (max > end_location->end()) {
2241 end_location->set_end (max);
2243 DurationChanged(); /* EMIT SIGNAL */
2248 Session::get_maximum_extent () const
2250 jack_nframes_t max = 0;
2253 /* Don't take the diskstream lock. Caller must have other ways to
2257 for (DiskStreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2258 Playlist* pl = (*i)->playlist();
2259 if ((me = pl->get_maximum_extent()) > max) {
2268 Session::diskstream_by_name (string name)
2270 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2272 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2273 if ((*i)->name() == name) {
2282 Session::diskstream_by_id (id_t id)
2284 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2286 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2287 if ((*i)->id() == id) {
2295 /* AudioRegion management */
2298 Session::new_region_name (string old)
2300 string::size_type last_period;
2302 string::size_type len = old.length() + 64;
2305 if ((last_period = old.find_last_of ('.')) == string::npos) {
2307 /* no period present - add one explicitly */
2310 last_period = old.length() - 1;
2315 number = atoi (old.substr (last_period+1).c_str());
2319 while (number < (UINT_MAX-1)) {
2321 AudioRegionList::const_iterator i;
2326 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
2329 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2330 if ((*i).second->name() == sbuf) {
2335 if (i == audio_regions.end()) {
2340 if (number != (UINT_MAX-1)) {
2344 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
2349 Session::region_name (string& result, string base, bool newlevel) const
2356 LockMonitor lm (region_lock, __LINE__, __FILE__);
2358 snprintf (buf, sizeof (buf), "%d", (int)audio_regions.size() + 1);
2366 /* XXX this is going to be slow. optimize me later */
2371 string::size_type pos;
2373 pos = base.find_last_of ('.');
2375 /* pos may be npos, but then we just use entire base */
2377 subbase = base.substr (0, pos);
2381 bool name_taken = true;
2384 LockMonitor lm (region_lock, __LINE__, __FILE__);
2386 for (int n = 1; n < 5000; ++n) {
2389 snprintf (buf, sizeof (buf), ".%d", n);
2394 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2395 if ((*i).second->name() == result) {
2408 fatal << string_compose(_("too many regions with names like %1"), base) << endmsg;
2416 Session::add_region (Region* region)
2418 AudioRegion* ar = 0;
2419 AudioRegion* oar = 0;
2423 LockMonitor lm (region_lock, __LINE__, __FILE__);
2425 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2427 AudioRegionList::iterator x;
2429 for (x = audio_regions.begin(); x != audio_regions.end(); ++x) {
2431 oar = dynamic_cast<AudioRegion*> (x->second);
2433 if (ar->region_list_equivalent (*oar)) {
2438 if (x == audio_regions.end()) {
2440 pair<AudioRegionList::key_type, AudioRegionList::mapped_type> entry;
2442 entry.first = region->id();
2445 pair<AudioRegionList::iterator,bool> x = audio_regions.insert (entry);
2456 fatal << _("programming error: ")
2457 << X_("unknown region type passed to Session::add_region()")
2464 /* mark dirty because something has changed even if we didn't
2465 add the region to the region list.
2471 region->GoingAway.connect (mem_fun (*this, &Session::remove_region));
2472 region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), region));
2473 AudioRegionAdded (ar); /* EMIT SIGNAL */
2478 Session::region_changed (Change what_changed, Region* region)
2480 if (what_changed & Region::HiddenChanged) {
2481 /* relay hidden changes */
2482 RegionHiddenChange (region);
2487 Session::region_renamed (Region* region)
2489 add_region (region);
2493 Session::remove_region (Region* region)
2495 AudioRegionList::iterator i;
2496 AudioRegion* ar = 0;
2497 bool removed = false;
2500 LockMonitor lm (region_lock, __LINE__, __FILE__);
2502 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2503 if ((i = audio_regions.find (region->id())) != audio_regions.end()) {
2504 audio_regions.erase (i);
2508 fatal << _("programming error: ")
2509 << X_("unknown region type passed to Session::remove_region()")
2515 /* mark dirty because something has changed even if we didn't
2516 remove the region from the region list.
2522 AudioRegionRemoved(ar); /* EMIT SIGNAL */
2527 Session::find_whole_file_parent (AudioRegion& child)
2529 AudioRegionList::iterator i;
2530 AudioRegion* region;
2531 LockMonitor lm (region_lock, __LINE__, __FILE__);
2533 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2535 region = (*i).second;
2537 if (region->whole_file()) {
2539 if (child.source_equivalent (*region)) {
2549 Session::find_equivalent_playlist_regions (AudioRegion& region, vector<AudioRegion*>& result)
2551 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2555 if ((pl = dynamic_cast<AudioPlaylist*>(*i)) == 0) {
2559 pl->get_region_list_equivalent_regions (region, result);
2564 Session::destroy_region (Region* region)
2566 AudioRegion* aregion;
2568 if ((aregion = dynamic_cast<AudioRegion*> (region)) == 0) {
2572 if (aregion->playlist()) {
2573 aregion->playlist()->destroy_region (region);
2576 vector<Source*> srcs;
2578 for (uint32_t n = 0; n < aregion->n_channels(); ++n) {
2579 srcs.push_back (&aregion->source (n));
2582 for (vector<Source*>::iterator i = srcs.begin(); i != srcs.end(); ++i) {
2584 if ((*i)->use_cnt() == 0) {
2585 (*i)->mark_for_remove ();
2594 Session::destroy_regions (list<Region*> regions)
2596 for (list<Region*>::iterator i = regions.begin(); i != regions.end(); ++i) {
2597 destroy_region (*i);
2603 Session::remove_last_capture ()
2607 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2609 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2610 list<Region*>& l = (*i)->last_capture_regions();
2613 r.insert (r.end(), l.begin(), l.end());
2618 destroy_regions (r);
2623 Session::remove_region_from_region_list (Region& r)
2629 /* Source Management */
2632 Session::add_source (Source* source)
2634 pair<SourceList::key_type, SourceList::mapped_type> entry;
2637 LockMonitor lm (source_lock, __LINE__, __FILE__);
2638 entry.first = source->id();
2639 entry.second = source;
2640 sources.insert (entry);
2643 source->GoingAway.connect (mem_fun (this, &Session::remove_source));
2646 SourceAdded (source); /* EMIT SIGNAL */
2650 Session::remove_source (Source* source)
2652 SourceList::iterator i;
2655 LockMonitor lm (source_lock, __LINE__, __FILE__);
2657 if ((i = sources.find (source->id())) != sources.end()) {
2662 if (!_state_of_the_state & InCleanup) {
2664 /* save state so we don't end up with a session file
2665 referring to non-existent sources.
2668 save_state (_current_snapshot_name);
2671 SourceRemoved(source); /* EMIT SIGNAL */
2675 Session::get_source (ARDOUR::id_t id)
2677 LockMonitor lm (source_lock, __LINE__, __FILE__);
2678 SourceList::iterator i;
2681 if ((i = sources.find (id)) != sources.end()) {
2682 source = (*i).second;
2689 Session::peak_path_from_audio_path (string audio_path)
2691 /* XXX hardly bombproof! fix me */
2695 res = PBD::dirname (audio_path);
2696 res = PBD::dirname (res);
2698 res += peak_dir_name;
2700 res += PBD::basename_nosuffix (audio_path);
2707 Session::change_audio_path_by_name (string path, string oldname, string newname, bool destructive)
2710 string old_basename = basename_nosuffix (oldname);
2711 string new_legalized = legalize_for_path (newname);
2713 /* note: we know (or assume) the old path is already valid */
2717 /* destructive file sources have a name of the form:
2719 /path/to/Tnnnn-NAME(%[LR])?.wav
2721 the task here is to replace NAME with the new name.
2724 /* find last slash */
2728 string::size_type slash;
2729 string::size_type dash;
2731 if ((slash = path.find_last_of ('/')) == string::npos) {
2735 dir = path.substr (0, slash+1);
2737 /* '-' is not a legal character for the NAME part of the path */
2739 if ((dash = path.find_last_of ('-')) == string::npos) {
2743 prefix = path.substr (slash+1, dash-(slash+1));
2748 path += new_legalized;
2749 path += ".wav"; /* XXX gag me with a spoon */
2753 /* non-destructive file sources have a name of the form:
2755 /path/to/NAME-nnnnn(%[LR])?.wav
2757 the task here is to replace NAME with the new name.
2760 /* find last slash */
2764 string::size_type slash;
2765 string::size_type dash;
2767 if ((slash = path.find_last_of ('/')) == string::npos) {
2771 dir = path.substr (0, slash+1);
2773 /* '-' is not a legal character for the NAME part of the path */
2775 if ((dash = path.find_last_of ('-')) == string::npos) {
2779 suffix = path.substr (dash);
2782 path += new_legalized;
2790 Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool destructive)
2794 char buf[PATH_MAX+1];
2795 const uint32_t limit = 10000;
2799 legalized = legalize_for_path (name);
2801 /* find a "version" of the file name that doesn't exist in
2802 any of the possible directories.
2805 for (cnt = (destructive ? destructive_index + 1 : 1); cnt <= limit; ++cnt) {
2807 vector<space_and_path>::iterator i;
2808 uint32_t existing = 0;
2810 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2815 spath += tape_dir_name;
2817 spath += sound_dir_name;
2822 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2823 } else if (nchan == 2) {
2825 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%L.wav", spath.c_str(), cnt, legalized.c_str());
2827 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%R.wav", spath.c_str(), cnt, legalized.c_str());
2829 } else if (nchan < 26) {
2830 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%%c.wav", spath.c_str(), cnt, legalized.c_str(), 'a' + chan);
2832 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2840 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2841 } else if (nchan == 2) {
2843 snprintf (buf, sizeof(buf), "%s-%u%%L.wav", spath.c_str(), cnt);
2845 snprintf (buf, sizeof(buf), "%s-%u%%R.wav", spath.c_str(), cnt);
2847 } else if (nchan < 26) {
2848 snprintf (buf, sizeof(buf), "%s-%u%%%c.wav", spath.c_str(), cnt, 'a' + chan);
2850 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2854 if (access (buf, F_OK) == 0) {
2859 if (existing == 0) {
2864 error << string_compose(_("There are already %1 recordings for %2, which I consider too many."), limit, name) << endmsg;
2865 throw failed_constructor();
2869 /* we now have a unique name for the file, but figure out where to
2876 spath = tape_dir ();
2878 spath = discover_best_sound_dir ();
2881 string::size_type pos = foo.find_last_of ('/');
2883 if (pos == string::npos) {
2886 spath += foo.substr (pos + 1);
2893 Session::create_file_source (DiskStream& ds, int32_t chan, bool destructive)
2895 string spath = audio_path_from_name (ds.name(), ds.n_channels(), chan, destructive);
2897 /* this might throw failed_constructor(), which is OK */
2900 return new DestructiveFileSource (spath, frame_rate(), false, Config->get_native_file_data_format());
2902 return new FileSource (spath, frame_rate(), false, Config->get_native_file_data_format());
2906 /* Playlist management */
2909 Session::get_playlist (string name)
2913 if ((ret = playlist_by_name (name)) == 0) {
2914 ret = new AudioPlaylist (*this, name);
2921 Session::playlist_by_name (string name)
2923 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2924 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2925 if ((*i)->name() == name) {
2929 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
2930 if ((*i)->name() == name) {
2938 Session::add_playlist (Playlist* playlist)
2940 if (playlist->hidden()) {
2945 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2946 if (find (playlists.begin(), playlists.end(), playlist) == playlists.end()) {
2947 playlists.insert (playlists.begin(), playlist);
2949 playlist->InUse.connect (mem_fun (*this, &Session::track_playlist));
2950 playlist->GoingAway.connect (mem_fun (*this, &Session::remove_playlist));
2956 PlaylistAdded (playlist); /* EMIT SIGNAL */
2960 Session::track_playlist (Playlist* pl, bool inuse)
2962 PlaylistList::iterator x;
2965 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2968 //cerr << "shifting playlist to unused: " << pl->name() << endl;
2970 unused_playlists.insert (pl);
2972 if ((x = playlists.find (pl)) != playlists.end()) {
2973 playlists.erase (x);
2978 //cerr << "shifting playlist to used: " << pl->name() << endl;
2980 playlists.insert (pl);
2982 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
2983 unused_playlists.erase (x);
2990 Session::remove_playlist (Playlist* playlist)
2992 if (_state_of_the_state & Deletion) {
2997 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2998 // cerr << "removing playlist: " << playlist->name() << endl;
3000 PlaylistList::iterator i;
3002 i = find (playlists.begin(), playlists.end(), playlist);
3004 if (i != playlists.end()) {
3005 playlists.erase (i);
3008 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
3009 if (i != unused_playlists.end()) {
3010 unused_playlists.erase (i);
3017 PlaylistRemoved (playlist); /* EMIT SIGNAL */
3021 Session::set_audition (AudioRegion* r)
3023 pending_audition_region = r;
3024 post_transport_work = PostTransportWork (post_transport_work | PostTransportAudition);
3025 schedule_butler_transport_work ();
3029 Session::non_realtime_set_audition ()
3031 if (pending_audition_region == (AudioRegion*) 0xfeedface) {
3032 auditioner->audition_current_playlist ();
3033 } else if (pending_audition_region) {
3034 auditioner->audition_region (*pending_audition_region);
3036 pending_audition_region = 0;
3037 AuditionActive (true); /* EMIT SIGNAL */
3041 Session::audition_playlist ()
3043 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3044 ev->set_ptr ((void*) 0xfeedface);
3049 Session::audition_region (AudioRegion& r)
3051 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3057 Session::cancel_audition ()
3059 if (auditioner->active()) {
3060 auditioner->cancel_audition ();
3061 AuditionActive (false); /* EMIT SIGNAL */
3066 Session::RoutePublicOrderSorter::operator() (Route* a, Route* b)
3068 return a->order_key(N_("signal")) < b->order_key(N_("signal"));
3072 Session::remove_empty_sounds ()
3075 PathScanner scanner;
3080 vector<string *>* possible_audiofiles = scanner (dir, "\\.wav$", false, true);
3082 for (vector<string *>::iterator i = possible_audiofiles->begin(); i != possible_audiofiles->end(); ++i) {
3084 if (FileSource::is_empty (*(*i))) {
3086 unlink ((*i)->c_str());
3088 string peak_path = peak_path_from_audio_path (**i);
3089 unlink (peak_path.c_str());
3095 delete possible_audiofiles;
3099 Session::is_auditioning () const
3101 /* can be called before we have an auditioner object */
3103 return auditioner->active();
3110 Session::set_all_solo (bool yn)
3113 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3115 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3116 if (!(*i)->hidden()) {
3117 (*i)->set_solo (yn, this);
3126 Session::set_all_mute (bool yn)
3129 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3131 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3132 if (!(*i)->hidden()) {
3133 (*i)->set_mute (yn, this);
3142 Session::n_diskstreams () const
3144 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
3147 for (DiskStreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3148 if (!(*i)->hidden()) {
3156 Session::foreach_diskstream (void (DiskStream::*func)(void))
3158 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
3159 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3160 if (!(*i)->hidden()) {
3167 Session::graph_reordered ()
3169 /* don't do this stuff if we are setting up connections
3170 from a set_state() call.
3173 if (_state_of_the_state & InitialConnecting) {
3177 RWLockMonitor lm1 (route_lock, true, __LINE__, __FILE__);
3178 RWLockMonitor lm2 (diskstream_lock, false, __LINE__, __FILE__);
3182 /* force all diskstreams to update their capture offset values to
3183 reflect any changes in latencies within the graph.
3186 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3187 (*i)->set_capture_offset ();
3192 Session::record_disenable_all ()
3194 record_enable_change_all (false);
3198 Session::record_enable_all ()
3200 record_enable_change_all (true);
3204 Session::record_enable_change_all (bool yn)
3206 RWLockMonitor lm1 (route_lock, false, __LINE__, __FILE__);
3208 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3211 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
3212 at->set_record_enable (yn, this);
3216 /* since we don't keep rec-enable state, don't mark session dirty */
3220 Session::add_redirect (Redirect* redirect)
3224 PortInsert* port_insert;
3225 PluginInsert* plugin_insert;
3227 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3228 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3229 _port_inserts.insert (_port_inserts.begin(), port_insert);
3230 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3231 _plugin_inserts.insert (_plugin_inserts.begin(), plugin_insert);
3233 fatal << _("programming error: unknown type of Insert created!") << endmsg;
3236 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3237 _sends.insert (_sends.begin(), send);
3239 fatal << _("programming error: unknown type of Redirect created!") << endmsg;
3243 redirect->GoingAway.connect (mem_fun (*this, &Session::remove_redirect));
3249 Session::remove_redirect (Redirect* redirect)
3253 PortInsert* port_insert;
3254 PluginInsert* plugin_insert;
3256 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3257 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3258 _port_inserts.remove (port_insert);
3259 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3260 _plugin_inserts.remove (plugin_insert);
3262 fatal << _("programming error: unknown type of Insert deleted!") << endmsg;
3265 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3266 _sends.remove (send);
3268 fatal << _("programming error: unknown type of Redirect deleted!") << endmsg;
3276 Session::available_capture_duration ()
3278 const double scale = 4096.0 / sizeof (Sample);
3280 if (_total_free_4k_blocks * scale > (double) max_frames) {
3284 return (jack_nframes_t) floor (_total_free_4k_blocks * scale);
3288 Session::add_connection (ARDOUR::Connection* connection)
3291 LockMonitor (connection_lock, __LINE__, __FILE__);
3292 _connections.push_back (connection);
3295 ConnectionAdded (connection); /* EMIT SIGNAL */
3301 Session::remove_connection (ARDOUR::Connection* connection)
3303 bool removed = false;
3306 LockMonitor (connection_lock, __LINE__, __FILE__);
3307 ConnectionList::iterator i = find (_connections.begin(), _connections.end(), connection);
3309 if (i != _connections.end()) {
3310 _connections.erase (i);
3316 ConnectionRemoved (connection); /* EMIT SIGNAL */
3322 ARDOUR::Connection *
3323 Session::connection_by_name (string name) const
3325 LockMonitor lm (connection_lock, __LINE__, __FILE__);
3327 for (ConnectionList::const_iterator i = _connections.begin(); i != _connections.end(); ++i) {
3328 if ((*i)->name() == name) {
3337 Session::set_edit_mode (EditMode mode)
3342 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3344 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3345 (*i)->set_edit_mode (mode);
3350 ControlChanged (EditingMode); /* EMIT SIGNAL */
3354 Session::tempo_map_changed (Change ignored)
3361 Session::ensure_passthru_buffers (uint32_t howmany)
3363 while (howmany > _passthru_buffers.size()) {
3365 #ifdef NO_POSIX_MEMALIGN
3366 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3368 posix_memalign((void **)&p,16,current_block_size * 4);
3370 _passthru_buffers.push_back (p);
3374 #ifdef NO_POSIX_MEMALIGN
3375 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3377 posix_memalign((void **)&p,16,current_block_size * 4);
3379 memset (p, 0, sizeof (Sample) * current_block_size);
3380 _silent_buffers.push_back (p);
3384 #ifdef NO_POSIX_MEMALIGN
3385 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3387 posix_memalign((void **)&p,16,current_block_size * 4);
3389 memset (p, 0, sizeof (Sample) * current_block_size);
3390 _send_buffers.push_back (p);
3393 allocate_pan_automation_buffers (current_block_size, howmany, false);
3397 Session::next_send_name ()
3400 snprintf (buf, sizeof (buf), "send %" PRIu32, ++send_cnt);
3405 Session::next_insert_name ()
3408 snprintf (buf, sizeof (buf), "insert %" PRIu32, ++insert_cnt);
3412 /* Named Selection management */
3415 Session::named_selection_by_name (string name)
3417 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3418 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
3419 if ((*i)->name == name) {
3427 Session::add_named_selection (NamedSelection* named_selection)
3430 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3431 named_selections.insert (named_selections.begin(), named_selection);
3436 NamedSelectionAdded (); /* EMIT SIGNAL */
3440 Session::remove_named_selection (NamedSelection* named_selection)
3442 bool removed = false;
3445 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3447 NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
3449 if (i != named_selections.end()) {
3451 named_selections.erase (i);
3458 NamedSelectionRemoved (); /* EMIT SIGNAL */
3463 Session::reset_native_file_format ()
3465 // jlc - WHY take routelock?
3466 //RWLockMonitor lm1 (route_lock, true, __LINE__, __FILE__);
3467 RWLockMonitor lm2 (diskstream_lock, false, __LINE__, __FILE__);
3469 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3470 (*i)->reset_write_sources (false);
3475 Session::route_name_unique (string n) const
3477 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3479 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3480 if ((*i)->name() == n) {
3489 Session::remove_file_source (FileSource& fs)
3491 return fs.move_to_trash (dead_sound_dir_name);
3495 Session::n_playlists () const
3497 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3498 return playlists.size();
3502 Session::set_solo_model (SoloModel sm)
3504 if (sm != _solo_model) {
3506 ControlChanged (SoloingModel);
3512 Session::allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howmany, bool force)
3514 if (!force && howmany <= _npan_buffers) {
3518 if (_pan_automation_buffer) {
3520 for (uint32_t i = 0; i < _npan_buffers; ++i) {
3521 delete [] _pan_automation_buffer[i];
3524 delete [] _pan_automation_buffer;
3527 _pan_automation_buffer = new pan_t*[howmany];
3529 for (uint32_t i = 0; i < howmany; ++i) {
3530 _pan_automation_buffer[i] = new pan_t[nframes];
3533 _npan_buffers = howmany;
3537 Session::add_instant_xml (XMLNode& node, const std::string& dir)
3539 Stateful::add_instant_xml (node, dir);
3540 Config->add_instant_xml (node, get_user_ardour_path());
3544 Session::freeze (InterThreadInfo& itt)
3546 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3548 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3552 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
3553 /* XXX this is wrong because itt.progress will keep returning to zero at the start
3564 Session::write_one_track (AudioTrack& track, jack_nframes_t start, jack_nframes_t len, bool overwrite, vector<Source*>& srcs,
3565 InterThreadInfo& itt)
3569 FileSource* fsource;
3571 char buf[PATH_MAX+1];
3574 jack_nframes_t position;
3575 jack_nframes_t this_chunk;
3576 jack_nframes_t to_do;
3577 vector<Sample*> buffers;
3579 const jack_nframes_t chunk_size = (256 * 1024)/4;
3581 atomic_set (&processing_prohibited, 1);
3583 /* call tree *MUST* hold route_lock */
3585 if ((playlist = track.disk_stream().playlist()) == 0) {
3589 /* external redirects will be a problem */
3591 if (track.has_external_redirects()) {
3595 nchans = track.disk_stream().n_channels();
3597 dir = discover_best_sound_dir ();
3599 for (uint32_t chan_n=0; chan_n < nchans; ++chan_n) {
3601 for (x = 0; x < 99999; ++x) {
3602 snprintf (buf, sizeof(buf), "%s/%s-%d-bounce-%" PRIu32 ".wav", dir.c_str(), playlist->name().c_str(), chan_n, x+1);
3603 if (access (buf, F_OK) != 0) {
3609 error << string_compose (_("too many bounced versions of playlist \"%1\""), playlist->name()) << endmsg;
3614 fsource = new FileSource (buf, frame_rate(), false, Config->get_native_file_data_format());
3617 catch (failed_constructor& err) {
3618 error << string_compose (_("cannot create new audio file \"%1\" for %2"), buf, track.name()) << endmsg;
3622 srcs.push_back(fsource);
3625 /* XXX need to flush all redirects */
3630 /* create a set of reasonably-sized buffers */
3632 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
3634 #ifdef NO_POSIX_MEMALIGN
3635 b = (Sample *) malloc(chunk_size * sizeof(Sample));
3637 posix_memalign((void **)&b,16,chunk_size * 4);
3639 buffers.push_back (b);
3642 workbuf = new char[chunk_size * 4];
3644 while (to_do && !itt.cancel) {
3646 this_chunk = min (to_do, chunk_size);
3648 if (track.export_stuff (buffers, workbuf, nchans, start, this_chunk)) {
3653 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
3654 if ((*src)->write (buffers[n], this_chunk, workbuf) != this_chunk) {
3659 start += this_chunk;
3660 to_do -= this_chunk;
3662 itt.progress = (float) (1.0 - ((double) to_do / len));
3671 xnow = localtime (&now);
3673 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3674 dynamic_cast<FileSource*>((*src))->update_header (position, *xnow, now);
3677 /* build peakfile for new source */
3679 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3680 dynamic_cast<FileSource*>(*src)->build_peaks ();
3688 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3689 dynamic_cast<FileSource*>(*src)->mark_for_remove ();
3694 for (vector<Sample*>::iterator i = buffers.begin(); i != buffers.end(); ++i) {
3702 atomic_set (&processing_prohibited, 0);
3710 Session::get_silent_buffers (uint32_t howmany)
3712 for (uint32_t i = 0; i < howmany; ++i) {
3713 memset (_silent_buffers[i], 0, sizeof (Sample) * current_block_size);
3715 return _silent_buffers;
3719 Session::ntracks () const
3722 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3724 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3725 if (dynamic_cast<AudioTrack*> (*i)) {
3734 Session::nbusses () const
3737 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3739 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3740 if (dynamic_cast<AudioTrack*> (*i) == 0) {
3749 Session::set_layer_model (LayerModel lm)
3751 if (lm != layer_model) {
3754 ControlChanged (LayeringModel);
3759 Session::set_xfade_model (CrossfadeModel xm)
3761 if (xm != xfade_model) {
3764 ControlChanged (CrossfadingModel);