local ease = require "lib.utils.easing" local AnimationNode = require "lib.animation_node" --- @alias SourceFilter { type: "bandpass"|"highpass"|"lowpass", volume: number, highgain: number, lowgain: number } --- @class Audio --- @field musicVolume number --- @field soundVolume number --- @field looped boolean --- @field animationNode AnimationNode? --- @field from love.Source? --- @field to love.Source? audio = {} audio.__index = audio --- здесь мы должны выгружать значения из файлика с сохранением настроек local function new(musicVolume, soundVolume) return setmetatable({ musicVolume = musicVolume, soundVolume = soundVolume, looped = true }, audio) end function audio:update(dt) if self.animationNode and self.animationNode.state == "running" then self.animationNode:update(dt) self.from:setVolume(self.musicVolume - self.animationNode:getValue() * self.musicVolume) self.to:setVolume(self.animationNode:getValue() * self.musicVolume) -- print(self.animationNode.t) elseif self.animationNode and self.animationNode.state == "finished" then self.from:stop() self.animationNode:finish() self.animationNode = nil end end --- if from is nil, than we have fade in to; --- if to is nil, than we have fade out from --- --- also we should guarantee, that from and to have the same volume --- @param from love.Source --- @param to love.Source --- @param ms number? in milliseconds function audio:crossfade(from, to, ms) print("[Audio]: Triggered crossfade") self:play(to) to:setVolume(0) self.from = from self.to = to self.animationNode = AnimationNode { function(node) end, onEnd = function() self.from:setVolume(0) self.to:setVolume(self.musicVolume) print("[Audio]: Crossfade done") end, duration = ms or 1000, easing = ease.easeOutCubic, } self.animationNode:run() end --- @param source love.Source --- @param settings SourceFilter? function audio:play(source, settings, effectName) if settings then source:setFilter(settings) end source:setEffect(effectName, true) if source:getType() == "stream" then source:setLooping(self.looped) source:setVolume(self.musicVolume) return source:play() end source:setVolume(self.soundVolume) return source:play() end function audio:setMusicVolume(volume) self.musicVolume = volume end function audio:setSoundVolume(volume) self.soundVolume = volume end return { new = new }