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.

54 lines
1.3KB

  1. // Rainbow RGB LED example
  2. import("stdfaust.lib");
  3. // From https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
  4. hsvToRgb(h, s, v) = r, g, b
  5. with {
  6. h1 = h * 6;
  7. c1 = v * s;
  8. v1 = v;
  9. x = c1 * (1 - abs(h1 % 2 - 1));
  10. r1 = ba.if((h1 < 1), c1,
  11. ba.if((h1 < 2), x,
  12. ba.if((h1 < 3), 0,
  13. ba.if((h1 < 4), 0,
  14. ba.if((h1 < 5), x, c1)))));
  15. g1 = ba.if((h1 < 1), x,
  16. ba.if((h1 < 2), c1,
  17. ba.if((h1 < 3), c1,
  18. ba.if((h1 < 4), x,
  19. ba.if((h1 < 5), 0, 0)))));
  20. b1 = ba.if((h1 < 1), 0,
  21. ba.if((h1 < 2), 0,
  22. ba.if((h1 < 3), x,
  23. ba.if((h1 < 4), c1,
  24. ba.if((h1 < 5), c1, x)))));
  25. m = v1 - c1;
  26. r = r1 + m;
  27. g = g1 + m;
  28. b = b1 + m;
  29. };
  30. process = par(i, 6, out(i+1))
  31. with {
  32. phasor(freq) = freq/ma.SR : (+ : decimal) ~ _ with { decimal(x) = x-int(x); };
  33. out(i) = (sin(2 * ma.PI * h) * 5 + 5) <: (red(i), green(i), blue(i)) :> _
  34. with {
  35. h = (1 - i / 6 + phase) % 1;
  36. rgb = hsvToRgb(h, 1, 1);
  37. r = rgb : _,!,!;
  38. g = rgb : !,_,!;
  39. b = rgb : !,!,_;
  40. red(i,x) = attach(x, r : hbargraph("r1%i [switchlight_red:%i]", 0, 1) : hbargraph("r2%i [light_red:%i]", 0, 1));
  41. green(i,x) = attach(x, g : hbargraph("g1%i [switchlight_green:%i]", 0, 1) : hbargraph("g2%i [light_green:%i]", 0, 1));
  42. blue(i,x) = attach(x, b : hbargraph("b1%i [switchlight_blue:%i]", 0, 1) : hbargraph("b2%i [light_blue:%i]", 0, 1));
  43. phase = phasor(0.5);
  44. };
  45. };