ExamplePluginParameters.cpp (10324B)
1 /* 2 * DISTRHO Plugin Framework (DPF) 3 * Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any purpose with 6 * or without fee is hereby granted, provided that the above copyright notice and this 7 * permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD 10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN 11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include "DistrhoPlugin.hpp" 18 19 START_NAMESPACE_DISTRHO 20 21 // ----------------------------------------------------------------------------------------------------------- 22 23 /** 24 Simple plugin to demonstrate parameter usage (including UI). 25 The plugin will be treated as an effect, but it will not change the host audio. 26 */ 27 class ExamplePluginParameters : public Plugin 28 { 29 public: 30 ExamplePluginParameters() 31 : Plugin(9, 2, 0) // 9 parameters, 2 programs, 0 states 32 { 33 /** 34 Initialize all our parameters to their defaults. 35 In this example all parameters have 0 as default, so we can simply zero them. 36 */ 37 std::memset(fParamGrid, 0, sizeof(float)*9); 38 } 39 40 protected: 41 /* -------------------------------------------------------------------------------------------------------- 42 * Information */ 43 44 /** 45 Get the plugin label. 46 A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers. 47 */ 48 const char* getLabel() const override 49 { 50 return "parameters"; 51 } 52 53 /** 54 Get an extensive comment/description about the plugin. 55 */ 56 const char* getDescription() const override 57 { 58 return "Simple plugin to demonstrate parameter usage (including UI).\n\ 59 The plugin will be treated as an effect, but it will not change the host audio."; 60 } 61 62 /** 63 Get the plugin author/maker. 64 */ 65 const char* getMaker() const override 66 { 67 return "DISTRHO"; 68 } 69 70 /** 71 Get the plugin homepage. 72 */ 73 const char* getHomePage() const override 74 { 75 return "https://github.com/DISTRHO/DPF"; 76 } 77 78 /** 79 Get the plugin license name (a single line of text). 80 For commercial plugins this should return some short copyright information. 81 */ 82 const char* getLicense() const override 83 { 84 return "ISC"; 85 } 86 87 /** 88 Get the plugin version, in hexadecimal. 89 */ 90 uint32_t getVersion() const override 91 { 92 return d_version(1, 0, 0); 93 } 94 95 /* -------------------------------------------------------------------------------------------------------- 96 * Init */ 97 98 enum { 99 kPortGroupTop = 0, 100 kPortGroupMiddle, 101 kPortGroupBottom 102 }; 103 104 /** 105 Initialize the audio port @a index.@n 106 This function will be called once, shortly after the plugin is created. 107 */ 108 void initAudioPort(bool input, uint32_t index, AudioPort& port) override 109 { 110 // treat meter audio ports as stereo 111 port.groupId = kPortGroupStereo; 112 113 // everything else is as default 114 Plugin::initAudioPort(input, index, port); 115 } 116 117 /** 118 Initialize the parameter @a index. 119 This function will be called once, shortly after the plugin is created. 120 */ 121 void initParameter(uint32_t index, Parameter& parameter) override 122 { 123 /** 124 All parameters in this plugin are similar except for name. 125 As such, we initialize the common details first, then set the unique name later. 126 */ 127 128 /** 129 Changing parameters does not cause any realtime-unsafe operations, so we can mark them as automatable. 130 Also set as boolean because they work as on/off switches. 131 */ 132 parameter.hints = kParameterIsAutomatable|kParameterIsBoolean; 133 134 /** 135 Minimum 0 (off), maximum 1 (on). 136 Default is off. 137 */ 138 parameter.ranges.min = 0.0f; 139 parameter.ranges.max = 1.0f; 140 parameter.ranges.def = 0.0f; 141 142 /** 143 Set the (unique) parameter name. 144 @see fParamGrid 145 */ 146 switch (index) 147 { 148 case 0: 149 parameter.name = "top-left"; 150 parameter.groupId = kPortGroupTop; 151 break; 152 case 1: 153 parameter.name = "top-center"; 154 parameter.groupId = kPortGroupTop; 155 break; 156 case 2: 157 parameter.name = "top-right"; 158 parameter.groupId = kPortGroupTop; 159 break; 160 case 3: 161 parameter.name = "middle-left"; 162 parameter.groupId = kPortGroupMiddle; 163 break; 164 case 4: 165 parameter.name = "middle-center"; 166 parameter.groupId = kPortGroupMiddle; 167 break; 168 case 5: 169 parameter.name = "middle-right"; 170 parameter.groupId = kPortGroupMiddle; 171 break; 172 case 6: 173 parameter.name = "bottom-left"; 174 parameter.groupId = kPortGroupBottom; 175 break; 176 case 7: 177 parameter.name = "bottom-center"; 178 parameter.groupId = kPortGroupBottom; 179 break; 180 case 8: 181 parameter.name = "bottom-right"; 182 parameter.groupId = kPortGroupBottom; 183 break; 184 } 185 186 /** 187 Our parameter names are valid symbols except for "-". 188 */ 189 parameter.symbol = parameter.name; 190 parameter.symbol.replace('-', '_'); 191 } 192 193 /** 194 Initialize the port group @a groupId.@n 195 This function will be called once, 196 shortly after the plugin is created and all audio ports and parameters have been enumerated. 197 */ 198 void initPortGroup(uint32_t groupId, PortGroup& portGroup) override 199 { 200 switch (groupId) { 201 case kPortGroupTop: 202 portGroup.name = "Top"; 203 portGroup.symbol = "top"; 204 break; 205 case kPortGroupMiddle: 206 portGroup.name = "Middle"; 207 portGroup.symbol = "middle"; 208 break; 209 case kPortGroupBottom: 210 portGroup.name = "Bottom"; 211 portGroup.symbol = "bottom"; 212 break; 213 } 214 } 215 216 /** 217 Set the name of the program @a index. 218 This function will be called once, shortly after the plugin is created. 219 */ 220 void initProgramName(uint32_t index, String& programName) override 221 { 222 switch (index) 223 { 224 case 0: 225 programName = "Default"; 226 break; 227 case 1: 228 programName = "Custom"; 229 break; 230 } 231 } 232 233 /* -------------------------------------------------------------------------------------------------------- 234 * Internal data */ 235 236 /** 237 Get the current value of a parameter. 238 */ 239 float getParameterValue(uint32_t index) const override 240 { 241 return fParamGrid[index]; 242 } 243 244 /** 245 Change a parameter value. 246 */ 247 void setParameterValue(uint32_t index, float value) override 248 { 249 fParamGrid[index] = value; 250 } 251 252 /** 253 Load a program. 254 The host may call this function from any context, including realtime processing. 255 */ 256 void loadProgram(uint32_t index) override 257 { 258 switch (index) 259 { 260 case 0: 261 fParamGrid[0] = 0.0f; 262 fParamGrid[1] = 0.0f; 263 fParamGrid[2] = 0.0f; 264 fParamGrid[3] = 0.0f; 265 fParamGrid[4] = 0.0f; 266 fParamGrid[5] = 0.0f; 267 fParamGrid[6] = 0.0f; 268 fParamGrid[7] = 0.0f; 269 fParamGrid[8] = 0.0f; 270 break; 271 case 1: 272 fParamGrid[0] = 1.0f; 273 fParamGrid[1] = 1.0f; 274 fParamGrid[2] = 0.0f; 275 fParamGrid[3] = 0.0f; 276 fParamGrid[4] = 1.0f; 277 fParamGrid[5] = 1.0f; 278 fParamGrid[6] = 1.0f; 279 fParamGrid[7] = 0.0f; 280 fParamGrid[8] = 1.0f; 281 break; 282 } 283 } 284 285 /* -------------------------------------------------------------------------------------------------------- 286 * Process */ 287 288 /** 289 Run/process function for plugins without MIDI input. 290 */ 291 void run(const float** inputs, float** outputs, uint32_t frames) override 292 { 293 /** 294 This plugin does nothing, it just demonstrates parameter usage. 295 So here we directly copy inputs over outputs, leaving the audio untouched. 296 We need to be careful in case the host re-uses the same buffer for both inputs and outputs. 297 */ 298 if (outputs[0] != inputs[0]) 299 std::memcpy(outputs[0], inputs[0], sizeof(float)*frames); 300 301 if (outputs[1] != inputs[1]) 302 std::memcpy(outputs[1], inputs[1], sizeof(float)*frames); 303 } 304 305 // ------------------------------------------------------------------------------------------------------- 306 307 private: 308 /** 309 Our parameters are used to display a 3x3 grid like this: 310 0 1 2 311 3 4 5 312 6 7 8 313 314 The index matches its grid position. 315 */ 316 float fParamGrid[9]; 317 318 /** 319 Set our plugin class as non-copyable and add a leak detector just in case. 320 */ 321 DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginParameters) 322 }; 323 324 /* ------------------------------------------------------------------------------------------------------------ 325 * Plugin entry point, called by DPF to create a new plugin instance. */ 326 327 Plugin* createPlugin() 328 { 329 return new ExamplePluginParameters(); 330 } 331 332 // ----------------------------------------------------------------------------------------------------------- 333 334 END_NAMESPACE_DISTRHO