Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- master.lua
- local modem = peripheral.find("modem") or error("No modem found!")
- modem.open(65535)
- local instrumentPorts = {
- gedeckt = 1000,
- steam_whistle = 1001,
- diapason = 1002,
- gamba = 1003,
- posaune = 1004,
- subbass = 1005,
- trompette = 1006,
- vox_humana = 1007,
- piccolo = 1008,
- nassard = 1009
- }
- local lastSong = nil
- -- Load song data from local file (downloaded via pastebin)
- local function load_song_from_file(filename)
- if not fs.exists(filename) then
- error("File does not exist: " .. filename)
- end
- local f = fs.open(filename, "r")
- local data = textutils.unserializeJSON(f.readAll())
- f.close()
- return data
- end
- local function send_song_data(song)
- for _, track in ipairs(song.tracks) do
- local instrument = track.instrument
- local port = instrumentPorts[instrument]
- if port then
- modem.transmit(port, 65535, {
- type = "song_data",
- instrument = instrument,
- title = song.title,
- notes = track.notes
- })
- print("Sent track to " .. instrument .. " on port " .. port)
- else
- print("⚠ No port mapped for instrument: " .. instrument)
- end
- end
- end
- local function request_playback(song)
- local delay = 3
- local start_time = os.time() + delay
- print("Broadcasting play request with start_at: " .. string.format("%.2f", start_time))
- local instruments = {}
- for _, track in ipairs(song.tracks) do
- instruments[track.instrument] = true
- end
- for instrument in pairs(instruments) do
- local port = instrumentPorts[instrument]
- if port then
- modem.transmit(port, 65535, {
- type = "play_request",
- title = song.title,
- start_at = start_time
- })
- print("→ Requested playback for " .. instrument)
- end
- end
- end
- local function main()
- while true do
- print("\n[ Master Menu ]\n1) Download + Load Song\n2) Play Last Song\n3) Exit")
- io.write("> ")
- local choice = read()
- if choice == "1" then
- io.write("Pastebin code: ")
- local code = read()
- local filename = "song.json"
- shell.run("delete " .. filename)
- shell.run("pastebin get " .. code .. " " .. filename)
- local ok, result = pcall(load_song_from_file, filename)
- if ok and result then
- lastSong = result
- send_song_data(lastSong)
- print("[✓] Song distributed.")
- else
- print("[✗] Failed to load or parse song: " .. tostring(result))
- end
- elseif choice == "2" then
- if lastSong then
- request_playback(lastSong)
- else
- print("[!] No song loaded.")
- end
- elseif choice == "3" then
- print("Exiting...")
- break
- else
- print("Invalid option.")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement