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.

168 lines
2.8KB

  1. #pragma once
  2. #include "common.hpp"
  3. namespace rack_plugin_TheXOR {
  4. struct UPSWITCH : SVGSwitch, MomentarySwitch
  5. {
  6. UPSWITCH()
  7. {
  8. addFrame(SVG::load(assetPlugin(plugin, "res/upswitch_0.svg")));
  9. addFrame(SVG::load(assetPlugin(plugin, "res/upswitch_1.svg")));
  10. }
  11. };
  12. struct DNSWITCH : SVGSwitch, MomentarySwitch
  13. {
  14. DNSWITCH()
  15. {
  16. addFrame(SVG::load(assetPlugin(plugin, "res/dnswitch_0.svg")));
  17. addFrame(SVG::load(assetPlugin(plugin, "res/dnswitch_1.svg")));
  18. }
  19. };
  20. struct Rogan1PSRedSmall : Rogan
  21. {
  22. Rogan1PSRedSmall()
  23. {
  24. setSVG(SVG::load(assetPlugin(plugin, "res/Rogan2PSRedSmall.svg")));
  25. }
  26. };
  27. #define OUT_SOCKETS (21)
  28. struct PwmClock;
  29. struct PwmClockWidget : SequencerWidget
  30. {
  31. PwmClockWidget(PwmClock *module);
  32. void SetBpm(float bpmint);
  33. };
  34. struct SA_TIMER //sample accurate version
  35. {
  36. float Reset()
  37. {
  38. prevTime = curTime = engineGetSampleTime();
  39. return Begin();
  40. }
  41. void RestartStopWatch() { stopwatch = 0; }
  42. float Begin()
  43. {
  44. RestartStopWatch();
  45. return totalPulseTime = 0;
  46. }
  47. float Elapsed() { return totalPulseTime; }
  48. float StopWatch() { return stopwatch; }
  49. float Step()
  50. {
  51. curTime += engineGetSampleTime();
  52. float deltaTime = curTime - prevTime;
  53. prevTime = curTime;
  54. totalPulseTime += deltaTime;
  55. stopwatch += deltaTime;
  56. return deltaTime;
  57. }
  58. private:
  59. float curTime;
  60. float prevTime;
  61. float totalPulseTime;
  62. float stopwatch;
  63. };
  64. struct PwmClock : Module
  65. {
  66. enum ParamIds
  67. {
  68. BPM_INC, BPM_DEC,
  69. PWM, BPM, BPMDEC,
  70. SWING,
  71. OFFON,
  72. NUM_PARAMS
  73. };
  74. enum InputIds
  75. {
  76. RESET,
  77. EXT_BPM,
  78. PWM_IN,
  79. SWING_IN,
  80. OFFON_IN,
  81. NUM_INPUTS
  82. };
  83. enum OutputIds
  84. {
  85. OUT_1,
  86. NUM_OUTPUTS = OUT_1 + OUT_SOCKETS
  87. };
  88. enum LightIds
  89. {
  90. ACTIVE,
  91. NUM_LIGHTS
  92. };
  93. PwmClock() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS)
  94. {
  95. on_loaded();
  96. }
  97. void step() override;
  98. json_t *toJson() override
  99. {
  100. json_t *rootJ = json_object();
  101. json_t *bpmJson = json_integer((int)bpm_integer);
  102. json_object_set_new(rootJ, "bpm_integer", bpmJson);
  103. return rootJ;
  104. }
  105. void fromJson(json_t *rootJ) override
  106. {
  107. json_t *bpmJson = json_object_get(rootJ, "bpm_integer");
  108. if(bpmJson)
  109. bpm_integer = json_integer_value(bpmJson);
  110. on_loaded();
  111. }
  112. void reset() override
  113. {
  114. bpm_integer = 120;
  115. load();
  116. }
  117. void randomize() override {}
  118. void setWidget(PwmClockWidget *pwdg) { pWidget = pwdg; }
  119. float bpm;
  120. float swing;
  121. private:
  122. SchmittTrigger btnup;
  123. SchmittTrigger btndwn;
  124. PwmClockWidget *pWidget;
  125. uint32_t tick = UINT32_MAX;
  126. int bpm_integer = 120;
  127. SchmittTrigger2 resetTrigger;
  128. void process_keys();
  129. void updateBpm();
  130. float getDuration(int n)
  131. {
  132. return odd_beat[n] ? swingAmt[n] : duration[n];
  133. }
  134. float duration[OUT_SOCKETS];
  135. float swingAmt[OUT_SOCKETS];
  136. bool odd_beat[OUT_SOCKETS];
  137. void on_loaded();
  138. void load();
  139. void _reset();
  140. float getPwm();
  141. float getSwing();
  142. SA_TIMER sa_timer[OUT_SOCKETS];
  143. };
  144. } // namespace rack_plugin_TheXOR