sounds
This commit is contained in:
parent
737ae2efe0
commit
a22b6d1e22
21 changed files with 239 additions and 11 deletions
|
@ -1,3 +1,5 @@
|
|||
require 'lib.audio'
|
||||
|
||||
local assets = {}
|
||||
|
||||
function assets.clear()
|
||||
|
@ -70,8 +72,8 @@ function assets.addAudio(base, d, type)
|
|||
if type == "sfx" then
|
||||
sound_path[audioname] = dir .. "/" .. file
|
||||
|
||||
if sounds and sounds[audioname] then
|
||||
registerSound(audioname, sounds[audioname].volume)
|
||||
if sounds then
|
||||
registerSound(audioname, (sounds[audioname] or {}).volume or 1)
|
||||
end
|
||||
elseif type == "bgm" then
|
||||
music_path[audioname] = dir .. "/" .. file
|
||||
|
|
34
lib/audio.lua
Normal file
34
lib/audio.lua
Normal file
|
@ -0,0 +1,34 @@
|
|||
local tick = require 'lib.tick'
|
||||
|
||||
sfx_volume = 1
|
||||
|
||||
sounds = {}
|
||||
local current_volume = 1
|
||||
local old_volume = 1
|
||||
local sound_instances = {}
|
||||
|
||||
function registerSound(sound, volume)
|
||||
sounds[sound] = {
|
||||
data = love.sound.newSoundData(sound_path[sound]),
|
||||
volume = volume or 1
|
||||
}
|
||||
end
|
||||
|
||||
function playSound(sound, volume, pitch)
|
||||
if sounds[sound] then
|
||||
if not sound_instances[sound] then
|
||||
sound_instances[sound] = 0
|
||||
end
|
||||
|
||||
local source = love.audio.newSource(sounds[sound].data, "static")
|
||||
|
||||
local adjusted_volume = 1/(2^sound_instances[sound])
|
||||
source:setVolume((volume or 1) * adjusted_volume * sounds[sound].volume * sfx_volume)
|
||||
source:setPitch(pitch or 1)
|
||||
|
||||
source:play()
|
||||
|
||||
sound_instances[sound] = sound_instances[sound] + 1
|
||||
tick.delay(function() sound_instances[sound] = sound_instances[sound] - 1 end, sounds[sound].data:getDuration()/4)
|
||||
end
|
||||
end
|
166
lib/tick.lua
Normal file
166
lib/tick.lua
Normal file
|
@ -0,0 +1,166 @@
|
|||
--
|
||||
-- tick
|
||||
--
|
||||
-- Copyright (c) 2015 rxi
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or modify it
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local tick = { _version = "0.1.1" }
|
||||
tick.__index = tick
|
||||
|
||||
|
||||
local iscallable = function(x)
|
||||
if type(x) == "function" then return true end
|
||||
local mt = getmetatable(x)
|
||||
return mt and mt.__call ~= nil
|
||||
end
|
||||
|
||||
local noop = function()
|
||||
end
|
||||
|
||||
|
||||
local event = {}
|
||||
event.__index = event
|
||||
|
||||
function event.new(parent, fn, delay, recur, err)
|
||||
err = err or 0
|
||||
-- Create and return event
|
||||
return setmetatable({
|
||||
parent = parent,
|
||||
delay = delay,
|
||||
timer = delay + err,
|
||||
fn = fn,
|
||||
recur = recur,
|
||||
}, event)
|
||||
end
|
||||
|
||||
|
||||
function event:after(fn, delay)
|
||||
-- Error check
|
||||
if self.recur then
|
||||
error("cannot chain a recurring event")
|
||||
end
|
||||
-- Chain event
|
||||
local oldfn = self.fn
|
||||
local e = event.new(self.parent, fn, delay, false)
|
||||
self.fn = function()
|
||||
oldfn()
|
||||
e.timer = e.timer + self.parent.err
|
||||
self.parent:add(e)
|
||||
end
|
||||
return e
|
||||
end
|
||||
|
||||
|
||||
function event:stop()
|
||||
tick.remove(self.parent, self)
|
||||
end
|
||||
|
||||
|
||||
|
||||
function tick.group()
|
||||
return setmetatable({ err = 0 }, tick)
|
||||
end
|
||||
|
||||
|
||||
function tick:add(e)
|
||||
self[e] = true
|
||||
table.insert(self, e)
|
||||
return e
|
||||
end
|
||||
|
||||
|
||||
function tick:remove(e)
|
||||
if type(e) == "number" then
|
||||
-- Remove and return event
|
||||
local idx = e
|
||||
e = self[idx]
|
||||
self[e] = nil
|
||||
self[idx] = self[#self]
|
||||
table.remove(self)
|
||||
return e
|
||||
end
|
||||
self[e] = false
|
||||
for i, v in ipairs(self) do
|
||||
if v == e then
|
||||
return self:remove(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function tick:update(dt)
|
||||
for i = #self, 1, -1 do
|
||||
local e = self[i]
|
||||
e.timer = e.timer - dt
|
||||
while e.timer <= 0 do
|
||||
if e.recur then
|
||||
e.timer = e.timer + e.delay
|
||||
else
|
||||
self:remove(i)
|
||||
end
|
||||
self.err = e.timer
|
||||
e.fn()
|
||||
if not e.recur then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self.err = 0
|
||||
end
|
||||
|
||||
|
||||
function tick:event(fn, delay, recur)
|
||||
delay = tonumber(delay)
|
||||
-- Error check
|
||||
if not iscallable(fn) then
|
||||
error("expected `fn` to be callable")
|
||||
end
|
||||
if type(delay) ~= "number" then
|
||||
error("expected `delay` to be a number")
|
||||
end
|
||||
if delay < 0 then
|
||||
error("expected `delay` of zero or greater")
|
||||
end
|
||||
-- If, factoring in the timing error, the event should happen *now* the
|
||||
-- function is immediately called and the error is temporarily carried
|
||||
-- through. This assures nested events with delays shorter than the update()
|
||||
-- delta-time do not accumulate error; several nested events with very small
|
||||
-- delays may end up being called on the same frame. A dummy event is created
|
||||
-- and returned so :after() still functions correctly.
|
||||
local d = delay + self.err
|
||||
if d < 0 then
|
||||
local err = self.err
|
||||
self.err = d
|
||||
fn()
|
||||
self.err = err
|
||||
return self:add(event.new(self, noop, delay, recur, self.err))
|
||||
end
|
||||
-- Create, add and return a normal event
|
||||
return self:add(event.new(self, fn, delay, recur, self.err))
|
||||
end
|
||||
|
||||
|
||||
function tick:delay(fn, delay)
|
||||
return self:event(fn, delay, false)
|
||||
end
|
||||
|
||||
|
||||
function tick:recur(fn, delay)
|
||||
return self:event(fn, delay, true)
|
||||
end
|
||||
|
||||
|
||||
local group = tick.group()
|
||||
|
||||
local bound = {
|
||||
update = function(...) return tick.update(group, ...) end,
|
||||
delay = function(...) return tick.delay (group, ...) end,
|
||||
recur = function(...) return tick.recur (group, ...) end,
|
||||
remove = function(...) return tick.remove(group, ...) end,
|
||||
}
|
||||
setmetatable(bound, tick)
|
||||
|
||||
return bound
|
Loading…
Add table
Add a link
Reference in a new issue