Adding params
Letimix library script to make adding input/output params and input/output strings more convenient. This is Angelscript version, there's also native (C++) version available.
See examples of use below. And here's a how-to article
- ////////////////////////////////////
- // This is a helper library for more convenient way of adding
- // input/output params and strings in Plug'n Script DSP API (AngelScript version)
- // examples of use: https://pns.letimix.com/scripts/library/params
- ////////////////////////////////////
- // 2020-2025 (c) LetiMix | Ilya Orlov
- ////////////////////////////////////
- // HOW TO USE
- // #include "_lib/params.as"
- // // create placeholders for params indexes
- // int ENABLE, MODE, THRESHOLD, MIX, GR, OUT_MODE, OUT_LEVEL, PP_LEVEL, PP_STATE, FILENAME, MYDATA1;
- // void initialize(){
- // // format for adding params
- // // index, "name", ["units", min, max, default, "format", [steps]]
- // // format for adding enumerated values params
- // // index, "name", "value1;value2;value3", ["default_value"]
- // // add input params
- // ENABLE = ip("Enable", "Off;On");
- // MODE = ip("Mode", "Simple;Classic;1176;Opto", "Classic");
- // THRESHOLD = ip("Threshold", "dB", -40, 0, -18, ".1");
- // MIX = ip("Wet/Dry");
- // // add output params
- // GR = op("Gain Reduction", "db", 0, 10);
- // OUT_MODE = op("Out mode", "Simple;Classic;1176;Opto");
- // OUT_LEVEL = op("Out level");
- // // add private output params
- // PP_LEVEL = pp("Out level", "dB", -40, 0, -18, ".1");
- // PP_STATE = pp("Out state", "Idle;Ready;Running", "Idle");
- // // add input strings
- // FILENAME = ips("Filename", [true = makes persistent]);
- // // add output strings (with max length)
- // MYDATA1 = ops("MyData1", 256);
- // }
- // You can use shortcuts IP (for inputParameters), OP (for outputParameters), PP (for privateOutputParameters)
- // IPS and OPS (for inputStrings and outputStrings)
- // void updateInputParameters() {
- // double threshold = IP[THRESHOLD];
- // string mystring = IPS[FILENAME];
- // }
- // void computeOutputData(){
- // OP[GR] = rand(0,10);
- // PP[PP_LEVEL] = rand(-40,0);
- // OPS[MYDATA1] = "Enable: "+IP[ENABLE];
- // }
- // create arrays for input/output params and strings
- array<double> inputParameters, outputParameters, privateOutputParameters,
- inputParametersMin, outputParametersMin, privateOutputParametersMin,
- inputParametersMax, outputParametersMax, privateOutputParametersMax,
- inputParametersDefault, outputParametersDefault, privateOutputParametersDefault;
- array<string> inputParametersNames, outputParametersNames, privateOutputParametersNames,
- inputParametersUnits, outputParametersUnits, privateOutputParametersUnits,
- inputParametersFormats, outputParametersFormats, privateOutputParametersFormats,
- inputParametersEnums, outputParametersEnums, privateOutputParametersEnums,
- inputStrings, outputStrings, inputStringsNames, outputStringsNames;
- array<int> inputParametersSteps, outputStringsMaxLengths, inputStringsPersistency;
- // shortcuts for input/output parameters and strings
- array<double>@ IP = inputParameters;
- array<double>@ OP = outputParameters;
- array<double>@ PP = privateOutputParameters;
- array<string>@ IPS = inputStrings;
- array<string>@ OPS = outputStrings;
- // INPUT PARAMETERS
- // add (or update) input parameter
- void ip(int index, string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", int steps = 0, string enums = "") {
- if (index < 0) index = int(inputParameters.length); // auto-increase index
- uint nn = index+1;
- // verify arrays size
- if (inputParameters.length < nn) {
- inputParameters.resize(nn);
- inputParametersNames.resize(nn);
- inputParametersUnits.resize(nn);
- inputParametersMin.resize(nn);
- inputParametersMax.resize(nn);
- inputParametersDefault.resize(nn);
- inputParametersFormats.resize(nn);
- inputParametersSteps.resize(nn);
- inputParametersEnums.resize(nn);
- }
- // add param name (name)
- inputParametersNames[index] = name;
- inputParametersUnits[index] = units;
- inputParametersMin[index] = min;
- inputParametersMax[index] = max;
- inputParametersDefault[index] = def_no;
- inputParametersFormats[index] = vf;
- // auto calculation of steps if value_format = .0
- if ((steps < 1) and ((parseFloat(vf) - floor(parseFloat(vf))) == 0)) {
- steps = int(max - min) + 1;
- }
- inputParametersSteps[index] = steps;
- inputParametersEnums[index] = enums;
- }
- // add or update enumerated input parameter
- void ip(int index, string name, string enums = "%", string def_value = ""){
- // check if we're adding enums
- if (enums.findFirst(";") > 0) {
- array<string> ar = enums.split(";");
- int max = ar.length - 1;
- int def_no = 0; // find index of default_value
- if (def_value != "") def_no = ar.find(def_value);
- if (def_no < 0) def_no = 0;
- // add enum
- ip(index, name, "", 0, max, def_no, "", ar.length, enums);
- } else {
- // add regular param
- string units = enums;
- ip(index, name, units, 0);
- }
- }
- // the same but adding item to the end of list and returning index
- int ip(string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", int steps = 0, string enums = ""){
- int index = int(inputParameters.length);
- ip(index, name, units, min, max, def_no, vf, steps, enums);
- return index;
- }
- // adding enum param to end of list and returning index
- int ip(string name, string enums = "%", string def_value = ""){
- int index = int(inputParameters.length);
- ip(index, name, enums, def_value);
- return index;
- }
- // OUTPUT PARAMETERS
- // add (or update) output parameter
- void op(int index, string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = "", bool is_private = false) {
- uint nn = index+1;
- if (!is_private) {
- // for regular output params (not private)
- // verify arrays size
- if (outputParameters.length < nn) {
- outputParameters.resize(nn);
- outputParametersNames.resize(nn);
- outputParametersUnits.resize(nn);
- outputParametersMin.resize(nn);
- outputParametersMax.resize(nn);
- outputParametersDefault.resize(nn);
- outputParametersFormats.resize(nn);
- outputParametersEnums.resize(nn);
- }
- // add param name (name)
- outputParametersNames[index] = name;
- outputParametersUnits[index] = units;
- outputParametersMin[index] = min;
- outputParametersMax[index] = max;
- outputParametersDefault[index] = def_no;
- outputParametersFormats[index] = vf;
- outputParametersEnums[index] = enums;
- } else {
- // for private params
- // verify arrays size
- if (privateOutputParameters.length < nn) {
- privateOutputParameters.resize(nn);
- privateOutputParametersNames.resize(nn);
- privateOutputParametersUnits.resize(nn);
- privateOutputParametersMin.resize(nn);
- privateOutputParametersMax.resize(nn);
- privateOutputParametersDefault.resize(nn);
- privateOutputParametersFormats.resize(nn);
- privateOutputParametersEnums.resize(nn);
- }
- // add param name (name)
- privateOutputParametersNames[index] = name;
- privateOutputParametersUnits[index] = units;
- privateOutputParametersMin[index] = min;
- privateOutputParametersMax[index] = max;
- privateOutputParametersDefault[index] = def_no;
- privateOutputParametersFormats[index] = vf;
- privateOutputParametersEnums[index] = enums;
- }
- }
- // add or update enumerated output parameter
- void op(int index, string name, string enums = "%", string def_value = "", bool is_private = false){
- // check if we're adding enums
- if (enums.findFirst(";") > 0) {
- array<string> ar = enums.split(";");
- int max = ar.length - 1;
- int def_no = 0; // find index of default_value
- if (def_value != "") def_no = ar.find(def_value);
- if (def_no < 0) def_no = 0;
- // add enum
- op(index, name, "", 0, max, def_no, "", enums, is_private);
- } else {
- // add regular param
- string units = enums;
- op(index, name, units, 0.0, 0.0, 0.0, ".1", "", is_private);
- }
- }
- // the same but adding item to the end of list and returning index
- int op(string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = ""){
- int index = int(outputParameters.length);
- op(index, name, units, min, max, def_no, vf, enums);
- return index;
- }
- // adding enum param to end of list and returning index
- int op(string name, string enums = "%", string def_value = ""){
- int index = int(outputParameters.length);
- op(index, name, enums, def_value);
- return index;
- }
- // PRIVATE OUTPUT PARAMS
- // shortcut to add (or update) private output parameter
- void pp(int index, string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = "") {
- op(index, name, units, min, max, def_no, vf, enums, true);
- }
- // shortcut to add or update enumerated private output parameter
- void pp(int index, string name, string enums = "%", string def_value = ""){
- op(index, name, enums, def_value, true);
- }
- // the same but adding item to the end of list and returning index
- int pp(string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = ""){
- int index = int(privateOutputParameters.length);
- pp(index, name, units, min, max, def_no, vf, enums);
- return index;
- }
- // adding enum param to end of list and returning index
- int pp(string name, string enums = "%", string def_value = ""){
- int index = int(privateOutputParameters.length);
- pp(index, name, enums, def_value);
- return index;
- }
- // INPUT STRINGS
- // add/update input string via index
- void ips(int index, string name, bool persistent = false) {
- uint nn = index+1;
- // verify arrays size
- if (inputStrings.length < nn) {
- inputStrings.resize(nn);
- inputStringsNames.resize(nn);
- inputStringsPersistency.resize(nn);
- }
- inputStringsNames[index] = name;
- inputStringsPersistency[index] = persistent ? 1 : 0;
- }
- // adding input string to the end of list and returning index
- int ips(string name, bool persistent = false){
- int index = int(inputStrings.length);
- ips(index, name, persistent);
- return index;
- }
- // OUTPUT STRINGS
- // add/update output string via index
- void ops(int index, string name, int maxlen = 1024) {
- uint nn = index+1;
- // verify arrays size
- if (outputStrings.length < nn) {
- outputStrings.resize(nn);
- outputStringsNames.resize(nn);
- outputStringsMaxLengths.resize(nn);
- }
- outputStringsNames[index] = name;
- outputStringsMaxLengths[index] = maxlen;
- }
- // adding output string to the end of list and returning index
- int ops(string name, int maxlen = 1024){
- int index = int(outputStrings.length);
- ops(index, name, maxlen);
- return index;
- }
Examples
1. Adding params and getting index in return
This is the preferred method. We create placeholders and then get param indexes from function calls. The advantage is that now we can easily re-order and mute (comment out) parameters.
- #include "library/params.as"
- // create placeholders for params indexes
- int ENABLE, MODE, THRESHOLD, MIX, GR, OUT_MODE, OUT_LEVEL, PP_LEVEL, PP_STATE, FILENAME, MYDATA1;
- // format for adding params
- // index, "name", ["units", min, max, default, "format", [steps]]
- // format for adding enumerated values params
- // index, "name", "value1;value2;value3", ["default_value"]
- // add input params
- ENABLE = ip("Enable", "Off;On");
- MODE = ip("Mode", "Simple;Classic;1176;Opto", "Classic");
- THRESHOLD = ip("Threshold", "dB", -40, 0, -18, ".1");
- MIX = ip("Wet/Dry");
- // add output params
- GR = op("Gain Reduction", "db", 0, 10);
- OUT_MODE = op("Out mode", "Simple;Classic;1176;Opto");
- OUT_LEVEL = op("Out level");
- // add private output params
- PP_LEVEL = pp("Out level", "dB", -40, 0, -18, ".1");
- PP_STATE = pp("Out state", "Idle;Ready;Running", "Idle");
- // add input strings
- FILENAME = ips("Filename");
- // add output strings (with max length)
- MYDATA1 = ops("MyData1", 256);
- }
2. Adding params with static index number (not recommended)
This is another approach, where we first add constants (0,1,2,3...) that we'll later use as param indexes.
- #include "library/params.as"
- // adding params indexes
- enum inputParamsEnum{
- ENABLE,
- MODE,
- THRESHOLD,
- MIX,
- }
- enum outParamsEnum{
- GR,
- OUT_MODE,
- OUT_LEVEL
- }
- enum privateOutParamsEnum{
- PP_LEVEL,
- PP_STATE,
- }
- enum inStringsEnum{
- FILENAME
- }
- enum outStringsEnum{
- MYDATA1
- }
- // format for adding params
- // index, "name", ["units", min, max, default, "format", [steps]]
- // format for adding enumerated values params
- // index, "name", "value1;value2;value3", ["default_value"]
- // add input params
- ip(ENABLE, "Enable", "Off;On");
- ip(MODE, "Mode", "Simple;Classic;1176;Opto", "Classic");
- ip(THRESHOLD, "Threshold", "dB", -40, 0, -18, ".1");
- ip(MIX, "Wet/Dry");
- // add output params
- op(GR, "Gain Reduction", "db", 0, 10);
- op(OUT_MODE, "Out mode", "Simple;Classic;1176;Opto");
- op(OUT_LEVEL, "Out level");
- // add private output params
- pp(PP_LEVEL, "Level", "db", 0, 10);
- pp(PP_STATE, "State", "Idle;Ready;Running");
- // add input strings
- ips(FILENAME, "Filename");
- // add output strings
- ops(MYDATA1, "MyData1", 256);
- }
Note, that we've used Enums instead of writing "const int ENABLE = 0, MODE = 1 ..." , because it's easier to add and re-order values.
3. Using short aliases for long names
To make our code more compact, we can use shortcuts IP (for inputParameters), OP (for outputParameters), IPS and OPS (for inputStrings and outputStrings)
- double threshold = IP[THRESHOLD];
- string mystring = IPS[FILENAME];
- }
- OP[GR] = rand(0,10);
- OPS[MYDATA1] = someString;
- PP[PP_LEVEL] = rand(-40,0);
- }
Comments
Please, authorize to view and post comments.