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 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. /** Sets the brightness directly with no LED modeling. */
  11. void setBrightness(float brightness) {
  12. value = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  13. }
  14. float getBrightness() {
  15. return std::sqrt(value);
  16. }
  17. /** Emulates slow fall (but immediate rise) of LED brightness.
  18. `frames` rescales the timestep.
  19. For example, if your module calls this method every 16 frames, use 16.f.
  20. */
  21. void setBrightnessSmooth(float brightness, float frames = 1.f) {
  22. float v = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  23. if (v < value) {
  24. // Fade out light with lambda = framerate
  25. // Use 44.1k here to avoid the call to Engine::getSampleRate().
  26. // This is close enough to look okay up to 96k
  27. value += (v - value) * frames * 30.f / 44100.f;
  28. }
  29. else {
  30. // Immediately illuminate light
  31. value = v;
  32. }
  33. }
  34. };
  35. } // namespace engine
  36. } // namespace rack