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.

178 lines
6.1KB

  1. #include "BaconPlugs.hpp"
  2. #include "ChipSym.hpp"
  3. namespace rack_plugin_BaconMusic {
  4. struct ChipNoise : virtual Module {
  5. enum ParamIds {
  6. NOISE_LENGTH,
  7. LONG_MODE,
  8. SHORT_LEN,
  9. PERIOD_93,
  10. NUM_PARAMS
  11. };
  12. enum InputIds {
  13. NOISE_LENGTH_INPUT,
  14. NUM_INPUTS
  15. };
  16. enum OutputIds {
  17. NOISE_OUTPUT,
  18. NUM_OUTPUTS
  19. };
  20. enum LightIds {
  21. NOISE_FROM_INPUT,
  22. NOISE_FROM_KNOB,
  23. NOISE_LENGTH_LIGHT,
  24. PERIOD_93_LIGHT,
  25. USING_93,
  26. NUM_LIGHTS
  27. };
  28. ChipSym::NESNoise noise;
  29. int prior_shortlen;
  30. bool prior_longmode;
  31. ChipNoise() : Module( NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS ),
  32. noise( -5.0, 5.0, engineGetSampleRate() )
  33. {
  34. params[ LONG_MODE ].value = 1;
  35. params[ NOISE_LENGTH ].value = 9;
  36. params[ SHORT_LEN ].value = 1;
  37. params[ PERIOD_93 ].value = 1;
  38. prior_shortlen = 1;
  39. prior_longmode = false;
  40. }
  41. void step() override
  42. {
  43. lights[ NOISE_FROM_KNOB ].value = !inputs[ NOISE_LENGTH_INPUT ].active;
  44. lights[ NOISE_FROM_INPUT ].value = inputs[ NOISE_LENGTH_INPUT ].active;
  45. unsigned int nl = (unsigned int)clamp( params[ NOISE_LENGTH ].value, 0.0f, 15.0f );
  46. if( inputs[ NOISE_LENGTH_INPUT ].active )
  47. nl = (unsigned int)clamp( inputs[ NOISE_LENGTH_INPUT ].value * 1.5, 0.0f, 15.0f );
  48. lights[ NOISE_LENGTH_LIGHT ].value = nl;
  49. noise.setPeriod(nl);
  50. int p93 = (int)params[ PERIOD_93 ].value;
  51. lights[ PERIOD_93_LIGHT ].value = p93;
  52. if( params[ LONG_MODE ].value == 0 && params[ SHORT_LEN ].value == 1 )
  53. {
  54. noise.set93Key( p93 );
  55. lights[ USING_93 ].value = 1;
  56. }
  57. else
  58. {
  59. lights[ USING_93 ].value = 0;
  60. }
  61. bool tmpNoiseFlag = ( params[ LONG_MODE ].value == 0 );
  62. if ( tmpNoiseFlag != prior_longmode )
  63. {
  64. prior_longmode = tmpNoiseFlag;
  65. noise.setModeFlag( prior_longmode );
  66. }
  67. if( params[ SHORT_LEN ].value != prior_shortlen )
  68. {
  69. prior_shortlen = params[SHORT_LEN].value;
  70. if( prior_shortlen == 1 )
  71. {
  72. noise.setShortLength( ChipSym::NESNoise::SHORT_93 );
  73. }
  74. else
  75. {
  76. noise.setShortLength( ChipSym::NESNoise::SHORT_31 );
  77. }
  78. }
  79. outputs[ NOISE_OUTPUT ].value = noise.step();
  80. }
  81. };
  82. struct ChipNoiseWidget : ModuleWidget {
  83. ChipNoiseWidget( ChipNoise *module);
  84. };
  85. ChipNoiseWidget::ChipNoiseWidget( ChipNoise *module ) : ModuleWidget( module )
  86. {
  87. box.size = Vec( SCREW_WIDTH * 6, RACK_HEIGHT );
  88. BaconBackground *bg = new BaconBackground( box.size, "ChipNoise" );
  89. addChild( bg->wrappedInFramebuffer());
  90. // Control the noise length
  91. bg->addRoundedBorder( Vec( 8, 45 ), Vec( SCREW_WIDTH * 6 - 16, 75 ) );
  92. bg->addLabel( Vec( bg->cx() + 7, 55 ), "wave", 11, NVG_ALIGN_LEFT | NVG_ALIGN_TOP );
  93. bg->addLabel( Vec( bg->cx() + 5, 66 ), "length", 11, NVG_ALIGN_LEFT | NVG_ALIGN_TOP );
  94. Vec inP = Vec( 16, 53 );
  95. addInput( Port::create< PJ301MPort >( inP,
  96. Port::INPUT,
  97. module,
  98. ChipNoise::NOISE_LENGTH_INPUT ) );
  99. addChild( ModuleLightWidget::create< SmallLight< BlueLight > >( inP.minus( Vec( 4, 4 ) ), module, ChipNoise::NOISE_FROM_INPUT ) );
  100. int ybot = 120;
  101. addParam( ParamWidget::create< RoundSmallBlackKnob >( Vec( 16, ybot - 3 - 28 ),
  102. module,
  103. ChipNoise::NOISE_LENGTH,
  104. 0, 15, 7 ) );
  105. addChild( ModuleLightWidget::create< SmallLight< BlueLight > >( Vec( 16-4, ybot - 3 - 28 -4 ), module, ChipNoise::NOISE_FROM_KNOB ) );
  106. addChild( MultiDigitSevenSegmentLight< BlueLight, 2, 2 >::create( Vec( 47, ybot - 5 - 24 ),
  107. module,
  108. ChipNoise::NOISE_LENGTH_LIGHT ) );
  109. bg->addRoundedBorder( Vec( 8, 135 ), Vec( SCREW_WIDTH * 6 - 16, 160 ) );
  110. bg->addLabel( Vec( bg->cx(), 155 ), "Sequence", 13, NVG_ALIGN_CENTER | NVG_ALIGN_BOTTOM );
  111. addParam( ParamWidget::create< NKK >( Vec( bg->cx() - 32, 175 ), module, ChipNoise::LONG_MODE, 0, 1, 1 ) );
  112. addParam( ParamWidget::create< NKK >( Vec( bg->cx() + 2, 175 ), module, ChipNoise::SHORT_LEN, 0, 1, 1 ) );
  113. bg->addLabel( Vec( bg->cx() + 16 - 32, 160 ), "long", 11, NVG_ALIGN_CENTER | NVG_ALIGN_TOP );
  114. bg->addLabel( Vec( bg->cx() + 16 - 32, 223 ), "short", 11, NVG_ALIGN_CENTER| NVG_ALIGN_TOP );
  115. bg->addLabel( Vec( bg->cx() + 16 + 2, 160 ), "93", 11, NVG_ALIGN_CENTER | NVG_ALIGN_TOP );
  116. bg->addLabel( Vec( bg->cx() + 16 + 2, 223 ), "31", 11, NVG_ALIGN_CENTER| NVG_ALIGN_TOP );
  117. bg->addLabel( Vec( bg->cx(), 258 ), "Which 93 seq", 11, NVG_ALIGN_CENTER | NVG_ALIGN_BOTTOM );
  118. addChild( MultiDigitSevenSegmentLight< BlueLight, 2, 3 >::create( Vec( 50 - 14, 262 ),
  119. module,
  120. ChipNoise::PERIOD_93_LIGHT ) );
  121. addParam( ParamWidget::create< RoundSmallBlackKnob >( Vec( 11, 262 ),
  122. module,
  123. ChipNoise::PERIOD_93,
  124. 0, 351, 17 ) );
  125. addChild( ModuleLightWidget::create< SmallLight< BlueLight > >( Vec( 12, 249 ), module, ChipNoise::USING_93 ) );
  126. // Output port
  127. Vec outP = Vec( bg->cx( 24 ), RACK_HEIGHT - 15 - 43 );
  128. bg->addPlugLabel( outP, BaconBackground::SIG_OUT, "out" );
  129. addOutput( Port::create< PJ301MPort >( outP,
  130. Port::OUTPUT,
  131. module,
  132. ChipNoise::NOISE_OUTPUT ) );
  133. }
  134. } // namespace rack_plugin_BaconMusic
  135. using namespace rack_plugin_BaconMusic;
  136. RACK_PLUGIN_MODEL_INIT(BaconMusic, ChipNoise) {
  137. Model *modelChipNoise = Model::create<ChipNoise, ChipNoiseWidget>("Bacon Music", "ChipNoise", "ChipNoise", NOISE_TAG );
  138. return modelChipNoise;
  139. }