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.

106 lines
3.3KB

  1. //**************************************************************************************
  2. //SineOSC module for VCV Rack by Alfredo Santamaria - AS - https://github.com/AScustomWorks/AS
  3. //Is just the tutorial module and nothing else hehe
  4. //
  5. //Code taken from the Fundamentals plugins by Andrew Belt http://www.vcvrack.com
  6. //**************************************************************************************
  7. #include "AS.hpp"
  8. struct SineOsc : Module {
  9. enum ParamIds {
  10. FREQ_PARAM,
  11. BASE_PARAM,
  12. NUM_PARAMS
  13. };
  14. enum InputIds {
  15. FREQ_CV,
  16. NUM_INPUTS
  17. };
  18. enum OutputIds {
  19. OSC_OUTPUT,
  20. TRI_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. float freq = 0.0f;
  30. int base_freq = 0;
  31. SineOsc() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  32. void step() override;
  33. };
  34. void SineOsc::step() {
  35. // Implement a simple sine oscillator
  36. // Compute the frequency from the pitch parameter and input
  37. base_freq = params[BASE_PARAM].value;
  38. float pitch = params[FREQ_PARAM].value;
  39. pitch += inputs[FREQ_CV].value;
  40. pitch = clamp(pitch, -4.0f, 4.0f);
  41. if(base_freq==1){
  42. //Note A4
  43. freq = 440.0f * powf(2.0f, pitch);
  44. }else{
  45. // Note C4
  46. freq = 261.626f * powf(2.0f, pitch);
  47. }
  48. // Accumulate the phase
  49. phase += freq / engineGetSampleRate();
  50. if (phase >= 1.0f)
  51. phase -= 1.0f;
  52. // Compute the sine output
  53. //correct sine
  54. float sine = sinf(2.0f * M_PI * (phase+1 * 0.125f)) * 5.0f;
  55. //original sine
  56. //float sine = sinf(2 * M_PI * phase)+ sinf(2 * M_PI * phase * 2)*5;
  57. //mod,like this it gives a unipolar saw-ish wave
  58. //float sine = sinf(2.0 * M_PI * (phase * 0.125)) * 5.0;
  59. outputs[OSC_OUTPUT].value = sine;
  60. lights[FREQ_LIGHT].value = (outputs[OSC_OUTPUT].value > 0.0f) ? 1.0f : 0.0f;
  61. }
  62. struct SineOscWidget : ModuleWidget
  63. {
  64. SineOscWidget(SineOsc *module);
  65. };
  66. SineOscWidget::SineOscWidget(SineOsc *module) : ModuleWidget(module) {
  67. setPanel(SVG::load(assetPlugin(plugin, "res/SineOSC.svg")));
  68. //SCREWS - SPECIAL SPACING FOR RACK WIDTH*4
  69. addChild(Widget::create<as_HexScrew>(Vec(0, 0)));
  70. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, 0)));
  71. addChild(Widget::create<as_HexScrew>(Vec(0, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  72. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  73. //LIGHT
  74. addChild(ModuleLightWidget::create<SmallLight<RedLight>>(Vec(22-15, 57), module, SineOsc::FREQ_LIGHT));
  75. //PARAMS
  76. //addParam(ParamWidget::create<as_KnobBlack>(Vec(26-15, 60), module, SineOsc::FREQ_PARAM, -3.75f, 3.75f, -0.75f));
  77. //addParam(ParamWidget::create<as_KnobBlack>(Vec(26-15, 60), module, SineOsc::FREQ_PARAM, -3.0f, 2.999934f, -0.000066f));
  78. addParam(ParamWidget::create<as_KnobBlack>(Vec(26-15, 60), module, SineOsc::FREQ_PARAM, -3.0f, 3.0f, 0.0f));
  79. //BASE FREQ SWITCH
  80. addParam(ParamWidget::create<as_CKSSH>(Vec(18, 220), module, SineOsc::BASE_PARAM, 0.0f, 1.0f, 1.0f));
  81. //INPUTS
  82. addInput(Port::create<as_PJ301MPort>(Vec(33-15, 260), Port::INPUT, module, SineOsc::FREQ_CV));
  83. //OUTPUTS
  84. addOutput(Port::create<as_PJ301MPort>(Vec(33-15, 310), Port::OUTPUT, module, SineOsc::OSC_OUTPUT));
  85. }
  86. RACK_PLUGIN_MODEL_INIT(AS, SineOsc) {
  87. Model *modelSineOsc = Model::create<SineOsc, SineOscWidget>("AS", "SineOSC", "TinySine", OSCILLATOR_TAG);
  88. return modelSineOsc;
  89. }