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.

vumeter.hpp 1.8KB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. struct VUMeter2 {
  28. enum Mode {
  29. PEAK,
  30. RMS
  31. };
  32. Mode mode = PEAK;
  33. float v = 0.f;
  34. /** Inverse time constant in 1/seconds */
  35. float lambda = 0.f;
  36. void reset() {
  37. v = 0.f;
  38. }
  39. void process(float deltaTime, float value) {
  40. if (mode == PEAK) {
  41. value = std::abs(value);
  42. if (value >= v) {
  43. v = value;
  44. }
  45. else {
  46. v += (value - v) * lambda * deltaTime;
  47. }
  48. }
  49. else {
  50. value = std::pow(value, 2);
  51. v += (value - v) * lambda * deltaTime;
  52. }
  53. }
  54. /** Returns the LED brightness measuring tick marks between dbMin and dbMax.
  55. For example, `getBrightness(-6.f, 0.f)` will be at minimum brightness at -6dB and maximum brightness at 0dB.
  56. Set dbMin == dbMax == 0.f for a clip indicator.
  57. Expensive, so call this infrequently.
  58. */
  59. float getBrightness(float dbMin, float dbMax) {
  60. float db = amplitudeToDb((mode == RMS) ? std::sqrt(v) : v);
  61. if (db > dbMax)
  62. return 1.f;
  63. else if (db < dbMin)
  64. return 0.f;
  65. else
  66. return math::rescale(db, dbMin, dbMax, 0.f, 1.f);
  67. }
  68. };
  69. } // namespace dsp
  70. } // namespace rack