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.

495 lines
15KB

  1. #include "Test.hpp"
  2. void Test::onReset() {
  3. }
  4. void Test::step() {
  5. if (!(outputs[OUT_OUTPUT].active || outputs[OUT2_OUTPUT].active)) {
  6. return;
  7. }
  8. #ifdef LPF
  9. if (!inputs[IN_INPUT].active) {
  10. return;
  11. }
  12. _lpf.setParams(
  13. engineGetSampleRate(),
  14. 10000.0 * clamp(params[PARAM1_PARAM].value, 0.0f, 1.0f),
  15. std::max(10.0 * clamp(params[PARAM2_PARAM].value, 0.0f, 1.0f), 0.1)
  16. );
  17. outputs[OUT_OUTPUT].value = _lpf.next(inputs[IN_INPUT].value);
  18. #elif LPFNOISE
  19. _lpf.setParams(
  20. engineGetSampleRate(),
  21. 22000.0 * clamp(params[PARAM1_PARAM].value, 0.0f, 1.0f),
  22. 0.717f
  23. );
  24. float noise = _noise.next();
  25. outputs[OUT_OUTPUT].value = _lpf.next(noise) * 10.0;;
  26. outputs[OUT2_OUTPUT].value = noise * 10.0;;
  27. #elif SINE
  28. _sine.setSampleRate(engineGetSampleRate());
  29. _sine.setFrequency(oscillatorPitch());
  30. outputs[OUT_OUTPUT].value = _sine.next() * 5.0f;
  31. _sine2.setSampleRate(engineGetSampleRate());
  32. _sine2.setFrequency(oscillatorPitch());
  33. outputs[OUT2_OUTPUT].value = _sine2.next() * 5.0f;
  34. #elif SQUARE
  35. _square.setSampleRate(engineGetSampleRate());
  36. _square.setFrequency(oscillatorPitch());
  37. float pw = params[PARAM2_PARAM].value;
  38. if (inputs[CV2_INPUT].active) {
  39. pw += clamp(inputs[CV2_INPUT].value, -5.0f, 5.0f) / 10.0f;
  40. }
  41. _square.setPulseWidth(pw);
  42. outputs[OUT_OUTPUT].value = _square.next() * 5.0f;
  43. _square2.setSampleRate(engineGetSampleRate());
  44. _square2.setFrequency(oscillatorPitch());
  45. _square2.setPulseWidth(pw);
  46. _square2.setQuality(params[PARAM3_PARAM].value * 200);
  47. outputs[OUT2_OUTPUT].value = _square2.next() * 5.0f;
  48. #elif SAW
  49. _saw.setSampleRate(engineGetSampleRate());
  50. _saw.setFrequency(oscillatorPitch());
  51. outputs[OUT_OUTPUT].value = _saw.next() * 5.0f;
  52. _saw2.setSampleRate(engineGetSampleRate());
  53. _saw2.setFrequency(oscillatorPitch());
  54. _saw2.setQuality(params[PARAM2_PARAM].value * 200);
  55. outputs[OUT2_OUTPUT].value = _saw2.next() * 5.0f;
  56. #elif SATSAW
  57. float saturation = params[PARAM2_PARAM].value * 10.0f;
  58. if (inputs[CV2_INPUT].active) {
  59. saturation *= clamp(inputs[CV2_INPUT].value / 10.0f, 0.0f, 1.0f);
  60. }
  61. _saw.setSampleRate(engineGetSampleRate());
  62. _saw.setFrequency(oscillatorPitch());
  63. _saw.setSaturation(saturation);
  64. outputs[OUT_OUTPUT].value = _saw.next() * 5.0f;
  65. _saw2.setSampleRate(engineGetSampleRate());
  66. _saw2.setFrequency(oscillatorPitch());
  67. _saw2.setSaturation(saturation);
  68. _saw2.setQuality(params[PARAM3_PARAM].value * 200);
  69. outputs[OUT2_OUTPUT].value = _saw2.next() * 5.0f;
  70. #elif TRIANGLE
  71. _triangle.setSampleRate(engineGetSampleRate());
  72. _triangle.setFrequency(oscillatorPitch());
  73. outputs[OUT_OUTPUT].value = _triangle.next() * 5.0f;
  74. #elif SAMPLED_TRIANGLE
  75. float sample = params[PARAM2_PARAM].value * Phasor::maxSampleWidth;
  76. if (inputs[CV2_INPUT].active) {
  77. sample *= clamp(inputs[CV2_INPUT].value / 10.0f, 0.0f, 1.0f);
  78. }
  79. _triangle.setSampleRate(engineGetSampleRate());
  80. _triangle.setFrequency(oscillatorPitch());
  81. _triangle.setSampleWidth(sample);
  82. outputs[OUT_OUTPUT].value = _triangle.next() * 5.0f;
  83. _triangle2.setSampleRate(engineGetSampleRate());
  84. _triangle2.setFrequency(oscillatorPitch());
  85. float maxSampleSteps = (_triangle2._sampleRate / _triangle2._frequency) / 4.0f;
  86. _sampleSteps = clamp((int)((4.0f * sample) * maxSampleSteps), 1, (int)maxSampleSteps);
  87. ++_sampleStep;
  88. if (_sampleStep >= _sampleSteps) {
  89. _sampleStep = 0;
  90. _sample = _triangle2.next() * 5.0f;
  91. }
  92. else {
  93. _triangle2.advancePhase();
  94. }
  95. outputs[OUT2_OUTPUT].value = _sample;
  96. #elif SINEBANK
  97. _sineBank.setSampleRate(engineGetSampleRate());
  98. _sineBank.setFrequency(oscillatorPitch());
  99. outputs[OUT_OUTPUT].value = _sineBank.next();
  100. #elif OVERSAMPLING
  101. _saw1.setSampleRate(engineGetSampleRate());
  102. _saw1.setFrequency(oscillatorPitch() / (float)OVERSAMPLEN);
  103. float buf[OVERSAMPLEN];
  104. for (int i = 0; i < OVERSAMPLEN; ++i) {
  105. buf[i] = _saw1.next();
  106. }
  107. outputs[OUT_OUTPUT].value = _rackDecimator.process(buf) * 5.0f;
  108. _saw2.setSampleRate(engineGetSampleRate());
  109. _saw2.setFrequency(oscillatorPitch() / (float)OVERSAMPLEN);
  110. _lpf.setParams(
  111. engineGetSampleRate(),
  112. engineGetSampleRate() / 4.0f,
  113. 0.03
  114. );
  115. _lpf2.setParams(
  116. engineGetSampleRate(),
  117. engineGetSampleRate() / 4.0f,
  118. 0.03
  119. );
  120. float s = 0.0f;
  121. for (int i = 0; i < OVERSAMPLEN; ++i) {
  122. // s = _lpf2.next(_lpf.next(_saw2.next()));
  123. s = _lpf.next(_saw2.next());
  124. // s = _saw2.next();
  125. }
  126. outputs[OUT2_OUTPUT].value = s * 5.0;
  127. #elif OVERSAMPLED_BL
  128. int quality = params[PARAM2_PARAM].value * 100;
  129. const int maxOversample = 16;
  130. int oversample = params[PARAM3_PARAM].value * maxOversample;
  131. _saw1.setSampleRate(engineGetSampleRate());
  132. _saw1.setFrequency(oscillatorPitch());
  133. _saw1.setQuality(quality);
  134. outputs[OUT_OUTPUT].value = _saw1.next() * 5.0f;
  135. _saw2.setSampleRate(engineGetSampleRate());
  136. _saw2.setQuality(quality);
  137. if (oversample < 2) {
  138. _saw2.setFrequency(oscillatorPitch());
  139. outputs[OUT2_OUTPUT].value = _saw2.next() * 5.0f;
  140. }
  141. else {
  142. _saw2.setFrequency(oscillatorPitch() / (float)oversample);
  143. _lpf.setParams(
  144. oversample * engineGetSampleRate(),
  145. 0.95f * engineGetSampleRate(),
  146. 0.03
  147. );
  148. float s = 0.0f;
  149. for (int i = 0; i < oversample; ++i) {
  150. s = _lpf.next(_saw2.next());
  151. }
  152. outputs[OUT2_OUTPUT].value = s * 5.0f;
  153. }
  154. #elif ANTIALIASING
  155. const int quality = 12;
  156. const float oversampleThreshold = 0.06f;
  157. const float oversampleMixWidth = 100.0f;
  158. float sampleRate = engineGetSampleRate();
  159. float frequency = oscillatorPitch(15000.0);
  160. float otf = oversampleThreshold * sampleRate;
  161. float mix, oMix;
  162. if (frequency > otf) {
  163. if (frequency > otf + oversampleMixWidth) {
  164. mix = 0.0f;
  165. oMix = 1.0f;
  166. }
  167. else {
  168. oMix = (frequency - otf) / oversampleMixWidth;
  169. mix = 1.0f - oMix;
  170. }
  171. }
  172. else {
  173. mix = 1.0f;
  174. oMix = 0.0f;
  175. }
  176. assert(mix >= 0.0f);
  177. assert(mix <= 1.0f);
  178. assert(oMix >= 0.0f);
  179. assert(oMix <= 1.0f);
  180. _phasor.setSampleRate(sampleRate);
  181. _phasor.setFrequency(frequency);
  182. _oversampledPhasor.setSampleRate(sampleRate);
  183. _oversampledPhasor.setFrequency(frequency / (float)OVERSAMPLEN);
  184. _saw.setSampleRate(sampleRate);
  185. _saw.setQuality(quality);
  186. _sawDecimator.setParams(sampleRate, OVERSAMPLEN);
  187. _square.setSampleRate(sampleRate);
  188. _square.setQuality(quality);
  189. _squareDecimator.setParams(sampleRate, OVERSAMPLEN);
  190. float out = 0.0f;
  191. float out2 = 0.0f;
  192. _phasor.advancePhase();
  193. if (mix > 0.0f) {
  194. _saw.setFrequency(frequency);
  195. _square.setFrequency(frequency);
  196. out += _saw.nextFromPhasor(_phasor) * mix;
  197. out2 += _square.nextFromPhasor(_phasor) * mix;
  198. }
  199. if (oMix > 0.0f) {
  200. float sawBuf[OVERSAMPLEN] {};
  201. float squareBuf[OVERSAMPLEN] {};
  202. _saw.setFrequency(frequency / (float)OVERSAMPLEN);
  203. _square.setFrequency(frequency / (float)OVERSAMPLEN);
  204. for (int i = 0; i < OVERSAMPLEN; ++i) {
  205. _oversampledPhasor.advancePhase();
  206. sawBuf[i] = _saw.nextFromPhasor(_oversampledPhasor);
  207. squareBuf[i] = _square.nextFromPhasor(_oversampledPhasor);
  208. }
  209. out += _sawDecimator.next(sawBuf) * oMix;
  210. // out += _sawRackDecimator.process(sawBuf) * oMix;
  211. out2 += _squareDecimator.next(squareBuf) * oMix;
  212. // out2 += _squareRackDecimator.process(squareBuf) * oMix;
  213. }
  214. else {
  215. for (int i = 0; i < OVERSAMPLEN; ++i) {
  216. _oversampledPhasor.advancePhase();
  217. }
  218. }
  219. outputs[OUT_OUTPUT].value = out * 5.0f;
  220. outputs[OUT2_OUTPUT].value = out2 * 5.0f;
  221. #elif DECIMATORS
  222. const int quality = 12;
  223. float sampleRate = engineGetSampleRate();
  224. float frequency = oscillatorPitch(15000.0);
  225. _saw.setSampleRate(sampleRate);
  226. _saw.setFrequency(frequency / (float)OVERSAMPLEN);
  227. _saw.setQuality(quality);
  228. _cicDecimator.setParams(sampleRate, OVERSAMPLEN);
  229. _lpfDecimator.setParams(sampleRate, OVERSAMPLEN);
  230. float buf[OVERSAMPLEN] {};
  231. for (int i = 0; i < OVERSAMPLEN; ++i) {
  232. buf[i] = _saw.next();
  233. }
  234. outputs[OUT_OUTPUT].value = _cicDecimator.next(buf) * 5.0f;
  235. // outputs[OUT2_OUTPUT].value = _lpfDecimator.next(buf) * 5.0f;
  236. outputs[OUT2_OUTPUT].value = _rackDecimator.process(buf) * 5.0f;
  237. #elif INTERPOLATOR
  238. const int quality = 12;
  239. float sampleRate = engineGetSampleRate();
  240. float frequency = oscillatorPitch();
  241. _saw.setSampleRate(sampleRate);
  242. _saw.setFrequency(frequency);
  243. _saw.setQuality(quality);
  244. _decimator.setParams(sampleRate, FACTOR);
  245. _interpolator.setParams(sampleRate, FACTOR);
  246. if (_steps >= FACTOR) {
  247. _steps = 0;
  248. for (int i = 0; i < FACTOR; ++i) {
  249. _rawSamples[i] = _saw.next();
  250. }
  251. _interpolator.next(_decimator.next(_rawSamples), _processedSamples);
  252. }
  253. outputs[OUT_OUTPUT].value = _processedSamples[_steps] * 5.0f;
  254. outputs[OUT2_OUTPUT].value = _rawSamples[_steps] * 5.0f;
  255. ++_steps;
  256. #elif FM
  257. const float amplitude = 5.0f;
  258. float baseHz = oscillatorPitch();
  259. float ratio = ratio2();
  260. float index = index3();
  261. float sampleRate = engineGetSampleRate();
  262. if (_baseHz != baseHz || _ratio != ratio || _index != index || _sampleRate != sampleRate) {
  263. _baseHz = baseHz;
  264. _ratio = ratio;
  265. _index = index;
  266. _sampleRate = sampleRate;
  267. float modHz = _ratio * _baseHz;
  268. // printf("baseHz=%f ratio=%f modHz=%f index=%f\n", _baseHz, _ratio, modHz, _index);
  269. _modulator.setFrequency(modHz);
  270. _modulator.setSampleRate(_sampleRate);
  271. _carrier.setSampleRate(_sampleRate);
  272. _carrier2.setSampleRate(engineGetSampleRate());
  273. _carrier2.setFrequency(baseHz);
  274. _modulator2.setSampleRate(engineGetSampleRate());
  275. _modulator2.setFrequency(modHz);
  276. }
  277. // linear FM.
  278. float modHz = _ratio * _baseHz;
  279. _carrier.setFrequency(_baseHz + _index * _modulator.next() * modHz); // linear FM requires knowing the modulator's frequency.
  280. outputs[OUT_OUTPUT].value = _carrier.next() * amplitude;
  281. // PM for comparison - identical output.
  282. _carrier2.advancePhase();
  283. outputs[OUT2_OUTPUT].value = _carrier2.nextFromPhasor(_carrier2, Phasor::radiansToPhase(_index * _modulator2.next())) * amplitude;
  284. #elif PM
  285. const float amplitude = 5.0f;
  286. float baseHz = oscillatorPitch();
  287. float modHz = ratio2() * baseHz;
  288. _carrier.setSampleRate(engineGetSampleRate());
  289. _carrier.setFrequency(baseHz);
  290. _modulator.setSampleRate(engineGetSampleRate());
  291. _modulator.setFrequency(modHz);
  292. _carrier.advancePhase();
  293. outputs[OUT_OUTPUT].value = _carrier.nextFromPhasor(_carrier, Phasor::radiansToPhase(index3() * _modulator.next())) * amplitude;
  294. #elif FEEDBACK_PM
  295. _carrier.setSampleRate(engineGetSampleRate());
  296. _carrier.setFrequency(oscillatorPitch());
  297. float feedback = params[PARAM2_PARAM].value;
  298. if (inputs[CV2_INPUT].active) {
  299. feedback *= clamp(inputs[CV2_INPUT].value, 0.0f, 10.0f) / 10.0f;
  300. }
  301. _carrier.advancePhase();
  302. outputs[OUT_OUTPUT].value = _feedbackSample = _carrier.nextFromPhasor(_carrier, Phasor::radiansToPhase(feedback * _feedbackSample)) * 5.0f;
  303. #elif EG
  304. _envelope.setSampleRate(engineGetSampleRate());
  305. _envelope.setAttack(params[PARAM1_PARAM].value);
  306. _envelope.setDecay(params[PARAM2_PARAM].value);
  307. _envelope.setSustain(params[PARAM3_PARAM].value);
  308. _envelope.setRelease(params[PARAM2_PARAM].value);
  309. _envelope.setGate(inputs[CV1_INPUT].value > 0.1f);
  310. outputs[OUT_OUTPUT].value = _envelope.next() * 10.0f;
  311. #elif TABLES
  312. _sine.setSampleRate(engineGetSampleRate());
  313. _sine.setFrequency(oscillatorPitch());
  314. outputs[OUT_OUTPUT].value = _sine.next() * 5.0f;
  315. _table.setSampleRate(engineGetSampleRate());
  316. _table.setFrequency(oscillatorPitch());
  317. outputs[OUT2_OUTPUT].value = _table.next() * 5.0f;
  318. #elif SLEW
  319. float ms = params[PARAM1_PARAM].value;
  320. if (inputs[CV1_INPUT].active) {
  321. ms *= clamp(inputs[CV2_INPUT].value, 0.0f, 10.0f) / 10.0f;
  322. }
  323. ms = powf(ms, 2.0f);
  324. ms *= 10000.0f;
  325. _slew.setParams(engineGetSampleRate(), ms);
  326. outputs[OUT_OUTPUT].value = _slew.next(inputs[IN_INPUT].value);
  327. float shape = params[PARAM2_PARAM].value;
  328. if (inputs[CV2_INPUT].active) {
  329. shape *= clamp(inputs[CV2_INPUT].value / 5.0f, -1.0f, 1.0f);
  330. }
  331. if (shape < 0.5) {
  332. shape /= 0.5;
  333. shape = _slew2.minShape + shape * (1.0f - _slew2.minShape);
  334. }
  335. else {
  336. shape -= 0.5f;
  337. shape /= 0.5f;
  338. shape *= (_slew2.maxShape - 1.0f);
  339. shape += 1.0f;
  340. }
  341. _slew2.setParams(engineGetSampleRate(), ms, shape);
  342. outputs[OUT2_OUTPUT].value = _slew2.next(inputs[IN_INPUT].value);
  343. #elif RMS
  344. float sensitivity = params[PARAM2_PARAM].value;
  345. if (inputs[CV2_INPUT].active) {
  346. sensitivity *= clamp(inputs[CV2_INPUT].value, 0.0f, 10.0f) / 10.0f;
  347. }
  348. _rms.setSampleRate(engineGetSampleRate());
  349. _rms.setSensitivity(sensitivity);
  350. outputs[OUT_OUTPUT].value = _rms.next(inputs[IN_INPUT].value);
  351. _pef.setSensitivity(sensitivity);
  352. outputs[OUT2_OUTPUT].value = _pef.next(inputs[IN_INPUT].value);
  353. #elif RAVG
  354. if (_reset.process(inputs[CV1_INPUT].value)) {
  355. _average.reset();
  356. }
  357. outputs[OUT_OUTPUT].value = _average.next(inputs[IN_INPUT].value);
  358. #elif SATURATOR
  359. float in = inputs[IN_INPUT].value;
  360. outputs[OUT_OUTPUT].value = _saturator.next(in);
  361. outputs[OUT2_OUTPUT].value = clamp(in, -Saturator::limit, Saturator::limit);
  362. #endif
  363. }
  364. float Test::oscillatorPitch(float max) {
  365. if (inputs[CV1_INPUT].active) {
  366. return cvToFrequency(inputs[CV1_INPUT].value);
  367. }
  368. return max * powf(params[PARAM1_PARAM].value, 2.0);
  369. }
  370. float Test::oscillatorPitch2(float max) {
  371. if (inputs[CV2_INPUT].active) {
  372. return cvToFrequency(inputs[CV2_INPUT].value);
  373. }
  374. return max * powf(params[PARAM2_PARAM].value, 2.0);
  375. }
  376. float Test::ratio2() {
  377. float ratio = (params[PARAM2_PARAM].value * 2.0f) - 1.0f;
  378. if (inputs[CV2_INPUT].active) {
  379. ratio *= clamp(inputs[CV2_INPUT].value / 5.0f, -1.0f, 1.0f);
  380. }
  381. if (ratio < 0.0f) {
  382. return 1.0f + ratio;
  383. }
  384. return 1.0f + 9.0f*ratio;
  385. }
  386. float Test::index3() {
  387. float index = params[PARAM3_PARAM].value;
  388. if (inputs[CV3_INPUT].active) {
  389. index *= clamp(inputs[CV3_INPUT].value, 0.0f, 10.0f) / 10.0f;
  390. }
  391. return index * 10.0f;
  392. }
  393. struct TestWidget : ModuleWidget {
  394. TestWidget(Test* module) : ModuleWidget(module) {
  395. box.size = Vec(RACK_GRID_WIDTH * 3, RACK_GRID_HEIGHT);
  396. {
  397. SVGPanel *panel = new SVGPanel();
  398. panel->box.size = box.size;
  399. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Test.svg")));
  400. addChild(panel);
  401. }
  402. addChild(Widget::create<ScrewSilver>(Vec(0, 0)));
  403. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 15, 365)));
  404. // generated by svg_widgets.rb
  405. auto param1ParamPosition = Vec(9.5, 13.5);
  406. auto param2ParamPosition = Vec(9.5, 98.5);
  407. auto param3ParamPosition = Vec(9.5, 183.5);
  408. auto cv1InputPosition = Vec(10.5, 53.0);
  409. auto cv2InputPosition = Vec(10.5, 138.0);
  410. auto cv3InputPosition = Vec(10.5, 223.0);
  411. auto inInputPosition = Vec(10.5, 268.0);
  412. auto outOutputPosition = Vec(10.5, 306.0);
  413. auto out2OutputPosition = Vec(20.5, 316.0);
  414. // end generated by svg_widgets.rb
  415. addParam(ParamWidget::create<Knob26>(param1ParamPosition, module, Test::PARAM1_PARAM, 0.0, 1.0, 0.5));
  416. addParam(ParamWidget::create<Knob26>(param2ParamPosition, module, Test::PARAM2_PARAM, 0.0, 1.0, 0.5));
  417. addParam(ParamWidget::create<Knob26>(param3ParamPosition, module, Test::PARAM3_PARAM, 0.0, 1.0, 0.5));
  418. addInput(Port::create<Port24>(cv1InputPosition, Port::INPUT, module, Test::CV1_INPUT));
  419. addInput(Port::create<Port24>(cv2InputPosition, Port::INPUT, module, Test::CV2_INPUT));
  420. addInput(Port::create<Port24>(cv3InputPosition, Port::INPUT, module, Test::CV3_INPUT));
  421. addInput(Port::create<Port24>(inInputPosition, Port::INPUT, module, Test::IN_INPUT));
  422. addOutput(Port::create<Port24>(outOutputPosition, Port::OUTPUT, module, Test::OUT_OUTPUT));
  423. addOutput(Port::create<Port24>(out2OutputPosition, Port::OUTPUT, module, Test::OUT2_OUTPUT));
  424. }
  425. };
  426. RACK_PLUGIN_MODEL_INIT(Bogaudio, Test) {
  427. Model *modelTest = Model::create<Test, TestWidget>("Bogaudio", "Bogaudio-Test", "Test");
  428. return modelTest;
  429. }