From 8641328aa6eba6b24391dca1d7f6516beaea8d04 Mon Sep 17 00:00:00 2001 From: David Runge Date: Wed, 6 Jul 2016 19:38:54 +0200 Subject: classes/BowelyzerConfig.sc: Adding writeConfigurationFile functionality (valid JSON, yay). Adding global configFilePath. Updating hfThreshold ControlSpec to default to 1.0. Removing obsolete comments. --- classes/BowelyzerConfig.sc | 103 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 3 deletions(-) diff --git a/classes/BowelyzerConfig.sc b/classes/BowelyzerConfig.sc index 287aad4..1a4ce87 100644 --- a/classes/BowelyzerConfig.sc +++ b/classes/BowelyzerConfig.sc @@ -66,6 +66,7 @@ BowelyzerConfig{ ]; var 0, \right -> 1 ]), - //names -> [left, right], \synthServerAddress -> "127.0.0.1", \synthServerPort -> 57110, \hubAddress -> "127.0.0.1", @@ -230,7 +233,100 @@ BowelyzerConfig{ }); } - //TODO: add writeConfigurationFile + // write a valid JSON file from configuration + writeConfigurationFile{ + arg fileName; + var configFile, + path, + overwriteConfigFilePath = false, + writeFinished = false; + configFilePath.postln; + fileName.postln; + if(fileName.isNil, { + if(configFilePath.notNil, { + path = configFilePath; + },{ + "No configuration file defined!".error; + }); + },{ + path = fileName; + overwriteConfigFilePath = true; + }); + if(path != "",{ + path.postln; + try{ + configFile = File.use( + path.asString, + "w", + {|configFile| + var controlNamesSize = config.at(\controls).size, + currentControlName = 0, + controlsSize = defaultControls.size, + currentControl = 0, + inputsSize = config.at(\inputs).size, + currentInput = 0; + configFile.write("{\n"); + // global settings + config.pairsDo({|key, value| + if(key != \controls && key != \inputs, { + if(hasToBeString.includes(key), { + configFile.write(" \""++key++"\": \""++value++"\",\n"); + }); + if(hasToBeInteger.includes(key),{ + configFile.write(" \""++key++"\": "++value++",\n"); + }); + }); + }); + // controls + configFile.write(" \"controls\": {\n"); + config.at(\controls).pairsDo({|key, value| + currentControlName = currentControlName+1; + configFile.write(" \""++key++"\": {\n"); + value.pairsDo({|control, controlValue| + currentControl = currentControl+1; + if(currentControl == controlsSize,{ + configFile.write(" \""++control++"\": "++controlValue++"\n"); + },{ + configFile.write(" \""++control++"\": "++controlValue++",\n"); + }); + }); + // closing brace + if(currentControlName == controlNamesSize,{ + configFile.write(" }\n"); + },{ + configFile.write(" },\n"); + }); + currentControl = 0; + }); + // closing brace for controls + configFile.write(" },\n"); + // inputs + configFile.write(" \"inputs\": {\n"); + config.at(\inputs).pairsDo({|key, value| + currentInput = currentInput+1; + if(currentInput == inputsSize,{ + configFile.write(" \""++key++"\": "++value++"\n"); + },{ + configFile.write(" \""++key++"\": "++value++",\n"); + }); + }); + // closing brace for inputs + configFile.write(" }\n"); + // closing brace for notation + configFile.write("}\n"); + configFile.close; + writeFinished = true; + } + ); + }{ + ("Failed writing the file: "++path).error; + ^false; + } + }); + if(writeFinished && overwriteConfigFilePath, { + configFilePath = fileName; + }); + } // return a control value as correct type and within its defined ControlSpec range getControlValue{ @@ -275,6 +371,7 @@ BowelyzerConfig{ var broken = false; //fail if there are no inputs defined if(config.includesKey(\inputs).not,{ + "No inputs defined!".error; ^false; }); // if there are no controls defined at all, add at least a Dictionary for them -- cgit v1.2.3-54-g00ecf