Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local speakers = {peripheral.find("speaker")}
- if #speakers <= 0 then error("No speaker attached") end
- local allocated_speakers = {}
- local basalt = require("basalt")
- local main = basalt.createFrame()
- local SPEAKER_PATH = "playlists"
- if not fs.isDir(SPEAKER_PATH) then fs.makeDir(SPEAKER_PATH) end
- [[
- playlists/Name.json:
- {
- type = 'playlist',
- repeats: number | nil,
- stages: [{
- type: 'sound' | 'note',
- volume: number | nil,
- duration: number | nil,
- sound: {
- path: string = <path>,
- pitch: number | nil
- }
- |
- note: {
- instrument: string = <instrument> | nil,
- pitch: number
- }
- }
- ]]
- local function loadSpeakerJson(path)
- if not fs.exists(path) or fs.isDir(path) then return false end
- local file = fs.open(path,"r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- if not data then return false end
- if data.type ~= 'playlist' then return false end
- if type(data.stages) ~= "table" then return false end
- data.repeats = tonumber(data.repeats) or 1
- for idx,stage in ipairs(data.stages) do
- speakers[1].stop()
- --Test out the sound
- if type(stage.type) == "sound" and type(stage.sound) == "table" then
- local s1,s2 = pcall(function() speakers[1].playSound(stage.sound.path,0,tonumber(stage.sound.pitch) or 1) end)
- if not s1 or not s2 then return false end
- elseif type(stage.type) == "note" and type(stage.note) == "table" then
- local s1,s2 = pcall(function() speakers[1].playNote(stage.note.instrument or "harp",0,tonumber(stage.note.pitch)) end)
- if not s1 or not s2 then return false end
- else
- return false
- end
- end
- return data
- end
- local playlists = {}
- pcall(function()
- for k,v in ipairs(fs.list(SPEAKER_PATH)) do
- local recipe = loadSpeakerJSON(fs.combine(SPEAKER_PATH,v))
- if recipe then playlists[v] = recipe end
- end
- end)
- local function allocateSpeaker(playlist)
- if #speakers <= 0 then return false, "no speakers available" end
- table.insert(allocated_speakers,{speaker=table.remove(speakers),index=0,playlist=playlist})
- end
- local function speaker_tick()
- for i=#allocated_speakers,1,-1 do
- spkr = allocated_speakers[i]
- if spkr.index >= spkr.playlist.repeats * #spkr.playlist.stages then
- table.remove(allocated_speakers,i)
- table.insert(speakers,spkr.speaker)
- elseif os.epoch() >= tonumber(spkr.nextSound) or 0 then
- local stage = spkr.playlist.stages[spkr.index%#spkr.playlist.stages + 1]
- if stage.type == "sound" then
- local s1,s2 = pcall(function() spkr.speaker.playSound(stage.sound.path,tonumber(stage.volume) or 1, tonumber(stage.sound.pitch) or 1) end)
- if s1 and s2 then index = index + 1 end
- elseif stage.type == "note" then
- local s1,s2 = pcall(function() spkr.speaker.playNote(stage.note.instrument or "harp",tonumber(stage.volume) or 1,tonumber(stage.note.pitch)) end)
- if s1 and s2 then index = index + 1 end
- end
- end
- end
- sleep(0.1)
- end
- local soundList = main:addList():setScrollable(true):setSize(25,5):setPosition(1,1)
- for k,v in ipairs(table.sort(fs.list(SPEAKER_PATH))) do
- soundList:addItem(v)
- end
- soundList:onSelect(function(self,event,item) basalt.debug(allocateSpeaker(playlists[item])) end)
- parallel.waitForAll(basalt.autoUpdate,function() while true do speaker_tick() end)
Advertisement
Add Comment
Please, Sign In to add comment