PLUG'N SCRIPT
rapid plugin development
Tutorial
DSP
KUIML
How-to
Scripts
GUI
  • Drawing
  • Simple knobs
Library
  • Params
ScriptsLibraryParams
August 18, 2020   Ilya

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

Download this library

  1. ////////////////////////////////////
  2. // This is a helper library for more convenient way of adding
  3. // input/output params and strings in Plug'n Script DSP API (AngelScript version)
  4. // examples of use: https://pns.letimix.com/scripts/library/params#examples
  5. ////////////////////////////////////
  6. // 2020 (c) LetiMix | Ilya Orlov
  7. ////////////////////////////////////
  8.  
  9. // create arrays for input/output params and strings
  10. array<double> inputParameters, outputParameters,
  11. inputParametersMin, outputParametersMin,
  12. inputParametersMax, outputParametersMax,
  13. inputParametersDefault, outputParametersDefault;
  14. array<string> inputParametersNames, outputParametersNames,
  15. inputParametersUnits, outputParametersUnits,
  16. inputParametersFormats, outputParametersFormats,
  17. inputParametersEnums, outputParametersEnums,
  18. inputStrings, outputStrings, inputStringsNames, outputStringsNames;
  19. array<int> inputParametersSteps, outputStringsMaxLengths;
  20.  
  21. // shortcuts for input/output parameters and strings
  22. array<double>@ IP = inputParameters;
  23. array<double>@ OP = outputParameters;
  24. array<string>@ IPS = inputStrings;
  25. array<string>@ OPS = outputStrings;
  26.  
  27. // INPUT PARAMETERS
  28.  
  29. // add (or update) input parameter
  30. 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 = "") {
  31. if (index < 0) index = int(inputParameters.length); // auto-increase index
  32. uint nn = index+1;
  33. // verify arrays size
  34. if (inputParameters.length < nn) {
  35. inputParameters.resize(nn);
  36. inputParametersNames.resize(nn);
  37. inputParametersUnits.resize(nn);
  38. inputParametersMin.resize(nn);
  39. inputParametersMax.resize(nn);
  40. inputParametersDefault.resize(nn);
  41. inputParametersFormats.resize(nn);
  42. inputParametersSteps.resize(nn);
  43. inputParametersEnums.resize(nn);
  44. }
  45. // add param name (name)
  46. inputParametersNames[index] = name;
  47. inputParametersUnits[index] = units;
  48. inputParametersMin[index] = min;
  49. inputParametersMax[index] = max;
  50. inputParametersDefault[index] = def_no;
  51. inputParametersFormats[index] = vf;
  52. // auto calculation of steps if value_format = .0
  53. if ((steps < 1) and ((parseFloat(vf) - floor(parseFloat(vf))) == 0)) {
  54. steps = int(max - min) + 1;
  55. }
  56. inputParametersSteps[index] = steps;
  57. inputParametersEnums[index] = enums;
  58. }
  59.  
  60. // add or update enumerated input parameter
  61. void ip(int index, string name, string enums = "%", string def_value = ""){
  62. // check if we're adding enums
  63. if (enums.findFirst(";") > 0) {
  64. array<string> ar = enums.split(";");
  65. int max = ar.length - 1;
  66. int def_no = 0; // find index of default_value
  67. if (def_value != "") def_no = ar.find(def_value);
  68. if (def_no < 0) def_no = 0;
  69. // add enum
  70. ip(index, name, "", 0, max, def_no, "", ar.length, enums);
  71. } else {
  72. // add regular param
  73. string units = enums;
  74. ip(index, name, units, 0);
  75. }
  76. }
  77.  
  78. // the same but adding item to the end of list and returning index
  79. int ip(string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", int steps = 0, string enums = ""){
  80. int index = int(inputParameters.length);
  81. ip(index, name, units, min, max, def_no, vf, steps, enums);
  82. return index;
  83. }
  84. // adding enum param to end of list and returning index
  85. int ip(string name, string enums = "%", string def_value = ""){
  86. int index = int(inputParameters.length);
  87. ip(index, name, enums, def_value);
  88. return index;
  89. }
  90.  
  91. // OUTPUT PARAMETERS
  92.  
  93. // add (or update) output parameter
  94. void op(int index, string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = "") {
  95. uint nn = index+1;
  96. // verify arrays size
  97. if (outputParameters.length < nn) {
  98. outputParameters.resize(nn);
  99. outputParametersNames.resize(nn);
  100. outputParametersUnits.resize(nn);
  101. outputParametersMin.resize(nn);
  102. outputParametersMax.resize(nn);
  103. outputParametersDefault.resize(nn);
  104. outputParametersFormats.resize(nn);
  105. outputParametersEnums.resize(nn);
  106. }
  107. // add param name (name)
  108. outputParametersNames[index] = name;
  109. outputParametersUnits[index] = units;
  110. outputParametersMin[index] = min;
  111. outputParametersMax[index] = max;
  112. outputParametersDefault[index] = def_no;
  113. outputParametersFormats[index] = vf;
  114. outputParametersEnums[index] = enums;
  115. }
  116.  
  117. // add or update enumerated output parameter
  118. void op(int index, string name, string enums = "%", string def_value = ""){
  119. // check if we're adding enums
  120. if (enums.findFirst(";") > 0) {
  121. array<string> ar = enums.split(";");
  122. int max = ar.length - 1;
  123. int def_no = 0; // find index of default_value
  124. if (def_value != "") def_no = ar.find(def_value);
  125. if (def_no < 0) def_no = 0;
  126. // add enum
  127. op(index, name, "", 0, max, def_no, "", enums);
  128. } else {
  129. // add regular param
  130. string units = enums;
  131. op(index, name, units, 0);
  132. }
  133. }
  134. // the same but adding item to the end of list and returning index
  135. int op(string name, string units, double min, double max = 0, double def_no = 0, string vf = ".1", string enums = ""){
  136. int index = int(outputParameters.length);
  137. op(index, name, units, min, max, def_no, vf, enums);
  138. return index;
  139. }
  140. // adding enum param to end of list and returning index
  141. int op(string name, string enums = "%", string def_value = ""){
  142. int index = int(outputParameters.length);
  143. op(index, name, enums, def_value);
  144. return index;
  145. }
  146.  
  147. // INPUT STRINGS
  148.  
  149. // add/update input string via index
  150. void ips(int index, string name) {
  151. uint nn = index+1;
  152. // verify arrays size
  153. if (inputStrings.length < nn) {
  154. inputStrings.resize(nn);
  155. inputStringsNames.resize(nn);
  156. }
  157. inputStringsNames[index] = name;
  158. }
  159.  
  160. // adding input string to the end of list and returning index
  161. int ips(string name){
  162. int index = int(inputStrings.length);
  163. ips(index, name);
  164. return index;
  165. }
  166.  
  167. // OUTPUT STRINGS
  168.  
  169. // add/update output string via index
  170. void ops(int index, string name, int maxlen = 1024) {
  171. uint nn = index+1;
  172. // verify arrays size
  173. if (outputStrings.length < nn) {
  174. outputStrings.resize(nn);
  175. outputStringsNames.resize(nn);
  176. outputStringsMaxLengths.resize(nn);
  177. }
  178. outputStringsNames[index] = name;
  179. outputStringsMaxLengths[index] = maxlen;
  180. }
  181.  
  182. // adding output string to the end of list and returning index
  183. int ops(string name, int maxlen = 1024){
  184. int index = int(outputStrings.length);
  185. ops(index, name, maxlen);
  186. return index;
  187. }

Examples

1. Adding params with static index number

* Maybe you'd like to use second example, cause it gives some advantages. 

First we add constants (0,1,2,3...) that we'll later use as param indexes.

  1. #include "library/params.as"
  2.  
  3. // adding params indexes
  4. enum inputParamsEnum{
  5. ENABLE,
  6. MODE,
  7. THRESHOLD,
  8. MIX,
  9. }
  10. enum outParamsEnum{
  11. GR,
  12. OUT_MODE,
  13. OUT_LEVEL
  14. }
  15. enum inStringsEnum{
  16. FILENAME
  17. }
  18. enum outStringsEnum{
  19. MYDATA1
  20. }
  21.  
  22. void initialize() {
  23.  
  24. // format for adding params
  25. // index, "name", ["units", min, max, default, "format", [steps]]
  26. // format for adding enumerated values params
  27. // index, "name", "value1;value2;value3", ["default_value"]
  28.  
  29. // add input params
  30. ip(ENABLE, "Enable", "Off;On");
  31. ip(MODE, "Mode", "Simple;Classic;1176;Opto", "Classic");
  32. ip(THRESHOLD, "Threshold", "dB", -40, 0, -18, ".1");
  33. ip(MIX, "Wet/Dry");
  34.  
  35. // add output params
  36. op(GR, "Gain Reduction", "db", 0, 10);
  37. op(OUT_MODE, "Out mode", "Simple;Classic;1176;Opto");
  38. op(OUT_LEVEL, "Out level");
  39.  
  40. // add input strings
  41. ips(FILENAME, "Filename");
  42.  
  43. // add output strings
  44. ops(MYDATA1, "MyData1", 256);
  45. }

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.

2. Adding params and getting index in return

This is the preferred method. Instead of using constants, we can get param indexes from functions. The advantage is that now we can easily re-order and mute (comment out) params.

  1. #include "library/params.as"
  2.  
  3. // create placeholders for params indexes
  4. int ENABLE, MODE, THRESHOLD, MIX, GR, OUT_MODE, OUT_LEVEL, FILENAME, MYDATA1;
  5.  
  6. void initialize() {
  7. // format for adding params
  8. // index, "name", ["units", min, max, default, "format", [steps]]
  9. // format for adding enumerated values params
  10. // index, "name", "value1;value2;value3", ["default_value"]
  11.  
  12. // add input params
  13. ENABLE = ip("Enable", "Off;On");
  14. MODE = ip("Mode", "Simple;Classic;1176;Opto", "Classic");
  15. THRESHOLD = ip("Threshold", "dB", -40, 0, -18, ".1");
  16. MIX = ip("Wet/Dry");
  17.  
  18. // add output params
  19. GR = op("Gain Reduction", "db", 0, 10);
  20. OUT_MODE = op("Out mode", "Simple;Classic;1176;Opto");
  21. OUT_LEVEL = op("Out level");
  22.  
  23. // add input strings
  24. FILENAME = ips("Filename");
  25.  
  26. // add output strings (with max length)
  27. MYDATA1 = ops("MyData1", 256);
  28. }

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)

  1. void updateInputParameters() {
  2. double threshold = IP[THRESHOLD];
  3. string mystring = IPS[FILENAME];
  4. }
  5.  
  6. void computeOutputData() {
  7. OP[GR] = rand(0,10);
  8. OPS[MYDATA1] = someString;
  9. }

Comments

2020 © Plug'n Script and KUIML by Blue Cat Audio  |  Site by LetiMix