Advertisement
TechManDylan

TheOrgan

Jun 9th, 2025 (edited)
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. local modem = peripheral.find("modem") or error("No modem attached!")
  2. rednet.open(peripheral.getName(modem))
  3.  
  4. local song_data = nil
  5. local song_name = nil
  6. local song_file = "song.lua"
  7.  
  8. -- DOWNLOAD SONG DATA FROM PASTEBIN
  9. local function download_song()
  10.   term.clear()
  11.   term.setCursorPos(1, 1)
  12.   write("Enter Pastebin code: ")
  13.   local code = read()
  14.   write("Enter song name to save as: ")
  15.   song_name = read()
  16.  
  17.   if fs.exists(song_file) then fs.delete(song_file) end
  18.   shell.run("pastebin", "get", code, song_file)
  19.   if fs.exists(song_file) then
  20.     song_data = dofile(song_file)
  21.     print("Song downloaded and loaded!")
  22.   else
  23.     print("Failed to download.")
  24.   end
  25.   sleep(2)
  26. end
  27.  
  28. -- SEND ONLY RELEVANT INSTRUMENT DATA TO SUBS
  29. local function send_song()
  30.   if not song_data then
  31.     print("No song loaded. Download first.")
  32.     sleep(2)
  33.     return
  34.   end
  35.   if not song_name then
  36.     print("No song name entered.")
  37.     sleep(2)
  38.     return
  39.   end
  40.  
  41.   print("Broadcasting to find sub-computers...")
  42.   rednet.broadcast("__ping__")
  43.  
  44.   local found = {}
  45.   local timer = os.startTimer(2)
  46.  
  47.   while true do
  48.     local id, msg = rednet.receive(2)
  49.     if msg == "__identify__" then
  50.       rednet.send(id, "__get_instrument__")
  51.       local _, instrument = rednet.receive(id, 2)
  52.       if instrument and song_data[instrument] then
  53.         table.insert(found, {id=id, instrument=instrument})
  54.       end
  55.     elseif id == "timer" then
  56.       break
  57.     end
  58.   end
  59.  
  60.   print("Sending song data...")
  61.   for _, sub in ipairs(found) do
  62.     print("  → ID "..sub.id.." ("..sub.instrument..")")
  63.     rednet.send(sub.id, {
  64.       type = "song_data",
  65.       name = song_name,
  66.       instrument = sub.instrument,
  67.       notes = song_data[sub.instrument]
  68.     })
  69.   end
  70.  
  71.   print("Finished.")
  72.   sleep(2)
  73. end
  74.  
  75. -- REMOTE SONG LIST + SYNCHRONIZED START
  76. local function play_song_on_subs()
  77.   print("Requesting song lists from subs...")
  78.   rednet.broadcast("__list_songs__")
  79.  
  80.   local responses = {}
  81.   local known_songs = {}
  82.   local seen = {}
  83.   local timer = os.startTimer(2)
  84.  
  85.   while true do
  86.     local id, msg = rednet.receive(2)
  87.     if type(msg) == "table" and msg.type == "song_list" then
  88.       responses[id] = msg.songs
  89.       for _, song in ipairs(msg.songs) do
  90.         if not seen[song] then
  91.           table.insert(known_songs, song)
  92.           seen[song] = true
  93.         end
  94.       end
  95.     elseif id == "timer" then
  96.       break
  97.     end
  98.   end
  99.  
  100.   if #known_songs == 0 then
  101.     print("No songs found.")
  102.     sleep(2)
  103.     return
  104.   end
  105.  
  106.   print("Available songs:")
  107.   for i, name in ipairs(known_songs) do
  108.     print(i .. ". " .. name)
  109.   end
  110.   write("Choose song number to play: ")
  111.   local sel = tonumber(read())
  112.  
  113.   local chosen = known_songs[sel]
  114.   if not chosen then
  115.     print("Invalid selection.")
  116.     sleep(2)
  117.     return
  118.   end
  119.  
  120.   local start_time = os.time() + 5
  121.   print("Scheduling playback in 5 seconds (at time " .. start_time .. ")")
  122.  
  123.   for id, song_list in pairs(responses) do
  124.     for _, song in ipairs(song_list) do
  125.       if song == chosen then
  126.         rednet.send(id, {
  127.           type = "__play_song__",
  128.           name = chosen,
  129.           start = start_time
  130.         })
  131.         break
  132.       end
  133.     end
  134.   end
  135.  
  136.   print("Sent to all matching subs.")
  137.   sleep(2)
  138. end
  139.  
  140. -- LOCAL PLAYBACK FOR TESTING
  141. local function play_song()
  142.   if not song_data then
  143.     print("No song loaded.")
  144.     sleep(2)
  145.     return
  146.   end
  147.  
  148.   print("Local playback (all instruments)...")
  149.   for inst, notes in pairs(song_data) do
  150.     print("Playing: " .. inst)
  151.     for _, note in ipairs(notes) do
  152.       redstone.setBundledOutput(note.side, note.color)
  153.       sleep(note.duration)
  154.       redstone.setBundledOutput(note.side, 0)
  155.     end
  156.   end
  157.   sleep(1)
  158. end
  159.  
  160. -- MAIN MENU
  161. while true do
  162.   term.clear()
  163.   term.setCursorPos(1, 1)
  164.   print("=== Music Hub ===")
  165.   print("1. Download song from Pastebin")
  166.   print("2. Send song to sub-computers")
  167.   print("3. Play song on subs (synchronized)")
  168.   print("4. Play song locally")
  169.   print("5. Exit")
  170.   write("Choose an option: ")
  171.   local choice = read()
  172.  
  173.   if choice == "1" then
  174.     download_song()
  175.   elseif choice == "2" then
  176.     send_song()
  177.   elseif choice == "3" then
  178.     play_song_on_subs()
  179.   elseif choice == "4" then
  180.     play_song()
  181.   elseif choice == "5" then
  182.     break
  183.   else
  184.     print("Invalid option.")
  185.     sleep(1.5)
  186.   end
  187. end
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement