musiclight2/old_scripts/vumeter.lua

98 lines
2.3 KiB
Lua
Raw Normal View History

COOLDOWN_FACTOR = 0.9998
FACTOR = 0.033
num_modules = 20
center_module = 10
-- maximum energy values for each band
maxRedEnergy = 1
maxGreenEnergy = 1
maxBlueEnergy = 1
maxRMS = 1
-- output color buffers
red = {}
green = {}
blue = {}
function periodic()
local redEnergy = get_energy_in_band(0, 400);
local greenEnergy = get_energy_in_band(400, 4000);
local blueEnergy = get_energy_in_band(4000, 22000);
local rms = get_rms();
maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR
if redEnergy > maxRedEnergy then
maxRedEnergy = redEnergy
end
maxGreenEnergy = maxGreenEnergy * COOLDOWN_FACTOR
if greenEnergy > maxGreenEnergy then
maxGreenEnergy = greenEnergy
end
maxBlueEnergy = maxBlueEnergy * COOLDOWN_FACTOR
if blueEnergy > maxBlueEnergy then
maxBlueEnergy = blueEnergy
end
maxRMS = maxRMS * COOLDOWN_FACTOR
if rms > maxRMS then
maxRMS = rms
end
local brightness = rms / maxRMS
for i = 1,num_modules do
if i <= center_module and
center_module - i < center_module * redEnergy / maxRedEnergy or
i > center_module and
i - (center_module + 1) < (num_modules-center_module) * redEnergy / maxRedEnergy
then
redTarget = brightness
else
redTarget = 0
end
red[i] = (1 - FACTOR) * red[i] + FACTOR * redTarget;
if i <= center_module and
center_module - i < center_module * greenEnergy / maxGreenEnergy or
i > center_module and
i - (center_module + 1) < (num_modules-center_module) * greenEnergy / maxGreenEnergy
then
greenTarget = brightness
else
greenTarget = 0
end
green[i] = (1 - FACTOR) * green[i] + FACTOR * greenTarget;
if i <= center_module and
center_module - i < center_module * blueEnergy / maxBlueEnergy or
i > center_module and
i - (center_module + 1) < (num_modules-center_module) * blueEnergy / maxBlueEnergy
then
blueTarget = brightness
else
blueTarget = 0
end
blue[i] = (1 - FACTOR) * blue[i] + FACTOR * blueTarget;
end
-- return the 3 color arrays
return red, green, blue
end
function init(nmod, cmod)
num_modules = nmod
center_module = cmod
for i = 1,nmod do
red[i] = 0
green[i] = 0
blue[i] = 0
end
-- fadestep (0 = no fading)
return 0
end