Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local LONG_PING = 5
- local MEDIUM_PING = 1
- local SHORT_PING = 0.
- local RANDOM_MIN = 60
- local RANDOM_MAX = 120
- local AUDIO_PATH = "audio/transit/"
- local VOLUME = 2.0
- local inst1 = "pling"
- local inst2 = "iron_xylophone"
- local inst3 = "bass"
- local JINGLES = {
- arrival = {
- {duration=0.5, notes={{inst1, VOLUME, 20}, {inst2, VOLUME, 20}, {inst3, VOLUME, 4}}},
- {duration=0.5, notes={{inst1, VOLUME, 16}, {inst2, VOLUME, 16}, {inst3, VOLUME, 8}}},
- {duration=0.5, notes={{inst1, VOLUME, 18}, {inst2, VOLUME, 18}, {inst3, VOLUME, 6}}},
- {duration=0.5, notes={{inst1, VOLUME, 11}, {inst2, VOLUME, 11}, {inst3, VOLUME, 11}}}
- },
- random = {
- {duration=0.4, notes={{inst1, VOLUME, 4}, {inst2, VOLUME, 4}, {inst3, VOLUME, 16}}},
- {duration=0.4, notes={{inst1, VOLUME, 8}, {inst2, VOLUME, 8}, {inst3, VOLUME, 11}}},
- {duration=0.4, notes={{inst1, VOLUME, 11}, {inst2, VOLUME, 11}, {inst3, VOLUME, 8}}}
- }
- }
- function splitWord(target, word)
- local i, j = string.find(target, word)
- if i == nil then return target end
- return string.sub(target, 1, i - 1), string.sub(target, j + 1, -1)
- end
- function getName(name)
- return string.gfind(string.gsub(name, "%s%d", ""), "[%a%s%p]+")()
- end
- function getFilename(name)
- return string.gsub(getName(name), "%s", "_")
- end
- function getNumber(name)
- return tonumber(string.gfind(name, "%d+$")())
- end
- local decoder = require("cc.audio.dfpwm").make_decoder()
- local speaker = peripheral.find("speaker")
- local stations = {}
- for _, name in pairs(peripheral.getNames()) do
- pType = peripheral.getType(name)
- if pType == "Create_Station" then
- local station = peripheral.wrap(name)
- station.fullName = station.getStationName()
- station.name=getName(station.fullName)
- station.platform=getNumber(station.fullName)
- station.present=station.isTrainPresent()
- table.insert(stations, station)
- end
- end
- shell.run("clear")
- local pingTimer = os.startTimer(SHORT_PING)
- local randomTimer = os.startTimer(math.random(RANDOM_MIN, RANDOM_MAX))
- local soundTasks = {}
- function addSoundTasks(newTasks)
- for i=1, #newTasks do
- if newTasks[i].filename ~= nil then
- for chunk in io.lines(AUDIO_PATH..newTasks[i].filename, 16*1024) do
- soundTasks[#soundTasks+1] = {buffer=chunk}
- end
- else
- soundTasks[#soundTasks+1] = newTasks[i]
- end
- end
- end
- local soundTaskIndex = 1
- local soundTaskTimer = nil
- local soundPlaying = false
- while true do
- local event, id = os.pullEvent()
- local startSound = false
- if event == "timer" and id == pingTimer then
- local newTime = LONG_PING
- for i, station in pairs(stations) do
- if station.isTrainPresent() then
- if not station.present then
- stations[i].present = true
- newTime = math.min(newTime, MEDIUM_PING)
- local name = station.getTrainName()
- write(name)
- if not soundPlaying then
- addSoundTasks(JINGLES.arrival)
- end
- local lineFilename = "lines/"..getFilename(name)..".dfpwm"
- if fs.exists(AUDIO_PATH..lineFilename) then
- addSoundTasks({{filename=lineFilename}})
- else
- addSoundTasks({{filename="Train.dfpwm"}})
- end
- if station.hasSchedule() then
- local schedule = station.getSchedule()
- local entries = schedule.entries
- local progress = schedule.progress -- BUG: This value is often incorrect.
- if progress == nil then progress = 1 end
- for i=1, #entries do
- if entries[i].instruction.id == "create:destination" and entries[i].instruction.data.text == station.fullName then
- progress = i
- break
- end
- end
- local stations = {}
- local label = nil
- local index = math.mod(progress, #entries) + 1
- while index ~= progress do
- --write(index..": ")
- local entry = entries[index]
- if entry.instruction.id == "create:destination" then
- stations[#stations + 1] = entry.instruction.data.text
- --print(stations[#stations])
- elseif entry.instruction.id == "create:rename" then
- label = entry.instruction.data.text
- --print("-> "..label)
- end
- index = math.mod(index, #entries) + 1
- end
- if label ~= nil then
- write(" to "..label)
- local name, andName = splitWord(label, " and ")
- local isVia = false
- if andName == nil then
- isVia = true
- name, andName = splitWord(label, " via ")
- end
- addSoundTasks({
- {filename="To.dfpwm"},
- {duration=0.4},
- {filename="stations/"..getFilename(name)..".dfpwm"}
- })
- if andName ~= nil then
- addSoundTasks({{duration=0.2}})
- if isVia then addSoundTasks({{filename="Via.dfpwm"}})
- else addSoundTasks({{filename="And.dfpwm"}}) end
- addSoundTasks({{duration=0.2}, {filename="stations/"..getFilename(andName)..".dfpwm"}})
- end
- end
- end
- addSoundTasks({{filename="ArrivesNowOnTrack.dfpwm"}})
- addSoundTasks({
- {duration=0.2},
- {filename="numbers/"..station.platform..".dfpwm"},
- {duration=1.0}
- })
- startSound = true
- randomTimer = os.startTimer(math.random(RANDOM_MIN, RANDOM_MAX))
- print(" arrived on platform "..station.platform)
- end
- else
- stations[i].present = false
- if station.isTrainEnroute() then
- newTime = math.min(newTime, MEDIUM_PING)
- elseif station.isTrainImminent() then
- newTime = math.min(newTime, SHORT_PING)
- randomTimer = nil
- end
- end
- end
- pingTimer = os.startTimer(newTime)
- end
- if event == "timer" and id == randomTimer then
- local filenames = fs.list(AUDIO_PATH.."random")
- local randomFile = filenames[math.random(#filenames)]
- print("Playing announcment \""..randomFile.."\"...")
- addSoundTasks(JINGLES.random)
- addSoundTasks({{filename="random/"..randomFile}})
- startSound = true
- randomTimer = os.startTimer(math.random(RANDOM_MIN, RANDOM_MAX))
- end
- if (event == "timer" and id == soundTaskTimer) or (event == "speaker_audio_empty") or (startSound and not soundPlaying) then
- soundPlaying = true
- local soundTask = soundTasks[soundTaskIndex]
- if soundTask == nil then
- soundPlaying = false
- soundTasks = {}
- soundTaskIndex = 1
- else
- if soundTask.notes ~= nil then
- for _, note in pairs(soundTask.notes) do
- speaker.playNote(unpack(note))
- end
- elseif soundTask.buffer ~= nil then
- speaker.playAudio(decoder(soundTask.buffer), VOLUME)
- end
- if soundTask.duration ~= nil then
- soundTaskTimer = os.startTimer(soundTask.duration)
- end
- soundTaskIndex = soundTaskIndex + 1
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment