From fc258ef102fa101697814478c83a3a2e83ba614a Mon Sep 17 00:00:00 2001
From: Nedko Arnaudov <nedko@arnaudov.name>
Date: Sun, 16 Jan 2011 21:02:57 +0200
Subject: [PATCH] Use port symbols when restoring lv2 plugin state. Bug #3717

Backport from 3.0 branch. The fix itself was commited as part of r5055.
r5055 needs _port_indices member of the class. _port_indices was
introduced in r4547.
---
 libs/ardour/ardour/lv2_plugin.h |    1 +
 libs/ardour/lv2_plugin.cc       |   29 +++++++++++++++++++----------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/libs/ardour/ardour/lv2_plugin.h b/libs/ardour/ardour/lv2_plugin.h
index 8e1536b..7026836 100644
--- a/libs/ardour/ardour/lv2_plugin.h
+++ b/libs/ardour/ardour/lv2_plugin.h
@@ -129,6 +129,7 @@ class LV2Plugin : public ARDOUR::Plugin
 	float*                   _latency_control_port;
 	bool                     _was_activated;
 	vector<bool>             _port_is_input;
+	map<string,uint32_t>     _port_indices;
 
 	typedef struct { const void* (*extension_data)(const char* uri); } LV2_DataAccess;
 	LV2_DataAccess _data_access_extension_data;
diff --git a/libs/ardour/lv2_plugin.cc b/libs/ardour/lv2_plugin.cc
index d46e6e9..b875ef8 100644
--- a/libs/ardour/lv2_plugin.cc
+++ b/libs/ardour/lv2_plugin.cc
@@ -119,8 +119,10 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
 	uint32_t latency_port = (latent ? slv2_plugin_get_latency_port_index(plugin) : 0);
 
 	for (uint32_t i = 0; i < num_ports; ++i) {
+		SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
+		SLV2Value sym = slv2_port_get_symbol(_plugin, port);
+		_port_indices.insert(std::make_pair(slv2_value_as_string(sym), i));
 		if (parameter_is_control(i)) {
-			SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
 			SLV2Value def;
 			slv2_port_get_range(plugin, port, &def, NULL, NULL);
 			_defaults[i] = def ? slv2_value_as_float(def) : 0.0f;
@@ -315,8 +317,8 @@ LV2Plugin::set_state(const XMLNode& node)
 	XMLProperty *prop;
 	XMLNodeConstIterator iter;
 	XMLNode *child;
-	const char *port;
-	const char *data;
+	const char *sym;
+	const char *value;
 	uint32_t port_id;
 	LocaleGuard lg (X_("POSIX"));
 
@@ -331,22 +333,29 @@ LV2Plugin::set_state(const XMLNode& node)
 
 		child = *iter;
 
-		if ((prop = child->property("number")) != 0) {
-			port = prop->value().c_str();
+		if ((prop = child->property("symbol")) != 0) {
+			sym = prop->value().c_str();
 		} else {
-			warning << _("LV2: no lv2 port number") << endmsg;
+			warning << _("LV2: port has no symbol, ignored") << endmsg;
+			continue;
+		}
+
+		map<string,uint32_t>::iterator i = _port_indices.find(sym);
+		if (i != _port_indices.end()) {
+			port_id = i->second;
+		} else {
+			warning << _("LV2: port has unknown index, ignored") << endmsg;
 			continue;
 		}
 
 		if ((prop = child->property("value")) != 0) {
-			data = prop->value().c_str();
+			value = prop->value().c_str();
 		} else {
-			warning << _("LV2: no lv2 port data") << endmsg;
+			warning << _("LV2: port has no value, ignored") << endmsg;
 			continue;
 		}
 
-		sscanf (port, "%" PRIu32, &port_id);
-		set_parameter (port_id, atof(data));
+		set_parameter (port_id, atof(value));
 	}
 	
 	latency_compute_run ();
-- 
1.6.5.GIT

