oaky / music-dj public computer
0
0
Web IDE
main
2 commits
oaky Update music-dj
cda7b6d1 2026-09-25
main
Update music-dj2026-09-25
Overview
MIT

Music Dj

Quick Install on your ComputerCraft device

Type this command in the terminal:

hub get music-dj
📖
No README.md in this repository

Documenting your script or package helps other players learn how to install, configure and run it.

main.lua main
--[[
=========================================================================
  Musik-DJ - zentraler Computer, steuert alle Musik-Nodes per Funk.
=========================================================================
  Sendet Play-/Stop-Befehle an alle music_node.lua-Computer in
  Reichweite. Jeder Node laedt die gewuenschte Datei bei Bedarf SELBST
  herunter (siehe music_node.lua) - dieser Computer braucht die
  Audiodateien selbst nicht und schickt nur den (bloßen) Dateinamen.

  Zwei Modi (wie bei Spotify):
    - "Ein Titel"  : der aktuelle Track wird endlos wiederholt (loopt
                      lokal im Node, DJ muss nichts weiter tun).
    - "Zufaellig"   : nach jedem Track wird zufaellig ein anderer aus
                      TRACKS gewaehlt. Dafuer meldet der Node dem DJ per
                      Funk zurueck, wenn ein Track natuerlich zu Ende
                      ist (siehe "finished" in music_node.lua) - der DJ
                      waehlt dann den naechsten zufaelligen Track.

  TRACKS unten an deine tatsaechlich vorhandenen Dateien anpassen -
  Dateiname muss exakt zu dem passen, was unter BASE_URL in
  music_node.lua abrufbar ist.

  Starten mit: music_dj
=========================================================================
]]

local PROTOCOL = "music_control"
local VOLUME_STEP = 0.1

-- Wie weit in der Zukunft der gemeinsame Startzeitpunkt liegt (ms).
-- Gibt allen Nodes Zeit, ihre HTTP-Verbindung aufzubauen und den ersten
-- Chunk zu laden, BEVOR wirklich Ton rauskommt - das gleicht
-- unterschiedlich schnelle Verbindungen aus und verhindert den
-- Sekunden-Versatz zwischen mehreren Speakern.
local START_DELAY_MS = 2000

-- Bekannte Tracks: Anzeige-Name -> blosser Dateiname (kein Pfad - der
-- Node haengt "sounds/" und die Download-URL selbst an).
local TRACKS = {
  { label = "Bounce", file = "bounce.dfpwm" },
  { label = "Lava Chicken", file = "lava_chicken.dfpwm" },
  { label = "Tears", file = "tears.dfpwm" },
  { label = "Precipice", file = "precipice.dfpwm" },
  { label = "Relic", file = "relic.dfpwm" },
  { label = "5", file = "5.dfpwm" },
  { label = "Creator (Music Box)", file = "creator_music_box.dfpwm" },
  { label = "Creator", file = "creator.dfpwm" },
  { label = "Otherside", file = "otherside.dfpwm" },
  { label = "Pigstep", file = "pigstep.dfpwm" },
}

------------------------------------------------------------------------
-- Reine Logik (ohne Peripheral-Zugriff - testbar)
------------------------------------------------------------------------

local function clampVolume(v)
  if v < 0 then return 0 end
  if v > 3 then return 3 end
  return v
end

-- Welcher Button (aus 'rects') wurde bei (x,y) getroffen? nil wenn keiner.
local function hitTest(rects, x, y)
  for _, r in ipairs(rects) do
    if y == r.y and x >= r.x1 and x <= r.x2 then
      return r.action
    end
  end
  return nil
end

-- Waehlt einen zufaelligen Index aus 1..count, der (falls moeglich)
-- NICHT 'excludeIndex' ist - verhindert, dass im Zufalls-Modus zweimal
-- hintereinander derselbe Track drankommt. Braucht eine Zufallsfunktion
-- von aussen (math.random oder ein Mock), damit es testbar bleibt.
local function pickRandomIndex(count, excludeIndex, randomFn)
  if count <= 1 then return 1 end
  local idx
  repeat
    idx = randomFn(1, count)
  until idx ~= excludeIndex
  return idx
end

------------------------------------------------------------------------
-- Peripherals
------------------------------------------------------------------------

local modem = peripheral.find("modem", function(name, wrapped) return wrapped.isWireless() end)
if not modem then
  error("Kein DRAHTLOSES Modem gefunden! Wireless- oder Ender-Modem an diesen Computer anschliessen.")
end
rednet.open(peripheral.getName(modem))
math.randomseed(os.epoch("utc"))

------------------------------------------------------------------------
-- Zustand
------------------------------------------------------------------------

local selectedIndex = 1
local volume = 0.5       -- leise als Standard
local looping = false
local mode = "repeat_one" -- "repeat_one" | "shuffle"

local function currentTrack()
  return TRACKS[selectedIndex]
end

-- Im "Ein Titel"-Modus loopt der Node selbst (loop=true). Im
-- "Zufaellig"-Modus loopt der Node NICHT (loop=false) - stattdessen
-- meldet er sich beim Fertigwerden, und der DJ waehlt den naechsten
-- Track (siehe handleTrackFinished).
local function broadcastPlay()
  local startAt = os.epoch("utc") + START_DELAY_MS
  local loopFlag = (mode == "repeat_one")
  rednet.broadcast({ type = "play", file = currentTrack().file, volume = volume, loop = loopFlag, startAt = startAt }, PROTOCOL)
end

local function broadcastStop()
  rednet.broadcast({ type = "stop" }, PROTOCOL)
end

local function playRandomTrack()
  selectedIndex = pickRandomIndex(#TRACKS, selectedIndex, math.random)
  broadcastPlay()
end

-- Wird aufgerufen, wenn ein Node meldet, dass ein Track natuerlich zu
-- Ende ist (nur im Zufalls-Modus relevant). Der Dateiname-Abgleich
-- filtert veraltete Meldungen heraus (z.B. von einem Track, den der
-- Nutzer laengst manuell gewechselt/gestoppt hat).
local function handleTrackFinished(file)
  if looping and mode == "shuffle" and file == currentTrack().file then
    playRandomTrack()
    return true
  end
  return false
end

------------------------------------------------------------------------
-- Anzeige
------------------------------------------------------------------------

local buttonRects = {}

local function render()
  local w = select(1, term.getSize())
  term.setBackgroundColor(colors.black)
  term.clear()
  term.setTextColor(colors.white)

  local title = "MUSIK-DJ"
  term.setCursorPos(math.max(1, math.floor((w - #title) / 2) + 1), 1)
  term.write(title)

  term.setCursorPos(2, 3)
  term.write(("Track: %s"):format(currentTrack().label))

  term.setCursorPos(2, 4)
  term.write(("Lautstaerke: %.1f"):format(volume))

  term.setCursorPos(2, 5)
  term.write(("Modus: %s"):format(mode == "shuffle" and "Zufaellig" or "Ein Titel"))

  term.setTextColor(looping and colors.lime or colors.gray)
  term.setCursorPos(2, 6)
  term.write(looping and "Status: LAEUFT" or "Status: gestoppt")
  term.setTextColor(colors.white)

  buttonRects = {}
  local y = 8
  local function btn(label, action)
    term.setBackgroundColor(colors.gray)
    local text = " " .. label .. " "
    term.setCursorPos(2, y)
    term.write(text)
    buttonRects[#buttonRects + 1] = { x1 = 2, x2 = 1 + #text, y = y, action = action }
    y = y + 2
  end

  btn(looping and "STOP" or "PLAY", "toggle")
  btn("Lautstaerke -", "vol_down")
  btn("Lautstaerke +", "vol_up")
  btn(mode == "shuffle" and "Modus: Ein Titel" or "Modus: Zufaellig", "toggle_mode")
  if #TRACKS > 1 then
    btn(mode == "shuffle" and "Naechster (Zufall)" or "Naechster Track", "next_track")
  end
end

local function handleClick(x, y)
  local action = hitTest(buttonRects, x, y)
  if action == "toggle" then
    if looping then
      looping = false
      broadcastStop()
    else
      looping = true
      if mode == "shuffle" then
        playRandomTrack()
      else
        broadcastPlay()
      end
    end
  elseif action == "vol_down" then
    volume = clampVolume(volume - VOLUME_STEP)
    if looping then broadcastPlay() end
  elseif action == "vol_up" then
    volume = clampVolume(volume + VOLUME_STEP)
    if looping then broadcastPlay() end
  elseif action == "toggle_mode" then
    mode = (mode == "shuffle") and "repeat_one" or "shuffle"
    if looping then
      -- Modus-Wechsel greift sofort - im neuen Modus neu starten.
      if mode == "shuffle" then playRandomTrack() else broadcastPlay() end
    end
  elseif action == "next_track" then
    if mode == "shuffle" then
      if looping then playRandomTrack() end
    else
      selectedIndex = (selectedIndex % #TRACKS) + 1
      if looping then broadcastPlay() end
    end
  end
  if action then render() end
end

------------------------------------------------------------------------
-- Hauptschleife
------------------------------------------------------------------------

local function guiLoop()
  while true do
    local _, _, x, y = os.pullEvent("mouse_click")
    handleClick(x, y)
  end
end

local function finishedListener()
  while true do
    local _, message = rednet.receive(PROTOCOL)
    if type(message) == "table" and message.type == "finished" and message.file then
      if handleTrackFinished(message.file) then render() end
    end
  end
end

render()
parallel.waitForAny(guiLoop, finishedListener)

Usage

Once installed with hub get music-dj, run:

music-dj
Careful what you run: This script was written by oaky, not by us. It runs with full access to the computer you install it on — it can read and delete files there. Read it before you run it.

About

No description provided.

MIT License
★ 0 stars
2 in-game downloads

Hardware Targets

computer

Owner