You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
11KB

  1. #include "plugin.hpp"
  2. #include "Shelves/shelves.hpp"
  3. static const float freqMin = std::log2(shelves::kFreqKnobMin);
  4. static const float freqMax = std::log2(shelves::kFreqKnobMax);
  5. static const float freqInit = (freqMin + freqMax) / 2;
  6. static const float gainMin = -shelves::kGainKnobRange;
  7. static const float gainMax = shelves::kGainKnobRange;
  8. static const float qMin = std::log2(shelves::kQKnobMin);
  9. static const float qMax = std::log2(shelves::kQKnobMax);
  10. static const float qInit = (qMin + qMax) / 2;
  11. struct Shelves : Module {
  12. enum ParamIds {
  13. HS_FREQ_PARAM,
  14. HS_GAIN_PARAM,
  15. P1_FREQ_PARAM,
  16. P1_GAIN_PARAM,
  17. P1_Q_PARAM,
  18. P2_FREQ_PARAM,
  19. P2_GAIN_PARAM,
  20. P2_Q_PARAM,
  21. LS_FREQ_PARAM,
  22. LS_GAIN_PARAM,
  23. NUM_PARAMS
  24. };
  25. enum InputIds {
  26. HS_FREQ_INPUT,
  27. HS_GAIN_INPUT,
  28. P1_FREQ_INPUT,
  29. P1_GAIN_INPUT,
  30. P1_Q_INPUT,
  31. P2_FREQ_INPUT,
  32. P2_GAIN_INPUT,
  33. P2_Q_INPUT,
  34. LS_FREQ_INPUT,
  35. LS_GAIN_INPUT,
  36. FREQ_INPUT,
  37. GAIN_INPUT,
  38. IN_INPUT,
  39. NUM_INPUTS
  40. };
  41. enum OutputIds {
  42. P1_HP_OUTPUT,
  43. P1_BP_OUTPUT,
  44. P1_LP_OUTPUT,
  45. P2_HP_OUTPUT,
  46. P2_BP_OUTPUT,
  47. P2_LP_OUTPUT,
  48. OUT_OUTPUT,
  49. NUM_OUTPUTS
  50. };
  51. enum LightIds {
  52. CLIP_LIGHT,
  53. NUM_LIGHTS
  54. };
  55. shelves::ShelvesEngine engines[16];
  56. bool preGain;
  57. Shelves() {
  58. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  59. configParam(HS_FREQ_PARAM, freqMin, freqMax, freqInit, "High-shelf frequency", " Hz", 2.f);
  60. configParam(P1_FREQ_PARAM, freqMin, freqMax, freqInit, "Parametric 1 frequency", " Hz", 2.f);
  61. configParam(P2_FREQ_PARAM, freqMin, freqMax, freqInit, "Parametric 2 frequency", " Hz", 2.f);
  62. configParam(LS_FREQ_PARAM, freqMin, freqMax, freqInit, "Low-shelf frequency", " Hz", 2.f);
  63. configParam(HS_GAIN_PARAM, gainMin, gainMax, 0.f, "High-shelf gain", " dB");
  64. configParam(P1_GAIN_PARAM, gainMin, gainMax, 0.f, "Parametric 1 gain", " dB");
  65. configParam(P2_GAIN_PARAM, gainMin, gainMax, 0.f, "Parametric 2 gain", " dB");
  66. configParam(LS_GAIN_PARAM, gainMin, gainMax, 0.f, "Low-shelf gain", " dB");
  67. configParam(P1_Q_PARAM, qMin, qMax, qInit, "Parametric 1 quality", "", 2.f);
  68. configParam(P2_Q_PARAM, qMin, qMax, qInit, "Parametric 2 quality", "", 2.f);
  69. onReset();
  70. }
  71. void onReset() override {
  72. preGain = false;
  73. onSampleRateChange();
  74. }
  75. void onSampleRateChange() override {
  76. // TODO In Rack v2, replace with args.sampleRate
  77. for (int c = 0; c < 16; c++) {
  78. engines[c].setSampleRate(APP->engine->getSampleRate());
  79. }
  80. }
  81. void process(const ProcessArgs& args) override {
  82. int channels = std::max(inputs[IN_INPUT].getChannels(), 1);
  83. // Reuse the same frame object for multiple engines because the params aren't touched.
  84. shelves::ShelvesEngine::Frame frame;
  85. frame.pre_gain = preGain;
  86. frame.hs_freq_knob = rescale(params[HS_FREQ_PARAM].getValue(), freqMin, freqMax, 0.f, 1.f);
  87. frame.p1_freq_knob = rescale(params[P1_FREQ_PARAM].getValue(), freqMin, freqMax, 0.f, 1.f);
  88. frame.p2_freq_knob = rescale(params[P2_FREQ_PARAM].getValue(), freqMin, freqMax, 0.f, 1.f);
  89. frame.ls_freq_knob = rescale(params[LS_FREQ_PARAM].getValue(), freqMin, freqMax, 0.f, 1.f);
  90. frame.hs_gain_knob = params[HS_GAIN_PARAM].getValue() / shelves::kGainKnobRange;
  91. frame.p1_gain_knob = params[P1_GAIN_PARAM].getValue() / shelves::kGainKnobRange;
  92. frame.p2_gain_knob = params[P2_GAIN_PARAM].getValue() / shelves::kGainKnobRange;
  93. frame.ls_gain_knob = params[LS_GAIN_PARAM].getValue() / shelves::kGainKnobRange;
  94. frame.p1_q_knob = rescale(params[P1_Q_PARAM].getValue(), qMin, qMax, 0.f, 1.f);
  95. frame.p2_q_knob = rescale(params[P2_Q_PARAM].getValue(), qMin, qMax, 0.f, 1.f);
  96. frame.hs_freq_cv_connected = inputs[HS_FREQ_INPUT].isConnected();
  97. frame.hs_gain_cv_connected = inputs[HS_GAIN_INPUT].isConnected();
  98. frame.p1_freq_cv_connected = inputs[P1_FREQ_INPUT].isConnected();
  99. frame.p1_gain_cv_connected = inputs[P1_GAIN_INPUT].isConnected();
  100. frame.p1_q_cv_connected = inputs[P1_Q_INPUT].isConnected();
  101. frame.p2_freq_cv_connected = inputs[P2_FREQ_INPUT].isConnected();
  102. frame.p2_gain_cv_connected = inputs[P2_GAIN_INPUT].isConnected();
  103. frame.p2_q_cv_connected = inputs[P2_Q_INPUT].isConnected();
  104. frame.ls_freq_cv_connected = inputs[LS_FREQ_INPUT].isConnected();
  105. frame.ls_gain_cv_connected = inputs[LS_GAIN_INPUT].isConnected();
  106. frame.global_freq_cv_connected = inputs[FREQ_INPUT].isConnected();
  107. frame.global_gain_cv_connected = inputs[GAIN_INPUT].isConnected();
  108. frame.p1_hp_out_connected = outputs[P1_HP_OUTPUT].isConnected();
  109. frame.p1_bp_out_connected = outputs[P1_BP_OUTPUT].isConnected();
  110. frame.p1_lp_out_connected = outputs[P1_LP_OUTPUT].isConnected();
  111. frame.p2_hp_out_connected = outputs[P2_HP_OUTPUT].isConnected();
  112. frame.p2_bp_out_connected = outputs[P2_BP_OUTPUT].isConnected();
  113. frame.p2_lp_out_connected = outputs[P2_LP_OUTPUT].isConnected();
  114. float clipLight = 0.f;
  115. for (int c = 0; c < channels; c++) {
  116. frame.main_in = inputs[IN_INPUT].getVoltage(c);
  117. frame.hs_freq_cv = inputs[HS_FREQ_INPUT].getPolyVoltage(c);
  118. frame.hs_gain_cv = inputs[HS_GAIN_INPUT].getPolyVoltage(c);
  119. frame.p1_freq_cv = inputs[P1_FREQ_INPUT].getPolyVoltage(c);
  120. frame.p1_gain_cv = inputs[P1_GAIN_INPUT].getPolyVoltage(c);
  121. frame.p1_q_cv = inputs[P1_Q_INPUT].getPolyVoltage(c);
  122. frame.p2_freq_cv = inputs[P2_FREQ_INPUT].getPolyVoltage(c);
  123. frame.p2_gain_cv = inputs[P2_GAIN_INPUT].getPolyVoltage(c);
  124. frame.p2_q_cv = inputs[P2_Q_INPUT].getPolyVoltage(c);
  125. frame.ls_freq_cv = inputs[LS_FREQ_INPUT].getPolyVoltage(c);
  126. frame.ls_gain_cv = inputs[LS_GAIN_INPUT].getPolyVoltage(c);
  127. frame.global_freq_cv = inputs[FREQ_INPUT].getPolyVoltage(c);
  128. frame.global_gain_cv = inputs[GAIN_INPUT].getPolyVoltage(c);
  129. engines[c].process(frame);
  130. outputs[P1_HP_OUTPUT].setVoltage(frame.p1_hp_out, c);
  131. outputs[P1_BP_OUTPUT].setVoltage(frame.p1_bp_out, c);
  132. outputs[P1_LP_OUTPUT].setVoltage(frame.p1_lp_out, c);
  133. outputs[P2_HP_OUTPUT].setVoltage(frame.p2_hp_out, c);
  134. outputs[P2_BP_OUTPUT].setVoltage(frame.p2_bp_out, c);
  135. outputs[P2_LP_OUTPUT].setVoltage(frame.p2_lp_out, c);
  136. outputs[OUT_OUTPUT].setVoltage(frame.main_out, c);
  137. clipLight += frame.clip;
  138. }
  139. outputs[P1_HP_OUTPUT].setChannels(channels);
  140. outputs[P1_BP_OUTPUT].setChannels(channels);
  141. outputs[P1_LP_OUTPUT].setChannels(channels);
  142. outputs[P2_HP_OUTPUT].setChannels(channels);
  143. outputs[P2_BP_OUTPUT].setChannels(channels);
  144. outputs[P2_LP_OUTPUT].setChannels(channels);
  145. outputs[OUT_OUTPUT].setChannels(channels);
  146. lights[CLIP_LIGHT].setSmoothBrightness(clipLight, args.sampleTime);
  147. }
  148. json_t* dataToJson() override {
  149. json_t* root_j = json_object();
  150. json_object_set_new(root_j, "preGain", json_boolean(preGain));
  151. return root_j;
  152. }
  153. void dataFromJson(json_t* root_j) override {
  154. json_t* preGainJ = json_object_get(root_j, "preGain");
  155. if (preGainJ)
  156. preGain = json_boolean_value(preGainJ);
  157. }
  158. };
  159. struct ShelvesWidget : ModuleWidget {
  160. ShelvesWidget(Shelves* module) {
  161. setModule(module);
  162. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/Shelves.svg")));
  163. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  164. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  165. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  166. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  167. addParam(createParamCentered<Rogan1PSWhite>(mm2px(Vec(41.582, 19.659)), module, Shelves::HS_FREQ_PARAM));
  168. addParam(createParamCentered<Rogan1PSWhite>(mm2px(Vec(65.699, 19.659)), module, Shelves::HS_GAIN_PARAM));
  169. addParam(createParamCentered<Rogan1PSRed>(mm2px(Vec(41.582, 43.473)), module, Shelves::P1_FREQ_PARAM));
  170. addParam(createParamCentered<Rogan1PSRed>(mm2px(Vec(65.699, 43.473)), module, Shelves::P1_GAIN_PARAM));
  171. addParam(createParamCentered<Trimpot>(mm2px(Vec(20.632, 48.111)), module, Shelves::P1_Q_PARAM));
  172. addParam(createParamCentered<Rogan1PSGreen>(mm2px(Vec(41.582, 67.286)), module, Shelves::P2_FREQ_PARAM));
  173. addParam(createParamCentered<Rogan1PSGreen>(mm2px(Vec(65.699, 67.286)), module, Shelves::P2_GAIN_PARAM));
  174. addParam(createParamCentered<Trimpot>(mm2px(Vec(20.632, 63.447)), module, Shelves::P2_Q_PARAM));
  175. addParam(createParamCentered<Rogan1PSWhite>(mm2px(Vec(41.582, 91.099)), module, Shelves::LS_FREQ_PARAM));
  176. addParam(createParamCentered<Rogan1PSWhite>(mm2px(Vec(65.699, 91.099)), module, Shelves::LS_GAIN_PARAM));
  177. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 17.329)), module, Shelves::HS_FREQ_INPUT));
  178. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(20.619, 17.329)), module, Shelves::HS_GAIN_INPUT));
  179. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 33.824)), module, Shelves::P1_FREQ_INPUT));
  180. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(20.619, 33.824)), module, Shelves::P1_GAIN_INPUT));
  181. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 48.111)), module, Shelves::P1_Q_INPUT));
  182. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 77.733)), module, Shelves::P2_FREQ_INPUT));
  183. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(20.619, 77.733)), module, Shelves::P2_GAIN_INPUT));
  184. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 63.447)), module, Shelves::P2_Q_INPUT));
  185. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 94.228)), module, Shelves::LS_FREQ_INPUT));
  186. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(20.619, 94.228)), module, Shelves::LS_GAIN_INPUT));
  187. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.983, 109.475)), module, Shelves::FREQ_INPUT));
  188. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(20.619, 109.475)), module, Shelves::GAIN_INPUT));
  189. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(41.565, 109.475)), module, Shelves::IN_INPUT));
  190. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.418, 17.329)), module, Shelves::P1_HP_OUTPUT));
  191. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.418, 32.725)), module, Shelves::P1_BP_OUTPUT));
  192. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.431, 48.111)), module, Shelves::P1_LP_OUTPUT));
  193. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.431, 63.447)), module, Shelves::P2_HP_OUTPUT));
  194. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.418, 78.832)), module, Shelves::P2_BP_OUTPUT));
  195. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(84.418, 94.228)), module, Shelves::P2_LP_OUTPUT));
  196. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(65.682, 109.475)), module, Shelves::OUT_OUTPUT));
  197. addChild(createLightCentered<MediumLight<RedLight>>(mm2px(Vec(53.629, 109.475)), module, Shelves::CLIP_LIGHT));
  198. }
  199. void appendContextMenu(Menu* menu) override {
  200. Shelves* module = dynamic_cast<Shelves*>(this->module);
  201. menu->addChild(new MenuSeparator);
  202. struct PreGainItem : MenuItem {
  203. Shelves* module;
  204. void onAction(const event::Action& e) override {
  205. module->preGain ^= true;
  206. }
  207. };
  208. PreGainItem* preGainItem = createMenuItem<PreGainItem>("Pad input by -6dB", CHECKMARK(module->preGain));
  209. preGainItem->module = module;
  210. menu->addChild(preGainItem);
  211. }
  212. };
  213. Model* modelShelves = createModel<Shelves, ShelvesWidget>("Shelves");