From 432035026d8a74e550a41a0d811a0f089a97dcc1 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Mon, 13 Feb 2012 02:34:41 +0000
Subject: Centralize URI map cache and use in both plugin and UI.

---
 plugins/eg-sampler.lv2/sampler.c    | 41 ++++++++++++++-----------------------
 plugins/eg-sampler.lv2/sampler_ui.c | 16 ++++++++-------
 plugins/eg-sampler.lv2/uris.h       | 31 ++++++++++++++++++++++++++--
 3 files changed, 53 insertions(+), 35 deletions(-)

(limited to 'plugins')

diff --git a/plugins/eg-sampler.lv2/sampler.c b/plugins/eg-sampler.lv2/sampler.c
index c6bf30c..a69a4a3 100644
--- a/plugins/eg-sampler.lv2/sampler.c
+++ b/plugins/eg-sampler.lv2/sampler.c
@@ -53,8 +53,9 @@
 #define STRING_BUF 8192
 
 enum {
-	SAMPLER_CONTROL = 0,
-	SAMPLER_OUT     = 1
+	SAMPLER_CONTROL  = 0,
+	SAMPLER_RESPONSE = 1,
+	SAMPLER_OUT      = 2
 };
 
 static const char* default_sample_file = "monosample.wav";
@@ -81,18 +82,11 @@ typedef struct {
 
 	/* Ports */
 	float*             output_port;
-	LV2_Atom_Sequence* event_port;
+	LV2_Atom_Sequence* control_port;
+	LV2_Atom_Sequence* response_port;
 
 	/* URIs */
-	struct {
-		LV2_URID atom_Blank;
-		LV2_URID atom_Resource;
-		LV2_URID filename_key;
-		LV2_URID midi_Event;
-		LV2_URID msg_Set;
-		LV2_URID msg_body;
-		LV2_URID state_Path;
-	} uris;
+	SamplerURIs uris;
 
 	/* Playback state */
 	sf_count_t frame;
@@ -129,9 +123,7 @@ handle_load_sample(Sampler* plugin)
 	}
 
 	sf_seek(sample, 0ul, SEEK_SET);
-	sf_read_float(sample,
-	              data,
-	              info->frames);
+	sf_read_float(sample, data, info->frames);
 	sf_close(sample);
 
 	/* Queue the sample for installation on next run() */
@@ -163,7 +155,10 @@ connect_port(LV2_Handle instance,
 
 	switch (port) {
 	case SAMPLER_CONTROL:
-		plugin->event_port = (LV2_Atom_Sequence*)data;
+		plugin->control_port = (LV2_Atom_Sequence*)data;
+		break;
+	case SAMPLER_RESPONSE:
+		plugin->response_port = (LV2_Atom_Sequence*)data;
 		break;
 	case SAMPLER_OUT:
 		plugin->output_port = (float*)data;
@@ -221,14 +216,8 @@ instantiate(const LV2_Descriptor*     descriptor,
 		goto fail;
 	}
 
-	plugin->map                = map;
-	plugin->uris.atom_Blank    = map->map(map->handle, ATOM_BLANK_URI);
-	plugin->uris.atom_Resource = map->map(map->handle, ATOM_RESOURCE_URI);
-	plugin->uris.filename_key  = map->map(map->handle, FILENAME_URI);
-	plugin->uris.midi_Event    = map->map(map->handle, MIDI_EVENT_URI);
-	plugin->uris.msg_Set       = map->map(map->handle, LV2_MESSAGE_Set);
-	plugin->uris.msg_body      = map->map(map->handle, LV2_MESSAGE_body);
-	plugin->uris.state_Path    = map->map(map->handle, LV2_STATE_PATH_URI);
+	plugin->map = map;
+	map_sampler_uris(plugin->map, &plugin->uris);
 
 	/* Open the default sample file */
 	strncpy(plugin->pending_samp->filepath, path, STRING_BUF);
@@ -271,7 +260,7 @@ run(LV2_Handle instance,
 	float*     output      = plugin->output_port;
 
 	/* Read incoming events */
-	LV2_SEQUENCE_FOREACH(plugin->event_port, i) {
+	LV2_SEQUENCE_FOREACH(plugin->control_port, i) {
 		LV2_Atom_Event* const ev = lv2_sequence_iter_get(i);
 		if (ev->body.type == plugin->uris.midi_Event) {
 			uint8_t* const data = (uint8_t* const)(ev + 1);
@@ -298,7 +287,7 @@ run(LV2_Handle instance,
 						
 				const LV2_Atom* filename = NULL;
 				LV2_Atom_Object_Query q2[] = {
-					{ plugin->uris.filename_key, &filename },
+					{ plugin->uris.eg_filename, &filename },
 					LV2_OBJECT_QUERY_END
 				};
 				lv2_object_get((LV2_Atom_Object*)body, q2);
diff --git a/plugins/eg-sampler.lv2/sampler_ui.c b/plugins/eg-sampler.lv2/sampler_ui.c
index b807be9..0fba481 100644
--- a/plugins/eg-sampler.lv2/sampler_ui.c
+++ b/plugins/eg-sampler.lv2/sampler_ui.c
@@ -38,6 +38,7 @@ typedef struct {
 	LV2_Atom_Forge forge;
 
 	LV2_URID_Map* map;
+	SamplerURIs   uris;
 
 	LV2UI_Write_Function write;
 	LV2UI_Controller     controller;
@@ -86,16 +87,15 @@ on_load_clicked(GtkWidget* widget,
 	 * ]
 	 */
 	LV2_Atom* set = (LV2_Atom*)lv2_atom_forge_blank(
-		&ui->forge, NULL, 0, uri_to_id(ui, LV2_MESSAGE_Set));
-	lv2_atom_forge_property_head(&ui->forge, set,
-	                             uri_to_id(ui, LV2_MESSAGE_body), 0);
+		&ui->forge, NULL, 0, ui->uris.msg_Set);
+
+	lv2_atom_forge_property_head(&ui->forge, set, ui->uris.msg_body, 0);
 	LV2_Atom* body = (LV2_Atom*)lv2_atom_forge_blank(&ui->forge, set, 0, 0);
-	lv2_atom_forge_property_head(&ui->forge, body,
-	                             uri_to_id(ui, FILENAME_URI), 0);
+
+	lv2_atom_forge_property_head(&ui->forge, body, ui->uris.eg_filename, 0);
 	lv2_atom_forge_string(&ui->forge, set, (uint8_t*)filename, filename_len);
 
-	lv2_atom_forge_property_head(&ui->forge, body,
-	                             uri_to_id(ui, LV2_MESSAGE_body), 0);
+	lv2_atom_forge_property_head(&ui->forge, body, ui->uris.msg_body, 0);
 	set->size += body->size;
 
 	ui->write(ui->controller, 0, sizeof(LV2_Atom) + set->size,
@@ -134,6 +134,8 @@ instantiate(const LV2UI_Descriptor*   descriptor,
 		return NULL;
 	}
 
+	map_sampler_uris(ui->map, &ui->uris);
+
 	lv2_atom_forge_init(&ui->forge, ui->map);
 
 	ui->button = gtk_button_new_with_label("Load Sample");
diff --git a/plugins/eg-sampler.lv2/uris.h b/plugins/eg-sampler.lv2/uris.h
index 87b16cc..e1c6edd 100644
--- a/plugins/eg-sampler.lv2/uris.h
+++ b/plugins/eg-sampler.lv2/uris.h
@@ -15,11 +15,38 @@
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
+#ifndef SAMPLER_URIS_H
+#define SAMPLER_URIS_H
+
+#include "lv2/lv2plug.in/ns/ext/state/state.h"
+
 #define NS_ATOM "http://lv2plug.in/ns/ext/atom#"
 #define NS_RDF  "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 
 #define SAMPLER_URI       "http://lv2plug.in/plugins/eg-sampler"
 #define MIDI_EVENT_URI    "http://lv2plug.in/ns/ext/midi#MidiEvent"
 #define FILENAME_URI      SAMPLER_URI "#filename"
-#define ATOM_BLANK_URI    NS_ATOM "Blank"
-#define ATOM_RESOURCE_URI NS_ATOM "Resource"
+
+typedef struct {
+	LV2_URID atom_Blank;
+	LV2_URID atom_Resource;
+	LV2_URID eg_filename;
+	LV2_URID midi_Event;
+	LV2_URID msg_Set;
+	LV2_URID msg_body;
+	LV2_URID state_Path;
+} SamplerURIs;
+
+static inline void
+map_sampler_uris(LV2_URID_Map* map, SamplerURIs* uris)
+{
+	uris->atom_Blank    = map->map(map->handle, NS_ATOM "Blank");
+	uris->atom_Resource = map->map(map->handle, NS_ATOM "Resource");
+	uris->eg_filename   = map->map(map->handle, FILENAME_URI);
+	uris->midi_Event    = map->map(map->handle, MIDI_EVENT_URI);
+	uris->msg_Set       = map->map(map->handle, LV2_MESSAGE_Set);
+	uris->msg_body      = map->map(map->handle, LV2_MESSAGE_body);
+	uris->state_Path    = map->map(map->handle, LV2_STATE_PATH_URI);
+}
+
+#endif  /* SAMPLER_URIS_H */
-- 
cgit v1.2.1