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.

109 lines
3.6KB

  1. //**************************************************************************************
  2. //SawOsc module for VCV Rack by Alfredo Santamaria - AS - https://github.com/AScustomWorks/AS
  3. //
  4. //Code taken from RODENTCAT https://github.com/RODENTCAT/RODENTMODULES
  5. //Code taken from the Fundamentals plugins by Andrew Belt http://www.vcvrack.com
  6. //**************************************************************************************
  7. #include "AS.hpp"
  8. struct SawOsc : Module {
  9. enum ParamIds {
  10. PITCH_PARAM,
  11. PW_PARAM,
  12. NUM_PARAMS
  13. };
  14. enum InputIds {
  15. PITCH_INPUT,
  16. PW_INPUT,
  17. NUM_INPUTS
  18. };
  19. enum OutputIds {
  20. OSC_OUTPUT,
  21. NUM_OUTPUTS
  22. };
  23. enum LightIds {
  24. FREQ_LIGHT,
  25. NUM_LIGHTS
  26. };
  27. float phase = 0.0f;
  28. float blinkPhase = 0.0f;
  29. SawOsc() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  30. void step() override;
  31. };
  32. void SawOsc::step() {
  33. // Implement a simple sine oscillator
  34. float deltaTime = 1.0f / engineGetSampleRate();
  35. // Compute the frequency from the pitch parameter and input
  36. float pitch = params[PITCH_PARAM].value;
  37. pitch += inputs[PITCH_INPUT].value;
  38. pitch = clamp(pitch, -4.0f, 4.0f);
  39. float freq = 440.0f * powf(2.0f, pitch);
  40. // Accumulate the phase
  41. phase += freq * deltaTime;
  42. if (phase >= 1.0f)
  43. phase -= 1.0f;
  44. //Mod param
  45. float pw = params[PW_PARAM].value*0.1f+1.0f;
  46. //Mod input
  47. float minput = inputs[PW_INPUT].value*0.3f;
  48. //Mod param+input
  49. float pinput = (pw + minput);
  50. // Compute the sine output
  51. //float sine = sinf(2 * M_PI * phase);
  52. //outputs[SINE_OUTPUT].value = 5.0 * sine;
  53. //saw stuff, original dev says square, but it sounds more like a SAW wave, hence this module name hehe
  54. float saw = cos(exp(pinput * M_PI * phase));///0.87;
  55. //dc block
  56. float block_coeff = 1.0f - (2.0f * M_PI * (10.0f / 44100.0f));
  57. float m_prev_in = 0.0f;
  58. float m_prev_out = 0.0f;
  59. m_prev_out = saw - m_prev_in + block_coeff * m_prev_out;
  60. m_prev_in = saw;
  61. //outputs[OSC_OUTPUT].value = 5 * saw;
  62. outputs[OSC_OUTPUT].value = m_prev_out*5;
  63. lights[FREQ_LIGHT].value = (outputs[OSC_OUTPUT].value > 0.0f) ? 1.0f : 0.0f;
  64. }
  65. struct SawOscWidget : ModuleWidget
  66. {
  67. SawOscWidget(SawOsc *module);
  68. };
  69. SawOscWidget::SawOscWidget(SawOsc *module) : ModuleWidget(module) {
  70. setPanel(SVG::load(assetPlugin(plugin, "res/SawOSC.svg")));
  71. //SCREWS - SPECIAL SPACING FOR RACK WIDTH*4
  72. addChild(Widget::create<as_HexScrew>(Vec(0, 0)));
  73. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, 0)));
  74. addChild(Widget::create<as_HexScrew>(Vec(0, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  75. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  76. //LIGHT
  77. addChild(ModuleLightWidget::create<SmallLight<RedLight>>(Vec(22-15, 57), module, SawOsc::FREQ_LIGHT));
  78. //PARAMS
  79. //addParam(ParamWidget::create<as_KnobBlack>(Vec(26, 60), module, SawOsc::PITCH_PARAM, -3.0, 3.0, 0.0));
  80. addParam(ParamWidget::create<as_KnobBlack>(Vec(26-15, 60), module, SawOsc::PITCH_PARAM, -4.0, 4.0, 0.0));
  81. //addParam(ParamWidget::create<as_KnobBlack>(Vec(26, 125), module, SawOsc::PW_PARAM, -4.0, 5.0, -4.0));
  82. addParam(ParamWidget::create<as_KnobBlack>(Vec(26-15, 125), module, SawOsc::PW_PARAM, -4.2, 5.0, -4.2));
  83. //INPUTS
  84. addInput(Port::create<as_PJ301MPort>(Vec(33-15, 200), Port::INPUT, module, SawOsc::PW_INPUT));
  85. addInput(Port::create<as_PJ301MPort>(Vec(33-15, 260), Port::INPUT, module, SawOsc::PITCH_INPUT));
  86. //OUTPUTS
  87. addOutput(Port::create<as_PJ301MPort>(Vec(33-15, 310), Port::OUTPUT, module, SawOsc::OSC_OUTPUT));
  88. }
  89. RACK_PLUGIN_MODEL_INIT(AS, SawOsc) {
  90. Model *modelSawOsc = Model::create<SawOsc, SawOscWidget>("AS", "SawOSC", "TinySawish", OSCILLATOR_TAG);
  91. return modelSawOsc;
  92. }