Sound2Light auf RGB(W)-LED-Leisten
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.

vumeter.lua 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. COOLDOWN_FACTOR = 0.9998
  2. FACTOR = 0.033
  3. num_modules = 20
  4. center_module = 10
  5. -- maximum energy values for each band
  6. maxRedEnergy = 1
  7. maxGreenEnergy = 1
  8. maxBlueEnergy = 1
  9. maxRMS = 1
  10. -- output color buffers
  11. red = {}
  12. green = {}
  13. blue = {}
  14. function periodic()
  15. local redEnergy = get_energy_in_band(0, 400);
  16. local greenEnergy = get_energy_in_band(400, 4000);
  17. local blueEnergy = get_energy_in_band(4000, 22000);
  18. local rms = get_rms();
  19. maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR
  20. if redEnergy > maxRedEnergy then
  21. maxRedEnergy = redEnergy
  22. end
  23. maxGreenEnergy = maxGreenEnergy * COOLDOWN_FACTOR
  24. if greenEnergy > maxGreenEnergy then
  25. maxGreenEnergy = greenEnergy
  26. end
  27. maxBlueEnergy = maxBlueEnergy * COOLDOWN_FACTOR
  28. if blueEnergy > maxBlueEnergy then
  29. maxBlueEnergy = blueEnergy
  30. end
  31. maxRMS = maxRMS * COOLDOWN_FACTOR
  32. if rms > maxRMS then
  33. maxRMS = rms
  34. end
  35. local brightness = rms / maxRMS
  36. for i = 1,num_modules do
  37. if i <= center_module and
  38. center_module - i < center_module * redEnergy / maxRedEnergy or
  39. i > center_module and
  40. i - (center_module + 1) < (num_modules-center_module) * redEnergy / maxRedEnergy
  41. then
  42. redTarget = brightness
  43. else
  44. redTarget = 0
  45. end
  46. red[i] = (1 - FACTOR) * red[i] + FACTOR * redTarget;
  47. if i <= center_module and
  48. center_module - i < center_module * greenEnergy / maxGreenEnergy or
  49. i > center_module and
  50. i - (center_module + 1) < (num_modules-center_module) * greenEnergy / maxGreenEnergy
  51. then
  52. greenTarget = brightness
  53. else
  54. greenTarget = 0
  55. end
  56. green[i] = (1 - FACTOR) * green[i] + FACTOR * greenTarget;
  57. if i <= center_module and
  58. center_module - i < center_module * blueEnergy / maxBlueEnergy or
  59. i > center_module and
  60. i - (center_module + 1) < (num_modules-center_module) * blueEnergy / maxBlueEnergy
  61. then
  62. blueTarget = brightness
  63. else
  64. blueTarget = 0
  65. end
  66. blue[i] = (1 - FACTOR) * blue[i] + FACTOR * blueTarget;
  67. end
  68. -- return the 3 color arrays
  69. return red, green, blue
  70. end
  71. function init(nmod, cmod)
  72. num_modules = nmod
  73. center_module = cmod
  74. for i = 1,nmod do
  75. red[i] = 0
  76. green[i] = 0
  77. blue[i] = 0
  78. end
  79. -- fadestep (0 = no fading)
  80. return 0
  81. end