From e7d907510baf457368fcd3496d66fc3c8bde682a Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Mon, 26 Dec 2016 13:46:49 +0100 Subject: [PATCH] Allow specification of target plugin for --parameter argument while retaining backwards compatibility. --- source/MrsWatsonOptions.c | 9 ++++----- source/plugin/PluginChain.c | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/source/MrsWatsonOptions.c b/source/MrsWatsonOptions.c index 7a91627b..abb7c43c 100644 --- a/source/MrsWatsonOptions.c +++ b/source/MrsWatsonOptions.c @@ -216,11 +216,10 @@ Use '-' to write to stdout..", options, newProgramOptionWithName( OPTION_PARAMETER, "parameter", - "Set a parameter in a plugin. May be specified multiple times, but can only \ -set parameters for the first plugin in a chain. Parameter indexes for plugins \ -can be found with the --display-info option. Use comma-separated arguments for \ -index/value, for example:\n\n\ -\t--parameter 1,0.3 --parameter 0,0.75", + "Set a parameter in a plugin. May be specified multiple times. Parameter indexes for plugins \ +can be found with the --display-info option. Use arguments for \ +plugin:index,value, for example:\n\n\ +\t--parameter 0:1,0.3 --parameter 2:0,0.75", NO_SHORT_FORM, kProgramOptionTypeList, kProgramOptionArgumentTypeRequired)); diff --git a/source/plugin/PluginChain.c b/source/plugin/PluginChain.c index 890c574a..024bc86a 100644 --- a/source/plugin/PluginChain.c +++ b/source/plugin/PluginChain.c @@ -273,7 +273,7 @@ unsigned long pluginChainGetProcessingDelay(PluginChain self) { } typedef struct { - Plugin plugin; + PluginChain self; boolByte success; } _PluginChainSetParameterPassData; @@ -283,11 +283,32 @@ void _pluginChainSetParameter(void *item, void *userData) { char *parameterValue = (char *)item; _PluginChainSetParameterPassData *passData = (_PluginChainSetParameterPassData *)userData; - Plugin plugin = passData->plugin; + PluginChain self = passData->self; + char *comma = NULL; + char *colon = NULL; + int pluginIndex; int index; float value; + colon = strchr(parameterValue, ':'); + + if (colon == NULL) { + logWarn("No plugin index set for parameter argument %s, defaulting to plugin chain head", parameterValue); + pluginIndex = 0; + } else { + pluginIndex = (int)strtod(parameterValue, NULL); + + if (pluginIndex < 0 || pluginIndex >= self->numPlugins) { + logWarn("Plugin index for parameter argument %s is out of bounds, skipping parameter", parameterValue); + return; + } + + parameterValue = colon + 1; + } + + Plugin plugin = self->plugins[pluginIndex]; + // If a previous attempt to set a parameter failed, then return right away // since this method will return false anyways. if (!passData->success) { @@ -306,16 +327,15 @@ void _pluginChainSetParameter(void *item, void *userData) { *comma = '\0'; index = (int)strtod(parameterValue, NULL); value = (float)strtod(comma + 1, NULL); - logDebug("Set parameter %d to %f", index, value); + logDebug("Set parameter %d of plugin %d to %f", index, pluginIndex, value); passData->success = plugin->setParameter(plugin, (unsigned int)index, value); } boolByte pluginChainSetParameters(PluginChain self, const LinkedList parameters) { _PluginChainSetParameterPassData passData; - passData.plugin = self->plugins[0]; + passData.self = self; passData.success = true; - logDebug("Setting parameters on head plugin in chain"); linkedListForeach(parameters, _pluginChainSetParameter, &passData); return passData.success; }