89 lines
2.5 KiB
Lua
89 lines
2.5 KiB
Lua
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 not self.animationNode then return end
|
||
self.from:setVolume(self.musicVolume - self.animationNode:getValue() * self.musicVolume)
|
||
self.to:setVolume(self.animationNode:getValue() * self.musicVolume)
|
||
self.animationNode:update(dt)
|
||
-- print(self.animationNode.t)
|
||
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)
|
||
self.from:stop()
|
||
self.animationNode = nil
|
||
print("[Audio]: Crossfade done")
|
||
end,
|
||
duration = ms or 1000,
|
||
easing = ease.easeOutCubic,
|
||
}
|
||
self.animationNode:run()
|
||
end
|
||
|
||
--- @param source love.Source
|
||
--- @param settings SourceFilter?
|
||
--- @param effectName string?
|
||
function audio:play(source, settings, effectName)
|
||
if settings then
|
||
source:setFilter(settings)
|
||
end
|
||
if effectName then
|
||
source:setEffect(effectName, true)
|
||
end
|
||
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 }
|