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.

62 lines
1.3KB

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "table.hpp"
  4. #include "analyzer.hpp"
  5. using namespace bogaudio::dsp;
  6. void Table::generate() {
  7. if (!_table) {
  8. _table = new float[_length] {};
  9. _generate();
  10. }
  11. }
  12. void SineTable::_generate() {
  13. const float twoPI = 2.0f * M_PI;
  14. for (int i = 0, j = _length / 4; i <= j; ++i) {
  15. _table[i] = sinf(twoPI * (i / (float)_length));
  16. }
  17. for (int i = 1, j = _length / 4; i < j; ++i) {
  18. _table[i + j] = _table[j - i];
  19. }
  20. for (int i = 0, j = _length / 2; i < j; ++i) {
  21. _table[i + j] = -_table[i];
  22. }
  23. }
  24. void BlepTable::_generate() {
  25. // some amount of a sinc function.
  26. const float scaledPi = M_PI * 10.0f;
  27. _table[_length / 2] = 0.0f;
  28. for (int i = 1, j = _length / 2; i < j; ++i) {
  29. float radians = scaledPi * (i / (float)j);
  30. _table[j + i] = sinf(radians) / radians;
  31. }
  32. // "integrate": FIXME: avoid magic normalization value.
  33. const float norm = _length / 40.0f;
  34. float sum = 0.0f;
  35. for (int i = _length / 2; i < _length; ++i) {
  36. sum += _table[i];
  37. _table[i] = sum / norm;
  38. }
  39. // offset.
  40. for (int i = _length / 2; i < _length; ++i) {
  41. _table[i] -= 1.0f; // assumes successful normalization to 1-ish.
  42. }
  43. // copy to first half of table.
  44. for (int i = 0, j = _length / 2; i < j; ++i) {
  45. _table[i] = -_table[_length - 1 - i];
  46. }
  47. // smooth it out even more.
  48. HammingWindow hw(_length);
  49. hw.apply(_table, _table);
  50. }