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.

44 lines
1009B

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