-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundHandler.lua
123 lines (108 loc) · 2.81 KB
/
soundHandler.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
118
119
120
121
122
123
local IterableMap = require("include/IterableMap")
local util = require("include/util")
local api = {}
local sounds = IterableMap.New()
local soundFiles = util.LoadDefDirectory("sounds/defs")
function AddSource(name)
local def = soundFiles[name]
if def then
return love.audio.newSource(def.file, "static")
end
end
function api.LoadSound(name, id)
if id then
id = name .. (id or 1)
else
id = name
end
local soundData = IterableMap.Get(sounds, id)
if not soundData then
local def = soundFiles[name]
soundData = {
name = name,
want = 0,
have = 0,
volumeMult = def.volMult * Global.MASTER_VOLUME,
source = AddSource(name)
}
IterableMap.Add(sounds, id, soundData)
end
return soundData
end
function api.PlaySound(name, id, fadeIn, fadeOut, delay, loop)
local soundData = api.LoadSound(name, id)
soundData.source:setLooping(loop and true or false)
soundData.fadeIn = fadeIn or 10
soundData.fadeOut = fadeOut or 10
soundData.want = 1
soundData.delay = delay
if not soundData.delay then
soundData.source:stop()
love.audio.play(soundData.source)
soundData.source:setVolume(soundData.want * soundData.volumeMult)
end
end
function api.StopSound(name, id, instant, delay)
if id then
id = name .. (id or 1)
else
id = name
end
local soundData = IterableMap.Get(sounds, id)
if not soundData then
return
end
soundData.want = 0
soundData.delay = delay
if instant then
soundData.source:stop()
end
end
function api.SetSoundFade(name, id, fadeOuRate)
local soundData = api.LoadSound(name, id)
soundData.want = 0
soundData.fadeOut = fadeOuRate
end
function api.Update(dt)
for id, soundData in IterableMap.Iterator(sounds) do
if soundData.delay then
soundData.delay = soundData.delay - dt
if soundData.delay < 0 then
soundData.delay = false
if soundData.want > 0 then
soundData.source:stop()
love.audio.play(soundData.source)
soundData.source:setVolume(soundData.want * soundData.volumeMult)
end
if soundData.killSound then
soundData.source:stop()
end
end
else
if soundData.want > soundData.have then
soundData.have = soundData.have + (soundData.fadeIn or 10)*dt
if soundData.have > soundData.want then
soundData.have = soundData.want
end
soundData.source:setVolume(soundData.have * soundData.volumeMult)
end
if soundData.want < soundData.have then
soundData.have = soundData.have - (soundData.fadeOut or 10)*dt
if soundData.have < soundData.want then
soundData.have = soundData.want
end
soundData.source:setVolume(soundData.have * soundData.volumeMult)
if soundData.have <= 0 then
soundData.source:stop()
end
end
end
end
end
function api.Initialize()
for _, soundData in IterableMap.Iterator(sounds) do
soundData.source:stop()
end
sounds = sounds or IterableMap.New()
end
return api