-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic-v10.lua
182 lines (169 loc) · 5.48 KB
/
music-v10.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local dfpwm = require("cc.audio.dfpwm")
local speaker = peripheral.find("speaker")
--local monitor = peripheral.find("monitor")
local monitor = peripheral.wrap("left")
local monitor2 = peripheral.wrap("top")
local modem = peripheral.find("modem")
local breakout = false
local p = 1
modem.open(114)
modem.open(514)
local decoder = dfpwm.make_decoder()
monitor2.clearLine(1)
monitor2.clearLine(2)
monitor2.setCursorPos(1, 1)
monitor2.write("Now Playing: ")
monitor2.clearLine(2)
monitor2.setCursorPos(1, 2)
monitor2.write("Nothing")
local MusicPlayer = {}
MusicPlayer.__index = MusicPlayer
function onlinecheck(go, back, message)
local expectedReply = "online" -- 期望的回复内容
while true do
print("sending: " .. message)
modem.transmit(go, back, message) -- 向指定频道发送消息
monitor.setCursorPos(1, 1)
monitor2.setCursorPos(1, 1)
monitor.write("The auxiliary host is offline. Please start the SidePC program on the auxiliary host.")
monitor2.write("The auxiliary host is offline. Please start the SidePC program on the auxiliary host.")
local event, modemSide, senderChannel,
replyChannel, message, senderDistance = os.pullEvent("modem_message")
-- 等待接收回复消息
if message == expectedReply then
break
else
sleep(1) -- 等待一秒后继续发送消息
end
end
end
function MusicPlayer.new()
local self = setmetatable({}, MusicPlayer)
self.musicList = {}
return self
end
function MusicPlayer:scanMusicFiles()
self.musicList = {}
local files = fs.list("./disk/")
for _, file in ipairs(files) do
if not fs.isDir(file) then
local fileName = fs.getName(file)
if string.sub(fileName, -6) == ".dfpwm" then
table.insert(self.musicList, fileName)
end
end
end
end
function MusicPlayer:printMusicList()
monitor.setTextScale(1)
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.write("Music List: (make sure sidePC is on)")
for i, musicName in ipairs(self.musicList) do
monitor.setCursorPos(1, i + 1)
monitor.write(i .. ". " .. musicName)
end
end
function MusicPlayer:downloadFile(url, path)
local response = http.get(url)
if response and response.getResponseCode() == 200 then
local file = fs.open(path, "w")
file.write(response.readAll())
file.close()
return true
else
return false
end
end
function MusicPlayer:playMusic(musicIndex)
local breakout = false
local musicName = self.musicList[musicIndex]
if musicName then
local filePath = "./disk/" .. musicName
if not fs.exists(filePath) then
local url = "https://raw.githubusercontent.com/GoldenDog1218/NekoVCmusic/main/" .. musicName
if self:downloadFile(url, filePath) then
print("Downloaded: " .. musicName)
else
print("Failed to download: " .. musicName)
return
end
end
monitor2.clearLine(1)
monitor2.clearLine(2)
monitor2.setCursorPos(1, 1)
monitor2.write("Now Playing: ")
monitor2.setCursorPos(1, 2)
monitor2.write(musicName)
-- 添加向另一台计算机发送当前歌曲名的代码
--modem.open(123)
--modem.open(456)
--modem.transmit(123, 456, musicName) -- 修改频道和目标ID
for chunk in io.lines(filePath, 16 * 1024) do
if breakout == "exit" then
break
end
local buffer = decoder(chunk)
while not speaker.playAudio(buffer) do
os.pullEvent("speaker_audio_empty")
-- 接收到信号后,停止播放音乐并返回初始状态
local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
if message == "ChangeMusic" then
modem.transmit(514, 114, "VCCAT")
print("Received signal to change music")
monitor2.setCursorPos(1, 1)
monitor2.clearLine()
monitor2.write("Nothing")
breakout = "exit" -- 修改全局变量的值
speaker.stop()
break -- 退出内部循环,回到外部循环
elseif message == "ExitPlz" then
modem.transmit(514, 114, "VCCAT")
monitor.clear()
monitor2.clear()
speaker.stop()
exit()
end
end
end
monitor2.clearLine(2)
monitor2.setCursorPos(1, 2)
monitor2.write("Nothing")
else
print("Invalid music index")
end
end
function MusicPlayer:start()
while true do
self:scanMusicFiles()
self:printMusicList()
print("Enter music index to play or 'exit' to quit:")
local input = io.read()
if input == "exit" then
break
elseif input == "list" then
-- Continue to next iteration of the loop to show the music list again
else
local musicIndex = tonumber(input)
if musicIndex then
self:playMusic(musicIndex)
print("Press any key to stop the music or 'list' to see the music list:")
print("(input 'exit' to quit)")
input = io.read()
if input == "list" then
-- Continue to next iteration of the loop to show the music list again
else
break
end
else
print("Invalid input")
end
end
end
monitor.clear()
speaker.stop()
-- 添加返回初始状态的代码
breakout = false
end
local player = MusicPlayer.new()
player:start()