-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio.lua
117 lines (97 loc) · 2.24 KB
/
audio.lua
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--[[
Bismillahirahmanirrahim
Audio control class for Gideros
by: Edwin Zaniar Putra (zaniar@nightspade.com)
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
Copyright � 2012 Nightspade (http://nightspade.com).
--]]
Audio = Core.class()
function Audio:init()
self.bgmChannel = nil
self.bgmCurrentName = nil
self.bgmCurrent = nil
self.bgmPos = 0
self.bgmMute = false
self.sfxMute = false
self.bgms = {}
self.sfxs = {}
end
function Audio:setBgms(bgms)
self.bgms = bgms
end
function Audio:setSfxs(sfxs)
for name, path in pairs(sfxs) do
self.sfxs[name] = Sound.new(path)
end
end
function Audio:clearBgms()
self.bgms = {}
end
function Audio:clearSfxs()
self.sfxs = {}
end
function Audio:addBgm(name, path)
self.bgms[name] = path
end
function Audio:addSfx(name, path)
self.sfxs[name] = Sound.new(path)
end
function Audio:removeBgm(name)
self.bgms[name] = nil
end
function Audio:removeSfx(name)
self.sfxs[name] = nil
end
function Audio:playBgm(name, force)
if name ~= self.bgmCurrentName or force then
if self.bgmChannel then
self.bgmChannel:stop()
self.bgmChannel = nil
self.bgmCurrentName = nil
self.bgmCurrent = nil
self.bgmPos = 0
end
if not self.bgmMute and not self.bgmChannel then
self.bgmCurrentName = name
self.bgmCurrent = Sound.new(self.bgms[name])
self.bgmChannel = self.bgmCurrent:play(self.bgmPos, math.huge)
end
end
end
function Audio:playSfx(name)
if not self.sfxMute then
return self.sfxs[name]:play()
end
end
function Audio:mute(mode)
if mode == "bgm" then
self.bgmMute = true
if self.bgmChannel then
self.bgmPos = self.bgmChannel:getPosition()
self.bgmChannel:stop()
self.bgmChannel = nil
end
elseif mode == "sfx" then
self.sfxMute = true
else
self.bgmMute = true
if self.bgmChannel then
self.bgmPos = self.bgmChannel:getPosition()
self.bgmChannel:stop()
self.bgmChannel = nil
end
self.sfxMute = true
end
end
function Audio:unmute(mode)
if mode == "bgm" then
self.bgmMute = false
self.bgmChannel = self.bgmCurrent:play(self.bgmPos, math.huge)
elseif mode == "sfx" then
self.sfxMute = false
else
self.bgmMute = false
self.bgmChannel = self.bgmCurrent:play(self.bgmPos, math.huge)
self.sfxMute = false
end
end