Advertisement
TechManDylan

TheOrgan

Jun 9th, 2025 (edited)
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. -- master.lua
  2. local modem = peripheral.find("modem") or error("No modem found!")
  3. modem.open(65535)
  4.  
  5. local instrumentPorts = {
  6.     gedeckt = 1000,
  7.     steam_whistle = 1001,
  8.     diapason = 1002,
  9.     gamba = 1003,
  10.     posaune = 1004,
  11.     subbass = 1005,
  12.     trompette = 1006,
  13.     vox_humana = 1007,
  14.     piccolo = 1008,
  15.     nassard = 1009
  16. }
  17.  
  18. local lastSong = nil
  19.  
  20. -- Load song data from local file (downloaded via pastebin)
  21. local function load_song_from_file(filename)
  22.     if not fs.exists(filename) then
  23.         error("File does not exist: " .. filename)
  24.     end
  25.     local f = fs.open(filename, "r")
  26.     local data = textutils.unserializeJSON(f.readAll())
  27.     f.close()
  28.     return data
  29. end
  30.  
  31. local function send_song_data(song)
  32.     for _, track in ipairs(song.tracks) do
  33.         local instrument = track.instrument
  34.         local port = instrumentPorts[instrument]
  35.         if port then
  36.             modem.transmit(port, 65535, {
  37.                 type = "song_data",
  38.                 instrument = instrument,
  39.                 title = song.title,
  40.                 notes = track.notes
  41.             })
  42.             print("Sent track to " .. instrument .. " on port " .. port)
  43.         else
  44.             print("⚠ No port mapped for instrument: " .. instrument)
  45.         end
  46.     end
  47. end
  48.  
  49. local function request_playback(song)
  50.     local delay = 3
  51.     local start_time = os.time() + delay
  52.     print("Broadcasting play request with start_at: " .. string.format("%.2f", start_time))
  53.  
  54.     local instruments = {}
  55.     for _, track in ipairs(song.tracks) do
  56.         instruments[track.instrument] = true
  57.     end
  58.  
  59.     for instrument in pairs(instruments) do
  60.         local port = instrumentPorts[instrument]
  61.         if port then
  62.             modem.transmit(port, 65535, {
  63.                 type = "play_request",
  64.                 title = song.title,
  65.                 start_at = start_time
  66.             })
  67.             print("→ Requested playback for " .. instrument)
  68.         end
  69.     end
  70. end
  71.  
  72. local function main()
  73.     while true do
  74.         print("\n[ Master Menu ]\n1) Download + Load Song\n2) Play Last Song\n3) Exit")
  75.         io.write("> ")
  76.         local choice = read()
  77.  
  78.         if choice == "1" then
  79.             io.write("Pastebin code: ")
  80.             local code = read()
  81.             local filename = "song.json"
  82.             shell.run("delete " .. filename)
  83.             shell.run("pastebin get " .. code .. " " .. filename)
  84.  
  85.             local ok, result = pcall(load_song_from_file, filename)
  86.             if ok and result then
  87.                 lastSong = result
  88.                 send_song_data(lastSong)
  89.                 print("[✓] Song distributed.")
  90.             else
  91.                 print("[✗] Failed to load or parse song: " .. tostring(result))
  92.             end
  93.  
  94.         elseif choice == "2" then
  95.             if lastSong then
  96.                 request_playback(lastSong)
  97.             else
  98.                 print("[!] No song loaded.")
  99.             end
  100.  
  101.         elseif choice == "3" then
  102.             print("Exiting...")
  103.             break
  104.  
  105.         else
  106.             print("Invalid option.")
  107.         end
  108.     end
  109. end
  110.  
  111. main()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement