2 Copyright (C) 1999-2002 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 <sigc++/bind.h>
28 #include <cstdio> /* snprintf(3) ... grrr */
43 #include <sys/mount.h>
44 #include <sys/param.h>
49 #include <midi++/mmc.h>
50 #include <midi++/port.h>
51 #include <pbd/error.h>
53 #include <glibmm/thread.h>
54 #include <pbd/pathscanner.h>
55 #include <pbd/pthread_utils.h>
56 #include <pbd/strsplit.h>
58 #include <ardour/audioengine.h>
59 #include <ardour/configuration.h>
60 #include <ardour/session.h>
61 #include <ardour/diskstream.h>
62 #include <ardour/utils.h>
63 #include <ardour/audioplaylist.h>
64 #include <ardour/source.h>
65 #include <ardour/filesource.h>
66 #include <ardour/destructive_filesource.h>
67 #include <ardour/sndfilesource.h>
68 #include <ardour/sndfile_helpers.h>
69 #include <ardour/auditioner.h>
70 #include <ardour/export.h>
71 #include <ardour/redirect.h>
72 #include <ardour/send.h>
73 #include <ardour/insert.h>
74 #include <ardour/connection.h>
75 #include <ardour/slave.h>
76 #include <ardour/tempo.h>
77 #include <ardour/audio_track.h>
78 #include <ardour/cycle_timer.h>
79 #include <ardour/utils.h>
80 #include <ardour/named_selection.h>
81 #include <ardour/version.h>
82 #include <ardour/location.h>
83 #include <ardour/audioregion.h>
84 #include <ardour/crossfade.h>
85 #include <ardour/control_protocol_manager.h>
91 using namespace ARDOUR;
94 Session::first_stage_init (string fullpath, string snapshot_name)
96 if (fullpath.length() == 0) {
97 throw failed_constructor();
100 char buf[PATH_MAX+1];
101 if (!realpath(fullpath.c_str(), buf) && (errno != ENOENT)) {
102 error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
103 throw failed_constructor();
107 if (_path[_path.length()-1] != '/') {
111 /* these two are just provisional settings. set_state()
112 will likely override them.
115 _name = _current_snapshot_name = snapshot_name;
116 setup_raid_path (_path);
118 _current_frame_rate = _engine.frame_rate ();
119 _tempo_map = new TempoMap (_current_frame_rate);
120 _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
122 g_atomic_int_set (&processing_prohibited, 0);
125 _transport_speed = 0;
126 _last_transport_speed = 0;
127 transport_sub_state = 0;
128 _transport_frame = 0;
130 end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
131 start_location = new Location (0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
132 _end_location_is_free = true;
133 g_atomic_int_set (&_record_status, Disabled);
138 seamless_loop = false;
139 loop_changing = false;
141 crossfades_active = false;
144 _last_roll_location = 0;
145 _last_record_location = 0;
146 pending_locate_frame = 0;
147 pending_locate_roll = false;
148 pending_locate_flush = false;
149 dstream_buffer_size = 0;
151 state_was_pending = false;
153 outbound_mtc_smpte_frame = 0;
154 next_quarter_frame_to_send = -1;
155 current_block_size = 0;
156 _solo_latched = true;
157 _solo_model = InverseMute;
158 solo_update_disabled = false;
159 currently_soloing = false;
160 _have_captured = false;
161 _worst_output_latency = 0;
162 _worst_input_latency = 0;
163 _worst_track_latency = 0;
164 _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
167 butler_mixdown_buffer = 0;
168 butler_gain_buffer = 0;
173 post_transport_work = PostTransportWork (0);
174 g_atomic_int_set (&butler_should_do_transport_work, 0);
175 g_atomic_int_set (&butler_active, 0);
176 g_atomic_int_set (&_playback_load, 100);
177 g_atomic_int_set (&_capture_load, 100);
178 g_atomic_int_set (&_playback_load_min, 100);
179 g_atomic_int_set (&_capture_load_min, 100);
180 pending_audition_region = 0;
182 pending_edit_mode = _edit_mode;
186 input_auto_connect = AutoConnectOption (0);
187 output_auto_connect = AutoConnectOption (0);
188 waiting_to_start = false;
190 _gain_automation_buffer = 0;
191 _pan_automation_buffer = 0;
193 pending_abort = false;
194 layer_model = MoveAddHigher;
195 xfade_model = ShortCrossfade;
196 destructive_index = 0;
198 /* allocate conversion buffers */
199 _conversion_buffers[ButlerContext] = new char[DiskStream::disk_io_frames() * 4];
200 _conversion_buffers[TransportContext] = new char[DiskStream::disk_io_frames() * 4];
202 /* default short fade = 15ms */
204 Crossfade::set_short_xfade_length ((jack_nframes_t) floor ((15.0 * frame_rate()) / 1000.0));
206 last_mmc_step.tv_sec = 0;
207 last_mmc_step.tv_usec = 0;
210 preroll.type = AnyTime::Frames;
212 postroll.type = AnyTime::Frames;
215 /* click sounds are unset by default, which causes us to internal
216 waveforms for clicks.
221 click_requested = false;
223 click_emphasis_data = 0;
225 click_emphasis_length = 0;
227 process_function = &Session::process_with_events;
231 _smpte_offset_negative = true;
232 last_smpte_valid = false;
234 last_rr_session_dir = session_dirs.begin();
235 refresh_disk_space ();
237 // set_default_fade (0.2, 5.0); /* steepness, millisecs */
239 /* default configuration */
241 do_not_record_plugins = false;
242 over_length_short = 2;
243 over_length_long = 10;
244 send_midi_timecode = false;
245 send_midi_machine_control = false;
246 shuttle_speed_factor = 1.0;
247 shuttle_speed_threshold = 5;
249 _meter_hold = 100; // XXX unknown units: number of calls to meter::set()
250 _meter_falloff = 1.5f; // XXX unknown units: refresh_rate
256 average_slave_delta = 1800;
257 have_first_delta_accumulator = false;
258 delta_accumulator_cnt = 0;
259 slave_state = Stopped;
261 /* default SMPTE type is 30 FPS, non-drop */
263 set_smpte_type (30.0, false);
265 _engine.GraphReordered.connect (mem_fun (*this, &Session::graph_reordered));
267 /* These are all static "per-class" signals */
269 Region::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
270 Source::SourceCreated.connect (mem_fun (*this, &Session::add_source));
271 Playlist::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
272 Redirect::RedirectCreated.connect (mem_fun (*this, &Session::add_redirect));
273 DiskStream::DiskStreamCreated.connect (mem_fun (*this, &Session::add_diskstream));
274 NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
276 IO::MoreOutputs.connect (mem_fun (*this, &Session::ensure_passthru_buffers));
278 /* stop IO objects from doing stuff until we're ready for them */
280 IO::disable_panners ();
281 IO::disable_ports ();
282 IO::disable_connecting ();
286 Session::second_stage_init (bool new_session)
288 ExternalSource::set_peak_dir (peak_dir());
291 if (load_state (_current_snapshot_name)) {
294 remove_empty_sounds ();
297 if (start_butler_thread()) {
301 /*if (start_midi_thread ()) {
306 if (set_state (*state_tree->root())) {
311 /* we can't save till after ::when_engine_running() is called,
312 because otherwise we save state with no connections made.
313 therefore, we reset _state_of_the_state because ::set_state()
314 will have cleared it.
316 we also have to include Loading so that any events that get
317 generated between here and the end of ::when_engine_running()
318 will be processed directly rather than queued.
321 _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave|Loading);
323 // set_auto_input (true);
324 _locations.changed.connect (mem_fun (this, &Session::locations_changed));
325 _locations.added.connect (mem_fun (this, &Session::locations_added));
326 setup_click_sounds (0);
327 setup_midi_control ();
329 /* Pay attention ... */
331 _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
332 _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
334 if (_engine.running()) {
335 when_engine_running();
337 first_time_running = _engine.Running.connect (mem_fun (*this, &Session::when_engine_running));
340 //send_full_time_code ();
341 _engine.transport_locate (0);
342 deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
343 deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
345 ControlProtocolManager::instance().set_session (*this);
348 _end_location_is_free = true;
350 _end_location_is_free = false;
357 Session::raid_path () const
361 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
366 return path.substr (0, path.length() - 1); // drop final colon
370 Session::set_raid_path (string path)
372 /* public-access to setup_raid_path() */
374 setup_raid_path (path);
378 Session::setup_raid_path (string path)
380 string::size_type colon;
384 string::size_type len = path.length();
389 if (path.length() == 0) {
393 session_dirs.clear ();
395 for (string::size_type n = 0; n < len; ++n) {
396 if (path[n] == ':') {
403 /* no multiple search path, just one location (common case) */
407 session_dirs.push_back (sp);
414 if (fspath[fspath.length()-1] != '/') {
417 fspath += sound_dir_name;
423 if (fspath[fspath.length()-1] != '/') {
426 fspath += tape_dir_name;
428 FileSource::set_search_path (fspath);
435 while ((colon = remaining.find_first_of (':')) != string::npos) {
438 sp.path = remaining.substr (0, colon);
439 session_dirs.push_back (sp);
441 /* add sounds to file search path */
444 if (fspath[fspath.length()-1] != '/') {
447 fspath += sound_dir_name;
450 /* add tape dir to file search path */
453 if (fspath[fspath.length()-1] != '/') {
456 fspath += tape_dir_name;
459 remaining = remaining.substr (colon+1);
462 if (remaining.length()) {
469 if (fspath[fspath.length()-1] != '/') {
472 fspath += sound_dir_name;
476 if (fspath[fspath.length()-1] != '/') {
479 fspath += tape_dir_name;
481 session_dirs.push_back (sp);
484 /* set the FileSource search path */
486 FileSource::set_search_path (fspath);
488 /* reset the round-robin soundfile path thingie */
490 last_rr_session_dir = session_dirs.begin();
494 Session::create (bool& new_session, string* mix_template, jack_nframes_t initial_length)
498 if (mkdir (_path.c_str(), 0755) < 0) {
499 if (errno == EEXIST) {
502 error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
511 if (mkdir (dir.c_str(), 0755) < 0) {
512 if (errno != EEXIST) {
513 error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
520 if (mkdir (dir.c_str(), 0755) < 0) {
521 if (errno != EEXIST) {
522 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
529 if (mkdir (dir.c_str(), 0755) < 0) {
530 if (errno != EEXIST) {
531 error << string_compose(_("Session: cannot create session tape dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
536 dir = dead_sound_dir ();
538 if (mkdir (dir.c_str(), 0755) < 0) {
539 if (errno != EEXIST) {
540 error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
545 dir = automation_dir ();
547 if (mkdir (dir.c_str(), 0755) < 0) {
548 if (errno != EEXIST) {
549 error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
555 /* check new_session so we don't overwrite an existing one */
559 std::string in_path = *mix_template;
561 ifstream in(in_path.c_str());
564 string out_path = _path;
566 out_path += _statefile_suffix;
568 ofstream out(out_path.c_str());
573 // okay, session is set up. Treat like normal saved
574 // session from now on.
580 error << string_compose (_("Could not open %1 for writing mix template"), out_path)
586 error << string_compose (_("Could not open mix template %1 for reading"), in_path)
593 warning << _("Session already exists. Not overwriting") << endmsg;
600 /* set initial start + end point */
602 start_location->set_end (0);
603 _locations.add (start_location);
605 end_location->set_end (initial_length);
606 _locations.add (end_location);
608 _state_of_the_state = Clean;
610 if (save_state (_current_snapshot_name)) {
619 Session::load_diskstreams (const XMLNode& node)
622 XMLNodeConstIterator citer;
624 clist = node.children();
626 for (citer = clist.begin(); citer != clist.end(); ++citer) {
631 dstream = new DiskStream (*this, **citer);
632 /* added automatically by DiskStreamCreated handler */
635 catch (failed_constructor& err) {
636 error << _("Session: could not load diskstream via XML state") << endmsg;
645 Session::remove_pending_capture_state ()
650 xml_path += _current_snapshot_name;
651 xml_path += _pending_suffix;
653 unlink (xml_path.c_str());
657 Session::save_state (string snapshot_name, bool pending)
663 if (_state_of_the_state & CannotSave) {
667 tree.set_root (&get_state());
669 if (snapshot_name.empty()) {
670 snapshot_name = _current_snapshot_name;
676 xml_path += snapshot_name;
677 xml_path += _statefile_suffix;
681 // Make backup of state file
683 if ((access (xml_path.c_str(), F_OK) == 0) &&
684 (rename(xml_path.c_str(), bak_path.c_str()))) {
685 error << _("could not backup old state file, current state not saved.") << endmsg;
692 xml_path += snapshot_name;
693 xml_path += _pending_suffix;
697 if (!tree.write (xml_path)) {
698 error << string_compose (_("state could not be saved to %1"), xml_path) << endmsg;
700 /* don't leave a corrupt file lying around if it is
704 if (unlink (xml_path.c_str())) {
705 error << string_compose (_("could not remove corrupt state file %1"), xml_path) << endmsg;
708 if (rename (bak_path.c_str(), xml_path.c_str())) {
709 error << string_compose (_("could not restore state file from backup %1"), bak_path) << endmsg;
719 bool was_dirty = dirty();
721 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
724 DirtyChanged (); /* EMIT SIGNAL */
727 StateSaved (snapshot_name); /* EMIT SIGNAL */
734 Session::restore_state (string snapshot_name)
736 if (load_state (snapshot_name) == 0) {
737 set_state (*state_tree->root());
744 Session::load_state (string snapshot_name)
753 state_was_pending = false;
755 /* check for leftover pending state from a crashed capture attempt */
758 xmlpath += snapshot_name;
759 xmlpath += _pending_suffix;
761 if (!access (xmlpath.c_str(), F_OK)) {
763 /* there is pending state from a crashed capture attempt */
765 if (AskAboutPendingState()) {
766 state_was_pending = true;
770 if (!state_was_pending) {
773 xmlpath += snapshot_name;
774 xmlpath += _statefile_suffix;
777 if (access (xmlpath.c_str(), F_OK)) {
778 error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
782 state_tree = new XMLTree;
786 if (state_tree->read (xmlpath)) {
789 error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
798 Session::load_options (const XMLNode& node)
802 bool have_fade_msecs = false;
803 bool have_fade_steepness = false;
804 float fade_msecs = 0;
805 float fade_steepness = 0;
806 SlaveSource slave_src = None;
808 LocaleGuard lg (X_("POSIX"));
810 if ((child = find_named_node (node, "input-auto-connect")) != 0) {
811 if ((prop = child->property ("val")) != 0) {
812 sscanf (prop->value().c_str(), "%x", &x);
813 input_auto_connect = AutoConnectOption (x);
817 if ((child = find_named_node (node, "output-auto-connect")) != 0) {
818 if ((prop = child->property ("val")) != 0) {
819 sscanf (prop->value().c_str(), "%x", &x);
820 output_auto_connect = AutoConnectOption (x);
824 if ((child = find_named_node (node, "slave")) != 0) {
825 if ((prop = child->property ("type")) != 0) {
826 if (prop->value() == "none") {
828 } else if (prop->value() == "mtc") {
830 } else if (prop->value() == "jack") {
833 set_slave_source (slave_src, 0);
837 /* we cannot set edit mode if we are loading a session,
838 because it might destroy the playlist's positioning
841 if ((child = find_named_node (node, "edit-mode")) != 0) {
842 if ((prop = child->property ("val")) != 0) {
843 if (prop->value() == "slide") {
844 pending_edit_mode = Slide;
845 } else if (prop->value() == "splice") {
846 pending_edit_mode = Splice;
851 if ((child = find_named_node (node, "send-midi-timecode")) != 0) {
852 if ((prop = child->property ("val")) != 0) {
853 bool x = (prop->value() == "yes");
854 send_mtc = !x; /* force change in value */
858 if ((child = find_named_node (node, "send-midi-machine-control")) != 0) {
859 if ((prop = child->property ("val")) != 0) {
860 bool x = (prop->value() == "yes");
861 send_mmc = !x; /* force change in value */
862 set_send_mmc (prop->value() == "yes");
865 if ((child = find_named_node (node, "max-level")) != 0) {
866 if ((prop = child->property ("val")) != 0) {
867 max_level = atoi (prop->value().c_str());
870 if ((child = find_named_node (node, "min-level")) != 0) {
871 if ((prop = child->property ("val")) != 0) {
872 min_level = atoi (prop->value().c_str());
875 if ((child = find_named_node (node, "meter-hold")) != 0) {
876 if ((prop = child->property ("val")) != 0) {
877 _meter_hold = atof (prop->value().c_str());
880 if ((child = find_named_node (node, "meter-falloff")) != 0) {
881 if ((prop = child->property ("val")) != 0) {
882 _meter_falloff = atof (prop->value().c_str());
885 if ((child = find_named_node (node, "long-over-length")) != 0) {
886 if ((prop = child->property ("val")) != 0) {
887 over_length_long = atoi (prop->value().c_str());
890 if ((child = find_named_node (node, "short-over-length")) != 0) {
891 if ((prop = child->property ("val")) != 0) {
892 over_length_short = atoi (prop->value().c_str());
895 if ((child = find_named_node (node, "shuttle-speed-factor")) != 0) {
896 if ((prop = child->property ("val")) != 0) {
897 shuttle_speed_factor = atof (prop->value().c_str());
900 if ((child = find_named_node (node, "shuttle-speed-threshold")) != 0) {
901 if ((prop = child->property ("val")) != 0) {
902 shuttle_speed_threshold = atof (prop->value().c_str());
905 if ((child = find_named_node (node, "rf-speed")) != 0) {
906 if ((prop = child->property ("val")) != 0) {
907 rf_speed = atof (prop->value().c_str());
910 if ((child = find_named_node (node, "smpte-frames-per-second")) != 0) {
911 if ((prop = child->property ("val")) != 0) {
912 set_smpte_type( atof (prop->value().c_str()), smpte_drop_frames );
915 if ((child = find_named_node (node, "smpte-drop-frames")) != 0) {
916 if ((prop = child->property ("val")) != 0) {
917 set_smpte_type( smpte_frames_per_second, (prop->value() == "yes") );
920 if ((child = find_named_node (node, "smpte-offset")) != 0) {
921 if ((prop = child->property ("val")) != 0) {
922 set_smpte_offset( atoi (prop->value().c_str()) );
925 if ((child = find_named_node (node, "smpte-offset-negative")) != 0) {
926 if ((prop = child->property ("val")) != 0) {
927 set_smpte_offset_negative( (prop->value() == "yes") );
930 if ((child = find_named_node (node, "click-sound")) != 0) {
931 if ((prop = child->property ("val")) != 0) {
932 click_sound = prop->value();
935 if ((child = find_named_node (node, "click-emphasis-sound")) != 0) {
936 if ((prop = child->property ("val")) != 0) {
937 click_emphasis_sound = prop->value();
941 if ((child = find_named_node (node, "solo-model")) != 0) {
942 if ((prop = child->property ("val")) != 0) {
943 if (prop->value() == "SoloBus")
944 _solo_model = SoloBus;
946 _solo_model = InverseMute;
950 /* BOOLEAN OPTIONS */
952 if ((child = find_named_node (node, "auto-play")) != 0) {
953 if ((prop = child->property ("val")) != 0) {
954 set_auto_play (prop->value() == "yes");
957 if ((child = find_named_node (node, "auto-input")) != 0) {
958 if ((prop = child->property ("val")) != 0) {
959 set_auto_input (prop->value() == "yes");
962 if ((child = find_named_node (node, "seamless-loop")) != 0) {
963 if ((prop = child->property ("val")) != 0) {
964 set_seamless_loop (prop->value() == "yes");
967 if ((child = find_named_node (node, "punch-in")) != 0) {
968 if ((prop = child->property ("val")) != 0) {
969 set_punch_in (prop->value() == "yes");
972 if ((child = find_named_node (node, "punch-out")) != 0) {
973 if ((prop = child->property ("val")) != 0) {
974 set_punch_out (prop->value() == "yes");
977 if ((child = find_named_node (node, "auto-return")) != 0) {
978 if ((prop = child->property ("val")) != 0) {
979 set_auto_return (prop->value() == "yes");
982 if ((child = find_named_node (node, "send-mtc")) != 0) {
983 if ((prop = child->property ("val")) != 0) {
984 set_send_mtc (prop->value() == "yes");
987 if ((child = find_named_node (node, "mmc-control")) != 0) {
988 if ((prop = child->property ("val")) != 0) {
989 set_mmc_control (prop->value() == "yes");
992 if ((child = find_named_node (node, "midi-control")) != 0) {
993 if ((prop = child->property ("val")) != 0) {
994 set_midi_control (prop->value() == "yes");
997 if ((child = find_named_node (node, "midi-feedback")) != 0) {
998 if ((prop = child->property ("val")) != 0) {
999 set_midi_feedback (prop->value() == "yes");
1002 // Legacy support for <recording-plugins>
1003 if ((child = find_named_node (node, "recording-plugins")) != 0) {
1004 if ((prop = child->property ("val")) != 0) {
1005 set_do_not_record_plugins (prop->value() == "no");
1008 if ((child = find_named_node (node, "do-not-record-plugins")) != 0) {
1009 if ((prop = child->property ("val")) != 0) {
1010 set_do_not_record_plugins (prop->value() == "yes");
1013 if ((child = find_named_node (node, "crossfades-active")) != 0) {
1014 if ((prop = child->property ("val")) != 0) {
1015 set_crossfades_active (prop->value() == "yes");
1018 if ((child = find_named_node (node, "audible-click")) != 0) {
1019 if ((prop = child->property ("val")) != 0) {
1020 set_clicking (prop->value() == "yes");
1024 if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
1025 if ((prop = child->property ("val")) != 0) {
1026 _end_location_is_free = (prop->value() == "yes");
1030 if ((child = find_named_node (node, "layer-model")) != 0) {
1031 if ((prop = child->property ("val")) != 0) {
1032 if (prop->value() == X_("LaterHigher")) {
1033 set_layer_model (LaterHigher);
1034 } else if (prop->value() == X_("AddHigher")) {
1035 set_layer_model (AddHigher);
1037 set_layer_model (MoveAddHigher);
1042 if ((child = find_named_node (node, "xfade-model")) != 0) {
1043 if ((prop = child->property ("val")) != 0) {
1044 if (prop->value() == X_("Short")) {
1045 set_xfade_model (ShortCrossfade);
1047 set_xfade_model (FullCrossfade);
1052 if ((child = find_named_node (node, "short-xfade-length")) != 0) {
1053 if ((prop = child->property ("val")) != 0) {
1054 /* value is stored as a fractional seconds */
1055 float secs = atof (prop->value().c_str());
1056 Crossfade::set_short_xfade_length ((jack_nframes_t) floor (secs * frame_rate()));
1060 if ((child = find_named_node (node, "full-xfades-unmuted")) != 0) {
1061 if ((prop = child->property ("val")) != 0) {
1062 crossfades_active = (prop->value() == "yes");
1068 if ((child = find_named_node (node, "default-fade-steepness")) != 0) {
1069 if ((prop = child->property ("val")) != 0) {
1070 fade_steepness = atof (prop->value().c_str());
1071 have_fade_steepness = true;
1074 if ((child = find_named_node (node, "default-fade-msec")) != 0) {
1075 if ((prop = child->property ("val")) != 0) {
1076 fade_msecs = atof (prop->value().c_str());
1077 have_fade_msecs = true;
1081 if (have_fade_steepness || have_fade_msecs) {
1082 // set_default_fade (fade_steepness, fade_msecs);
1089 Session::get_options () const
1094 LocaleGuard lg (X_("POSIX"));
1096 opthead = new XMLNode ("Options");
1098 SlaveSource src = slave_source ();
1102 src_string = "none";
1108 src_string = "jack";
1111 child = opthead->add_child ("slave");
1112 child->add_property ("type", src_string);
1114 child = opthead->add_child ("send-midi-timecode");
1115 child->add_property ("val", send_midi_timecode?"yes":"no");
1117 child = opthead->add_child ("send-midi-machine-control");
1118 child->add_property ("val", send_midi_machine_control?"yes":"no");
1120 snprintf (buf, sizeof(buf)-1, "%x", (int) input_auto_connect);
1121 child = opthead->add_child ("input-auto-connect");
1122 child->add_property ("val", buf);
1124 snprintf (buf, sizeof(buf)-1, "%x", (int) output_auto_connect);
1125 child = opthead->add_child ("output-auto-connect");
1126 child->add_property ("val", buf);
1128 snprintf (buf, sizeof(buf)-1, "%d", max_level);
1129 child = opthead->add_child ("max-level");
1130 child->add_property ("val", buf);
1132 snprintf (buf, sizeof(buf)-1, "%d", min_level);
1133 child = opthead->add_child ("min-level");
1134 child->add_property ("val", buf);
1136 snprintf (buf, sizeof(buf)-1, "%f", _meter_hold);
1137 child = opthead->add_child ("meter-hold");
1138 child->add_property ("val", buf);
1140 snprintf (buf, sizeof(buf)-1, "%f", _meter_falloff);
1141 child = opthead->add_child ("meter-falloff");
1142 child->add_property ("val", buf);
1144 snprintf (buf, sizeof(buf)-1, "%u", over_length_long);
1145 child = opthead->add_child ("long-over-length");
1146 child->add_property ("val", buf);
1148 snprintf (buf, sizeof(buf)-1, "%u", over_length_short);
1149 child = opthead->add_child ("short-over-length");
1150 child->add_property ("val", buf);
1152 snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_factor);
1153 child = opthead->add_child ("shuttle-speed-factor");
1154 child->add_property ("val", buf);
1156 snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_threshold);
1157 child = opthead->add_child ("shuttle-speed-threshold");
1158 child->add_property ("val", buf);
1160 snprintf (buf, sizeof(buf)-1, "%f", rf_speed);
1161 child = opthead->add_child ("rf-speed");
1162 child->add_property ("val", buf);
1164 snprintf (buf, sizeof(buf)-1, "%.2f", smpte_frames_per_second);
1165 child = opthead->add_child ("smpte-frames-per-second");
1166 child->add_property ("val", buf);
1168 child = opthead->add_child ("smpte-drop-frames");
1169 child->add_property ("val", smpte_drop_frames ? "yes" : "no");
1171 snprintf (buf, sizeof(buf)-1, "%u", smpte_offset ());
1172 child = opthead->add_child ("smpte-offset");
1173 child->add_property ("val", buf);
1175 child = opthead->add_child ("smpte-offset-negative");
1176 child->add_property ("val", smpte_offset_negative () ? "yes" : "no");
1178 child = opthead->add_child ("edit-mode");
1179 switch (_edit_mode) {
1181 child->add_property ("val", "splice");
1185 child->add_property ("val", "slide");
1189 child = opthead->add_child ("auto-play");
1190 child->add_property ("val", get_auto_play () ? "yes" : "no");
1191 child = opthead->add_child ("auto-input");
1192 child->add_property ("val", get_auto_input () ? "yes" : "no");
1193 child = opthead->add_child ("seamless-loop");
1194 child->add_property ("val", get_seamless_loop () ? "yes" : "no");
1195 child = opthead->add_child ("punch-in");
1196 child->add_property ("val", get_punch_in () ? "yes" : "no");
1197 child = opthead->add_child ("punch-out");
1198 child->add_property ("val", get_punch_out () ? "yes" : "no");
1199 child = opthead->add_child ("all-safe");
1200 child->add_property ("val", get_all_safe () ? "yes" : "no");
1201 child = opthead->add_child ("auto-return");
1202 child->add_property ("val", get_auto_return () ? "yes" : "no");
1203 child = opthead->add_child ("mmc-control");
1204 child->add_property ("val", get_mmc_control () ? "yes" : "no");
1205 child = opthead->add_child ("midi-control");
1206 child->add_property ("val", get_midi_control () ? "yes" : "no");
1207 child = opthead->add_child ("midi-feedback");
1208 child->add_property ("val", get_midi_feedback () ? "yes" : "no");
1209 child = opthead->add_child ("do-not-record-plugins");
1210 child->add_property ("val", get_do_not_record_plugins () ? "yes" : "no");
1211 child = opthead->add_child ("auto-crossfade");
1212 child->add_property ("val", get_crossfades_active () ? "yes" : "no");
1213 child = opthead->add_child ("audible-click");
1214 child->add_property ("val", get_clicking () ? "yes" : "no");
1215 child = opthead->add_child ("end-marker-is-free");
1216 child->add_property ("val", _end_location_is_free ? "yes" : "no");
1218 if (click_sound.length()) {
1219 child = opthead->add_child ("click-sound");
1220 child->add_property ("val", click_sound);
1223 if (click_emphasis_sound.length()) {
1224 child = opthead->add_child ("click-emphasis-sound");
1225 child->add_property ("val", click_emphasis_sound);
1228 child = opthead->add_child ("solo-model");
1229 child->add_property ("val", _solo_model == SoloBus ? "SoloBus" : "InverseMute");
1231 child = opthead->add_child ("layer-model");
1232 switch (layer_model) {
1234 child->add_property ("val", X_("LaterHigher"));
1237 child->add_property ("val", X_("MoveAddHigher"));
1240 child->add_property ("val", X_("AddHigher"));
1244 child = opthead->add_child ("xfade-model");
1245 switch (xfade_model) {
1247 child->add_property ("val", X_("Full"));
1249 case ShortCrossfade:
1250 child->add_property ("val", X_("Short"));
1253 child = opthead->add_child ("short-xfade-length");
1254 /* store as fractions of a second */
1255 snprintf (buf, sizeof(buf)-1, "%f",
1256 (float) Crossfade::short_xfade_length() / frame_rate());
1257 child->add_property ("val", buf);
1259 child = opthead->add_child ("full-xfades-unmuted");
1260 child->add_property ("val", crossfades_active ? "yes" : "no");
1266 Session::get_state()
1272 Session::get_template()
1274 /* if we don't disable rec-enable, diskstreams
1275 will believe they need to store their capture
1276 sources in their state node.
1279 disable_record (false);
1281 return state(false);
1285 Session::state(bool full_state)
1287 XMLNode* node = new XMLNode("Session");
1290 // store libardour version, just in case
1292 snprintf(buf, sizeof(buf)-1, "%d.%d.%d",
1293 libardour_major_version, libardour_minor_version, libardour_micro_version);
1294 node->add_property("version", string(buf));
1296 /* store configuration settings */
1300 /* store the name */
1301 node->add_property ("name", _name);
1303 if (session_dirs.size() > 1) {
1307 vector<space_and_path>::iterator i = session_dirs.begin();
1308 vector<space_and_path>::iterator next;
1310 ++i; /* skip the first one */
1314 while (i != session_dirs.end()) {
1318 if (next != session_dirs.end()) {
1328 child = node->add_child ("Path");
1329 child->add_content (p);
1333 node->add_child_nocopy (get_options());
1335 child = node->add_child ("Sources");
1338 Glib::Mutex::Lock sl (source_lock);
1340 for (SourceList::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
1342 /* Don't save information about FileSources that are empty */
1346 if ((fs = dynamic_cast<FileSource*> ((*siter).second)) != 0) {
1347 DestructiveFileSource* dfs = dynamic_cast<DestructiveFileSource*> (fs);
1349 /* destructive file sources are OK if they are empty, because
1350 we will re-use them every time.
1354 if (fs->length() == 0) {
1360 child->add_child_nocopy ((*siter).second->get_state());
1364 child = node->add_child ("Regions");
1367 Glib::Mutex::Lock rl (region_lock);
1369 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
1371 /* only store regions not attached to playlists */
1373 if ((*i).second->playlist() == 0) {
1374 child->add_child_nocopy (i->second->state (true));
1379 child = node->add_child ("DiskStreams");
1382 Glib::RWLock::ReaderLock dl (diskstream_lock);
1383 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1384 if (!(*i)->hidden()) {
1385 child->add_child_nocopy ((*i)->get_state());
1390 node->add_child_nocopy (_locations.get_state());
1392 child = node->add_child ("Connections");
1394 Glib::Mutex::Lock lm (connection_lock);
1395 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ++i) {
1396 if (!(*i)->system_dependent()) {
1397 child->add_child_nocopy ((*i)->get_state());
1402 child = node->add_child ("Routes");
1404 Glib::RWLock::ReaderLock lm (route_lock);
1406 RoutePublicOrderSorter cmp;
1407 RouteList public_order(routes);
1408 public_order.sort (cmp);
1410 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1411 if (!(*i)->hidden()) {
1413 child->add_child_nocopy ((*i)->get_state());
1415 child->add_child_nocopy ((*i)->get_template());
1422 child = node->add_child ("EditGroups");
1423 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ++i) {
1424 child->add_child_nocopy ((*i)->get_state());
1427 child = node->add_child ("MixGroups");
1428 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ++i) {
1429 child->add_child_nocopy ((*i)->get_state());
1432 child = node->add_child ("Playlists");
1433 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1434 if (!(*i)->hidden()) {
1435 if (!(*i)->empty()) {
1437 child->add_child_nocopy ((*i)->get_state());
1439 child->add_child_nocopy ((*i)->get_template());
1445 child = node->add_child ("UnusedPlaylists");
1446 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
1447 if (!(*i)->hidden()) {
1448 if (!(*i)->empty()) {
1450 child->add_child_nocopy ((*i)->get_state());
1452 child->add_child_nocopy ((*i)->get_template());
1460 child = node->add_child ("Click");
1461 child->add_child_nocopy (_click_io->state (full_state));
1465 child = node->add_child ("NamedSelections");
1466 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
1468 child->add_child_nocopy ((*i)->get_state());
1473 node->add_child_nocopy (_tempo_map->get_state());
1476 node->add_child_copy (*_extra_xml);
1483 Session::set_state (const XMLNode& node)
1487 const XMLProperty* prop;
1490 _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1492 if (node.name() != "Session"){
1493 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1497 StateManager::prohibit_save ();
1499 if ((prop = node.property ("name")) != 0) {
1500 _name = prop->value ();
1503 IO::disable_ports ();
1504 IO::disable_connecting ();
1506 /* Object loading order:
1523 if (use_config_midi_ports ()) {
1526 if ((child = find_named_node (node, "Path")) != 0) {
1527 /* XXX this XML content stuff horrible API design */
1528 string raid_path = _path + ':' + child->children().front()->content();
1529 setup_raid_path (raid_path);
1531 /* the path is already set */
1534 if ((child = find_named_node (node, "extra")) != 0) {
1535 _extra_xml = new XMLNode (*child);
1538 if ((child = find_named_node (node, "Options")) == 0) {
1539 error << _("Session: XML state has no options section") << endmsg;
1540 } else if (load_options (*child)) {
1543 if ((child = find_named_node (node, "Sources")) == 0) {
1544 error << _("Session: XML state has no sources section") << endmsg;
1546 } else if (load_sources (*child)) {
1550 if ((child = find_named_node (node, "Regions")) == 0) {
1551 error << _("Session: XML state has no Regions section") << endmsg;
1553 } else if (load_regions (*child)) {
1557 if ((child = find_named_node (node, "Playlists")) == 0) {
1558 error << _("Session: XML state has no playlists section") << endmsg;
1560 } else if (load_playlists (*child)) {
1564 if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1566 } else if (load_unused_playlists (*child)) {
1570 if ((child = find_named_node (node, "NamedSelections")) != 0) {
1571 if (load_named_selections (*child)) {
1576 if ((child = find_named_node (node, "DiskStreams")) == 0) {
1577 error << _("Session: XML state has no diskstreams section") << endmsg;
1579 } else if (load_diskstreams (*child)) {
1583 if ((child = find_named_node (node, "Connections")) == 0) {
1584 error << _("Session: XML state has no connections section") << endmsg;
1586 } else if (load_connections (*child)) {
1590 if ((child = find_named_node (node, "Locations")) == 0) {
1591 error << _("Session: XML state has no locations section") << endmsg;
1593 } else if (_locations.set_state (*child)) {
1599 if ((location = _locations.auto_loop_location()) != 0) {
1600 set_auto_loop_location (location);
1603 if ((location = _locations.auto_punch_location()) != 0) {
1604 set_auto_punch_location (location);
1607 if ((location = _locations.end_location()) == 0) {
1608 _locations.add (end_location);
1610 delete end_location;
1611 end_location = location;
1614 if ((location = _locations.start_location()) == 0) {
1615 _locations.add (start_location);
1617 delete start_location;
1618 start_location = location;
1621 _locations.save_state (_("initial state"));
1623 if ((child = find_named_node (node, "EditGroups")) == 0) {
1624 error << _("Session: XML state has no edit groups section") << endmsg;
1626 } else if (load_edit_groups (*child)) {
1630 if ((child = find_named_node (node, "MixGroups")) == 0) {
1631 error << _("Session: XML state has no mix groups section") << endmsg;
1633 } else if (load_mix_groups (*child)) {
1637 if ((child = find_named_node (node, "TempoMap")) == 0) {
1638 error << _("Session: XML state has no Tempo Map section") << endmsg;
1640 } else if (_tempo_map->set_state (*child)) {
1644 if ((child = find_named_node (node, "Routes")) == 0) {
1645 error << _("Session: XML state has no routes section") << endmsg;
1647 } else if (load_routes (*child)) {
1651 if ((child = find_named_node (node, "Click")) == 0) {
1652 warning << _("Session: XML state has no click section") << endmsg;
1653 } else if (_click_io) {
1654 _click_io->set_state (*child);
1657 /* OK, now we can set edit mode */
1659 set_edit_mode (pending_edit_mode);
1661 /* here beginneth the second phase ... */
1663 StateReady (); /* EMIT SIGNAL */
1665 _state_of_the_state = Clean;
1667 StateManager::allow_save (_("initial state"), true);
1669 if (state_was_pending) {
1670 save_state (_current_snapshot_name);
1671 remove_pending_capture_state ();
1672 state_was_pending = false;
1678 /* we failed, re-enable state saving but don't actually save internal state */
1679 StateManager::allow_save (X_("ignored"), false);
1684 Session::load_routes (const XMLNode& node)
1687 XMLNodeConstIterator niter;
1690 nlist = node.children();
1694 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1696 if ((route = XMLRouteFactory (**niter)) == 0) {
1697 error << _("Session: cannot create Route from XML description.") << endmsg;
1708 Session::XMLRouteFactory (const XMLNode& node)
1710 if (node.name() != "Route") {
1714 if (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0) {
1715 return new AudioTrack (*this, node);
1717 return new Route (*this, node);
1722 Session::load_regions (const XMLNode& node)
1725 XMLNodeConstIterator niter;
1726 AudioRegion* region;
1728 nlist = node.children();
1732 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1734 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1735 error << _("Session: cannot create Region from XML description.") << endmsg;
1743 Session::XMLRegionFactory (const XMLNode& node, bool full)
1745 const XMLProperty* prop;
1748 AudioRegion::SourceList sources;
1749 uint32_t nchans = 1;
1752 if (node.name() != X_("Region")) {
1756 if ((prop = node.property (X_("channels"))) != 0) {
1757 nchans = atoi (prop->value().c_str());
1761 if ((prop = node.property (X_("source-0"))) == 0) {
1762 if ((prop = node.property ("source")) == 0) {
1763 error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1768 sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
1770 if ((source = get_source (s_id)) == 0) {
1771 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1775 sources.push_back(source);
1777 /* pickup other channels */
1779 for (uint32_t n=1; n < nchans; ++n) {
1780 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1781 if ((prop = node.property (buf)) != 0) {
1782 sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
1784 if ((source = get_source (s_id)) == 0) {
1785 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1788 sources.push_back(source);
1794 return new AudioRegion (sources, node);
1797 catch (failed_constructor& err) {
1803 Session::get_sources_as_xml ()
1806 XMLNode* node = new XMLNode (X_("Sources"));
1807 Glib::Mutex::Lock lm (source_lock);
1809 for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
1810 node->add_child_nocopy ((*i).second->get_state());
1817 Session::path_from_region_name (string name, string identifier)
1819 char buf[PATH_MAX+1];
1821 string dir = discover_best_sound_dir ();
1823 for (n = 0; n < 999999; ++n) {
1824 if (identifier.length()) {
1825 snprintf (buf, sizeof(buf), "%s/%s%s%" PRIu32 ".wav", dir.c_str(), name.c_str(),
1826 identifier.c_str(), n);
1828 snprintf (buf, sizeof(buf), "%s/%s-%" PRIu32 ".wav", dir.c_str(), name.c_str(), n);
1830 if (access (buf, F_OK) != 0) {
1840 Session::load_sources (const XMLNode& node)
1843 XMLNodeConstIterator niter;
1846 nlist = node.children();
1850 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1852 if ((source = XMLSourceFactory (**niter)) == 0) {
1853 error << _("Session: cannot create Source from XML description.") << endmsg;
1861 Session::XMLSourceFactory (const XMLNode& node)
1865 if (node.name() != "Source") {
1870 if (node.property (X_("destructive")) != 0) {
1871 src = new DestructiveFileSource (node, frame_rate());
1873 src = new FileSource (node, frame_rate());
1877 catch (failed_constructor& err) {
1880 src = ExternalSource::create (node);
1883 catch (failed_constructor& err) {
1884 error << _("Found a sound file that cannot be used by Ardour. See the progammers.") << endmsg;
1893 Session::save_template (string template_name)
1896 string xml_path, bak_path, template_path;
1898 if (_state_of_the_state & CannotSave) {
1903 string dir = template_dir();
1905 if ((dp = opendir (dir.c_str()))) {
1908 if (mkdir (dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)<0) {
1909 error << string_compose(_("Could not create mix templates directory \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
1914 tree.set_root (&get_template());
1917 xml_path += template_name;
1918 xml_path += _template_suffix;
1920 ifstream in(xml_path.c_str());
1923 warning << string_compose(_("Template \"%1\" already exists - new version not created"), template_name) << endmsg;
1929 if (!tree.write (xml_path)) {
1930 error << _("mix template not saved") << endmsg;
1938 Session::rename_template (string old_name, string new_name)
1940 string old_path = template_dir() + old_name + _template_suffix;
1941 string new_path = template_dir() + new_name + _template_suffix;
1943 return rename (old_path.c_str(), new_path.c_str());
1947 Session::delete_template (string name)
1949 string template_path = template_dir();
1950 template_path += name;
1951 template_path += _template_suffix;
1953 return remove (template_path.c_str());
1957 Session::refresh_disk_space ()
1960 struct statfs statfsbuf;
1961 vector<space_and_path>::iterator i;
1962 Glib::Mutex::Lock lm (space_lock);
1965 /* get freespace on every FS that is part of the session path */
1967 _total_free_4k_blocks = 0;
1969 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
1970 statfs ((*i).path.c_str(), &statfsbuf);
1972 scale = statfsbuf.f_bsize/4096.0;
1974 (*i).blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
1975 _total_free_4k_blocks += (*i).blocks;
1981 Session::ensure_sound_dir (string path, string& result)
1986 /* Ensure that the parent directory exists */
1988 if (mkdir (path.c_str(), 0775)) {
1989 if (errno != EEXIST) {
1990 error << string_compose(_("cannot create session directory \"%1\"; ignored"), path) << endmsg;
1995 /* Ensure that the sounds directory exists */
1999 result += sound_dir_name;
2001 if (mkdir (result.c_str(), 0775)) {
2002 if (errno != EEXIST) {
2003 error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
2010 dead += dead_sound_dir_name;
2012 if (mkdir (dead.c_str(), 0775)) {
2013 if (errno != EEXIST) {
2014 error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
2021 peak += peak_dir_name;
2023 if (mkdir (peak.c_str(), 0775)) {
2024 if (errno != EEXIST) {
2025 error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
2030 /* callers expect this to be terminated ... */
2037 Session::discover_best_sound_dir (bool destructive)
2039 vector<space_and_path>::iterator i;
2042 /* destructive files all go into the same place */
2048 /* handle common case without system calls */
2050 if (session_dirs.size() == 1) {
2054 /* OK, here's the algorithm we're following here:
2056 We want to select which directory to use for
2057 the next file source to be created. Ideally,
2058 we'd like to use a round-robin process so as to
2059 get maximum performance benefits from splitting
2060 the files across multiple disks.
2062 However, in situations without much diskspace, an
2063 RR approach may end up filling up a filesystem
2064 with new files while others still have space.
2065 Its therefore important to pay some attention to
2066 the freespace in the filesystem holding each
2067 directory as well. However, if we did that by
2068 itself, we'd keep creating new files in the file
2069 system with the most space until it was as full
2070 as all others, thus negating any performance
2071 benefits of this RAID-1 like approach.
2073 So, we use a user-configurable space threshold. If
2074 there are at least 2 filesystems with more than this
2075 much space available, we use RR selection between them.
2076 If not, then we pick the filesystem with the most space.
2078 This gets a good balance between the two
2082 refresh_disk_space ();
2084 int free_enough = 0;
2086 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2087 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2092 if (free_enough >= 2) {
2094 bool found_it = false;
2096 /* use RR selection process, ensuring that the one
2100 i = last_rr_session_dir;
2103 if (++i == session_dirs.end()) {
2104 i = session_dirs.begin();
2107 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2108 if (ensure_sound_dir ((*i).path, result) == 0) {
2109 last_rr_session_dir = i;
2115 } while (i != last_rr_session_dir);
2118 result = sound_dir();
2123 /* pick FS with the most freespace (and that
2124 seems to actually work ...)
2127 vector<space_and_path> sorted;
2128 space_and_path_ascending_cmp cmp;
2130 sorted = session_dirs;
2131 sort (sorted.begin(), sorted.end(), cmp);
2133 for (i = sorted.begin(); i != sorted.end(); ++i) {
2134 if (ensure_sound_dir ((*i).path, result) == 0) {
2135 last_rr_session_dir = i;
2140 /* if the above fails, fall back to the most simplistic solution */
2142 if (i == sorted.end()) {
2151 Session::load_playlists (const XMLNode& node)
2154 XMLNodeConstIterator niter;
2157 nlist = node.children();
2161 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2163 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2164 error << _("Session: cannot create Playlist from XML description.") << endmsg;
2172 Session::load_unused_playlists (const XMLNode& node)
2175 XMLNodeConstIterator niter;
2178 nlist = node.children();
2182 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2184 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2185 error << _("Session: cannot create Playlist from XML description.") << endmsg;
2189 // now manually untrack it
2191 track_playlist (playlist, false);
2199 Session::XMLPlaylistFactory (const XMLNode& node)
2202 return new AudioPlaylist (*this, node);
2205 catch (failed_constructor& err) {
2211 Session::load_named_selections (const XMLNode& node)
2214 XMLNodeConstIterator niter;
2217 nlist = node.children();
2221 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2223 if ((ns = XMLNamedSelectionFactory (**niter)) == 0) {
2224 error << _("Session: cannot create Named Selection from XML description.") << endmsg;
2232 Session::XMLNamedSelectionFactory (const XMLNode& node)
2235 return new NamedSelection (*this, node);
2238 catch (failed_constructor& err) {
2244 Session::dead_sound_dir () const
2247 res += dead_sound_dir_name;
2253 Session::sound_dir () const
2256 res += sound_dir_name;
2262 Session::tape_dir () const
2265 res += tape_dir_name;
2271 Session::peak_dir () const
2274 res += peak_dir_name;
2280 Session::automation_dir () const
2283 res += "automation/";
2288 Session::template_dir ()
2290 string path = get_user_ardour_path();
2291 path += "templates/";
2297 Session::suffixed_search_path (string suffix, bool data)
2301 path += get_user_ardour_path();
2302 if (path[path.length()-1] != ':') {
2307 path += get_system_data_path();
2309 path += get_system_module_path();
2312 vector<string> split_path;
2314 split (path, split_path, ':');
2317 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
2322 if (distance (i, split_path.end()) != 1) {
2331 Session::template_path ()
2333 return suffixed_search_path (X_("templates"), true);
2337 Session::control_protocol_path ()
2339 return suffixed_search_path (X_("surfaces"), false);
2343 Session::load_connections (const XMLNode& node)
2345 XMLNodeList nlist = node.children();
2346 XMLNodeConstIterator niter;
2350 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2351 if ((*niter)->name() == "InputConnection") {
2352 add_connection (new ARDOUR::InputConnection (**niter));
2353 } else if ((*niter)->name() == "OutputConnection") {
2354 add_connection (new ARDOUR::OutputConnection (**niter));
2356 error << string_compose(_("Unknown node \"%1\" found in Connections list from state file"), (*niter)->name()) << endmsg;
2365 Session::load_edit_groups (const XMLNode& node)
2367 return load_route_groups (node, true);
2371 Session::load_mix_groups (const XMLNode& node)
2373 return load_route_groups (node, false);
2377 Session::load_route_groups (const XMLNode& node, bool edit)
2379 XMLNodeList nlist = node.children();
2380 XMLNodeConstIterator niter;
2385 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2386 if ((*niter)->name() == "RouteGroup") {
2388 rg = add_edit_group ("");
2389 rg->set_state (**niter);
2391 rg = add_mix_group ("");
2392 rg->set_state (**niter);
2401 Session::swap_configuration(Configuration** new_config)
2403 Glib::RWLock::WriterLock lm (route_lock); // jlc - WHY?
2404 Configuration* tmp = *new_config;
2405 *new_config = Config;
2411 Session::copy_configuration(Configuration* new_config)
2413 Glib::RWLock::WriterLock lm (route_lock);
2414 new_config = new Configuration(*Config);
2418 state_file_filter (const string &str, void *arg)
2420 return (str.length() > strlen(Session::statefile_suffix()) &&
2421 str.find (Session::statefile_suffix()) == (str.length() - strlen (Session::statefile_suffix())));
2425 bool operator()(const string* a, const string* b) {
2431 remove_end(string* state)
2433 string statename(*state);
2435 string::size_type start,end;
2436 if ((start = statename.find_last_of ('/')) != string::npos) {
2437 statename = statename.substr (start+1);
2440 if ((end = statename.rfind(".ardour")) < 0) {
2441 end = statename.length();
2444 return new string(statename.substr (0, end));
2448 Session::possible_states (string path)
2450 PathScanner scanner;
2451 vector<string*>* states = scanner (path, state_file_filter, 0, false, false);
2453 transform(states->begin(), states->end(), states->begin(), remove_end);
2456 sort (states->begin(), states->end(), cmp);
2462 Session::possible_states () const
2464 return possible_states(_path);
2468 Session::auto_save()
2470 save_state (_current_snapshot_name);
2474 Session::add_edit_group (string name)
2476 RouteGroup* rg = new RouteGroup (*this, name);
2477 edit_groups.push_back (rg);
2478 edit_group_added (rg); /* EMIT SIGNAL */
2484 Session::add_mix_group (string name)
2486 RouteGroup* rg = new RouteGroup (*this, name, RouteGroup::Relative);
2487 mix_groups.push_back (rg);
2488 mix_group_added (rg); /* EMIT SIGNAL */
2494 Session::remove_edit_group (RouteGroup& rg)
2496 list<RouteGroup*>::iterator i;
2498 if ((i = find (edit_groups.begin(), edit_groups.end(), &rg)) != edit_groups.end()) {
2499 (*i)->apply (&Route::drop_edit_group, this);
2500 edit_groups.erase (i);
2501 edit_group_removed (); /* EMIT SIGNAL */
2508 Session::remove_mix_group (RouteGroup& rg)
2510 list<RouteGroup*>::iterator i;
2512 if ((i = find (mix_groups.begin(), mix_groups.end(), &rg)) != mix_groups.end()) {
2513 (*i)->apply (&Route::drop_mix_group, this);
2514 mix_groups.erase (i);
2515 mix_group_removed (); /* EMIT SIGNAL */
2522 Session::mix_group_by_name (string name)
2524 list<RouteGroup *>::iterator i;
2526 for (i = mix_groups.begin(); i != mix_groups.end(); ++i) {
2527 if ((*i)->name() == name) {
2535 Session::edit_group_by_name (string name)
2537 list<RouteGroup *>::iterator i;
2539 for (i = edit_groups.begin(); i != edit_groups.end(); ++i) {
2540 if ((*i)->name() == name) {
2548 Session::set_meter_hold (float val)
2551 MeterHoldChanged(); // emit
2555 Session::set_meter_falloff (float val)
2557 _meter_falloff = val;
2558 MeterFalloffChanged(); // emit
2563 Session::begin_reversible_command (string name, UndoAction* private_undo)
2565 current_cmd.clear ();
2566 current_cmd.set_name (name);
2569 current_cmd.add_undo (*private_undo);
2574 Session::commit_reversible_command (UndoAction* private_redo)
2579 current_cmd.add_redo_no_execute (*private_redo);
2582 gettimeofday (&now, 0);
2583 current_cmd.set_timestamp (now);
2585 history.add (current_cmd);
2588 Session::GlobalRouteBooleanState
2589 Session::get_global_route_boolean (bool (Route::*method)(void) const)
2591 GlobalRouteBooleanState s;
2592 Glib::RWLock::ReaderLock lm (route_lock);
2594 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2595 if (!(*i)->hidden()) {
2596 RouteBooleanState v;
2599 v.second = ((*i)->*method)();
2608 Session::GlobalRouteMeterState
2609 Session::get_global_route_metering ()
2611 GlobalRouteMeterState s;
2612 Glib::RWLock::ReaderLock lm (route_lock);
2614 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2615 if (!(*i)->hidden()) {
2619 v.second = (*i)->meter_point();
2629 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg)
2631 for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
2632 i->first->set_meter_point (i->second, arg);
2637 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
2639 for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
2640 (i->first->*method) (i->second, arg);
2645 Session::set_global_mute (GlobalRouteBooleanState s, void* src)
2647 set_global_route_boolean (s, &Route::set_mute, src);
2651 Session::set_global_solo (GlobalRouteBooleanState s, void* src)
2653 set_global_route_boolean (s, &Route::set_solo, src);
2657 Session::set_global_record_enable (GlobalRouteBooleanState s, void* src)
2659 set_global_route_boolean (s, &Route::set_record_enable, src);
2663 Session::global_mute_memento (void* src)
2665 return sigc::bind (mem_fun (*this, &Session::set_global_mute), get_global_route_boolean (&Route::muted), src);
2669 Session::global_metering_memento (void* src)
2671 return sigc::bind (mem_fun (*this, &Session::set_global_route_metering), get_global_route_metering (), src);
2675 Session::global_solo_memento (void* src)
2677 return sigc::bind (mem_fun (*this, &Session::set_global_solo), get_global_route_boolean (&Route::soloed), src);
2681 Session::global_record_enable_memento (void* src)
2683 return sigc::bind (mem_fun (*this, &Session::set_global_record_enable), get_global_route_boolean (&Route::record_enabled), src);
2687 template_filter (const string &str, void *arg)
2689 return (str.length() > strlen(Session::template_suffix()) &&
2690 str.find (Session::template_suffix()) == (str.length() - strlen (Session::template_suffix())));
2694 Session::get_template_list (list<string> &template_names)
2696 vector<string *> *templates;
2697 PathScanner scanner;
2700 path = template_path ();
2702 templates = scanner (path, template_filter, 0, false, true);
2704 vector<string*>::iterator i;
2705 for (i = templates->begin(); i != templates->end(); ++i) {
2706 string fullpath = *(*i);
2709 start = fullpath.find_last_of ('/') + 1;
2710 if ((end = fullpath.find_last_of ('.')) <0) {
2711 end = fullpath.length();
2714 template_names.push_back(fullpath.substr(start, (end-start)));
2719 Session::read_favorite_dirs (FavoriteDirs & favs)
2721 string path = get_user_ardour_path();
2722 path += "/favorite_dirs";
2724 ifstream fav (path.c_str());
2729 if (errno != ENOENT) {
2730 //error << string_compose (_("cannot open favorite file %1 (%2)"), path, strerror (errno)) << endmsg;
2741 getline(fav, newfav);
2747 favs.push_back (newfav);
2754 Session::write_favorite_dirs (FavoriteDirs & favs)
2756 string path = get_user_ardour_path();
2757 path += "/favorite_dirs";
2759 ofstream fav (path.c_str());
2765 for (FavoriteDirs::iterator i = favs.begin(); i != favs.end(); ++i) {
2766 fav << (*i) << endl;
2773 accept_all_non_peak_files (const string& path, void *arg)
2775 return (path.length() > 5 && path.find (".peak") != (path.length() - 5));
2779 accept_all_state_files (const string& path, void *arg)
2781 return (path.length() > 7 && path.find (".ardour") == (path.length() - 7));
2785 Session::find_all_sources (string path, set<string>& result)
2790 if (!tree.read (path)) {
2794 if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2799 XMLNodeConstIterator niter;
2801 nlist = node->children();
2805 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2809 if ((prop = (*niter)->property (X_("name"))) == 0) {
2813 if (prop->value()[0] == '/') {
2814 /* external file, ignore */
2818 string path = _path; /* /-terminated */
2819 path += sound_dir_name;
2821 path += prop->value();
2823 result.insert (path);
2830 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2832 PathScanner scanner;
2833 vector<string*>* state_files;
2835 string this_snapshot_path;
2841 if (ripped[ripped.length()-1] == '/') {
2842 ripped = ripped.substr (0, ripped.length() - 1);
2845 state_files = scanner (ripped, accept_all_state_files, (void *) 0, false, true);
2847 if (state_files == 0) {
2852 this_snapshot_path = _path;
2853 this_snapshot_path += _current_snapshot_name;
2854 this_snapshot_path += _statefile_suffix;
2856 for (vector<string*>::iterator i = state_files->begin(); i != state_files->end(); ++i) {
2858 if (exclude_this_snapshot && **i == this_snapshot_path) {
2862 if (find_all_sources (**i, result) < 0) {
2871 Session::cleanup_sources (Session::cleanup_report& rep)
2873 vector<Source*> dead_sources;
2874 vector<Playlist*> playlists_tbd;
2875 PathScanner scanner;
2877 vector<space_and_path>::iterator i;
2878 vector<space_and_path>::iterator nexti;
2879 vector<string*>* soundfiles;
2880 vector<string> unused;
2881 set<string> all_sources;
2886 _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
2888 /* step 1: consider deleting all unused playlists */
2890 for (PlaylistList::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
2893 status = AskAboutPlaylistDeletion (*x);
2902 playlists_tbd.push_back (*x);
2906 /* leave it alone */
2911 /* now delete any that were marked for deletion */
2913 for (vector<Playlist*>::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
2914 PlaylistList::iterator foo;
2916 if ((foo = unused_playlists.find (*x)) != unused_playlists.end()) {
2917 unused_playlists.erase (foo);
2922 /* step 2: clear the undo/redo history for all playlists */
2924 for (PlaylistList::iterator x = playlists.begin(); x != playlists.end(); ++x) {
2925 (*x)->drop_all_states ();
2928 /* step 3: find all un-referenced sources */
2933 for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
2935 SourceList::iterator tmp;
2940 /* only remove files that are not in use and have some size
2941 to them. otherwise we remove the current "nascent"
2945 if ((*i).second->use_cnt() == 0 && (*i).second->length() > 0) {
2946 dead_sources.push_back (i->second);
2948 /* remove this source from our own list to avoid us
2949 adding it to the list of all sources below
2958 /* Step 4: get rid of all regions in the region list that use any dead sources
2959 in case the sources themselves don't go away (they might be referenced in
2963 for (vector<Source*>::iterator i = dead_sources.begin(); i != dead_sources.end();++i) {
2965 for (AudioRegionList::iterator r = audio_regions.begin(); r != audio_regions.end(); ) {
2966 AudioRegionList::iterator tmp;
2974 for (uint32_t n = 0; n < ar->n_channels(); ++n) {
2975 if (&ar->source (n) == (*i)) {
2976 /* this region is dead */
2985 /* build a list of all the possible sound directories for the session */
2987 for (i = session_dirs.begin(); i != session_dirs.end(); ) {
2992 sound_path += (*i).path;
2993 sound_path += sound_dir_name;
2995 if (nexti != session_dirs.end()) {
3002 /* now do the same thing for the files that ended up in the sounds dir(s)
3003 but are not referenced as sources in any snapshot.
3006 soundfiles = scanner (sound_path, accept_all_non_peak_files, (void *) 0, false, true);
3008 if (soundfiles == 0) {
3012 /* find all sources, but don't use this snapshot because the
3013 state file on disk still references sources we may have already
3017 find_all_sources_across_snapshots (all_sources, true);
3019 /* add our current source list
3022 for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
3024 ExternalSource* sfs;
3026 if ((fs = dynamic_cast<FileSource*> ((*i).second)) != 0) {
3027 all_sources.insert (fs->path());
3028 } else if ((sfs = dynamic_cast<ExternalSource*> ((*i).second)) != 0) {
3029 all_sources.insert (sfs->path());
3033 for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
3038 for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
3048 unused.push_back (spath);
3052 /* now try to move all unused files into the "dead_sounds" directory(ies) */
3054 for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
3055 struct stat statbuf;
3057 rep.paths.push_back (*x);
3058 if (stat ((*x).c_str(), &statbuf) == 0) {
3059 rep.space += statbuf.st_size;
3064 /* don't move the file across filesystems, just
3065 stick it in the `dead_sound_dir_name' directory
3066 on whichever filesystem it was already on.
3069 newpath = Glib::path_get_dirname (*x);
3070 newpath = Glib::path_get_dirname (newpath);
3073 newpath += dead_sound_dir_name;
3075 newpath += Glib::path_get_basename ((*x));
3077 if (access (newpath.c_str(), F_OK) == 0) {
3079 /* the new path already exists, try versioning */
3081 char buf[PATH_MAX+1];
3085 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
3088 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
3089 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
3093 if (version == 999) {
3094 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
3098 newpath = newpath_v;
3103 /* it doesn't exist, or we can't read it or something */
3107 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
3108 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
3109 (*x), newpath, strerror (errno))
3115 /* see if there an easy to find peakfile for this file, and remove it.
3118 string peakpath = (*x).substr (0, (*x).find_last_of ('.'));
3119 peakpath += ".peak";
3121 if (access (peakpath.c_str(), W_OK) == 0) {
3122 if (::unlink (peakpath.c_str()) != 0) {
3123 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
3124 peakpath, _path, strerror (errno))
3126 /* try to back out */
3127 rename (newpath.c_str(), _path.c_str());
3136 /* dump the history list */
3140 /* save state so we don't end up a session file
3141 referring to non-existent sources.
3147 _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
3152 Session::cleanup_trash_sources (Session::cleanup_report& rep)
3154 vector<space_and_path>::iterator i;
3155 string dead_sound_dir;
3156 struct dirent* dentry;
3157 struct stat statbuf;
3163 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3165 dead_sound_dir = (*i).path;
3166 dead_sound_dir += dead_sound_dir_name;
3168 if ((dead = opendir (dead_sound_dir.c_str())) == 0) {
3172 while ((dentry = readdir (dead)) != 0) {
3174 /* avoid '.' and '..' */
3176 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
3177 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
3183 fullpath = dead_sound_dir;
3185 fullpath += dentry->d_name;
3187 if (stat (fullpath.c_str(), &statbuf)) {
3191 if (!S_ISREG (statbuf.st_mode)) {
3195 if (unlink (fullpath.c_str())) {
3196 error << string_compose (_("cannot remove dead sound file %1 (%2)"),
3197 fullpath, strerror (errno))
3201 rep.paths.push_back (dentry->d_name);
3202 rep.space += statbuf.st_size;
3213 Session::set_dirty ()
3215 bool was_dirty = dirty();
3217 _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
3220 DirtyChanged(); /* EMIT SIGNAL */
3226 Session::set_clean ()
3228 bool was_dirty = dirty();
3230 _state_of_the_state = Clean;
3233 DirtyChanged(); /* EMIT SIGNAL */