上传文件至 /
This commit is contained in:
329
music168.lua
Normal file
329
music168.lua
Normal file
@@ -0,0 +1,329 @@
|
||||
-----------------------------------------------------------------系统启动阶段-------------------------------------------------------------------------------------------------
|
||||
|
||||
--*获取程序所在目录
|
||||
local mypath = "/"..fs.getDir(shell.getRunningProgram())
|
||||
|
||||
if not fs.exists(mypath.."/lib/basalt.lua") then shell.run("wget http://alist.liulikeji.cn/d/HFS/Installer/lib/basalt.lua lib/basalt.lua") end
|
||||
if not fs.exists(mypath.."/speaker.lua") then shell.run("wget http://alist.liulikeji.cn/d/HFS/music168/speaker.lua") end
|
||||
|
||||
--*GUI库导入
|
||||
basalt = require(mypath.."/lib/basalt")
|
||||
--*初始化GUI框架
|
||||
local mainf = basalt.createFrame()
|
||||
main = {
|
||||
mainf:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h"):setBackground(colors.red),
|
||||
}
|
||||
_G.Playprint = false
|
||||
_G.Playopen =false
|
||||
|
||||
--*GUI框架配置表
|
||||
local sub = {
|
||||
["UI"] = {
|
||||
main[1]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h -2"):setBackground(colors.red),
|
||||
main[1]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h -2"):setBackground(colors.white):hide(),
|
||||
main[1]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h -2"):setBackground(colors.white):hide(),
|
||||
main[1]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h -2"):setBackground(colors.red):hide(),
|
||||
main[1]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h -2"):setBackground(colors.white):hide(),
|
||||
},
|
||||
["menu"] ={
|
||||
main[1]:addFrame():setPosition(1, "parent.h"):setSize("parent.w", 1):setBackground(colors.lightGray),
|
||||
},
|
||||
["BF"] = {
|
||||
mainf:addFrame():setPosition(1, "parent.h + 1"):setSize("parent.w", "parent.h"):setBackground(colors.red),
|
||||
main[1]:addFrame():setPosition(1, "parent.h - 1"):setSize("parent.w", 1):setBackground(colors.lightGray):hide(),
|
||||
},
|
||||
["play_table"] = {
|
||||
mainf:addFrame():setPosition(2, "parent.h + 1"):setSize("parent.w-2", 13):setBackground(colors.orange),
|
||||
}
|
||||
}
|
||||
|
||||
--创建动画
|
||||
play_Gui_UP = mainf:addAnimation():setObject(sub["BF"][1]):move(1,1,0.3)
|
||||
play_Gui_DO = mainf:addAnimation():setObject(sub["BF"][1]):move(1,mainf:getHeight()+1,1)
|
||||
play_table_Gui_UP = mainf:addAnimation():setObject(sub["play_table"][1]):move(2,mainf:getHeight()-12,0.3)
|
||||
play_table_Gui_DO = mainf:addAnimation():setObject(sub["play_table"][1]):move(2,mainf:getHeight()+1,1)
|
||||
--play_Gui_UP:play()
|
||||
--main[1]:hide()
|
||||
--main[1]:addAnimation():setObject(sub["BF"][1]):move(1,"parent.h+1",1.5):play()
|
||||
--创建播放界面
|
||||
play_name = "NO Music"
|
||||
play_id = "NO Music"
|
||||
play_Gui = {
|
||||
sub["BF"][1]:addButton():setPosition(1,1):setSize(3, 1):setText("V"):onClick(function() play_Gui_DO:play() play_GUI_state=false main[1]:enable() end):setBackground(colors.red):setForeground(colors.white),
|
||||
sub["BF"][1]:addLabel():setText("NO Music"):setPosition(sub["BF"][1]:getWidth()/2 - #play_name/2,1):setBackground(colors.red):setForeground(colors.white),
|
||||
sub["BF"][1]:addLabel():setText("NO Music"):setPosition(sub["BF"][1]:getWidth()/2 - #play_id/2,2):setBackground(colors.red):setForeground(colors.white),
|
||||
sub["BF"][1]:addLabel():setText(" "):setPosition(3,4):setSize("parent.w-4", "parent.h-10"):setBackground(colors.white):setForeground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition(3,"parent.h-5"):setSize(1, 1):setText("\3"):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition(8,"parent.h-5"):setSize(1, 1):setText("\25"):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w/2","parent.h-5"):setSize(2, 1):setText("+-"):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w-3","parent.h-5"):setSize(1, 1):setText("@"):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w-8","parent.h-5"):setSize(1, 1):setText("E"):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addProgressbar():setPosition(3, "parent.h - 4"):setSize("parent.w - 4", 1):setProgressBar(colors.red, "=", colors.white):setBackground(colors.red):setBackgroundSymbol("-"):setForeground(colors.white),
|
||||
sub["BF"][1]:addLabel():setText("00:00"):setPosition("3", "parent.h - 3"):setSize(5, 1):setForeground(colors.white),
|
||||
sub["BF"][1]:addLabel():setText("00:00"):setPosition("parent.w - 6", "parent.h - 3"):setSize(5, 1):setForeground(colors.white),
|
||||
sub["BF"][1]:addButton():setPosition(3, "parent.h - 2"):setSize(3, 1):setText("=O="):onClick(function() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w /2 - 4","parent.h - 2"):setSize(2, 1):setText("|\17"):onClick(function() play_set_1() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w / 2 ", "parent.h - 2"):setSize(2, 1):setText("I>"):onClick(function() if play_data_table["play"] then _G.Playstop = true play_data_table["play"]=false else play_data_table["play"]=true end end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w / 2 +4", "parent.h - 2"):setSize(2, 1):setText("\16|"):onClick(function() play_set_0() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addButton():setPosition("parent.w - 4", "parent.h - 2"):setSize(3, 1):setText("=T="):onClick(function() play_table_Gui_UP:play() main[1]:disable() sub["BF"][1]:disable() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["BF"][1]:addSlider():setPosition(3, "parent.h - 4"):setSize("parent.w - 4", 1):setMaxValue(100):setBackground(colors.red):setForeground(colors.white),--:setBackgroundSymbol("\x8c"):setSymbol(" "),
|
||||
}
|
||||
--创建播放UI
|
||||
play_column_Gui = {
|
||||
sub["BF"][2]:addLabel():setText(""):setPosition(1,1):setSize("parent.w-7",1):setBackground(colors.lightGray):setForeground(colors.white),
|
||||
sub["BF"][2]:addButton():setPosition("parent.w -4 ", 1):setSize(2, 1):setText("I>"):onClick(function() if play_data_table["play"] then _G.Playstop = true play_data_table["play"]=false else play_data_table["play"]=true end end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["BF"][2]:addButton():setPosition("parent.w-1", 1):setSize(1, 1):setText("T"):onClick(function() play_table_Gui_UP:play() main[1]:disable() end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["BF"][2]:addButton():setPosition(1, 1):setSize("parent.w -5", 1):setText(""):onClick(function() play_Gui_UP:play() play_GUI_state=true main[1]:disable() end):setBackground(colors.lights),
|
||||
}
|
||||
|
||||
play_table_Gui = {
|
||||
sub["play_table"][1]:addButton():setPosition("parent.w-3",1):setSize(3, 1):setText("V"):onClick(function() if not play_GUI_state then main[1]:enable() end sub["BF"][1]:enable() play_table_Gui_DO:play() end):setBackground(colors.no):setForeground(colors.white),
|
||||
sub["play_table"][1]:addLabel():setText("PlyaTable"):setPosition(1,1):setForeground(colors.white),
|
||||
sub["play_table"][1]:addList():setPosition(2,3):setSize("parent.w-2", "parent.h-2"):setScrollable(true),
|
||||
}
|
||||
|
||||
--创建菜单栏
|
||||
menuBut = {
|
||||
sub["menu"][1]:addButton():setPosition(3,1):setSize(3, 1):setText("{Q}"):onClick(function() for index, value in ipairs(menuBut) do value:setBackground(colors.lightGray) end menuBut[1]:setBackground(colors.red) for index, value in ipairs(sub["UI"]) do value:hide() end sub["UI"][1]:show() end):setForeground(colors.white):setBackground(colors.red),
|
||||
sub["menu"][1]:addButton():setPosition(8,1):setSize(3, 1):setText("{T}"):onClick(function() for index, value in ipairs(menuBut) do value:setBackground(colors.lightGray) end menuBut[2]:setBackground(colors.red) for index, value in ipairs(sub["UI"]) do value:hide() end sub["UI"][2]:show() end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["menu"][1]:addButton():setPosition(12,1):setSize(4, 1):setText("{PH}"):onClick(function() for index, value in ipairs(menuBut) do value:setBackground(colors.lightGray) end menuBut[3]:setBackground(colors.red) for index, value in ipairs(sub["UI"]) do value:hide() end sub["UI"][3]:show() end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["menu"][1]:addButton():setPosition(17,1):setSize(3, 1):setText("{G}"):onClick(function() for index, value in ipairs(menuBut) do value:setBackground(colors.lightGray) end menuBut[4]:setBackground(colors.red) for index, value in ipairs(sub["UI"]) do value:hide() end sub["UI"][4]:show() end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["menu"][1]:addButton():setPosition(22,1):setSize(3, 1):setText("{Z}"):onClick(function() for index, value in ipairs(menuBut) do value:setBackground(colors.lightGray) end menuBut[5]:setBackground(colors.red) for index, value in ipairs(sub["UI"]) do value:hide() end sub["UI"][5]:show() end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
}
|
||||
-----------------------------------------------------------------DATA---------------------------------------------------------------------------------------------------------
|
||||
play_data_table = { ["music"] = {} , ["play"] = false ,["play_table"] = {}, ["play_table_index"] = 0, ["mode"] = "" , }
|
||||
_G.Playopen = false
|
||||
-----------------------------------------------------------------模块---------------------------------------------------------------------------------------------------------
|
||||
--多线程
|
||||
thread_table = {}
|
||||
function AddThread(funct)
|
||||
thread1 = mainf:addThread()
|
||||
thread1:start(funct)
|
||||
table.insert(thread_table,thread1)
|
||||
return #thread_table
|
||||
end
|
||||
|
||||
--音乐+
|
||||
function play_set_1()
|
||||
_G.music168_playopen = false
|
||||
_G.getPlay = 0
|
||||
_G.Playopen = false
|
||||
_G.Playstop = false
|
||||
table_index = play_table_Gui[3]:getItemIndex()
|
||||
if table_index <= 1 then
|
||||
play_table_Gui[3]:selectItem(play_table_Gui[3]:getItemCount())
|
||||
else
|
||||
play_table_Gui[3]:selectItem(table_index-1)
|
||||
end
|
||||
end
|
||||
--音乐-
|
||||
function play_set_0()
|
||||
_G.music168_playopen = false
|
||||
_G.getPlay = 0
|
||||
_G.Playopen = false
|
||||
_G.Playstop = false
|
||||
table_index = play_table_Gui[3]:getItemIndex()
|
||||
if table_index >= play_table_Gui[3]:getItemCount() then
|
||||
play_table_Gui[3]:selectItem(1)
|
||||
else
|
||||
play_table_Gui[3]:selectItem(table_index+1)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--获取URL
|
||||
function GetmusicUrl(music_id)
|
||||
while true do
|
||||
local http = http.post(server_url.."api/song/url",textutils.serialiseJSON({["id"]=music_id}))
|
||||
if http then
|
||||
json_str = http.readAll()
|
||||
local table = textutils.unserialiseJSON(json_str)
|
||||
if table["data"][1]["url"] then
|
||||
return(table["data"][1]["url"])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--dfpwm转码
|
||||
|
||||
--播放
|
||||
function playmusic(music_name,music_id,play_table,index)
|
||||
_G.getPlay = 0
|
||||
_G.Playopen = false
|
||||
_G.Playstop = false
|
||||
play_Gui[2]:setText(music_name):setPosition(sub["BF"][1]:getWidth()/2 +1 - #music_name/2,1)
|
||||
play_Gui[3]:setText(music_id):setPosition(sub["BF"][1]:getWidth()/2 +1 - #tostring(music_id)/2,2)
|
||||
play_column_Gui[1]:setText(music_name.." | "..tostring(music_id))
|
||||
play_data_table["music"] = { ["music_id"] = music_id, ["music_name"] = music_name }
|
||||
play_data_table["play_table"] = play_table
|
||||
play_data_table["play_table_index"] = index
|
||||
play_data_table["play"] = true
|
||||
|
||||
play_table_Gui[3]:clear()
|
||||
for index, value in ipairs(play_table) do
|
||||
play_table_Gui[3]:addItem(value["name"].." | "..tostring(value["id"]))
|
||||
end
|
||||
play_table_Gui[3]:selectItem(index)
|
||||
_G.music168_music_id = music_id
|
||||
|
||||
_G.music168_playopen = true
|
||||
--basalt.debug("true")
|
||||
--play_thread_id = AddThread(function ()
|
||||
--
|
||||
|
||||
|
||||
--end)
|
||||
|
||||
end
|
||||
|
||||
--搜索
|
||||
server_url = "http://music168.liulikeji.cn:15843/"
|
||||
function Search(input_str,GUI_in,api)
|
||||
Search_table = {}
|
||||
while true do
|
||||
kg_a=false
|
||||
if api=="search" then
|
||||
http1 = http.post(server_url.."api/search",textutils.serialiseJSON({["value"]=input_str}))
|
||||
json_str = http1.readAll()
|
||||
table_get = textutils.unserialiseJSON(json_str)
|
||||
if table_get["result"]["songCount"] ~= 0 then kg_a=true end
|
||||
elseif api=="playlist" then
|
||||
http1 = http.post(server_url.."api/playlist/detail",textutils.serialiseJSON({["id"]=input_str}))
|
||||
json_str = http1.readAll()
|
||||
table_get = textutils.unserialiseJSON(json_str)
|
||||
if table_get["code"] ~= 404 then kg_a=true end
|
||||
end
|
||||
|
||||
if http1 then
|
||||
if kg_a then
|
||||
if api=="search" then
|
||||
for index, value in ipairs(table_get["result"]["songs"]) do
|
||||
out_table = {["id"] = value["id"],["name"]=value["name"],["artists_id"]=value["artists"][1]["id"],["artists_name"]=value["artists"][1]["name"]}
|
||||
Search_table[index]=out_table
|
||||
end
|
||||
elseif api=="playlist" then
|
||||
for index, value in ipairs(table_get["playlist"]["tracks"]) do
|
||||
out_table = {["id"] = value["id"],["name"]=value["name"],["artists_id"]=value["ar"][1]["id"],["artists_name"]=value["ar"][1]["name"]}
|
||||
Search_table[index]=out_table
|
||||
end
|
||||
end
|
||||
|
||||
a=2
|
||||
if play_lib_F then play_lib_F:remove() end
|
||||
play_lib_F = GUI_in[3]:addFrame():setPosition(1, 1):setSize("parent.w", "parent.h"):setBackground(colors.white):setScrollable()
|
||||
|
||||
for index, value in ipairs(Search_table) do
|
||||
frame = play_lib_F:addFrame():setPosition(2, a):setSize("parent.w-2", 3):setBackground(colors.lightBlue):onClick(function() if play_data_table["play"] then shell.run(mypath.."/speaker.lua stop") if _G.Playopen then end _G.music168_playopen = false play_data_table["play"]=false end play_Gui_UP:play() play_GUI_state = true main[1]:disable() playmusic(value["name"],value["id"],Search_table,index) end)
|
||||
frame:addLabel():setText(value["name"]):setPosition(1, 1)
|
||||
frame:addLabel():setText("id: "..value["id"]):setPosition(1, 2)
|
||||
frame:addLabel():setText("artists: "..value["artists_name"]):setPosition(1, 3)
|
||||
a=a+4
|
||||
end
|
||||
break;
|
||||
else
|
||||
frame = GUI_in[3]:addFrame():setPosition(2, 2):setSize("parent.w-2", 3):setBackground(colors.lightBlue)
|
||||
frame:addLabel():setText("[songCount] == 0"):setPosition(1, 1)
|
||||
break;
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------渲染界面阶段-------------------------------------------------------------------------------------------------
|
||||
GUI = {
|
||||
{
|
||||
sub["UI"][1]:addInput():setPosition(2,1):setSize("parent.w-3", 1):setForeground(colors.gray):setBackground(colors.lightGray),
|
||||
sub["UI"][1]:addButton():setPosition("parent.w-1",1):setSize(1, 1):setText("Q"):onClick(function() Search(GUI[1][1]:getValue(),GUI[1],"search") end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["UI"][1]:addFrame():setPosition(1, 3):setSize("parent.w", "parent.h -3"):setBackground(colors.white)
|
||||
},
|
||||
{
|
||||
sub["UI"][4]:addInput():setPosition(2,1):setSize("parent.w-3", 1):setForeground(colors.gray):setBackground(colors.lightGray),
|
||||
sub["UI"][4]:addButton():setPosition("parent.w-1",1):setSize(1, 1):setText("Q"):onClick(function() Search(GUI[1][1]:getValue(),GUI[2],"playlist") end):setForeground(colors.white):setBackground(colors.lightGray),
|
||||
sub["UI"][4]:addFrame():setPosition(1, 3):setSize("parent.w", "parent.h -3"):setBackground(colors.white)
|
||||
},
|
||||
}
|
||||
|
||||
_G.getPlay = nil
|
||||
|
||||
function thread2()
|
||||
while true do
|
||||
--basalt.debug(_G.music168_playopen)
|
||||
local screenWidth, _ = term.getSize()
|
||||
if play_Gui[18]:getIndex() ~=1 then _G.setPlay = (play_Gui[18]:getIndex() / (screenWidth-2)) * 100 play_Gui[18]:setIndex(1) end
|
||||
sleep(0.1)
|
||||
--
|
||||
if _G.getPlay ~= nil then
|
||||
play_Gui[10]:setProgress(_G.getPlay*100)
|
||||
end
|
||||
if play_data_table["play"]== true then
|
||||
_G.Playstop = false
|
||||
play_Gui[15]:setText("II")
|
||||
play_column_Gui[2]:setText("II")
|
||||
sub["BF"][2]:show()
|
||||
else
|
||||
play_Gui[15]:setText("I>")
|
||||
play_column_Gui[2]:setText("I>")
|
||||
--play_Gui[11]:setText("00:00")
|
||||
_G.Playstop = true
|
||||
end
|
||||
if play_data_table["play_table_index"] ~= 0 then
|
||||
if play_data_table["play_table_index"] ~= play_table_Gui[3]:getItemIndex() then
|
||||
index = play_table_Gui[3]:getItemIndex()
|
||||
if play_data_table["play"] then
|
||||
|
||||
shell.run(mypath.."/speaker.lua stop")
|
||||
if _G.Playopen then
|
||||
|
||||
end
|
||||
play_data_table["play"]=false
|
||||
end
|
||||
_G.music168_playopen = false
|
||||
sleep(0.1)
|
||||
playmusic(play_data_table["play_table"][index]["name"],play_data_table["play_table"][index]["id"],play_data_table["play_table"],index)
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function paste()
|
||||
while true do
|
||||
local event, text = os.pullEvent("paste")
|
||||
GUI[1][1]:setValue(text)
|
||||
GUI[2][1]:setValue(text)
|
||||
end
|
||||
end
|
||||
|
||||
function speakerp()
|
||||
function speaker_thread()
|
||||
|
||||
|
||||
--play_time_thread_id = AddThread(function() local time_f = os.date("%M%S") while true do while _G.Playopen do time_f = os.date("%M%S") sleep(0.1) end time_F = os.date("%M%S") play_Gui[11]:setText(string.format("%02d", os.date("*t",time_F-time_f).min or 00)..":"..string.format("%02d", os.date("*t",time_F-time_f).sec or 00) or "00:00") sleep(1) end end)
|
||||
--basalt.debug(mypath)
|
||||
--shell.run(mypath.."/speaker play "..dfpwmURL.readAll())
|
||||
if _G.music168_music_id then
|
||||
--basalt.debug(music168_music_id)
|
||||
_G.Playopen = true
|
||||
dfpwmURL = http.post("http://gmapi.liulikeji.cn:15842/dfpwm",textutils.serialiseJSON({ ["url"] = GetmusicUrl(_G.music168_music_id) } ))
|
||||
shell.run(mypath.."/speaker play "..dfpwmURL.readAll())
|
||||
play_set_0()
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
function while_thread() while _G.music168_playopen do sleep(0.01) end end
|
||||
|
||||
while true do
|
||||
|
||||
if _G.music168_playopen then parallel.waitForAny(speaker_thread, while_thread) end
|
||||
sleep(0.01)
|
||||
end
|
||||
end
|
||||
|
||||
_G.music168_playopen = false
|
||||
-----------------------------------------------------------------启动循环渲染器-----------------------------------------------------------------------------------------------
|
||||
parallel.waitForAll(basalt.autoUpdate, thread2, paste, speakerp)
|
||||
-----------------------------------------------------------------以下结束-----------------------------------------------------------------------------------------------------
|
||||
254
speaker.lua
Normal file
254
speaker.lua
Normal file
@@ -0,0 +1,254 @@
|
||||
if _G.Playprint == nil then _G.Playprint = true end
|
||||
local function get_speakers(name)
|
||||
if name then
|
||||
local speaker = peripheral.wrap(name)
|
||||
if speaker == nil then
|
||||
error(("Speaker %q does not exist"):format(name), 0)
|
||||
return
|
||||
elseif not peripheral.hasType(name, "speaker") then
|
||||
error(("%q is not a speaker"):format(name), 0)
|
||||
end
|
||||
|
||||
return { speaker }
|
||||
else
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
if #speakers == 0 then
|
||||
error("No speakers attached", 0)
|
||||
end
|
||||
return speakers
|
||||
end
|
||||
end
|
||||
function spekerStop()
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for _, speaker in pairs(speakers) do
|
||||
speaker.stop()
|
||||
end
|
||||
end
|
||||
|
||||
function speakerPlay(buffer)
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for _, speaker in pairs(speakers) do
|
||||
a = speaker.playAudio(buffer)
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
|
||||
local function pcm_decoder(chunk)
|
||||
local buffer = {}
|
||||
for i = 1, #chunk do
|
||||
buffer[i] = chunk:byte(i) - 128
|
||||
end
|
||||
return buffer
|
||||
end
|
||||
|
||||
|
||||
function displayProgressBar(percent)
|
||||
term.setCursorPos(1, 1)
|
||||
local screenWidth, _ = term.getSize() -- 获取终端的宽度和高度
|
||||
local barLength = math.floor(screenWidth - 8) -- 进度条长度为屏幕宽度减去固定长度(用于百分比显示)
|
||||
|
||||
local numBars = math.floor(percent / (100 / barLength))
|
||||
|
||||
-- 构建进度条字符串
|
||||
local progressBar = "["
|
||||
for i = 1, barLength do
|
||||
if i <= numBars then
|
||||
progressBar = progressBar .. "="
|
||||
else
|
||||
progressBar = progressBar .. " "
|
||||
end
|
||||
end
|
||||
progressBar = progressBar .. "] " .. math.floor(percent) .. "%" -- 百分比不显示小数点
|
||||
|
||||
-- 清空屏幕并输出进度条到屏幕顶部
|
||||
|
||||
term.setCursorPos(1, 3)
|
||||
print(progressBar)
|
||||
end
|
||||
|
||||
-- 测试函数
|
||||
|
||||
|
||||
|
||||
|
||||
local cmd = ...
|
||||
if cmd == "stop" then
|
||||
_G.Playopen = false
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for _, speaker in pairs(speakers) do
|
||||
speaker.stop()
|
||||
end
|
||||
spekerStop()
|
||||
elseif cmd == "play" then
|
||||
local _, file, type = ...
|
||||
|
||||
local handle, err
|
||||
if http and file:match("^https?://") then
|
||||
if type == "mp3" then
|
||||
if _G.Playprint then print("mp3 > dfpwm.....") end
|
||||
local json = textutils.serialiseJSON({ ["url"] = file } )
|
||||
handle, err = http.get{ url = http.post("http://gmapi.liulikeji.cn:15842/dfpwm",json).readAll(), binary = true }
|
||||
else
|
||||
handle, err = http.get{ url = file, binary = true }
|
||||
end
|
||||
else
|
||||
handle, err = fs.open(file, "rb")
|
||||
end
|
||||
|
||||
if not handle then
|
||||
error(err, 0)
|
||||
end
|
||||
|
||||
local start = handle.read(4)
|
||||
local pcm = false
|
||||
local size = 16 * 1024 - 4
|
||||
if start == "RIFF" then
|
||||
handle.read(4)
|
||||
if handle.read(8) ~= "WAVEfmt " then
|
||||
handle.close()
|
||||
error("Could not play audio: Unsupported WAV file", 0)
|
||||
end
|
||||
|
||||
local fmtsize = ("<I4"):unpack(handle.read(4))
|
||||
local fmt = handle.read(fmtsize)
|
||||
local format, channels, rate, _, _, bits = ("<I2I2I4I4I2I2"):unpack(fmt)
|
||||
if not ((format == 1 and bits == 8) or (format == 0xFFFE and bits == 1)) then
|
||||
handle.close()
|
||||
error("Could not play audio: Unsupported WAV file", 0)
|
||||
end
|
||||
if channels ~= 1 or rate ~= 48000 then
|
||||
end
|
||||
if format == 0xFFFE then
|
||||
local guid = fmt:sub(25)
|
||||
if guid ~= "\x3A\xC1\xFA\x38\x81\x1D\x43\x61\xA4\x0D\xCE\x53\xCA\x60\x7C\xD1" then -- DFPWM format GUID
|
||||
handle.close()
|
||||
error("Could not play audio: Unsupported WAV file", 0)
|
||||
end
|
||||
size = size + 4
|
||||
else
|
||||
pcm = true
|
||||
size = 16 * 1024 * 8
|
||||
end
|
||||
|
||||
repeat
|
||||
local chunk = handle.read(4)
|
||||
if chunk == nil then
|
||||
handle.close()
|
||||
error("Could not play audio: Invalid WAV file", 0)
|
||||
elseif chunk ~= "data" then -- Ignore extra chunks
|
||||
local size = ("<I4"):unpack(handle.read(4))
|
||||
handle.read(size)
|
||||
end
|
||||
until chunk == "data"
|
||||
|
||||
handle.read(4)
|
||||
start = nil
|
||||
end
|
||||
|
||||
|
||||
local decoder = pcm and pcm_decoder or require "cc.audio.dfpwm".make_decoder()
|
||||
local b1=true
|
||||
achunk_1 = handle.readAll()
|
||||
local achunk_s = achunk_1
|
||||
local achunk_K = #achunk_1
|
||||
whilecs_1 = 0
|
||||
_G.setPlay = nil
|
||||
|
||||
function play1()
|
||||
_G.Playopen = true
|
||||
|
||||
_G.Playstop = false
|
||||
c1=true
|
||||
|
||||
while _G.Playopen do
|
||||
|
||||
if _G.Playstop then
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for _, speaker in pairs(speakers) do
|
||||
speaker.stop()
|
||||
end
|
||||
|
||||
if _G.Playprint then print("play stop....") end
|
||||
while _G.Playstop do sleep(0.1) end
|
||||
b1=true
|
||||
c1=false
|
||||
end
|
||||
|
||||
--local chunk = handle.read(size)
|
||||
if c1 then
|
||||
chunk = achunk_1:sub(0,size)
|
||||
achunk_1 = achunk_1:sub(size+1,-1)
|
||||
|
||||
local achunk_ZK = achunk_K / size
|
||||
|
||||
if _G.setPlay ~= nil then
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for _, speaker in pairs(speakers) do
|
||||
speaker.stop()
|
||||
end
|
||||
whilecs_1 = math.floor((setPlay / 100) * achunk_ZK)
|
||||
|
||||
achunk_1 = achunk_s:sub(size*whilecs_1,-1)
|
||||
chunk = achunk_1:sub(0,size)
|
||||
achunk_1 = achunk_1:sub(size+1,-1)
|
||||
_G.setPlay = nil
|
||||
b1 = true
|
||||
end
|
||||
|
||||
local f = whilecs_1 / achunk_ZK
|
||||
_G.getPlay = f
|
||||
if _G.Playprint then
|
||||
term.clear()
|
||||
displayProgressBar(f*100)
|
||||
print(whilecs_1)
|
||||
print(achunk_ZK)
|
||||
print(f)
|
||||
end
|
||||
|
||||
whilecs_1 = whilecs_1+1
|
||||
|
||||
if f >= 1 then break end
|
||||
|
||||
if chunk == nil then break end
|
||||
|
||||
if start then
|
||||
chunk, start = start .. chunk, nil
|
||||
size = size + 4
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
local buffer = decoder(chunk)
|
||||
if b1 then
|
||||
speakerPlay(buffer)
|
||||
b1=false
|
||||
c1 = true
|
||||
|
||||
else
|
||||
|
||||
while setPlay==nil and _G.Playstop==false do
|
||||
local speakers = { peripheral.find("speaker") }
|
||||
for i,speaker in pairs(speakers) do
|
||||
os.pullEvent("speaker_audio_empty")
|
||||
end
|
||||
speakerPlay(buffer)
|
||||
break
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
_G.getPlay=nil
|
||||
end
|
||||
|
||||
|
||||
play1()
|
||||
|
||||
|
||||
spekerStop()
|
||||
|
||||
handle.close()
|
||||
else
|
||||
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
||||
end
|
||||
Reference in New Issue
Block a user