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.

308 lines
6.5KB

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