-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmStream.lua
94 lines (78 loc) · 2.58 KB
/
mStream.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local config = require("mconfig") or error("You must set up a config file")
local channel = config.channel or error("You must configure a channel")
local station = config.station or "an mStream Station"
local id = os.getComputerID()
local protocol = "CCSMB-5"
local directories = config.directories
local volume = config.volume or 1
local version = 1.2
local modem = peripheral.wrap(config.modem) or peripheral.find("modem") or error("A modem must be attached")
local speaker = peripheral.find("speaker") or error("A speaker must be attached")
local title = "Nothing is playing right now"
local currentFile = ""
local buffer
local dfpwm = require("cc.audio.dfpwm")
local decoder = dfpwm.make_decoder()
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
local function update()
local s = shell.getRunningProgram()
handle = http.get("https://raw.githubusercontent.com/knijn/mStream/main/mStream.lua")
if not handle then
error("Could not download new version, Please update manually.",0)
else
data = handle.readAll()
local f = fs.open(s, "w")
handle.close()
f.write(data)
f.close()
error("Please reopen mStream")
end
end
local h = http.get("https://raw.githubusercontent.com/knijn/mStream/main/data.json")
local latestVersion = textutils.unserialiseJSON(h.readAll()).latestVersion
if latestVersion > version then update() end
local function playSong(file)
currentFile = file
title = fs.getName(unescape(file)):sub(1, -7)
for chunk in io.lines(file, 16 * 1024) do
local buffer = decoder(chunk)
local packet = {buffer = buffer, id = id, station = station, title = title, protocol = protocol}
modem.transmit(channel,channel,packet)
while not speaker.playAudio(buffer,volume) do
os.pullEvent("speaker_audio_empty")
sleep(0)
end
end
end
local function draw()
while true do
local xSize, ySize = term.getSize()
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.write("mStream " .. version .. ": " .. station .. " | " .. channel)
term.setCursorPos(1,ySize - 1)
term.write("Now Playing")
term.setCursorPos(1,ySize)
term.write(title)
sleep(0.2)
end
end
local function player()
while true do
for i,folder in pairs(directories) do
for i,file in pairs(fs.list(folder)) do
playSong(folder .. file)
end
end
end
end
while true do
parallel.waitForAny(draw,player)
end