-
Notifications
You must be signed in to change notification settings - Fork 108
/
musicloader.lua
110 lines (95 loc) · 2.21 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
music = {
thread = love.thread.newThread("musicloader_thread.lua"),
toload = {},
loaded = {},
list = {},
list_fast = {},
pitch = 1,
}
music.stringlist = table.concat(music.toload, ";")
function music:init()
self.thread:start()
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(v)
end
else
self:preload(musicfile)
end
self.stringlist = table.concat(self.toload, ";")
love.thread.getChannel("musiclist"):push(self.stringlist)
end
function music:preload(musicfile)
if self.loaded[musicfile] == nil then
self.loaded[musicfile] = false
table.insert(self.toload, musicfile)
end
end
function music:play(name)
if name and soundenabled then
if self.loaded[name] == false then
local source = self.thread:demand(name)
self:onLoad(name, source)
end
if self.loaded[name] then
self.loaded[name]:play()
end
end
end
function music:playIndex(index, isfast)
local name = isfast and self.list_fast[index] or self.list[index]
self:play(name)
end
function music:stop(name)
if name and self.loaded[name] then
love.audio.stop(self.loaded[name])
end
end
function music:stopIndex(index, isfast)
local name = isfast and self.list_fast[index] or self.list[index]
self:stop(name)
end
function music:update()
for i,v in ipairs(self.toload) do
local source = love.thread.getChannel(v):pop()
if source then
self:onLoad(v, source)
end
end
for name, source in pairs(self.loaded) do
if source ~= false then
source:setPitch(self.pitch)
end
end
local err = self.thread:getError()
if err then print(err) end
end
function music:onLoad(name, source)
self.loaded[name] = source
source:setLooping(true)
source:setPitch(self.pitch)
end
music:load{
"overworld",
"overworld-fast",
"underground",
"underground-fast",
"castle",
"castle-fast",
"underwater",
"underwater-fast",
"starmusic",
"starmusic-fast",
"princessmusic",
}
-- the original/default music needs to be put in the correct lists
for i,v in ipairs(music.toload) do
if v:match("fast") then
table.insert(music.list_fast, v)
elseif not v:match("princessmusic") then
table.insert(music.list, v)
end
end
music:init()