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.

242 lines
7.0KB

  1. //**************************************************************************************
  2. //CV to Trigger convenrter module for VCV Rack by Alfredo Santamaria - AS - https://github.com/AScustomWorks/AS
  3. //
  4. //
  5. //**************************************************************************************
  6. #include "AS.hpp"
  7. #include "dsp/digital.hpp"
  8. struct Cv2T : Module {
  9. enum ParamIds {
  10. TRIG_SWITCH_1,
  11. TRIG_SWITCH_2,
  12. TRIG_SWITCH_3,
  13. TRIG_SWITCH_4,
  14. NUM_PARAMS
  15. };
  16. enum InputIds {
  17. CV_IN_1,
  18. CV_IN_2,
  19. CV_IN_3,
  20. CV_IN_4,
  21. NUM_INPUTS
  22. };
  23. enum OutputIds {
  24. TRIG_OUT_1,
  25. TRIG_OUT_2,
  26. TRIG_OUT_3,
  27. TRIG_OUT_4,
  28. NUM_OUTPUTS
  29. };
  30. enum LightIds {
  31. TRIG_LED_1,
  32. TRIG_LED_2,
  33. TRIG_LED_3,
  34. TRIG_LED_4,
  35. NUM_LIGHTS
  36. };
  37. SchmittTrigger trig_1, trig_2, trig_3, trig_4;
  38. PulseGenerator trigPulse1, trigPulse2, trigPulse3, trigPulse4;
  39. bool trig_pulse_1 = false;
  40. bool trig_pulse_2 = false;
  41. bool trig_pulse_3 = false;
  42. bool trig_pulse_4 = false;
  43. float trigger_length = 0.0001f;
  44. const float lightLambda = 0.075f;
  45. float trigLight1 = 0.0f;
  46. float trigLight2 = 0.0f;
  47. float trigLight3 = 0.0f;
  48. float trigLight4 = 0.0f;
  49. bool cv_1_engaged = false;
  50. bool cv_2_engaged = false;
  51. bool cv_3_engaged = false;
  52. bool cv_4_engaged = false;
  53. float current_cv_1_volts = 0.0f;
  54. float current_cv_2_volts = 0.0f;
  55. float current_cv_3_volts = 0.0f;
  56. float current_cv_4_volts = 0.0f;
  57. Cv2T() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  58. void step() override;
  59. };
  60. void Cv2T::step() {
  61. //CV 2 TRIG 1
  62. if ( trig_1.process( params[TRIG_SWITCH_1].value ) ) {
  63. trigLight1 = 1.0;
  64. trigPulse1.trigger( trigger_length );
  65. }
  66. current_cv_1_volts = inputs[CV_IN_1].value;
  67. if ( !cv_1_engaged ) {
  68. if ( current_cv_1_volts > 0.0f ) {
  69. cv_1_engaged = true;
  70. trigLight1 = 1.0;
  71. trigPulse1.trigger( trigger_length );
  72. // send start trigger
  73. }
  74. } else {
  75. if ( (int)current_cv_1_volts <= 0 ) {
  76. // send stop trigger
  77. trigLight1 = 1.0;
  78. trigPulse1.trigger( trigger_length );
  79. cv_1_engaged = false;
  80. }
  81. }
  82. trigLight1 -= trigLight1 / lightLambda / engineGetSampleRate();
  83. lights[TRIG_LED_1].value = trigLight1;
  84. trig_pulse_1 = trigPulse1.process( 1.0 / engineGetSampleRate() );
  85. outputs[TRIG_OUT_1].value = ( trig_pulse_1 ? 10.0f : 0.0f );
  86. //CV 2 TRIG 2
  87. if ( trig_2.process( params[TRIG_SWITCH_2].value ) ) {
  88. trigLight2 = 1.0;
  89. trigPulse2.trigger( trigger_length );
  90. }
  91. current_cv_2_volts = inputs[CV_IN_2].value;
  92. if ( !cv_2_engaged ) {
  93. if ( current_cv_2_volts > 0.0f ) {
  94. cv_2_engaged = true;
  95. trigLight2 = 1.0;
  96. trigPulse2.trigger( trigger_length );
  97. // send start trigger
  98. }
  99. } else {
  100. if ( (int)current_cv_2_volts <= 0 ) {
  101. // send stop trigger
  102. trigLight2 = 1.0;
  103. trigPulse2.trigger( trigger_length );
  104. cv_2_engaged = false;
  105. }
  106. }
  107. trigLight2 -= trigLight2 / lightLambda / engineGetSampleRate();
  108. lights[TRIG_LED_2].value = trigLight2;
  109. trig_pulse_2 = trigPulse2.process( 1.0 / engineGetSampleRate() );
  110. outputs[TRIG_OUT_2].value = ( trig_pulse_2 ? 10.0f : 0.0f );
  111. //CV 2 TRIG 3
  112. if ( trig_3.process( params[TRIG_SWITCH_3].value ) ) {
  113. trigLight3 = 1.0;
  114. trigPulse3.trigger( trigger_length );
  115. }
  116. current_cv_3_volts = inputs[CV_IN_3].value;
  117. if ( !cv_3_engaged ) {
  118. if ( current_cv_3_volts > 0.0f ) {
  119. cv_3_engaged = true;
  120. trigLight3 = 1.0;
  121. trigPulse3.trigger( trigger_length );
  122. // send start trigger
  123. }
  124. } else {
  125. if ( (int)current_cv_3_volts <= 0 ) {
  126. // send stop trigger
  127. trigLight3 = 1.0;
  128. trigPulse3.trigger( trigger_length );
  129. cv_3_engaged = false;
  130. }
  131. }
  132. trigLight3 -= trigLight3 / lightLambda / engineGetSampleRate();
  133. lights[TRIG_LED_3].value = trigLight3;
  134. trig_pulse_3 = trigPulse3.process( 1.0 / engineGetSampleRate() );
  135. outputs[TRIG_OUT_3].value = ( trig_pulse_3 ? 10.0f : 0.0f );
  136. //CV 2 TRIG 4
  137. if ( trig_4.process( params[TRIG_SWITCH_4].value ) ) {
  138. trigLight4 = 1.0;
  139. trigPulse4.trigger( trigger_length );
  140. }
  141. current_cv_4_volts = inputs[CV_IN_4].value;
  142. if ( !cv_4_engaged ) {
  143. if ( current_cv_4_volts > 0.0f ) {
  144. cv_4_engaged = true;
  145. trigLight4 = 1.0;
  146. trigPulse4.trigger( trigger_length );
  147. // send start trigger
  148. }
  149. } else {
  150. if ( (int)current_cv_4_volts <= 0 ) {
  151. // send stop trigger
  152. trigLight4 = 1.0;
  153. trigPulse4.trigger( trigger_length );
  154. cv_4_engaged = false;
  155. }
  156. }
  157. trigLight4 -= trigLight4 / lightLambda / engineGetSampleRate();
  158. lights[TRIG_LED_4].value = trigLight4;
  159. trig_pulse_4 = trigPulse4.process( 1.0 / engineGetSampleRate() );
  160. outputs[TRIG_OUT_4].value = ( trig_pulse_4 ? 10.0f : 0.0f );
  161. }
  162. struct Cv2TWidget : ModuleWidget
  163. {
  164. Cv2TWidget(Cv2T *module);
  165. };
  166. Cv2TWidget::Cv2TWidget(Cv2T *module) : ModuleWidget(module) {
  167. setPanel(SVG::load(assetPlugin(plugin, "res/CV2T.svg")));
  168. //SCREWS - SPECIAL SPACING FOR RACK WIDTH*4
  169. addChild(Widget::create<as_HexScrew>(Vec(0, 0)));
  170. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, 0)));
  171. addChild(Widget::create<as_HexScrew>(Vec(0, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  172. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  173. const int gp_offset = 75;
  174. //CV 2 TRIG 1
  175. //SWITCH & LED
  176. addParam(ParamWidget::create<LEDBezel>(Vec(6, 101), module, Cv2T::TRIG_SWITCH_1 , 0.0f, 1.0f, 0.0f));
  177. addChild(ModuleLightWidget::create<LedLight<RedLight>>(Vec(6+2.2, 103.2), module, Cv2T::TRIG_LED_1));
  178. //INPUTS
  179. addInput(Port::create<as_PJ301MPort>(Vec(18,60), Port::INPUT, module, Cv2T::CV_IN_1));
  180. //OUTPUTS
  181. addOutput(Port::create<as_PJ301MPort>(Vec(32, 100), Port::OUTPUT, module, Cv2T::TRIG_OUT_1));
  182. //CV 2 TRIG 2
  183. //SWITCH & LED
  184. addParam(ParamWidget::create<LEDBezel>(Vec(6, 101+gp_offset*1), module, Cv2T::TRIG_SWITCH_2 , 0.0f, 1.0f, 0.0f));
  185. addChild(ModuleLightWidget::create<LedLight<RedLight>>(Vec(6+2.2, 103.2+gp_offset*1), module, Cv2T::TRIG_LED_2));
  186. //INPUTS
  187. addInput(Port::create<as_PJ301MPort>(Vec(18,60+gp_offset*1), Port::INPUT, module, Cv2T::CV_IN_2));
  188. //OUTPUTS
  189. addOutput(Port::create<as_PJ301MPort>(Vec(32, 100+gp_offset*1), Port::OUTPUT, module, Cv2T::TRIG_OUT_2));
  190. //CV 2 TRIG 3
  191. //SWITCH & LED
  192. addParam(ParamWidget::create<LEDBezel>(Vec(6, 101+gp_offset*2), module, Cv2T::TRIG_SWITCH_3 , 0.0f, 1.0f, 0.0f));
  193. addChild(ModuleLightWidget::create<LedLight<RedLight>>(Vec(6+2.2, 103.2+gp_offset*2), module, Cv2T::TRIG_LED_3));
  194. //INPUTS
  195. addInput(Port::create<as_PJ301MPort>(Vec(18,60+gp_offset*2), Port::INPUT, module, Cv2T::CV_IN_3));
  196. //OUTPUTS
  197. addOutput(Port::create<as_PJ301MPort>(Vec(32, 100+gp_offset*2), Port::OUTPUT, module, Cv2T::TRIG_OUT_3));
  198. //CV 2 TRIG 4
  199. //SWITCH & LED
  200. addParam(ParamWidget::create<LEDBezel>(Vec(6, 101+gp_offset*3), module, Cv2T::TRIG_SWITCH_4 , 0.0f, 1.0f, 0.0f));
  201. addChild(ModuleLightWidget::create<LedLight<RedLight>>(Vec(6+2.2, 103.2+gp_offset*3), module, Cv2T::TRIG_LED_4));
  202. //INPUTS
  203. addInput(Port::create<as_PJ301MPort>(Vec(18,60+gp_offset*3), Port::INPUT, module, Cv2T::CV_IN_4));
  204. //OUTPUTS
  205. addOutput(Port::create<as_PJ301MPort>(Vec(32, 100+gp_offset*3), Port::OUTPUT, module, Cv2T::TRIG_OUT_4));
  206. }
  207. RACK_PLUGIN_MODEL_INIT(AS, Cv2T) {
  208. Model *modelCv2T = Model::create<Cv2T, Cv2TWidget>("AS", "Cv2T", "CV to Trigger Switch", SWITCH_TAG);
  209. return modelCv2T;
  210. }