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.

Light.hpp 968B

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