forked from alesan99/mari0_ae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicloader.lua
180 lines (161 loc) · 4.29 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
music = {
toload = {
"overworld",
"underground",
"castle",
"underwater",
"starmusic",
"princessmusic",
},
loaded = {},
list = {},
list_fast = {},
pitch = 1,
}
local musicpath = "sounds/%s.ogg"
local intromusic = false --{intro name, started music}
function music:init()
intromusic = false --music that is currently in it's intro phase
end
function music:load(musicfile) -- can take a single file string or an array of file strings
if type(musicfile) == "table" then
for i, v in ipairs(musicfile) do
self:preload(musicpath:format(v), v)
--load fast music
if love.filesystem.getInfo(musicpath:format(v .. "-fast")) then
self:preload(musicpath:format(v .. "-fast"), v .. "-fast")
end
end
else --custom music
self:preload(musicfile)
--load fast music
if love.filesystem.getInfo(musicfile:sub(1, -5) .. "-fast" .. musicfile:sub(-4, -1)) then
self:preload(musicfile:sub(1, -5) .. "-fast" .. musicfile:sub(-4, -1))
end
--load intro music
if love.filesystem.getInfo(musicfile:sub(1, -5) .. "-intro" .. musicfile:sub(-4, -1)) then
self:preloadintro(musicfile:sub(1, -5) .. "-intro" .. musicfile:sub(-4, -1))
end
end
end
function music:preload(musicfile, name)
if name or self.loaded[musicfile] == nil then
local source = love.audio.newSource(musicfile, "stream")
source:setLooping(true)
source:setPitch(self.pitch)
self.loaded[name or musicfile] = source
end
end
function music:preloadintro(musicfile, name)
if name or self.loaded[musicfile] == nil then
local source = love.audio.newSource(musicfile, "stream")
source:setPitch(self.pitch)
self.loaded[name or musicfile] = source
end
end
function music:play(name, isfast, nointro)
local filename = name
local isintro = false
if name and soundenabled then
--find fast
intromusic = false
if isfast and self.loaded[name:sub(1, -5) .. "-fast" .. name:sub(-4, -1)] then
name = name:sub(1, -5) .. "-fast" .. name:sub(-4, -1)
elseif (not nointro) and self.loaded[name:sub(1, -5) .. "-intro" .. name:sub(-4, -1)] then
name = name:sub(1, -5) .. "-intro" .. name:sub(-4, -1)
isintro = true
end
if self.loaded[name] then
--self.loaded[name]:setPitch(0.61538)
playsound(self.loaded[name])
if isintro then
intromusic = {name, filename}
end
elseif self.loaded[musicpath:format(name)] then
playsound(self.loaded[musicpath:format(name)])
end
end
end
function music:playIndex(index, isfast)
local name = self.list[index]
--find fast default music
if isfast then
name = name .. "-fast"
end
self:play(name)
end
function music:stop(name, isfast)
if name then
--find fast
if isfast and self.loaded[name:sub(1, -5) .. "-fast" .. name:sub(-4, -1)] then
name = name:sub(1, -5) .. "-fast" .. name:sub(-4, -1)
elseif intromusic and name == intromusic[2] then
name = name:sub(1, -5) .. "-intro" .. name:sub(-4, -1)
end
if self.loaded[name] then
love.audio.stop(self.loaded[name])
elseif self.loaded[musicpath:format(name)] then
love.audio.stop(self.loaded[musicpath:format(name)])
end
if intromusic and name == intromusic[1] then
self:disableintromusic()
end
end
end
function music:stopIndex(index, isfast)
local name = self.list[index]
--find fast default music
if isfast then
name = name .. "-fast"
end
self:stop(name)
intromusic = false
end
function music:stopall()
for name, source in pairs(self.loaded) do
if source ~= false then
source:stop()
end
end
intromusic = false
end
function music:getPlaying()
for name, source in pairs(self.loaded) do
if isActive(source) then
return name, source
end
end
return false
end
function music:update()
for name, source in pairs(self.loaded) do
if source ~= false then
source:setPitch(self.pitch)
end
end
if intromusic then
if self.loaded[intromusic[1]] and not isActive(self.loaded[intromusic[1]]) then
music:play(intromusic[2], nil, true)
intromusic = false
end
end
end
function music:playingmusic()
for name, source in pairs(self.loaded) do
if source and isActive(source) then
return true
end
end
return false
end
function music:disableintromusic()
intromusic = false
end
music:load(music.toload)
-- the original/default music needs to be put in the correct lists
for i,v in ipairs(music.toload) do
if not v:match("princessmusic") then
table.insert(music.list, v)
end
end
music:init()