1local this = love.thread.getThread()
2
3require("love.filesystem")
4require("love.sound")
5require("love.audio")
6require("love.timer")
7
8love.filesystem.setIdentity("mari0")
9
10local musicpath = "sounds/%s.ogg"
11
12local musiclist = {}
13local musictoload = {} -- waiting to be loaded into memory
14
15local function getmusiclist()
16	-- the music string should have names separated by the ";" character
17	-- music will be loaded in in the same order as they appear in the string
18	local musicliststr = this:get("musiclist")
19	if musicliststr then
20		for musicname in musicliststr:gmatch("[^;]+") do
21			if not musiclist[musicname] then
22				musiclist[musicname] = true
23				table.insert(musictoload, musicname)
24			end
25		end
26	end
27end
28
29local function getfilename(name)
30	local filename = name:match("%.[mo][pg][3g]$") and name or musicpath:format(name) -- mp3 or ogg
31	if love.filesystem.exists(filename) and love.filesystem.isFile(filename) then
32		return filename
33	else
34		print(string.format("thread can't load \"%s\": not a file!", filename))
35	end
36end
37
38local function loadmusic()
39	if #musictoload > 0 then
40		local name = table.remove(musictoload, 1)
41		local filename = getfilename(name)
42		if filename then
43			local source = love.audio.newSource(love.sound.newDecoder(filename, 512 * 1024), "static")
44			--print("thread loaded music", name)
45			this:set(name, source)
46		end
47	end
48end
49
50while true do
51	getmusiclist()
52	loadmusic()
53	love.timer.sleep(1/60)
54end
55