---@type engine.module local module = {} local abot, sound, rate, channels local spectrum = {} local visualizers = {} require("modules.luafft") local function divide(list, factor) for i, v in ipairs(list) do list[i] = list[i] / factor end end ---@param character engine.character function module.onAdded(character) print(character.stagePosition:Add(character.sprite.position)) abot = Atlas("sprites/characters/abot/abotSystem") abot:PlayAnimation("") abot.layer = character.sprite.layer - 0.1 for i = 1, 7 do print(i) local viz = Rect("sprites/characters/abot/aBotViz.png", "sprites/characters/abot/aBotViz.xml") viz.position = abot.position:Add(Vector2(i * 50 + 20, 50)) viz.layer = 500 viz:Frame(string.format("viz%s", i), 5) visualizers[i] = viz end end function module.onCreate(song) local audiopath = string.format("songs/%s/Inst.ogg", song) print(audiopath) sound = love.sound.newSoundData(audiopath) rate = sound:getSampleRate() channels = sound:getChannels() end function module.onBeat(beat) -- if beat % 2 == 0 then abot:PlayAnimation() -- end end function module.onUpdate(dt, el) local curSample = inst:tell('samples') local wave = {} local size = next_possible_size(2048) if channels == 2 then for i = curSample, (curSample + (size - 1) / 2) do local sample = (sound:getSample(i * 2) + sound:getSample(i * 2 + 1)) * 0.5 table.insert(wave, complex.new(sample, 0)) end else for i = curSample, curSample + (size - 1) do local sample = (sound:getSample(i * 2) + sound:getSample(i * 2 + 1)) * 0.5 table.insert(wave, complex.new(sample, 0)) table.insert(wave, complex.new(sound:getSample(i), 0)) end end local spec = fft(wave, false) --local reconstructed = fft(spec,true) --divide(reconstructed,size) divide(spec, size / 2) spectrum = spec local division = 142.857142857 local actuali = 1 for i=1, #spectrum/division do local n = spectrum[i]:abs() visualizers[actuali]:Frame(string.format("viz%s", actuali), math.floor(n / 0.06) > 5 and 0 or 5 - math.floor(n / 0.06)) actuali = actuali + 1 end end function module.onClose() end return module