t8rt0t

speaker.lua

May 26th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. local speakers = {peripheral.find("speaker")}
  2. if #speakers <= 0 then error("No speaker attached") end
  3. local allocated_speakers = {}
  4.  
  5. local basalt = require("basalt")
  6. local main = basalt.createFrame()
  7.  
  8. local SPEAKER_PATH = "playlists"
  9. if not fs.isDir(SPEAKER_PATH) then fs.makeDir(SPEAKER_PATH) end
  10. [[
  11. playlists/Name.json:
  12. {
  13.   type = 'playlist',
  14.   repeats: number | nil,
  15.   stages: [{
  16.    type: 'sound' | 'note',
  17.    volume: number | nil,
  18.    duration: number | nil,
  19.    sound: {
  20.     path: string = <path>,
  21.     pitch: number | nil
  22.    }
  23.    |
  24.    note: {
  25.     instrument: string = <instrument> | nil,
  26.     pitch: number
  27.    }
  28. }
  29. ]]
  30.  
  31. local function loadSpeakerJson(path)
  32.     if not fs.exists(path) or fs.isDir(path) then return false end
  33.     local file = fs.open(path,"r")
  34.     local data = textutils.unserialiseJSON(file.readAll())
  35.     file.close()
  36.     if not data then return false end
  37.     if data.type ~= 'playlist' then return false end
  38.     if type(data.stages) ~= "table" then return false end
  39.     data.repeats = tonumber(data.repeats) or 1
  40.     for idx,stage in ipairs(data.stages) do
  41.         speakers[1].stop()
  42.         --Test out the sound
  43.         if type(stage.type) == "sound" and type(stage.sound) == "table" then
  44.             local s1,s2 = pcall(function() speakers[1].playSound(stage.sound.path,0,tonumber(stage.sound.pitch) or 1) end)
  45.             if not s1 or not s2 then return false end
  46.         elseif type(stage.type) == "note" and type(stage.note) == "table" then
  47.             local s1,s2 = pcall(function() speakers[1].playNote(stage.note.instrument or "harp",0,tonumber(stage.note.pitch)) end)
  48.             if not s1 or not s2 then return false end
  49.         else
  50.             return false
  51.         end
  52.     end
  53.     return data
  54. end
  55.  
  56. local playlists = {}
  57. pcall(function()
  58.         for k,v in ipairs(fs.list(SPEAKER_PATH)) do
  59.             local recipe = loadSpeakerJSON(fs.combine(SPEAKER_PATH,v))
  60.             if recipe then playlists[v] = recipe end
  61.         end
  62.     end)
  63.  
  64. local function allocateSpeaker(playlist)
  65.     if #speakers <= 0 then return false, "no speakers available" end
  66.     table.insert(allocated_speakers,{speaker=table.remove(speakers),index=0,playlist=playlist})
  67. end
  68.  
  69. local function speaker_tick()
  70.     for i=#allocated_speakers,1,-1 do
  71.         spkr = allocated_speakers[i]
  72.         if spkr.index >= spkr.playlist.repeats * #spkr.playlist.stages then
  73.             table.remove(allocated_speakers,i)
  74.             table.insert(speakers,spkr.speaker)
  75.         elseif os.epoch() >= tonumber(spkr.nextSound) or 0 then
  76.             local stage = spkr.playlist.stages[spkr.index%#spkr.playlist.stages + 1]
  77.             if stage.type == "sound" then
  78.                 local s1,s2 = pcall(function() spkr.speaker.playSound(stage.sound.path,tonumber(stage.volume) or 1, tonumber(stage.sound.pitch) or 1) end)
  79.                 if s1 and s2 then index = index + 1 end
  80.             elseif stage.type == "note" then
  81.                 local s1,s2 = pcall(function() spkr.speaker.playNote(stage.note.instrument or "harp",tonumber(stage.volume) or 1,tonumber(stage.note.pitch)) end)
  82.                 if s1 and s2 then index = index + 1 end
  83.             end
  84.         end
  85.     end
  86.     sleep(0.1)
  87. end
  88.  
  89. local soundList = main:addList():setScrollable(true):setSize(25,5):setPosition(1,1)
  90. for k,v in ipairs(table.sort(fs.list(SPEAKER_PATH))) do
  91.     soundList:addItem(v)
  92. end
  93. soundList:onSelect(function(self,event,item) basalt.debug(allocateSpeaker(playlists[item])) end)
  94.  
  95. parallel.waitForAll(basalt.autoUpdate,function() while true do speaker_tick() end)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment