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.

324 lines
6.9KB

  1. #pragma once
  2. #include "bogaudio.hpp"
  3. extern Model* modelTest;
  4. // #define LPF 1
  5. // #define LPFNOISE 1
  6. // #define SINE 1
  7. // #define SQUARE 1
  8. // #define SAW 1
  9. // #define SATSAW 1
  10. // #define TRIANGLE 1
  11. // #define SAMPLED_TRIANGLE 1
  12. // #define SINEBANK 1
  13. // #define OVERSAMPLING 1
  14. // #define OVERSAMPLED_BL 1
  15. // #define ANTIALIASING 1
  16. // #define DECIMATORS 1
  17. #define INTERPOLATOR 1
  18. // #define FM 1
  19. // #define PM 1
  20. // #define FEEDBACK_PM 1
  21. // #define EG 1
  22. // #define TABLES 1
  23. // #define SLEW 1
  24. // #define RMS 1
  25. // #define RAVG 1
  26. // #define SATURATOR 1
  27. #include "pitch.hpp"
  28. #ifdef LPF
  29. #include "dsp/filter.hpp"
  30. #elif LPFNOISE
  31. #include "dsp/filter.hpp"
  32. #include "dsp/noise.hpp"
  33. #elif SINE
  34. #include "dsp/oscillator.hpp"
  35. #elif SQUARE
  36. #include "dsp/oscillator.hpp"
  37. #elif SAW
  38. #include "dsp/oscillator.hpp"
  39. #elif SATSAW
  40. #include "dsp/oscillator.hpp"
  41. #elif TRIANGLE
  42. #include "dsp/oscillator.hpp"
  43. #elif SAMPLED_TRIANGLE
  44. #include "dsp/oscillator.hpp"
  45. #elif SINEBANK
  46. #include "dsp/oscillator.hpp"
  47. #elif OVERSAMPLING
  48. #include "dsp/oscillator.hpp"
  49. #include "dsp/decimator.hpp" // rack
  50. #include "dsp/filter.hpp"
  51. #define OVERSAMPLEN 16
  52. #elif OVERSAMPLED_BL
  53. #include "dsp/oscillator.hpp"
  54. #include "dsp/filter.hpp"
  55. #elif ANTIALIASING
  56. #include "dsp/oscillator.hpp"
  57. #include "dsp/decimator.hpp" // rack
  58. #include "dsp/filter.hpp"
  59. #elif DECIMATORS
  60. #include "dsp/oscillator.hpp"
  61. #include "dsp/filter.hpp"
  62. #include "dsp/decimator.hpp" // rack
  63. #elif INTERPOLATOR
  64. #include "dsp/oscillator.hpp"
  65. #include "dsp/filter.hpp"
  66. #elif FM
  67. #include "dsp/oscillator.hpp"
  68. #elif PM
  69. #include "dsp/oscillator.hpp"
  70. #elif FEEDBACK_PM
  71. #include "dsp/oscillator.hpp"
  72. #elif EG
  73. #include "dsp/envelope.hpp"
  74. #elif TABLES
  75. #include "dsp/oscillator.hpp"
  76. #elif SLEW
  77. #include "dsp/signal.hpp"
  78. #elif RMS
  79. #include "dsp/signal.hpp"
  80. #elif RAVG
  81. #include "dsp/signal.hpp"
  82. #elif SATURATOR
  83. #include "dsp/oscillator.hpp"
  84. #include "dsp/signal.hpp"
  85. #else
  86. #error what
  87. #endif
  88. using namespace bogaudio::dsp;
  89. namespace bogaudio {
  90. struct Test : Module {
  91. enum ParamsIds {
  92. PARAM1_PARAM,
  93. PARAM2_PARAM,
  94. PARAM3_PARAM,
  95. NUM_PARAMS
  96. };
  97. enum InputsIds {
  98. CV1_INPUT,
  99. CV2_INPUT,
  100. CV3_INPUT,
  101. IN_INPUT,
  102. NUM_INPUTS
  103. };
  104. enum OutputsIds {
  105. OUT_OUTPUT,
  106. OUT2_OUTPUT,
  107. NUM_OUTPUTS
  108. };
  109. enum LightsIds {
  110. NUM_LIGHTS
  111. };
  112. #ifdef LPF
  113. LowPassFilter _lpf;
  114. #elif LPFNOISE
  115. WhiteNoiseGenerator _noise;
  116. LowPassFilter _lpf;
  117. #elif SINE
  118. SineOscillator _sine;
  119. SineTable _table;
  120. TablePhasor _sine2;
  121. #elif SQUARE
  122. SquareOscillator _square;
  123. BandLimitedSquareOscillator _square2;
  124. #elif SAW
  125. SawOscillator _saw;
  126. BandLimitedSawOscillator _saw2;
  127. #elif SATSAW
  128. SaturatingSawOscillator _saw;
  129. BandLimitedSawOscillator _saw2;
  130. #elif TRIANGLE
  131. TriangleOscillator _triangle;
  132. #elif SAMPLED_TRIANGLE
  133. TriangleOscillator _triangle;
  134. TriangleOscillator _triangle2;
  135. int _sampleSteps = 1 << 20;
  136. int _sampleStep = 0;
  137. float _sample = 0.0f;
  138. #elif SINEBANK
  139. SineBankOscillator _sineBank;
  140. #elif OVERSAMPLING
  141. SawOscillator _saw1;
  142. SawOscillator _saw2;
  143. LowPassFilter _lpf;
  144. LowPassFilter _lpf2;
  145. rack::Decimator<OVERSAMPLEN, OVERSAMPLEN> _rackDecimator;
  146. #elif OVERSAMPLED_BL
  147. BandLimitedSawOscillator _saw1;
  148. BandLimitedSawOscillator _saw2;
  149. LowPassFilter _lpf;
  150. #elif ANTIALIASING
  151. #define OVERSAMPLEN 8
  152. Phasor _phasor;
  153. Phasor _oversampledPhasor;
  154. BandLimitedSawOscillator _saw;
  155. BandLimitedSquareOscillator _square;
  156. bogaudio::dsp::LPFDecimator _sawDecimator;
  157. bogaudio::dsp::LPFDecimator _squareDecimator;
  158. rack::Decimator<OVERSAMPLEN, OVERSAMPLEN> _sawRackDecimator;
  159. rack::Decimator<OVERSAMPLEN, OVERSAMPLEN> _squareRackDecimator;
  160. #elif DECIMATORS
  161. #define OVERSAMPLEN 8
  162. #define STAGES 4
  163. BandLimitedSawOscillator _saw;
  164. bogaudio::dsp::CICDecimator _cicDecimator;
  165. bogaudio::dsp::LPFDecimator _lpfDecimator;
  166. rack::Decimator<OVERSAMPLEN, OVERSAMPLEN> _rackDecimator;
  167. #elif INTERPOLATOR
  168. #define FACTOR 8
  169. #define STAGES 4
  170. BandLimitedSawOscillator _saw;
  171. bogaudio::dsp::CICDecimator _decimator;
  172. bogaudio::dsp::CICInterpolator _interpolator;
  173. int _steps;
  174. float _rawSamples[FACTOR] {};
  175. float _processedSamples[FACTOR] {};
  176. #elif FM
  177. float _baseHz = 0.0f;
  178. float _ratio = 0.0f;
  179. float _index = 0.0f;
  180. float _sampleRate = 0.0f;
  181. SineTableOscillator _modulator;
  182. SineTableOscillator _carrier;
  183. SineTableOscillator _modulator2;
  184. SineTableOscillator _carrier2;
  185. #elif PM
  186. SineTableOscillator _modulator;
  187. SineTableOscillator _carrier;
  188. #elif FEEDBACK_PM
  189. SineTableOscillator _carrier;
  190. float _feedbackSample = 0.0f;
  191. #elif EG
  192. ADSR _envelope;
  193. #elif TABLES
  194. SineTableOscillator _sine;
  195. TablePhasor _table;
  196. #elif SLEW
  197. SlewLimiter _slew;
  198. ShapedSlewLimiter _slew2;
  199. #elif RMS
  200. RootMeanSquare _rms;
  201. PucketteEnvelopeFollower _pef;
  202. #elif RAVG
  203. RunningAverage _average;
  204. Trigger _reset;
  205. #elif SATURATOR
  206. Saturator _saturator;
  207. #endif
  208. Test()
  209. : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS)
  210. #if SINE
  211. , _table(12)
  212. , _sine2(_table)
  213. #elif DECIMATORS
  214. , _cicDecimator(STAGES)
  215. #elif INTERPOLATOR
  216. , _decimator(STAGES)
  217. , _interpolator(STAGES)
  218. #elif TABLES
  219. , _table(StaticBlepTable::table(), 44100.0, 1000.0)
  220. #elif RAVG
  221. , _average(engineGetSampleRate(), 1.0f, 1000.0f)
  222. #endif
  223. {
  224. onReset();
  225. #ifdef SINE
  226. _table.generate();
  227. _sine2.setPhase(M_PI);
  228. #elif SAW
  229. _saw2.setPhase(M_PI);
  230. #elif SATSAW
  231. _saw2.setPhase(M_PI);
  232. #elif SAMPLED_TRIANGLE
  233. _triangle2.setPhase(M_PI);
  234. #elif SINEBANK
  235. const float baseAmplitude = 5.0;
  236. switch (5) {
  237. case 1: {
  238. // saw
  239. for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) {
  240. _sineBank.setPartial(i, i, baseAmplitude / (float)i);
  241. }
  242. _sineBank.syncToPhase(M_PI);
  243. break;
  244. }
  245. case 2: {
  246. // square
  247. for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) {
  248. _sineBank.setPartial(i, i, i % 2 == 1 ? baseAmplitude / (float)i : 0.0);
  249. }
  250. break;
  251. }
  252. case 3: {
  253. // triangle
  254. if (false) {
  255. for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) {
  256. _sineBank.setPartial(i, i, i % 2 == 1 ? baseAmplitude / (float)(i * i) : 0.0);
  257. }
  258. _sineBank.syncToPhase(M_PI / 2.0);
  259. }
  260. else {
  261. _sineBank.setPartial(1, 1.0f, baseAmplitude);
  262. for (int i = 2, n = _sineBank.partialCount(); i < n; ++i) {
  263. float k = 2*i - 1;
  264. _sineBank.setPartial(i, k, powf(-1.0f, k) * baseAmplitude/(k * k));
  265. }
  266. }
  267. break;
  268. }
  269. case 4: {
  270. // saw-square
  271. for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) {
  272. _sineBank.setPartial(i, i, i % 2 == 1 ? baseAmplitude / (float)i : baseAmplitude / (float)(2 * i));
  273. }
  274. break;
  275. }
  276. case 5: {
  277. // ?
  278. float factor = 0.717;
  279. float factor2 = factor;
  280. float multiple = 1.0;
  281. for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) {
  282. _sineBank.setPartial(i, multiple, baseAmplitude / multiple);
  283. multiple += i % 2 == 1 ? factor : factor2;
  284. }
  285. break;
  286. }
  287. }
  288. #elif OVERSAMPLED_BL
  289. _saw2.setPhase(M_PI);
  290. #endif
  291. }
  292. void onReset() override;
  293. void step() override;
  294. float oscillatorPitch(float max = 10000.0);
  295. float oscillatorPitch2(float max = 10000.0);
  296. float ratio2();
  297. float index3();
  298. };
  299. } // namespace bogaudio