-
Notifications
You must be signed in to change notification settings - Fork 17
/
musicloader.lua
101 lines (88 loc) · 2.01 KB
/
musicloader.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
music = {
loaded = {},
pitch = 1,
list = {
"overworld.ogg",
"underground.ogg",
"castle.ogg",
"underwater.ogg",
"starmusic.ogg",
},
list_fast = {
"overworld-fast.ogg",
"underground-fast.ogg",
"castle-fast.ogg",
"underwater-fast.ogg",
"starmusic-fast.ogg",
},
}
function getfilepath(name)
local musicpaths = {"sounds/", "mappacks/" .. mappack .. "/music/"}
for i = 1, #musicpaths do
if love.filesystem.getInfo(musicpaths[i] .. name, "file") then
return musicpaths[i] .. name
end
end
end
function music:load(name)
local filepath = getfilepath(name)
if not filepath then
print(string.format("can't load music %q: can't find file!", name))
return false
end
if not self.loaded[filepath] then
local loaded, source = pcall(love.audio.newSource, filepath, MUSICFIX)
if loaded then
-- all music should loop
source:setLooping(true)
source:setPitch(self.pitch)
self.loaded[name] = source
else
print(string.format("can't load music %q: can't create source!", filepath))
return false
end
end
return true
end
function music:play(name, fast)
if fast then
local newname = name:sub(0, -5) .. "-fast" .. name:sub(-4)
if getfilepath(newname) then
name = newname
end
end
-- try to load source from disk if it hasn't been loaded already
if not self.loaded[name] and not self:load(name) then
return
end
if self.loaded[name] then
if soundenabled then
self.loaded[name]:stop()
self.loaded[name]:seek(0)
self.loaded[name]:setVolume(volumemusic or 1)
self.loaded[name]:play()
end
end
end
function music:stop(name, fast)
if fast then
local newname = name:sub(0, -5) .. "-fast" .. name:sub(-4)
if getfilepath(newname) then
name = newname
end
end
if self.loaded[name] then
self.loaded[name]:stop()
self.loaded[name]:seek(0)
end
end
function music:update()
for filepath, source in pairs(self.loaded) do
source:setPitch(self.pitch)
end
end
function music:setVolume(n)
for i, v in pairs(self.loaded) do
v:setVolume(volumemusic or 1)
end
end