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.

85 lines
1.8KB

  1. #pragma once
  2. #include "dsp/common.hpp"
  3. namespace rack {
  4. namespace dsp {
  5. /** Deprecated. Use VuMeter2. */
  6. struct VuMeter {
  7. /** Decibel level difference between adjacent meter lights */
  8. float dBInterval = 3.0;
  9. float dBScaled;
  10. /** Value should be scaled so that 1.0 is clipping */
  11. void setValue(float v) {
  12. dBScaled = std::log10(std::abs(v)) * 20.0 / dBInterval;
  13. }
  14. /** Returns the brightness of the light indexed by i.
  15. Light 0 is a clip light (red) which is either on or off.
  16. All others are smooth lights which are fully bright at -dBInterval*i and higher, and fully off at -dBInterval*(i-1).
  17. */
  18. float getBrightness(int i) {
  19. if (i == 0) {
  20. return (dBScaled >= 0.0) ? 1.0 : 0.0;
  21. }
  22. else {
  23. return math::clamp(dBScaled + i, 0.0, 1.0);
  24. }
  25. }
  26. };
  27. DEPRECATED typedef VuMeter VUMeter;
  28. struct VuMeter2 {
  29. enum Mode {
  30. PEAK,
  31. RMS
  32. };
  33. Mode mode = PEAK;
  34. float v = 0.f;
  35. /** Inverse time constant in 1/seconds */
  36. float lambda = 0.f;
  37. void reset() {
  38. v = 0.f;
  39. }
  40. void process(float deltaTime, float value) {
  41. if (mode == PEAK) {
  42. value = std::abs(value);
  43. if (value >= v) {
  44. v = value;
  45. }
  46. else {
  47. v += (value - v) * lambda * deltaTime;
  48. }
  49. }
  50. else {
  51. value = std::pow(value, 2);
  52. v += (value - v) * lambda * deltaTime;
  53. }
  54. }
  55. /** Returns the LED brightness measuring tick marks between dbMin and dbMax.
  56. For example, `getBrightness(-6.f, 0.f)` will be at minimum brightness at -6dB and maximum brightness at 0dB.
  57. Set dbMin == dbMax == 0.f for a clip indicator.
  58. Expensive, so call this infrequently.
  59. */
  60. float getBrightness(float dbMin, float dbMax) {
  61. float db = amplitudeToDb((mode == RMS) ? std::sqrt(v) : v);
  62. if (db > dbMax)
  63. return 1.f;
  64. else if (db < dbMin)
  65. return 0.f;
  66. else
  67. return math::rescale(db, dbMin, dbMax, 0.f, 1.f);
  68. }
  69. };
  70. } // namespace dsp
  71. } // namespace rack