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.

40 lines
926B

  1. #pragma once
  2. #include "common.hpp"
  3. namespace rack {
  4. struct Light {
  5. /** The mean-square of the brightness */
  6. float value = 0.f;
  7. float getBrightness() {
  8. return std::sqrt(value);
  9. }
  10. void setBrightness(float brightness) {
  11. value = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  12. }
  13. /** Emulates slow fall (but immediate rise) of LED brightness.
  14. `frames` rescales the timestep. For example, if your module calls this method every 16 frames, use 16.f.
  15. */
  16. void setBrightnessSmooth(float brightness, float frames = 1.f) {
  17. float v = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  18. if (v < value) {
  19. // Fade out light with lambda = framerate
  20. // Use 44.1k here to avoid the call to Engine::getSampleRate().
  21. // This is close enough to look okay up to 96k
  22. value += (v - value) * frames * 60.f / 44100.f;
  23. }
  24. else {
  25. // Immediately illuminate light
  26. value = v;
  27. }
  28. }
  29. };
  30. } // namespace rack