* wrong calculation of frames_moved in Session::process_*, resulting in drift against...
[ardour.git] / libs / ardour / session_state.cc
index 0d0499b5a1f4b800184f5092a61b36926955fa35..0fcd1c95a9d464dc87c79df65f11e679cbc59354 100644 (file)
@@ -64,6 +64,7 @@
 #include <ardour/session_directory.h>
 #include <ardour/session_utils.h>
 #include <ardour/session_state_utils.h>
+#include <ardour/session_metadata.h>
 #include <ardour/buffer.h>
 #include <ardour/audio_diskstream.h>
 #include <ardour/midi_diskstream.h>
@@ -77,7 +78,6 @@
 #include <ardour/midi_source.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/auditioner.h>
-#include <ardour/export.h>
 #include <ardour/io_processor.h>
 #include <ardour/send.h>
 #include <ardour/processor.h>
@@ -86,6 +86,7 @@
 #include <ardour/tempo.h>
 #include <ardour/audio_track.h>
 #include <ardour/midi_track.h>
+#include <ardour/midi_patch_manager.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/utils.h>
 #include <ardour/named_selection.h>
 #include <ardour/filename_extensions.h>
 #include <ardour/directory_names.h>
 #include <ardour/template_utils.h>
+#include <ardour/ticker.h>
 
 #include <control_protocol/control_protocol.h>
 
@@ -153,6 +155,8 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        insert_cnt = 0;
        _transport_speed = 0;
        _last_transport_speed = 0;
+       phi = (uint64_t) (0x1000000);
+       target_phi = phi;
        auto_play_legal = false;
        transport_sub_state = 0;
        _transport_frame = 0;
@@ -163,13 +167,14 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        g_atomic_int_set (&_record_status, Disabled);
        loop_changing = false;
        play_loop = false;
+       have_looped = false;
        _last_roll_location = 0;
        _last_record_location = 0;
        pending_locate_frame = 0;
        pending_locate_roll = false;
        pending_locate_flush = false;
-       dstream_buffer_size = 0;
-       state_tree = 0;
+       audio_dstream_buffer_size = 0;
+       midi_dstream_buffer_size = 0;
        state_was_pending = false;
        set_next_event ();
        outbound_mtc_smpte_frame = 0;
@@ -184,21 +189,17 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
 
        _slave = 0;
-       butler_mixdown_buffer = 0;
-       butler_gain_buffer = 0;
-       mmc = 0;
        session_send_mmc = false;
        session_send_mtc = false;
        post_transport_work = PostTransportWork (0);
        g_atomic_int_set (&butler_should_do_transport_work, 0);
-       g_atomic_int_set (&butler_active, 0);
        g_atomic_int_set (&_playback_load, 100);
        g_atomic_int_set (&_capture_load, 100);
        g_atomic_int_set (&_playback_load_min, 100);
        g_atomic_int_set (&_capture_load_min, 100);
        _play_range = false;
-       waiting_to_start = false;
        _exporting = false;
+       _exporting_realtime = false;
        _gain_automation_buffer = 0;
        _pan_automation_buffer = 0;
        _npan_buffers = 0;
@@ -225,8 +226,6 @@ Session::first_stage_init (string fullpath, string snapshot_name)
           waveforms for clicks.
        */
        
-       click_data = 0;
-       click_emphasis_data = 0;
        click_length = 0;
        click_emphasis_length = 0;
        _clicking = false;
@@ -253,7 +252,7 @@ Session::first_stage_init (string fullpath, string snapshot_name)
 
        /* slave stuff */
 
-       average_slave_delta = 1800;
+       average_slave_delta = 1800; // !!! why 1800 ????
        have_first_delta_accumulator = false;
        delta_accumulator_cnt = 0;
        slave_state = Stopped;
@@ -356,6 +355,11 @@ Session::second_stage_init (bool new_session)
        _engine.transport_locate (0);
        deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
        deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
+       
+       MidiClockTicker::instance().set_session(*this);
+       MIDI::Name::MidiPatchManager::instance().set_session(*this);
+
+       /* initial program change will be delivered later; see ::config_changed() */
 
        BootMessage (_("Reset Control Protocols"));
 
@@ -431,8 +435,9 @@ Session::setup_raid_path (string path)
        AudioFileSource::set_search_path (sound_search_path.to_string ());
        SMFSource::set_search_path (midi_search_path.to_string ());
 
+
        // reset the round-robin soundfile path thingie
-       
+
        last_rr_session_dir = session_dirs.begin();
 }
 
@@ -535,6 +540,10 @@ Session::create (bool& new_session, const string& mix_template, nframes_t initia
                }
 
        }
+       
+       /* Instantiate metadata */
+       
+       _metadata = new SessionMetadata ();
 
        /* set initial start + end point */
 
@@ -761,10 +770,8 @@ Session::restore_state (string snapshot_name)
 int
 Session::load_state (string snapshot_name)
 {
-       if (state_tree) {
-               delete state_tree;
-               state_tree = 0;
-       }
+       delete state_tree;
+       state_tree = 0;
 
        state_was_pending = false;
 
@@ -813,7 +820,7 @@ Session::load_state (string snapshot_name)
        }
 
        const XMLProperty* prop;
-       bool is_old = false;
+       bool is_old = false; // session is _very_ old (pre-2.0)
 
        if ((prop = root.property ("version")) == 0) {
                /* no version implies very old version of Ardour */
@@ -866,6 +873,12 @@ Session::load_options (const XMLNode& node)
 
        Config->set_variables (node, ConfigVariableBase::Session);
 
+       /* now reset MIDI ports because the session can have its own 
+          MIDI configuration.
+       */
+
+       setup_midi ();
+
        if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
                if ((prop = child->property ("val")) != 0) {
                        _end_location_is_free = (prop->value() == "yes");
@@ -975,6 +988,8 @@ Session::state(bool full_state)
 
        node->add_child_nocopy (get_options());
 
+       node->add_child_nocopy (_metadata->get_state());
+
        child = node->add_child ("Sources");
 
        if (full_state) {
@@ -1196,10 +1211,11 @@ Session::set_state (const XMLNode& node)
 
        /* Object loading order:
 
-       MIDI Control
        Path
        extra
        Options/Config
+       MIDI Control // relies on data from Options/Config
+       Metadata
        Locations
        Sources
        AudioRegions
@@ -1212,9 +1228,6 @@ Session::set_state (const XMLNode& node)
        ControlProtocols
        */
 
-       if (use_config_midi_ports ()) {
-       }
-
        if ((child = find_named_node (node, "extra")) != 0) {
                _extra_xml = new XMLNode (*child);
        }
@@ -1227,6 +1240,15 @@ Session::set_state (const XMLNode& node)
                error << _("Session: XML state has no options section") << endmsg;
        }
 
+       if (use_config_midi_ports ()) {
+       }
+
+       if ((child = find_named_node (node, "Metadata")) == 0) {
+               warning << _("Session: XML state has no metadata section (2.0 session?)") << endmsg;
+       } else if (_metadata->set_state (*child)) {
+               goto out;
+       }
+
        if ((child = find_named_node (node, "Locations")) == 0) {
                error << _("Session: XML state has no locations section") << endmsg;
                goto out;
@@ -1374,7 +1396,7 @@ Session::load_routes (const XMLNode& node)
                boost::shared_ptr<Route> route (XMLRouteFactory (**niter));
 
                if (route == 0) {
-                       error << _("Session: cannot create Route from XML description.")                              << endmsg;
+                       error << _("Session: cannot create Route from XML description.") << endmsg;
                        return -1;
                }
 
@@ -1793,6 +1815,43 @@ Session::save_template (string template_name)
        return 0;
 }
 
+int
+Session::rename_template (string old_name, string new_name) 
+{
+       sys::path old_path (user_template_directory());
+       old_path /= old_name + template_suffix;
+
+       sys::path new_path(user_template_directory());
+       new_path /= new_name + template_suffix;
+
+       if (sys::exists (new_path)) {
+               warning << string_compose(_("Template \"%1\" already exists - template not renamed"),
+                                         new_path.to_string()) << endmsg;
+               return -1;
+       }
+
+       try {
+               sys::rename (old_path, new_path);
+               return 0;
+       } catch (...) {
+               return -1;
+       }
+}
+
+int
+Session::delete_template (string name) 
+{
+       sys::path path = user_template_directory();
+       path /= name + template_suffix;
+
+       try {
+               sys::remove (path);
+               return 0;
+       } catch (...) {
+               return -1;
+       }
+}
+
 void
 Session::refresh_disk_space ()
 {
@@ -2008,17 +2067,13 @@ Session::XMLNamedSelectionFactory (const XMLNode& node)
 string
 Session::automation_dir () const
 {
-       string res = _path;
-       res += "automation/";
-       return res;
+       return Glib::build_filename (_path, "automation");
 }
 
 string
 Session::analysis_dir () const
 {
-       string res = _path;
-       res += "analysis/";
-       return res;
+       return Glib::build_filename (_path, "analysis");
 }
 
 int
@@ -2860,10 +2915,12 @@ Session::controllable_by_id (const PBD::ID& id)
 }
 
 void 
-Session::add_instant_xml (XMLNode& node)
+Session::add_instant_xml (XMLNode& node, bool write_to_config)
 {
        Stateful::add_instant_xml (node, _path);
-       Config->add_instant_xml (node);
+       if (write_to_config) {
+               Config->add_instant_xml (node);
+       }
 }
 
 XMLNode*
@@ -2962,22 +3019,21 @@ Session::restore_history (string snapshot_name)
            struct timeval tv;
            
            ut->set_name(t->property("name")->value());
-           stringstream ss(t->property("tv_sec")->value());
+           stringstream ss(t->property("tv-sec")->value());
            ss >> tv.tv_sec;
-           ss.str(t->property("tv_usec")->value());
+           ss.str(t->property("tv-usec")->value());
            ss >> tv.tv_usec;
            ut->set_timestamp(tv);
            
            for (XMLNodeConstIterator child_it  = t->children().begin();
-                child_it != t->children().end();
-                child_it++)
+                               child_it != t->children().end(); child_it++)
            {
                    XMLNode *n = *child_it;
                    Command *c;
        
                    if (n->name() == "MementoCommand" ||
-                       n->name() == "MementoUndoCommand" ||
-                       n->name() == "MementoRedoCommand") {
+                                       n->name() == "MementoUndoCommand" ||
+                                       n->name() == "MementoRedoCommand") {
 
                            if ((c = memento_command_factory(n))) {
                                    ut->add_command(c);
@@ -2990,7 +3046,7 @@ Session::restore_history (string snapshot_name)
                            }
                            
                    } else if (n->name() == "DeltaCommand") {
-                        PBD::ID  id(n->property("midi_source")->value());
+                        PBD::ID  id(n->property("midi-source")->value());
                         boost::shared_ptr<MidiSource> midi_source = 
                                 boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
                         if(midi_source) {
@@ -3202,7 +3258,28 @@ Session::config_changed (const char* parameter_name)
        } else if (PARAM_IS ("history-depth")) {
                set_history_depth (Config->get_history_depth());
        } else if (PARAM_IS ("sync-all-route-ordering")) {
-               sync_order_keys ();
+               sync_order_keys ("session");
+       } else if (PARAM_IS ("initial-program-change")) {
+
+               if (_mmc_port && Config->get_initial_program_change() >= 0) {
+                       MIDI::byte buf[2];
+                       
+                       buf[0] = MIDI::program; // channel zero by default
+                       buf[1] = (Config->get_initial_program_change() & 0x7f);
+
+                       _mmc_port->midimsg (buf, sizeof (buf), 0);
+               }
+       } else if (PARAM_IS ("initial-program-change")) {
+
+               if (_mmc_port && Config->get_initial_program_change() >= 0) {
+                       MIDI::byte* buf = new MIDI::byte[2];
+                       
+                       buf[0] = MIDI::program; // channel zero by default
+                       buf[1] = (Config->get_initial_program_change() & 0x7f);
+                       // deliver_midi (_mmc_port, buf, 2);
+               }
+       } else if (PARAM_IS ("solo-mute-override")) {
+               catch_up_on_solo_mute_override ();
        }
 
        set_dirty ();