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/audio_diskstream.h>
62 #include <ardour/utils.h>
63 #include <ardour/audioplaylist.h>
64 #include <ardour/audiofilesource.h>
65 #include <ardour/destructive_filesource.h>
66 #include <ardour/sndfile_helpers.h>
67 #include <ardour/auditioner.h>
68 #include <ardour/export.h>
69 #include <ardour/redirect.h>
70 #include <ardour/send.h>
71 #include <ardour/insert.h>
72 #include <ardour/connection.h>
73 #include <ardour/slave.h>
74 #include <ardour/tempo.h>
75 #include <ardour/audio_track.h>
76 #include <ardour/cycle_timer.h>
77 #include <ardour/utils.h>
78 #include <ardour/named_selection.h>
79 #include <ardour/version.h>
80 #include <ardour/location.h>
81 #include <ardour/audioregion.h>
82 #include <ardour/crossfade.h>
83 #include <ardour/control_protocol_manager.h>
89 using namespace ARDOUR;
93 Session::first_stage_init (string fullpath, string snapshot_name)
95 if (fullpath.length() == 0) {
96 throw failed_constructor();
100 if (!realpath(fullpath.c_str(), buf) && (errno != ENOENT)) {
101 error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
102 throw failed_constructor();
106 if (_path[_path.length()-1] != '/') {
110 /* these two are just provisional settings. set_state()
111 will likely override them.
114 _name = _current_snapshot_name = snapshot_name;
115 setup_raid_path (_path);
117 _current_frame_rate = _engine.frame_rate ();
118 _tempo_map = new TempoMap (_current_frame_rate);
119 _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
121 g_atomic_int_set (&processing_prohibited, 0);
124 _transport_speed = 0;
125 _last_transport_speed = 0;
126 transport_sub_state = 0;
127 _transport_frame = 0;
129 end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
130 start_location = new Location (0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
131 _end_location_is_free = true;
132 g_atomic_int_set (&_record_status, Disabled);
137 seamless_loop = false;
138 loop_changing = false;
140 crossfades_active = false;
143 _last_roll_location = 0;
144 _last_record_location = 0;
145 pending_locate_frame = 0;
146 pending_locate_roll = false;
147 pending_locate_flush = false;
148 dstream_buffer_size = 0;
150 state_was_pending = false;
152 outbound_mtc_smpte_frame = 0;
153 next_quarter_frame_to_send = -1;
154 current_block_size = 0;
155 _solo_latched = true;
156 _solo_model = InverseMute;
157 solo_update_disabled = false;
158 currently_soloing = false;
159 _have_captured = false;
160 _worst_output_latency = 0;
161 _worst_input_latency = 0;
162 _worst_track_latency = 0;
163 _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
166 butler_mixdown_buffer = 0;
167 butler_gain_buffer = 0;
171 post_transport_work = PostTransportWork (0);
172 g_atomic_int_set (&butler_should_do_transport_work, 0);
173 g_atomic_int_set (&butler_active, 0);
174 g_atomic_int_set (&_playback_load, 100);
175 g_atomic_int_set (&_capture_load, 100);
176 g_atomic_int_set (&_playback_load_min, 100);
177 g_atomic_int_set (&_capture_load_min, 100);
178 pending_audition_region = 0;
180 pending_edit_mode = _edit_mode;
182 input_auto_connect = AutoConnectOption (0);
183 output_auto_connect = AutoConnectOption (0);
184 waiting_to_start = false;
186 _gain_automation_buffer = 0;
187 _pan_automation_buffer = 0;
189 pending_abort = false;
190 layer_model = MoveAddHigher;
191 xfade_model = ShortCrossfade;
192 destructive_index = 0;
194 /* allocate conversion buffers */
195 _conversion_buffers[ButlerContext] = new char[AudioDiskstream::disk_io_frames() * 4];
196 _conversion_buffers[TransportContext] = new char[AudioDiskstream::disk_io_frames() * 4];
198 /* default short fade = 15ms */
200 Crossfade::set_short_xfade_length ((jack_nframes_t) floor ((15.0 * frame_rate()) / 1000.0));
201 DestructiveFileSource::setup_standard_crossfades (frame_rate());
203 last_mmc_step.tv_sec = 0;
204 last_mmc_step.tv_usec = 0;
207 preroll.type = AnyTime::Frames;
209 postroll.type = AnyTime::Frames;
212 /* click sounds are unset by default, which causes us to internal
213 waveforms for clicks.
217 click_requested = false;
219 click_emphasis_data = 0;
221 click_emphasis_length = 0;
223 process_function = &Session::process_with_events;
227 _smpte_offset_negative = true;
228 last_smpte_valid = false;
230 last_rr_session_dir = session_dirs.begin();
231 refresh_disk_space ();
233 // set_default_fade (0.2, 5.0); /* steepness, millisecs */
235 /* default configuration */
237 do_not_record_plugins = false;
238 over_length_short = 2;
239 over_length_long = 10;
240 send_midi_timecode = false;
241 send_midi_machine_control = false;
242 shuttle_speed_factor = 1.0;
243 shuttle_speed_threshold = 5;
245 _meter_hold = 100; // XXX unknown units: number of calls to meter::set()
246 _meter_falloff = 1.5f; // XXX unknown units: refresh_rate
252 average_slave_delta = 1800;
253 have_first_delta_accumulator = false;
254 delta_accumulator_cnt = 0;
255 slave_state = Stopped;
257 /* default SMPTE type is 30 FPS, non-drop */
259 set_smpte_type (30.0, false);
261 _engine.GraphReordered.connect (mem_fun (*this, &Session::graph_reordered));
263 /* These are all static "per-class" signals */
265 Region::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
266 AudioSource::AudioSourceCreated.connect (mem_fun (*this, &Session::add_audio_source));
267 Playlist::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
268 Redirect::RedirectCreated.connect (mem_fun (*this, &Session::add_redirect));
269 AudioDiskstream::AudioDiskstreamCreated.connect (mem_fun (*this, &Session::add_diskstream));
270 NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
272 Controllable::Created.connect (mem_fun (*this, &Session::add_controllable));
273 Controllable::GoingAway.connect (mem_fun (*this, &Session::remove_controllable));
275 IO::MoreOutputs.connect (mem_fun (*this, &Session::ensure_passthru_buffers));
277 /* stop IO objects from doing stuff until we're ready for them */
279 IO::disable_panners ();
280 IO::disable_ports ();
281 IO::disable_connecting ();
285 Session::second_stage_init (bool new_session)
287 AudioFileSource::set_peak_dir (peak_dir());
290 if (load_state (_current_snapshot_name)) {
293 remove_empty_sounds ();
296 if (start_butler_thread()) {
300 if (start_midi_thread ()) {
305 if (set_state (*state_tree->root())) {
310 /* we can't save till after ::when_engine_running() is called,
311 because otherwise we save state with no connections made.
312 therefore, we reset _state_of_the_state because ::set_state()
313 will have cleared it.
315 we also have to include Loading so that any events that get
316 generated between here and the end of ::when_engine_running()
317 will be processed directly rather than queued.
320 _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave|Loading);
322 // set_auto_input (true);
323 _locations.changed.connect (mem_fun (this, &Session::locations_changed));
324 _locations.added.connect (mem_fun (this, &Session::locations_added));
325 setup_click_sounds (0);
326 setup_midi_control ();
328 /* Pay attention ... */
330 _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
331 _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
333 if (_engine.running()) {
334 when_engine_running();
336 first_time_running = _engine.Running.connect (mem_fun (*this, &Session::when_engine_running));
339 send_full_time_code ();
340 _engine.transport_locate (0);
341 deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
342 deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
344 ControlProtocolManager::instance().set_session (*this);
347 _end_location_is_free = true;
349 _end_location_is_free = false;
356 Session::raid_path () const
360 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
365 return path.substr (0, path.length() - 1); // drop final colon
369 Session::set_raid_path (string path)
371 /* public-access to setup_raid_path() */
373 setup_raid_path (path);
377 Session::setup_raid_path (string path)
379 string::size_type colon;
383 string::size_type len = path.length();
388 if (path.length() == 0) {
392 session_dirs.clear ();
394 for (string::size_type n = 0; n < len; ++n) {
395 if (path[n] == ':') {
402 /* no multiple search path, just one location (common case) */
406 session_dirs.push_back (sp);
413 if (fspath[fspath.length()-1] != '/') {
416 fspath += sound_dir_name;
422 if (fspath[fspath.length()-1] != '/') {
425 fspath += tape_dir_name;
427 AudioFileSource::set_search_path (fspath);
434 while ((colon = remaining.find_first_of (':')) != string::npos) {
437 sp.path = remaining.substr (0, colon);
438 session_dirs.push_back (sp);
440 /* add sounds to file search path */
443 if (fspath[fspath.length()-1] != '/') {
446 fspath += sound_dir_name;
449 /* add tape dir to file search path */
452 if (fspath[fspath.length()-1] != '/') {
455 fspath += tape_dir_name;
458 remaining = remaining.substr (colon+1);
461 if (remaining.length()) {
468 if (fspath[fspath.length()-1] != '/') {
471 fspath += sound_dir_name;
475 if (fspath[fspath.length()-1] != '/') {
478 fspath += tape_dir_name;
480 session_dirs.push_back (sp);
483 /* set the AudioFileSource search path */
485 AudioFileSource::set_search_path (fspath);
487 /* reset the round-robin soundfile path thingie */
489 last_rr_session_dir = session_dirs.begin();
493 Session::create (bool& new_session, string* mix_template, jack_nframes_t initial_length)
497 if (mkdir (_path.c_str(), 0755) < 0) {
498 if (errno == EEXIST) {
501 error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
510 if (mkdir (dir.c_str(), 0755) < 0) {
511 if (errno != EEXIST) {
512 error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
519 if (mkdir (dir.c_str(), 0755) < 0) {
520 if (errno != EEXIST) {
521 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
528 if (mkdir (dir.c_str(), 0755) < 0) {
529 if (errno != EEXIST) {
530 error << string_compose(_("Session: cannot create session tape dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
535 dir = dead_sound_dir ();
537 if (mkdir (dir.c_str(), 0755) < 0) {
538 if (errno != EEXIST) {
539 error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
544 dir = automation_dir ();
546 if (mkdir (dir.c_str(), 0755) < 0) {
547 if (errno != EEXIST) {
548 error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
554 /* check new_session so we don't overwrite an existing one */
558 std::string in_path = *mix_template;
560 ifstream in(in_path.c_str());
563 string out_path = _path;
565 out_path += _statefile_suffix;
567 ofstream out(out_path.c_str());
572 // okay, session is set up. Treat like normal saved
573 // session from now on.
579 error << string_compose (_("Could not open %1 for writing mix template"), out_path)
585 error << string_compose (_("Could not open mix template %1 for reading"), in_path)
592 warning << _("Session already exists. Not overwriting") << endmsg;
599 /* set initial start + end point */
601 start_location->set_end (0);
602 _locations.add (start_location);
604 end_location->set_end (initial_length);
605 _locations.add (end_location);
607 _state_of_the_state = Clean;
609 if (save_state (_current_snapshot_name)) {
618 Session::load_diskstreams (const XMLNode& node)
621 XMLNodeConstIterator citer;
623 clist = node.children();
625 for (citer = clist.begin(); citer != clist.end(); ++citer) {
627 AudioDiskstream* dstream;
630 dstream = new AudioDiskstream (*this, **citer);
631 /* added automatically by AudioDiskstreamCreated handler */
634 catch (failed_constructor& err) {
635 error << _("Session: could not load diskstream via XML state") << endmsg;
644 Session::remove_pending_capture_state ()
649 xml_path += _current_snapshot_name;
650 xml_path += _pending_suffix;
652 unlink (xml_path.c_str());
656 Session::save_state (string snapshot_name, bool pending)
662 if (_state_of_the_state & CannotSave) {
666 tree.set_root (&get_state());
668 if (snapshot_name.empty()) {
669 snapshot_name = _current_snapshot_name;
675 xml_path += snapshot_name;
676 xml_path += _statefile_suffix;
680 // Make backup of state file
682 if ((access (xml_path.c_str(), F_OK) == 0) &&
683 (rename(xml_path.c_str(), bak_path.c_str()))) {
684 error << _("could not backup old state file, current state not saved.") << endmsg;
691 xml_path += snapshot_name;
692 xml_path += _pending_suffix;
696 if (!tree.write (xml_path)) {
697 error << string_compose (_("state could not be saved to %1"), xml_path) << endmsg;
699 /* don't leave a corrupt file lying around if it is
703 if (unlink (xml_path.c_str())) {
704 error << string_compose (_("could not remove corrupt state file %1"), xml_path) << endmsg;
707 if (rename (bak_path.c_str(), xml_path.c_str())) {
708 error << string_compose (_("could not restore state file from backup %1"), bak_path) << endmsg;
718 bool was_dirty = dirty();
720 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
723 DirtyChanged (); /* EMIT SIGNAL */
726 StateSaved (snapshot_name); /* EMIT SIGNAL */
733 Session::restore_state (string snapshot_name)
735 if (load_state (snapshot_name) == 0) {
736 set_state (*state_tree->root());
743 Session::load_state (string snapshot_name)
752 state_was_pending = false;
754 /* check for leftover pending state from a crashed capture attempt */
757 xmlpath += snapshot_name;
758 xmlpath += _pending_suffix;
760 if (!access (xmlpath.c_str(), F_OK)) {
762 /* there is pending state from a crashed capture attempt */
764 if (AskAboutPendingState()) {
765 state_was_pending = true;
769 if (!state_was_pending) {
772 xmlpath += snapshot_name;
773 xmlpath += _statefile_suffix;
776 if (access (xmlpath.c_str(), F_OK)) {
777 error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
781 state_tree = new XMLTree;
785 if (state_tree->read (xmlpath)) {
788 error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
797 Session::load_options (const XMLNode& node)
801 bool have_fade_msecs = false;
802 bool have_fade_steepness = false;
803 float fade_msecs = 0;
804 float fade_steepness = 0;
805 SlaveSource slave_src = None;
807 LocaleGuard lg (X_("POSIX"));
809 if ((child = find_named_node (node, "input-auto-connect")) != 0) {
810 if ((prop = child->property ("val")) != 0) {
811 sscanf (prop->value().c_str(), "%x", &x);
812 input_auto_connect = AutoConnectOption (x);
816 if ((child = find_named_node (node, "output-auto-connect")) != 0) {
817 if ((prop = child->property ("val")) != 0) {
818 sscanf (prop->value().c_str(), "%x", &x);
819 output_auto_connect = AutoConnectOption (x);
823 if ((child = find_named_node (node, "slave")) != 0) {
824 if ((prop = child->property ("type")) != 0) {
825 if (prop->value() == "none") {
827 } else if (prop->value() == "mtc") {
829 } else if (prop->value() == "jack") {
832 set_slave_source (slave_src, 0);
836 /* we cannot set edit mode if we are loading a session,
837 because it might destroy the playlist's positioning
840 if ((child = find_named_node (node, "edit-mode")) != 0) {
841 if ((prop = child->property ("val")) != 0) {
842 if (prop->value() == "slide") {
843 pending_edit_mode = Slide;
844 } else if (prop->value() == "splice") {
845 pending_edit_mode = Splice;
850 if ((child = find_named_node (node, "send-midi-timecode")) != 0) {
851 if ((prop = child->property ("val")) != 0) {
852 bool x = (prop->value() == "yes");
853 send_mtc = !x; /* force change in value */
857 if ((child = find_named_node (node, "send-midi-machine-control")) != 0) {
858 if ((prop = child->property ("val")) != 0) {
859 bool x = (prop->value() == "yes");
860 send_mmc = !x; /* force change in value */
861 set_send_mmc (prop->value() == "yes");
864 if ((child = find_named_node (node, "max-level")) != 0) {
865 if ((prop = child->property ("val")) != 0) {
866 max_level = atoi (prop->value().c_str());
869 if ((child = find_named_node (node, "min-level")) != 0) {
870 if ((prop = child->property ("val")) != 0) {
871 min_level = atoi (prop->value().c_str());
874 if ((child = find_named_node (node, "meter-hold")) != 0) {
875 if ((prop = child->property ("val")) != 0) {
876 _meter_hold = atof (prop->value().c_str());
879 if ((child = find_named_node (node, "meter-falloff")) != 0) {
880 if ((prop = child->property ("val")) != 0) {
881 _meter_falloff = atof (prop->value().c_str());
884 if ((child = find_named_node (node, "long-over-length")) != 0) {
885 if ((prop = child->property ("val")) != 0) {
886 over_length_long = atoi (prop->value().c_str());
889 if ((child = find_named_node (node, "short-over-length")) != 0) {
890 if ((prop = child->property ("val")) != 0) {
891 over_length_short = atoi (prop->value().c_str());
894 if ((child = find_named_node (node, "shuttle-speed-factor")) != 0) {
895 if ((prop = child->property ("val")) != 0) {
896 shuttle_speed_factor = atof (prop->value().c_str());
899 if ((child = find_named_node (node, "shuttle-speed-threshold")) != 0) {
900 if ((prop = child->property ("val")) != 0) {
901 shuttle_speed_threshold = atof (prop->value().c_str());
904 if ((child = find_named_node (node, "rf-speed")) != 0) {
905 if ((prop = child->property ("val")) != 0) {
906 rf_speed = atof (prop->value().c_str());
909 if ((child = find_named_node (node, "smpte-frames-per-second")) != 0) {
910 if ((prop = child->property ("val")) != 0) {
911 set_smpte_type( atof (prop->value().c_str()), smpte_drop_frames );
914 if ((child = find_named_node (node, "smpte-drop-frames")) != 0) {
915 if ((prop = child->property ("val")) != 0) {
916 set_smpte_type( smpte_frames_per_second, (prop->value() == "yes") );
919 if ((child = find_named_node (node, "smpte-offset")) != 0) {
920 if ((prop = child->property ("val")) != 0) {
921 set_smpte_offset( atoi (prop->value().c_str()) );
924 if ((child = find_named_node (node, "smpte-offset-negative")) != 0) {
925 if ((prop = child->property ("val")) != 0) {
926 set_smpte_offset_negative( (prop->value() == "yes") );
929 if ((child = find_named_node (node, "click-sound")) != 0) {
930 if ((prop = child->property ("val")) != 0) {
931 click_sound = prop->value();
934 if ((child = find_named_node (node, "click-emphasis-sound")) != 0) {
935 if ((prop = child->property ("val")) != 0) {
936 click_emphasis_sound = prop->value();
940 if ((child = find_named_node (node, "solo-model")) != 0) {
941 if ((prop = child->property ("val")) != 0) {
942 if (prop->value() == "SoloBus")
943 _solo_model = SoloBus;
945 _solo_model = InverseMute;
949 /* BOOLEAN OPTIONS */
951 if ((child = find_named_node (node, "auto-play")) != 0) {
952 if ((prop = child->property ("val")) != 0) {
953 set_auto_play (prop->value() == "yes");
956 if ((child = find_named_node (node, "auto-input")) != 0) {
957 if ((prop = child->property ("val")) != 0) {
958 set_auto_input (prop->value() == "yes");
961 if ((child = find_named_node (node, "seamless-loop")) != 0) {
962 if ((prop = child->property ("val")) != 0) {
963 set_seamless_loop (prop->value() == "yes");
966 if ((child = find_named_node (node, "punch-in")) != 0) {
967 if ((prop = child->property ("val")) != 0) {
968 set_punch_in (prop->value() == "yes");
971 if ((child = find_named_node (node, "punch-out")) != 0) {
972 if ((prop = child->property ("val")) != 0) {
973 set_punch_out (prop->value() == "yes");
976 if ((child = find_named_node (node, "auto-return")) != 0) {
977 if ((prop = child->property ("val")) != 0) {
978 set_auto_return (prop->value() == "yes");
981 if ((child = find_named_node (node, "send-mtc")) != 0) {
982 if ((prop = child->property ("val")) != 0) {
983 set_send_mtc (prop->value() == "yes");
986 if ((child = find_named_node (node, "mmc-control")) != 0) {
987 if ((prop = child->property ("val")) != 0) {
988 set_mmc_control (prop->value() == "yes");
991 if ((child = find_named_node (node, "midi-control")) != 0) {
992 if ((prop = child->property ("val")) != 0) {
993 set_midi_control (prop->value() == "yes");
996 if ((child = find_named_node (node, "midi-feedback")) != 0) {
997 if ((prop = child->property ("val")) != 0) {
998 set_midi_feedback (prop->value() == "yes");
1001 // Legacy support for <recording-plugins>
1002 if ((child = find_named_node (node, "recording-plugins")) != 0) {
1003 if ((prop = child->property ("val")) != 0) {
1004 set_do_not_record_plugins (prop->value() == "no");
1007 if ((child = find_named_node (node, "do-not-record-plugins")) != 0) {
1008 if ((prop = child->property ("val")) != 0) {
1009 set_do_not_record_plugins (prop->value() == "yes");
1012 if ((child = find_named_node (node, "crossfades-active")) != 0) {
1013 if ((prop = child->property ("val")) != 0) {
1014 set_crossfades_active (prop->value() == "yes");
1017 if ((child = find_named_node (node, "audible-click")) != 0) {
1018 if ((prop = child->property ("val")) != 0) {
1019 set_clicking (prop->value() == "yes");
1023 if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
1024 if ((prop = child->property ("val")) != 0) {
1025 _end_location_is_free = (prop->value() == "yes");
1029 if ((child = find_named_node (node, "layer-model")) != 0) {
1030 if ((prop = child->property ("val")) != 0) {
1031 if (prop->value() == X_("LaterHigher")) {
1032 set_layer_model (LaterHigher);
1033 } else if (prop->value() == X_("AddHigher")) {
1034 set_layer_model (AddHigher);
1036 set_layer_model (MoveAddHigher);
1041 if ((child = find_named_node (node, "xfade-model")) != 0) {
1042 if ((prop = child->property ("val")) != 0) {
1043 if (prop->value() == X_("Short")) {
1044 set_xfade_model (ShortCrossfade);
1046 set_xfade_model (FullCrossfade);
1051 if ((child = find_named_node (node, "short-xfade-length")) != 0) {
1052 if ((prop = child->property ("val")) != 0) {
1053 /* value is stored as a fractional seconds */
1054 float secs = atof (prop->value().c_str());
1055 Crossfade::set_short_xfade_length ((jack_nframes_t) floor (secs * frame_rate()));
1059 if ((child = find_named_node (node, "full-xfades-unmuted")) != 0) {
1060 if ((prop = child->property ("val")) != 0) {
1061 crossfades_active = (prop->value() == "yes");
1067 if ((child = find_named_node (node, "default-fade-steepness")) != 0) {
1068 if ((prop = child->property ("val")) != 0) {
1069 fade_steepness = atof (prop->value().c_str());
1070 have_fade_steepness = true;
1073 if ((child = find_named_node (node, "default-fade-msec")) != 0) {
1074 if ((prop = child->property ("val")) != 0) {
1075 fade_msecs = atof (prop->value().c_str());
1076 have_fade_msecs = true;
1080 if (have_fade_steepness || have_fade_msecs) {
1081 // set_default_fade (fade_steepness, fade_msecs);
1088 Session::get_options () const
1093 LocaleGuard lg (X_("POSIX"));
1095 opthead = new XMLNode ("Options");
1097 SlaveSource src = slave_source ();
1101 src_string = "none";
1107 src_string = "jack";
1110 child = opthead->add_child ("slave");
1111 child->add_property ("type", src_string);
1113 child = opthead->add_child ("send-midi-timecode");
1114 child->add_property ("val", send_midi_timecode?"yes":"no");
1116 child = opthead->add_child ("send-midi-machine-control");
1117 child->add_property ("val", send_midi_machine_control?"yes":"no");
1119 snprintf (buf, sizeof(buf)-1, "%x", (int) input_auto_connect);
1120 child = opthead->add_child ("input-auto-connect");
1121 child->add_property ("val", buf);
1123 snprintf (buf, sizeof(buf)-1, "%x", (int) output_auto_connect);
1124 child = opthead->add_child ("output-auto-connect");
1125 child->add_property ("val", buf);
1127 snprintf (buf, sizeof(buf)-1, "%d", max_level);
1128 child = opthead->add_child ("max-level");
1129 child->add_property ("val", buf);
1131 snprintf (buf, sizeof(buf)-1, "%d", min_level);
1132 child = opthead->add_child ("min-level");
1133 child->add_property ("val", buf);
1135 snprintf (buf, sizeof(buf)-1, "%f", _meter_hold);
1136 child = opthead->add_child ("meter-hold");
1137 child->add_property ("val", buf);
1139 snprintf (buf, sizeof(buf)-1, "%f", _meter_falloff);
1140 child = opthead->add_child ("meter-falloff");
1141 child->add_property ("val", buf);
1143 snprintf (buf, sizeof(buf)-1, "%u", over_length_long);
1144 child = opthead->add_child ("long-over-length");
1145 child->add_property ("val", buf);
1147 snprintf (buf, sizeof(buf)-1, "%u", over_length_short);
1148 child = opthead->add_child ("short-over-length");
1149 child->add_property ("val", buf);
1151 snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_factor);
1152 child = opthead->add_child ("shuttle-speed-factor");
1153 child->add_property ("val", buf);
1155 snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_threshold);
1156 child = opthead->add_child ("shuttle-speed-threshold");
1157 child->add_property ("val", buf);
1159 snprintf (buf, sizeof(buf)-1, "%f", rf_speed);
1160 child = opthead->add_child ("rf-speed");
1161 child->add_property ("val", buf);
1163 snprintf (buf, sizeof(buf)-1, "%.2f", smpte_frames_per_second);
1164 child = opthead->add_child ("smpte-frames-per-second");
1165 child->add_property ("val", buf);
1167 child = opthead->add_child ("smpte-drop-frames");
1168 child->add_property ("val", smpte_drop_frames ? "yes" : "no");
1170 snprintf (buf, sizeof(buf)-1, "%u", smpte_offset ());
1171 child = opthead->add_child ("smpte-offset");
1172 child->add_property ("val", buf);
1174 child = opthead->add_child ("smpte-offset-negative");
1175 child->add_property ("val", smpte_offset_negative () ? "yes" : "no");
1177 child = opthead->add_child ("edit-mode");
1178 switch (_edit_mode) {
1180 child->add_property ("val", "splice");
1184 child->add_property ("val", "slide");
1188 child = opthead->add_child ("auto-play");
1189 child->add_property ("val", get_auto_play () ? "yes" : "no");
1190 child = opthead->add_child ("auto-input");
1191 child->add_property ("val", get_auto_input () ? "yes" : "no");
1192 child = opthead->add_child ("seamless-loop");
1193 child->add_property ("val", get_seamless_loop () ? "yes" : "no");
1194 child = opthead->add_child ("punch-in");
1195 child->add_property ("val", get_punch_in () ? "yes" : "no");
1196 child = opthead->add_child ("punch-out");
1197 child->add_property ("val", get_punch_out () ? "yes" : "no");
1198 child = opthead->add_child ("all-safe");
1199 child->add_property ("val", get_all_safe () ? "yes" : "no");
1200 child = opthead->add_child ("auto-return");
1201 child->add_property ("val", get_auto_return () ? "yes" : "no");
1202 child = opthead->add_child ("mmc-control");
1203 child->add_property ("val", get_mmc_control () ? "yes" : "no");
1204 child = opthead->add_child ("midi-control");
1205 child->add_property ("val", get_midi_control () ? "yes" : "no");
1206 child = opthead->add_child ("midi-feedback");
1207 child->add_property ("val", get_midi_feedback () ? "yes" : "no");
1208 child = opthead->add_child ("do-not-record-plugins");
1209 child->add_property ("val", get_do_not_record_plugins () ? "yes" : "no");
1210 child = opthead->add_child ("auto-crossfade");
1211 child->add_property ("val", get_crossfades_active () ? "yes" : "no");
1212 child = opthead->add_child ("audible-click");
1213 child->add_property ("val", get_clicking () ? "yes" : "no");
1214 child = opthead->add_child ("end-marker-is-free");
1215 child->add_property ("val", _end_location_is_free ? "yes" : "no");
1217 if (click_sound.length()) {
1218 child = opthead->add_child ("click-sound");
1219 child->add_property ("val", click_sound);
1222 if (click_emphasis_sound.length()) {
1223 child = opthead->add_child ("click-emphasis-sound");
1224 child->add_property ("val", click_emphasis_sound);
1227 child = opthead->add_child ("solo-model");
1228 child->add_property ("val", _solo_model == SoloBus ? "SoloBus" : "InverseMute");
1230 child = opthead->add_child ("layer-model");
1231 switch (layer_model) {
1233 child->add_property ("val", X_("LaterHigher"));
1236 child->add_property ("val", X_("MoveAddHigher"));
1239 child->add_property ("val", X_("AddHigher"));
1243 child = opthead->add_child ("xfade-model");
1244 switch (xfade_model) {
1246 child->add_property ("val", X_("Full"));
1248 case ShortCrossfade:
1249 child->add_property ("val", X_("Short"));
1252 child = opthead->add_child ("short-xfade-length");
1253 /* store as fractions of a second */
1254 snprintf (buf, sizeof(buf)-1, "%f",
1255 (float) Crossfade::short_xfade_length() / frame_rate());
1256 child->add_property ("val", buf);
1258 child = opthead->add_child ("full-xfades-unmuted");
1259 child->add_property ("val", crossfades_active ? "yes" : "no");
1265 Session::get_state()
1271 Session::get_template()
1273 /* if we don't disable rec-enable, diskstreams
1274 will believe they need to store their capture
1275 sources in their state node.
1278 disable_record (false);
1280 return state(false);
1284 Session::state(bool full_state)
1286 XMLNode* node = new XMLNode("Session");
1289 // store libardour version, just in case
1291 snprintf(buf, sizeof(buf)-1, "%d.%d.%d",
1292 libardour_major_version, libardour_minor_version, libardour_micro_version);
1293 node->add_property("version", string(buf));
1295 /* store configuration settings */
1299 /* store the name */
1300 node->add_property ("name", _name);
1302 if (session_dirs.size() > 1) {
1306 vector<space_and_path>::iterator i = session_dirs.begin();
1307 vector<space_and_path>::iterator next;
1309 ++i; /* skip the first one */
1313 while (i != session_dirs.end()) {
1317 if (next != session_dirs.end()) {
1327 child = node->add_child ("Path");
1328 child->add_content (p);
1332 /* save the ID counter */
1334 snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
1335 node->add_property ("id-counter", buf);
1337 /* various options */
1339 node->add_child_nocopy (get_options());
1341 child = node->add_child ("Sources");
1344 Glib::Mutex::Lock sl (audio_source_lock);
1346 for (AudioSourceList::iterator siter = audio_sources.begin(); siter != audio_sources.end(); ++siter) {
1348 /* Don't save information about AudioFileSources that are empty */
1350 AudioFileSource* fs;
1352 if ((fs = dynamic_cast<AudioFileSource*> (siter->second)) != 0) {
1353 DestructiveFileSource* dfs = dynamic_cast<DestructiveFileSource*> (fs);
1355 /* destructive file sources are OK if they are empty, because
1356 we will re-use them every time.
1360 if (fs->length() == 0) {
1366 child->add_child_nocopy (siter->second->get_state());
1370 child = node->add_child ("Regions");
1373 Glib::Mutex::Lock rl (region_lock);
1375 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
1377 /* only store regions not attached to playlists */
1379 if (i->second->playlist() == 0) {
1380 child->add_child_nocopy (i->second->state (true));
1385 child = node->add_child ("DiskStreams");
1388 Glib::RWLock::ReaderLock dl (diskstream_lock);
1389 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
1390 if (!(*i)->hidden()) {
1391 child->add_child_nocopy ((*i)->get_state());
1396 node->add_child_nocopy (_locations.get_state());
1398 child = node->add_child ("Connections");
1400 Glib::Mutex::Lock lm (connection_lock);
1401 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ++i) {
1402 if (!(*i)->system_dependent()) {
1403 child->add_child_nocopy ((*i)->get_state());
1408 child = node->add_child ("Routes");
1410 boost::shared_ptr<RouteList> r = routes.reader ();
1412 RoutePublicOrderSorter cmp;
1413 RouteList public_order (*r);
1414 public_order.sort (cmp);
1416 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1417 if (!(*i)->hidden()) {
1419 child->add_child_nocopy ((*i)->get_state());
1421 child->add_child_nocopy ((*i)->get_template());
1428 child = node->add_child ("EditGroups");
1429 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ++i) {
1430 child->add_child_nocopy ((*i)->get_state());
1433 child = node->add_child ("MixGroups");
1434 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ++i) {
1435 child->add_child_nocopy ((*i)->get_state());
1438 child = node->add_child ("Playlists");
1439 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1440 if (!(*i)->hidden()) {
1441 if (!(*i)->empty()) {
1443 child->add_child_nocopy ((*i)->get_state());
1445 child->add_child_nocopy ((*i)->get_template());
1451 child = node->add_child ("UnusedPlaylists");
1452 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
1453 if (!(*i)->hidden()) {
1454 if (!(*i)->empty()) {
1456 child->add_child_nocopy ((*i)->get_state());
1458 child->add_child_nocopy ((*i)->get_template());
1466 child = node->add_child ("Click");
1467 child->add_child_nocopy (_click_io->state (full_state));
1471 child = node->add_child ("NamedSelections");
1472 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
1474 child->add_child_nocopy ((*i)->get_state());
1479 node->add_child_nocopy (_tempo_map->get_state());
1482 node->add_child_copy (*_extra_xml);
1489 Session::set_state (const XMLNode& node)
1493 const XMLProperty* prop;
1496 _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1498 if (node.name() != X_("Session")){
1499 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1503 StateManager::prohibit_save ();
1505 if ((prop = node.property ("name")) != 0) {
1506 _name = prop->value ();
1509 if ((prop = node.property (X_("id-counter"))) != 0) {
1511 sscanf (prop->value().c_str(), "%" PRIu64, &x);
1512 ID::init_counter (x);
1514 /* old sessions used a timebased counter, so fake
1515 the startup ID counter based on a standard
1520 ID::init_counter (now);
1524 IO::disable_ports ();
1525 IO::disable_connecting ();
1527 /* Object loading order:
1544 if (use_config_midi_ports ()) {
1547 if ((child = find_named_node (node, "Path")) != 0) {
1548 /* XXX this XML content stuff horrible API design */
1549 string raid_path = _path + ':' + child->children().front()->content();
1550 setup_raid_path (raid_path);
1552 /* the path is already set */
1555 if ((child = find_named_node (node, "extra")) != 0) {
1556 _extra_xml = new XMLNode (*child);
1559 if ((child = find_named_node (node, "Options")) == 0) {
1560 error << _("Session: XML state has no options section") << endmsg;
1561 } else if (load_options (*child)) {
1564 if ((child = find_named_node (node, "Sources")) == 0) {
1565 error << _("Session: XML state has no sources section") << endmsg;
1567 } else if (load_sources (*child)) {
1571 if ((child = find_named_node (node, "Regions")) == 0) {
1572 error << _("Session: XML state has no Regions section") << endmsg;
1574 } else if (load_regions (*child)) {
1578 if ((child = find_named_node (node, "Playlists")) == 0) {
1579 error << _("Session: XML state has no playlists section") << endmsg;
1581 } else if (load_playlists (*child)) {
1585 if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1587 } else if (load_unused_playlists (*child)) {
1591 if ((child = find_named_node (node, "NamedSelections")) != 0) {
1592 if (load_named_selections (*child)) {
1597 if ((child = find_named_node (node, "DiskStreams")) == 0) {
1598 error << _("Session: XML state has no diskstreams section") << endmsg;
1600 } else if (load_diskstreams (*child)) {
1604 if ((child = find_named_node (node, "Connections")) == 0) {
1605 error << _("Session: XML state has no connections section") << endmsg;
1607 } else if (load_connections (*child)) {
1611 if ((child = find_named_node (node, "Locations")) == 0) {
1612 error << _("Session: XML state has no locations section") << endmsg;
1614 } else if (_locations.set_state (*child)) {
1620 if ((location = _locations.auto_loop_location()) != 0) {
1621 set_auto_loop_location (location);
1624 if ((location = _locations.auto_punch_location()) != 0) {
1625 set_auto_punch_location (location);
1628 if ((location = _locations.end_location()) == 0) {
1629 _locations.add (end_location);
1631 delete end_location;
1632 end_location = location;
1635 if ((location = _locations.start_location()) == 0) {
1636 _locations.add (start_location);
1638 delete start_location;
1639 start_location = location;
1642 _locations.save_state (_("initial state"));
1644 if ((child = find_named_node (node, "EditGroups")) == 0) {
1645 error << _("Session: XML state has no edit groups section") << endmsg;
1647 } else if (load_edit_groups (*child)) {
1651 if ((child = find_named_node (node, "MixGroups")) == 0) {
1652 error << _("Session: XML state has no mix groups section") << endmsg;
1654 } else if (load_mix_groups (*child)) {
1658 if ((child = find_named_node (node, "TempoMap")) == 0) {
1659 error << _("Session: XML state has no Tempo Map section") << endmsg;
1661 } else if (_tempo_map->set_state (*child)) {
1665 if ((child = find_named_node (node, "Routes")) == 0) {
1666 error << _("Session: XML state has no routes section") << endmsg;
1668 } else if (load_routes (*child)) {
1672 if ((child = find_named_node (node, "Click")) == 0) {
1673 warning << _("Session: XML state has no click section") << endmsg;
1674 } else if (_click_io) {
1675 _click_io->set_state (*child);
1678 /* OK, now we can set edit mode */
1680 set_edit_mode (pending_edit_mode);
1682 /* here beginneth the second phase ... */
1684 StateReady (); /* EMIT SIGNAL */
1686 _state_of_the_state = Clean;
1688 StateManager::allow_save (_("initial state"), true);
1690 if (state_was_pending) {
1691 save_state (_current_snapshot_name);
1692 remove_pending_capture_state ();
1693 state_was_pending = false;
1699 /* we failed, re-enable state saving but don't actually save internal state */
1700 StateManager::allow_save (X_("ignored"), false);
1705 Session::load_routes (const XMLNode& node)
1708 XMLNodeConstIterator niter;
1710 nlist = node.children();
1714 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1716 boost::shared_ptr<Route> route (XMLRouteFactory (**niter));
1719 error << _("Session: cannot create Route from XML description.") << endmsg;
1729 boost::shared_ptr<Route>
1730 Session::XMLRouteFactory (const XMLNode& node)
1732 if (node.name() != "Route") {
1733 return boost::shared_ptr<Route> ((Route*) 0);
1736 if (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0) {
1737 boost::shared_ptr<Route> x (new AudioTrack (*this, node));
1740 boost::shared_ptr<Route> x (new Route (*this, node));
1746 Session::load_regions (const XMLNode& node)
1749 XMLNodeConstIterator niter;
1750 AudioRegion* region;
1752 nlist = node.children();
1756 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1757 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1758 error << _("Session: cannot create Region from XML description.") << endmsg;
1765 Session::XMLRegionFactory (const XMLNode& node, bool full)
1767 const XMLProperty* prop;
1770 AudioRegion::SourceList sources;
1771 uint32_t nchans = 1;
1774 if (node.name() != X_("Region")) {
1778 if ((prop = node.property (X_("channels"))) != 0) {
1779 nchans = atoi (prop->value().c_str());
1783 if ((prop = node.property (X_("source-0"))) == 0) {
1784 if ((prop = node.property ("source")) == 0) {
1785 error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1790 PBD::ID s_id (prop->value());
1792 if ((source = source_by_id (s_id)) == 0) {
1793 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1797 as = dynamic_cast<AudioSource*>(source);
1799 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
1803 sources.push_back (as);
1805 /* pickup other channels */
1807 for (uint32_t n=1; n < nchans; ++n) {
1808 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1809 if ((prop = node.property (buf)) != 0) {
1811 PBD::ID id2 (prop->value());
1813 if ((source = source_by_id (id2)) == 0) {
1814 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1818 as = dynamic_cast<AudioSource*>(source);
1820 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1823 sources.push_back (as);
1828 return new AudioRegion (sources, node);
1831 catch (failed_constructor& err) {
1837 Session::get_sources_as_xml ()
1840 XMLNode* node = new XMLNode (X_("Sources"));
1841 Glib::Mutex::Lock lm (audio_source_lock);
1843 for (AudioSourceList::iterator i = audio_sources.begin(); i != audio_sources.end(); ++i) {
1844 node->add_child_nocopy (i->second->get_state());
1847 /* XXX get MIDI and other sources here */
1853 Session::path_from_region_name (string name, string identifier)
1855 char buf[PATH_MAX+1];
1857 string dir = discover_best_sound_dir ();
1859 for (n = 0; n < 999999; ++n) {
1860 if (identifier.length()) {
1861 snprintf (buf, sizeof(buf), "%s/%s%s%" PRIu32 ".wav", dir.c_str(), name.c_str(),
1862 identifier.c_str(), n);
1864 snprintf (buf, sizeof(buf), "%s/%s-%" PRIu32 ".wav", dir.c_str(), name.c_str(), n);
1866 if (access (buf, F_OK) != 0) {
1876 Session::load_sources (const XMLNode& node)
1879 XMLNodeConstIterator niter;
1882 nlist = node.children();
1886 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1888 if ((source = XMLSourceFactory (**niter)) == 0) {
1889 error << _("Session: cannot create Source from XML description.") << endmsg;
1897 Session::XMLSourceFactory (const XMLNode& node)
1901 if (node.name() != "Source") {
1906 src = AudioFileSource::create (node);
1909 catch (failed_constructor& err) {
1910 error << _("Found a sound file that cannot be used by Ardour. Talk to the progammers.") << endmsg;
1918 Session::save_template (string template_name)
1921 string xml_path, bak_path, template_path;
1923 if (_state_of_the_state & CannotSave) {
1928 string dir = template_dir();
1930 if ((dp = opendir (dir.c_str()))) {
1933 if (mkdir (dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)<0) {
1934 error << string_compose(_("Could not create mix templates directory \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
1939 tree.set_root (&get_template());
1942 xml_path += template_name;
1943 xml_path += _template_suffix;
1945 ifstream in(xml_path.c_str());
1948 warning << string_compose(_("Template \"%1\" already exists - new version not created"), template_name) << endmsg;
1954 if (!tree.write (xml_path)) {
1955 error << _("mix template not saved") << endmsg;
1963 Session::rename_template (string old_name, string new_name)
1965 string old_path = template_dir() + old_name + _template_suffix;
1966 string new_path = template_dir() + new_name + _template_suffix;
1968 return rename (old_path.c_str(), new_path.c_str());
1972 Session::delete_template (string name)
1974 string template_path = template_dir();
1975 template_path += name;
1976 template_path += _template_suffix;
1978 return remove (template_path.c_str());
1982 Session::refresh_disk_space ()
1985 struct statfs statfsbuf;
1986 vector<space_and_path>::iterator i;
1987 Glib::Mutex::Lock lm (space_lock);
1990 /* get freespace on every FS that is part of the session path */
1992 _total_free_4k_blocks = 0;
1994 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
1995 statfs ((*i).path.c_str(), &statfsbuf);
1997 scale = statfsbuf.f_bsize/4096.0;
1999 (*i).blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
2000 _total_free_4k_blocks += (*i).blocks;
2006 Session::ensure_sound_dir (string path, string& result)
2011 /* Ensure that the parent directory exists */
2013 if (mkdir (path.c_str(), 0775)) {
2014 if (errno != EEXIST) {
2015 error << string_compose(_("cannot create session directory \"%1\"; ignored"), path) << endmsg;
2020 /* Ensure that the sounds directory exists */
2024 result += sound_dir_name;
2026 if (mkdir (result.c_str(), 0775)) {
2027 if (errno != EEXIST) {
2028 error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
2035 dead += dead_sound_dir_name;
2037 if (mkdir (dead.c_str(), 0775)) {
2038 if (errno != EEXIST) {
2039 error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
2046 peak += peak_dir_name;
2048 if (mkdir (peak.c_str(), 0775)) {
2049 if (errno != EEXIST) {
2050 error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
2055 /* callers expect this to be terminated ... */
2062 Session::discover_best_sound_dir (bool destructive)
2064 vector<space_and_path>::iterator i;
2067 /* destructive files all go into the same place */
2073 /* handle common case without system calls */
2075 if (session_dirs.size() == 1) {
2079 /* OK, here's the algorithm we're following here:
2081 We want to select which directory to use for
2082 the next file source to be created. Ideally,
2083 we'd like to use a round-robin process so as to
2084 get maximum performance benefits from splitting
2085 the files across multiple disks.
2087 However, in situations without much diskspace, an
2088 RR approach may end up filling up a filesystem
2089 with new files while others still have space.
2090 Its therefore important to pay some attention to
2091 the freespace in the filesystem holding each
2092 directory as well. However, if we did that by
2093 itself, we'd keep creating new files in the file
2094 system with the most space until it was as full
2095 as all others, thus negating any performance
2096 benefits of this RAID-1 like approach.
2098 So, we use a user-configurable space threshold. If
2099 there are at least 2 filesystems with more than this
2100 much space available, we use RR selection between them.
2101 If not, then we pick the filesystem with the most space.
2103 This gets a good balance between the two
2107 refresh_disk_space ();
2109 int free_enough = 0;
2111 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2112 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2117 if (free_enough >= 2) {
2119 bool found_it = false;
2121 /* use RR selection process, ensuring that the one
2125 i = last_rr_session_dir;
2128 if (++i == session_dirs.end()) {
2129 i = session_dirs.begin();
2132 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2133 if (ensure_sound_dir ((*i).path, result) == 0) {
2134 last_rr_session_dir = i;
2140 } while (i != last_rr_session_dir);
2143 result = sound_dir();
2148 /* pick FS with the most freespace (and that
2149 seems to actually work ...)
2152 vector<space_and_path> sorted;
2153 space_and_path_ascending_cmp cmp;
2155 sorted = session_dirs;
2156 sort (sorted.begin(), sorted.end(), cmp);
2158 for (i = sorted.begin(); i != sorted.end(); ++i) {
2159 if (ensure_sound_dir ((*i).path, result) == 0) {
2160 last_rr_session_dir = i;
2165 /* if the above fails, fall back to the most simplistic solution */
2167 if (i == sorted.end()) {
2176 Session::load_playlists (const XMLNode& node)
2179 XMLNodeConstIterator niter;
2182 nlist = node.children();
2186 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2188 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2189 error << _("Session: cannot create Playlist from XML description.") << endmsg;
2197 Session::load_unused_playlists (const XMLNode& node)
2200 XMLNodeConstIterator niter;
2203 nlist = node.children();
2207 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2209 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2210 error << _("Session: cannot create Playlist from XML description.") << endmsg;
2214 // now manually untrack it
2216 track_playlist (playlist, false);
2224 Session::XMLPlaylistFactory (const XMLNode& node)
2227 return new AudioPlaylist (*this, node);
2230 catch (failed_constructor& err) {
2236 Session::load_named_selections (const XMLNode& node)
2239 XMLNodeConstIterator niter;
2242 nlist = node.children();
2246 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2248 if ((ns = XMLNamedSelectionFactory (**niter)) == 0) {
2249 error << _("Session: cannot create Named Selection from XML description.") << endmsg;
2257 Session::XMLNamedSelectionFactory (const XMLNode& node)
2260 return new NamedSelection (*this, node);
2263 catch (failed_constructor& err) {
2269 Session::dead_sound_dir () const
2272 res += dead_sound_dir_name;
2278 Session::sound_dir () const
2281 res += sound_dir_name;
2287 Session::tape_dir () const
2290 res += tape_dir_name;
2296 Session::peak_dir () const
2299 res += peak_dir_name;
2305 Session::automation_dir () const
2308 res += "automation/";
2313 Session::template_dir ()
2315 string path = get_user_ardour_path();
2316 path += "templates/";
2322 Session::suffixed_search_path (string suffix, bool data)
2326 path += get_user_ardour_path();
2327 if (path[path.length()-1] != ':') {
2332 path += get_system_data_path();
2334 path += get_system_module_path();
2337 vector<string> split_path;
2339 split (path, split_path, ':');
2342 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
2347 if (distance (i, split_path.end()) != 1) {
2356 Session::template_path ()
2358 return suffixed_search_path (X_("templates"), true);
2362 Session::control_protocol_path ()
2364 return suffixed_search_path (X_("surfaces"), false);
2368 Session::load_connections (const XMLNode& node)
2370 XMLNodeList nlist = node.children();
2371 XMLNodeConstIterator niter;
2375 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2376 if ((*niter)->name() == "InputConnection") {
2377 add_connection (new ARDOUR::InputConnection (**niter));
2378 } else if ((*niter)->name() == "OutputConnection") {
2379 add_connection (new ARDOUR::OutputConnection (**niter));
2381 error << string_compose(_("Unknown node \"%1\" found in Connections list from state file"), (*niter)->name()) << endmsg;
2390 Session::load_edit_groups (const XMLNode& node)
2392 return load_route_groups (node, true);
2396 Session::load_mix_groups (const XMLNode& node)
2398 return load_route_groups (node, false);
2402 Session::load_route_groups (const XMLNode& node, bool edit)
2404 XMLNodeList nlist = node.children();
2405 XMLNodeConstIterator niter;
2410 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2411 if ((*niter)->name() == "RouteGroup") {
2413 rg = add_edit_group ("");
2414 rg->set_state (**niter);
2416 rg = add_mix_group ("");
2417 rg->set_state (**niter);
2426 state_file_filter (const string &str, void *arg)
2428 return (str.length() > strlen(Session::statefile_suffix()) &&
2429 str.find (Session::statefile_suffix()) == (str.length() - strlen (Session::statefile_suffix())));
2433 bool operator()(const string* a, const string* b) {
2439 remove_end(string* state)
2441 string statename(*state);
2443 string::size_type start,end;
2444 if ((start = statename.find_last_of ('/')) != string::npos) {
2445 statename = statename.substr (start+1);
2448 if ((end = statename.rfind(".ardour")) == string::npos) {
2449 end = statename.length();
2452 return new string(statename.substr (0, end));
2456 Session::possible_states (string path)
2458 PathScanner scanner;
2459 vector<string*>* states = scanner (path, state_file_filter, 0, false, false);
2461 transform(states->begin(), states->end(), states->begin(), remove_end);
2464 sort (states->begin(), states->end(), cmp);
2470 Session::possible_states () const
2472 return possible_states(_path);
2476 Session::auto_save()
2478 save_state (_current_snapshot_name);
2482 Session::add_edit_group (string name)
2484 RouteGroup* rg = new RouteGroup (*this, name);
2485 edit_groups.push_back (rg);
2486 edit_group_added (rg); /* EMIT SIGNAL */
2492 Session::add_mix_group (string name)
2494 RouteGroup* rg = new RouteGroup (*this, name, RouteGroup::Relative);
2495 mix_groups.push_back (rg);
2496 mix_group_added (rg); /* EMIT SIGNAL */
2502 Session::remove_edit_group (RouteGroup& rg)
2504 list<RouteGroup*>::iterator i;
2506 if ((i = find (edit_groups.begin(), edit_groups.end(), &rg)) != edit_groups.end()) {
2507 (*i)->apply (&Route::drop_edit_group, this);
2508 edit_groups.erase (i);
2509 edit_group_removed (); /* EMIT SIGNAL */
2516 Session::remove_mix_group (RouteGroup& rg)
2518 list<RouteGroup*>::iterator i;
2520 if ((i = find (mix_groups.begin(), mix_groups.end(), &rg)) != mix_groups.end()) {
2521 (*i)->apply (&Route::drop_mix_group, this);
2522 mix_groups.erase (i);
2523 mix_group_removed (); /* EMIT SIGNAL */
2530 Session::mix_group_by_name (string name)
2532 list<RouteGroup *>::iterator i;
2534 for (i = mix_groups.begin(); i != mix_groups.end(); ++i) {
2535 if ((*i)->name() == name) {
2543 Session::edit_group_by_name (string name)
2545 list<RouteGroup *>::iterator i;
2547 for (i = edit_groups.begin(); i != edit_groups.end(); ++i) {
2548 if ((*i)->name() == name) {
2556 Session::set_meter_hold (float val)
2559 MeterHoldChanged(); // emit
2563 Session::set_meter_falloff (float val)
2565 _meter_falloff = val;
2566 MeterFalloffChanged(); // emit
2571 Session::begin_reversible_command (string name, UndoAction* private_undo)
2573 current_cmd.clear ();
2574 current_cmd.set_name (name);
2577 current_cmd.add_undo (*private_undo);
2582 Session::commit_reversible_command (UndoAction* private_redo)
2587 current_cmd.add_redo_no_execute (*private_redo);
2590 gettimeofday (&now, 0);
2591 current_cmd.set_timestamp (now);
2593 history.add (current_cmd);
2596 Session::GlobalRouteBooleanState
2597 Session::get_global_route_boolean (bool (Route::*method)(void) const)
2599 GlobalRouteBooleanState s;
2600 boost::shared_ptr<RouteList> r = routes.reader ();
2602 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2603 if (!(*i)->hidden()) {
2604 RouteBooleanState v;
2607 Route* r = (*i).get();
2608 v.second = (r->*method)();
2617 Session::GlobalRouteMeterState
2618 Session::get_global_route_metering ()
2620 GlobalRouteMeterState s;
2621 boost::shared_ptr<RouteList> r = routes.reader ();
2623 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2624 if (!(*i)->hidden()) {
2628 v.second = (*i)->meter_point();
2638 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg)
2640 for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
2641 i->first->set_meter_point (i->second, arg);
2646 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
2648 for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
2649 Route* r = i->first.get();
2650 (r->*method) (i->second, arg);
2655 Session::set_global_mute (GlobalRouteBooleanState s, void* src)
2657 set_global_route_boolean (s, &Route::set_mute, src);
2661 Session::set_global_solo (GlobalRouteBooleanState s, void* src)
2663 set_global_route_boolean (s, &Route::set_solo, src);
2667 Session::set_global_record_enable (GlobalRouteBooleanState s, void* src)
2669 set_global_route_boolean (s, &Route::set_record_enable, src);
2673 Session::global_mute_memento (void* src)
2675 return sigc::bind (mem_fun (*this, &Session::set_global_mute), get_global_route_boolean (&Route::muted), src);
2679 Session::global_metering_memento (void* src)
2681 return sigc::bind (mem_fun (*this, &Session::set_global_route_metering), get_global_route_metering (), src);
2685 Session::global_solo_memento (void* src)
2687 return sigc::bind (mem_fun (*this, &Session::set_global_solo), get_global_route_boolean (&Route::soloed), src);
2691 Session::global_record_enable_memento (void* src)
2693 return sigc::bind (mem_fun (*this, &Session::set_global_record_enable), get_global_route_boolean (&Route::record_enabled), src);
2697 template_filter (const string &str, void *arg)
2699 return (str.length() > strlen(Session::template_suffix()) &&
2700 str.find (Session::template_suffix()) == (str.length() - strlen (Session::template_suffix())));
2704 Session::get_template_list (list<string> &template_names)
2706 vector<string *> *templates;
2707 PathScanner scanner;
2710 path = template_path ();
2712 templates = scanner (path, template_filter, 0, false, true);
2714 vector<string*>::iterator i;
2715 for (i = templates->begin(); i != templates->end(); ++i) {
2716 string fullpath = *(*i);
2719 start = fullpath.find_last_of ('/') + 1;
2720 if ((end = fullpath.find_last_of ('.')) <0) {
2721 end = fullpath.length();
2724 template_names.push_back(fullpath.substr(start, (end-start)));
2729 Session::read_favorite_dirs (FavoriteDirs & favs)
2731 string path = get_user_ardour_path();
2732 path += "/favorite_dirs";
2734 ifstream fav (path.c_str());
2739 if (errno != ENOENT) {
2740 //error << string_compose (_("cannot open favorite file %1 (%2)"), path, strerror (errno)) << endmsg;
2751 getline(fav, newfav);
2757 favs.push_back (newfav);
2764 Session::write_favorite_dirs (FavoriteDirs & favs)
2766 string path = get_user_ardour_path();
2767 path += "/favorite_dirs";
2769 ofstream fav (path.c_str());
2775 for (FavoriteDirs::iterator i = favs.begin(); i != favs.end(); ++i) {
2776 fav << (*i) << endl;
2783 accept_all_non_peak_files (const string& path, void *arg)
2785 return (path.length() > 5 && path.find (".peak") != (path.length() - 5));
2789 accept_all_state_files (const string& path, void *arg)
2791 return (path.length() > 7 && path.find (".ardour") == (path.length() - 7));
2795 Session::find_all_sources (string path, set<string>& result)
2800 if (!tree.read (path)) {
2804 if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2809 XMLNodeConstIterator niter;
2811 nlist = node->children();
2815 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2819 if ((prop = (*niter)->property (X_("name"))) == 0) {
2823 if (prop->value()[0] == '/') {
2824 /* external file, ignore */
2828 string path = _path; /* /-terminated */
2829 path += sound_dir_name;
2831 path += prop->value();
2833 result.insert (path);
2840 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2842 PathScanner scanner;
2843 vector<string*>* state_files;
2845 string this_snapshot_path;
2851 if (ripped[ripped.length()-1] == '/') {
2852 ripped = ripped.substr (0, ripped.length() - 1);
2855 state_files = scanner (ripped, accept_all_state_files, (void *) 0, false, true);
2857 if (state_files == 0) {
2862 this_snapshot_path = _path;
2863 this_snapshot_path += _current_snapshot_name;
2864 this_snapshot_path += _statefile_suffix;
2866 for (vector<string*>::iterator i = state_files->begin(); i != state_files->end(); ++i) {
2868 if (exclude_this_snapshot && **i == this_snapshot_path) {
2872 if (find_all_sources (**i, result) < 0) {
2881 Session::cleanup_sources (Session::cleanup_report& rep)
2883 vector<Source*> dead_sources;
2884 vector<Playlist*> playlists_tbd;
2885 PathScanner scanner;
2887 vector<space_and_path>::iterator i;
2888 vector<space_and_path>::iterator nexti;
2889 vector<string*>* soundfiles;
2890 vector<string> unused;
2891 set<string> all_sources;
2896 _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
2898 /* step 1: consider deleting all unused playlists */
2900 for (PlaylistList::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
2903 status = AskAboutPlaylistDeletion (*x);
2912 playlists_tbd.push_back (*x);
2916 /* leave it alone */
2921 /* now delete any that were marked for deletion */
2923 for (vector<Playlist*>::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
2924 PlaylistList::iterator foo;
2926 if ((foo = unused_playlists.find (*x)) != unused_playlists.end()) {
2927 unused_playlists.erase (foo);
2932 /* step 2: clear the undo/redo history for all playlists */
2934 for (PlaylistList::iterator x = playlists.begin(); x != playlists.end(); ++x) {
2935 (*x)->drop_all_states ();
2938 /* step 3: find all un-referenced sources */
2943 for (AudioSourceList::iterator i = audio_sources.begin(); i != audio_sources.end(); ) {
2945 AudioSourceList::iterator tmp;
2950 /* only remove files that are not in use and have some size
2951 to them. otherwise we remove the current "nascent"
2955 if (i->second->use_cnt() == 0 && i->second->length() > 0) {
2956 dead_sources.push_back (i->second);
2958 /* remove this source from our own list to avoid us
2959 adding it to the list of all sources below
2962 audio_sources.erase (i);
2968 /* Step 4: get rid of all regions in the region list that use any dead sources
2969 in case the sources themselves don't go away (they might be referenced in
2973 for (vector<Source*>::iterator i = dead_sources.begin(); i != dead_sources.end();++i) {
2975 for (AudioRegionList::iterator r = audio_regions.begin(); r != audio_regions.end(); ) {
2976 AudioRegionList::iterator tmp;
2984 for (uint32_t n = 0; n < ar->n_channels(); ++n) {
2985 if (&ar->source (n) == (*i)) {
2986 /* this region is dead */
2995 /* build a list of all the possible sound directories for the session */
2997 for (i = session_dirs.begin(); i != session_dirs.end(); ) {
3002 sound_path += (*i).path;
3003 sound_path += sound_dir_name;
3005 if (nexti != session_dirs.end()) {
3012 /* now do the same thing for the files that ended up in the sounds dir(s)
3013 but are not referenced as sources in any snapshot.
3016 soundfiles = scanner (sound_path, accept_all_non_peak_files, (void *) 0, false, true);
3018 if (soundfiles == 0) {
3022 /* find all sources, but don't use this snapshot because the
3023 state file on disk still references sources we may have already
3027 find_all_sources_across_snapshots (all_sources, true);
3029 /* add our current source list
3032 for (AudioSourceList::iterator i = audio_sources.begin(); i != audio_sources.end(); ++i) {
3033 AudioFileSource* fs;
3035 if ((fs = dynamic_cast<AudioFileSource*> (i->second)) != 0) {
3036 all_sources.insert (fs->path());
3040 for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
3045 for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
3055 unused.push_back (spath);
3059 /* now try to move all unused files into the "dead_sounds" directory(ies) */
3061 for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
3062 struct stat statbuf;
3064 rep.paths.push_back (*x);
3065 if (stat ((*x).c_str(), &statbuf) == 0) {
3066 rep.space += statbuf.st_size;
3071 /* don't move the file across filesystems, just
3072 stick it in the `dead_sound_dir_name' directory
3073 on whichever filesystem it was already on.
3076 newpath = Glib::path_get_dirname (*x);
3077 newpath = Glib::path_get_dirname (newpath);
3080 newpath += dead_sound_dir_name;
3082 newpath += Glib::path_get_basename ((*x));
3084 if (access (newpath.c_str(), F_OK) == 0) {
3086 /* the new path already exists, try versioning */
3088 char buf[PATH_MAX+1];
3092 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
3095 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
3096 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
3100 if (version == 999) {
3101 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
3105 newpath = newpath_v;
3110 /* it doesn't exist, or we can't read it or something */
3114 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
3115 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
3116 (*x), newpath, strerror (errno))
3122 /* see if there an easy to find peakfile for this file, and remove it.
3125 string peakpath = (*x).substr (0, (*x).find_last_of ('.'));
3126 peakpath += ".peak";
3128 if (access (peakpath.c_str(), W_OK) == 0) {
3129 if (::unlink (peakpath.c_str()) != 0) {
3130 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
3131 peakpath, _path, strerror (errno))
3133 /* try to back out */
3134 rename (newpath.c_str(), _path.c_str());
3143 /* dump the history list */
3147 /* save state so we don't end up a session file
3148 referring to non-existent sources.
3154 _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
3159 Session::cleanup_trash_sources (Session::cleanup_report& rep)
3161 vector<space_and_path>::iterator i;
3162 string dead_sound_dir;
3163 struct dirent* dentry;
3164 struct stat statbuf;
3170 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3172 dead_sound_dir = (*i).path;
3173 dead_sound_dir += dead_sound_dir_name;
3175 if ((dead = opendir (dead_sound_dir.c_str())) == 0) {
3179 while ((dentry = readdir (dead)) != 0) {
3181 /* avoid '.' and '..' */
3183 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
3184 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
3190 fullpath = dead_sound_dir;
3192 fullpath += dentry->d_name;
3194 if (stat (fullpath.c_str(), &statbuf)) {
3198 if (!S_ISREG (statbuf.st_mode)) {
3202 if (unlink (fullpath.c_str())) {
3203 error << string_compose (_("cannot remove dead sound file %1 (%2)"),
3204 fullpath, strerror (errno))
3208 rep.paths.push_back (dentry->d_name);
3209 rep.space += statbuf.st_size;
3220 Session::set_dirty ()
3222 bool was_dirty = dirty();
3224 _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
3227 DirtyChanged(); /* EMIT SIGNAL */
3233 Session::set_clean ()
3235 bool was_dirty = dirty();
3237 _state_of_the_state = Clean;
3240 DirtyChanged(); /* EMIT SIGNAL */
3245 Session::add_controllable (Controllable* c)
3247 Glib::Mutex::Lock lm (controllables_lock);
3248 controllables.push_back (c);
3252 Session::remove_controllable (Controllable* c)
3254 if (_state_of_the_state | Deletion) {
3258 Glib::Mutex::Lock lm (controllables_lock);
3259 controllables.remove (c);
3263 Session::controllable_by_id (const PBD::ID& id)
3265 Glib::Mutex::Lock lm (controllables_lock);
3267 for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
3268 if ((*i)->id() == id) {
3277 Session::add_instant_xml (XMLNode& node, const std::string& dir)
3279 Stateful::add_instant_xml (node, dir);
3280 Config->add_instant_xml (node, get_user_ardour_path());