musiclight2/pulsetunnel_fast.lua

144 lines
3.2 KiB
Lua

COOLDOWN_FACTOR = 0.9998
FADE_FACTOR = 1
OVERDRIVE = 1.30
EXPONENT = 1.8
INTERP_FACTOR = 2
INTERP_FILTER = {0.5, 1.0, 0.5}
num_modules = 128
center_module = 64
-- maximum energy values for each band
maxRedEnergy = 1
maxGreenEnergy = 1
maxBlueEnergy = 1
-- output color buffers
red = {}
green = {}
blue = {}
tmpRed = {}
tmpGreen = {}
tmpBlue = {}
tmpRed2 = {}
tmpGreen2 = {}
tmpBlue2 = {}
tmpRed3 = {}
tmpGreen3 = {}
tmpBlue3 = {}
function limit(val)
if val > 1 then
return 1
else
return val
end
end
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 centerIndex = 2 * center_module + 1;
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
-- move the color buffers on by one
for i = center_module/INTERP_FACTOR,1,-1 do
tmpRed[i+1] = FADE_FACTOR * tmpRed[i]
tmpGreen[i+1] = FADE_FACTOR * tmpGreen[i]
tmpBlue[i+1] = FADE_FACTOR * tmpBlue[i]
end
for i = center_module/INTERP_FACTOR+1,num_modules/INTERP_FACTOR,1 do
tmpRed[i-1] = FADE_FACTOR * tmpRed[i]
tmpGreen[i-1] = FADE_FACTOR * tmpGreen[i]
tmpBlue[i-1] = FADE_FACTOR * tmpBlue[i]
end
-- set the new value for the center module
newRed = redEnergy / maxRedEnergy
tmpRed[1] = newRed
tmpRed[num_modules/INTERP_FACTOR] = newRed
newGreen = greenEnergy / maxGreenEnergy
tmpGreen[1] = newGreen
tmpGreen[num_modules/INTERP_FACTOR] = newGreen
newBlue = blueEnergy / maxBlueEnergy
tmpBlue[1] = newBlue
tmpBlue[num_modules/INTERP_FACTOR] = newBlue
for i = INTERP_FACTOR,num_modules,INTERP_FACTOR do
tmpRed2[i] = tmpRed[math.floor(i/INTERP_FACTOR)]
tmpGreen2[i] = tmpGreen[math.floor(i/INTERP_FACTOR)]
tmpBlue2[i] = tmpBlue[math.floor(i/INTERP_FACTOR)]
end
for i = 1,num_modules do
tmpRed3[i] = 0
tmpGreen3[i] = 0
tmpBlue3[i] = 0
for j = 1,#INTERP_FILTER do
idx = math.floor(i+j-#INTERP_FILTER/2)
if idx >= 1 and idx <= num_modules then
tmpRed3[i] = tmpRed3[i] + tmpRed2[idx] * INTERP_FILTER[j]
tmpGreen3[i] = tmpGreen3[i] + tmpGreen2[idx] * INTERP_FILTER[j]
tmpBlue3[i] = tmpBlue3[i] + tmpBlue2[idx] * INTERP_FILTER[j]
end
end
end
for i = 1,num_modules do
red[i] = limit(OVERDRIVE * math.pow(tmpRed3[i], EXPONENT))
green[i] = limit(OVERDRIVE * math.pow(tmpGreen3[i], EXPONENT))
blue[i] = limit(OVERDRIVE * math.pow(tmpBlue3[i], EXPONENT))
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
for i = 1,nmod do
tmpRed[i] = 0
tmpGreen[i] = 0
tmpBlue[i] = 0
tmpRed2[i] = 0
tmpGreen2[i] = 0
tmpBlue2[i] = 0
end
-- don't use fading
return 0
end