106 lines
3.1 KiB
Lua
106 lines
3.1 KiB
Lua
local ease = require "lib.utils.easing"
|
||
local AnimationNode = require "lib.animation_node"
|
||
|
||
--- @class Audio
|
||
--- @field musicVolume number
|
||
--- @field soundVolume number
|
||
audio = {}
|
||
audio.__index = audio
|
||
|
||
--- здесь мы должны выгружать значения из файлика с сохранением настроек
|
||
local function new(musicVolume, soundVolume)
|
||
return setmetatable({
|
||
musicVolume = musicVolume,
|
||
soundVolume = soundVolume
|
||
}, audio)
|
||
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)
|
||
to:setVolume(0)
|
||
to:play()
|
||
local fromVol = from:getVolume() or self.musicVolume
|
||
local toVol = to:getVolume() or self.musicVolume
|
||
local dt = love.timer.getDelta() * (ms or 1000)
|
||
|
||
while fromVol > 0 and toVol < self.musicVolume do
|
||
print(fromVol, toVol)
|
||
fromVol = self.musicVolume - fromVol + dt / self.musicVolume
|
||
from:setVolume(ease.easeOutQuad(fromVol))
|
||
toVol = toVol + dt / self.musicVolume
|
||
to:setVolume(ease.easeInQuad(toVol))
|
||
print(fromVol, toVol)
|
||
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:crossfadeAnim(from, to, ms)
|
||
to:setVolume(0)
|
||
to:play()
|
||
print(from:getVolume(), to:getVolume())
|
||
local t = 0
|
||
local anim = AnimationNode {
|
||
function(node)
|
||
from:setVolume(self.musicVolume - node:getValue() * self.musicVolume)
|
||
to:setVolume(node:getValue() * self.musicVolume)
|
||
t = node.t
|
||
print(node.duration, node.t)
|
||
end,
|
||
onEnd = function()
|
||
from:setVolume(0)
|
||
to:setVolume(self.musicVolume)
|
||
end,
|
||
duration = ms or 1000,
|
||
easing = ease.linear,
|
||
-- children = {
|
||
-- AnimationNode {
|
||
-- function(node)
|
||
-- from:setVolume(self.musicVolume - node:getValue() * self.musicVolume)
|
||
-- end
|
||
-- },
|
||
-- AnimationNode {
|
||
-- function(node)
|
||
-- to:setVolume(node:getValue() * self.musicVolume)
|
||
-- print(node.count)
|
||
-- end
|
||
-- }
|
||
-- }
|
||
}
|
||
anim:run()
|
||
anim:finish()
|
||
print(from:getVolume(), to:getVolume(), t, t, t)
|
||
end
|
||
|
||
--- @param source love.Source
|
||
function audio:play(source)
|
||
if source:getType() == "stream" then
|
||
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 }
|