summaryrefslogtreecommitdiff
path: root/game/music.lua
blob: d9aa80defe93081e37304d21d453300dd1e17e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
local M = {}
M.__index = M
M.playing = nil
M.volume = 0.5

function M.new(assets)
	local new = setmetatable({}, M)
	new.assets = assets
	return new
end

function M:play(name)
	assert(self.assets.music[name], name.." isn't extant music")
	if M.playing ~= self.assets.music[name] then
        if M.playing then M.playing:stop() end
        self.assets.music[name]:seek(0)
        M.playing = self.assets.music[name]
    end
    self.assets.music[name]:setVolume(self.volume)
	self.assets.music[name]:play()
    self.assets.music[name]:setLooping(true)
end

function M:fade(loop, time)
    for i=1, math.ceil(time*20) do
        loop.poll(1/20)
        local volume = self.volume * (1-i/(time*20))
        self.playing:setVolume(volume)
    end
end

return M