Skip to content

Commit 6b677e4

Browse files
committed
删除json模块默认的pcall封装,修改几个用到了的地方
1 parent ecb0680 commit 6b677e4

File tree

4 files changed

+189
-12
lines changed

4 files changed

+189
-12
lines changed

file.lua

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ function FILE.load(path,args)
5252
error("FILE.load: Compile error: "..err_mes)
5353
end
5454
elseif mode=='json' then
55-
local res=JSON.decode(s)
56-
if res then
57-
return res
58-
end
59-
error("FILE.load: Decode error")
55+
local res,data=pcall(JSON.decode,s)
56+
return res and data or error("FILE.load: Decode error")
6057
elseif mode=='string' then
6158
return s
6259
else
@@ -75,7 +72,7 @@ end
7572
function FILE.save(data,path,args)
7673
if not args then args='' end
7774
if STRING.sArg(args,'-d') and fs.getInfo(path) then
78-
error("FILE.save: Duplicate")
75+
error("FILE.save: File already exist")
7976
end
8077

8178
if type(data)=='table' then
@@ -86,13 +83,12 @@ function FILE.save(data,path,args)
8683
data='return'..TABLE.dumpDeflate(data)
8784
end
8885
if not data then
89-
error("FILE.save: Encode error")
86+
error("FILE.save: Luaon-encoding error")
9087
end
9188
else
92-
data=JSON.encode(data)
93-
if not data then
94-
error("FILE.save: Encode error")
95-
end
89+
local res
90+
res,data=pcall(JSON.encode,data)
91+
assert(res,"FILE.save: Json-encoding error")
9692
end
9793
else
9894
data=tostring(data)

http.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function HTTP.request(arg)
102102

103103
if arg.body~=nil then
104104
assertf(type(arg.body)=='table',"HTTP.request(arg): arg.body need table, got %s",arg.body)
105-
arg.body=JSON.encode(arg.body)
105+
local res
106+
res,arg.body=pcall(JSON.encode,arg.body)
107+
assert(res,"HTTP.request(arg): arg.body json-encoding error")
106108
if not arg.headers then arg.headers={} end
107109
TABLE.cover({
108110
['Content-Type']="application/json",

init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ FILE= require'Zenitha.file'
164164
SCR= require'Zenitha.screen'
165165
GC= require'Zenitha.gcExtend'
166166
TCP= require'Zenitha.tcp'
167+
MIDI= require'Zenitha.midi'
167168

168169
-- Love-based modules (complex, with update/draw)
169170
HTTP= require'Zenitha.http'

midi.lua

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---@class Zenitha.MIDI.Event
2+
---@field tick number tick
3+
---@field type number midi-type number
4+
---@field name string readable event type name
5+
---@field channel number? For all event except MetaEvent
6+
---@field note number? NoteStart & NoteEnd
7+
---@field velocity number? NoteStart & NoteEnd
8+
---@field control number? ControlChange
9+
---@field value number? ControlChange & ProgramChange & PitchBend
10+
---@field subType number? MetaEvent
11+
---@field data string MetaEvent
12+
13+
---@class Zenitha.MIDI
14+
---@field midFormat number
15+
---@field trackCount number
16+
---@field tickPerQuarterNote number
17+
---@field beatPerMinute number
18+
---
19+
---@field tracks Zenitha.MIDI.Event[]
20+
---@field trackHeads number[]
21+
---@field time number
22+
local MIDI={}
23+
MIDI.__index=MIDI
24+
25+
local r={0}
26+
for i=1,12 do r[i+1]=r[i]+2^(7*i) end -- Starting value of N-byte VLQ
27+
local function VLQ(str)
28+
local e=1
29+
while str:byte(e)>127 do e=e+1 end
30+
if e==1 then
31+
return str:byte(),str:sub(2)
32+
else
33+
local sum=0
34+
for i=1,e do
35+
sum=sum*2^7+str:byte(i)%128
36+
end
37+
return sum+r[e],str:sub(e+1)
38+
end
39+
end
40+
41+
local read=STRING.readChars
42+
function MIDI.newSong(sData)
43+
local Song={}
44+
45+
local sec
46+
assert(type(sData)=='string',"file not found")
47+
48+
sec,sData=read(sData,8)
49+
assert(sec=="MThd\0\0\0\6","not a midi file")
50+
51+
sec,sData=read(sData,2)
52+
Song.midFormat=STRING.binNum(sec)
53+
54+
sec,sData=read(sData,2)
55+
Song.trackCount=STRING.binNum(sec)
56+
57+
sec,sData=read(sData,2)
58+
Song.tickPerQuarterNote=STRING.binNum(sec)
59+
60+
Song.tracks={}
61+
for _=1,Song.trackCount do
62+
print("TRACK ".._..":")
63+
local track={}
64+
sec,sData=read(sData,4)
65+
assert(sec=="MTrk","not a midi track")
66+
67+
sec,sData=read(sData,4)
68+
local trackDataLen=STRING.binNum(sec)
69+
local tData
70+
tData,sData=read(sData,trackDataLen)
71+
72+
local now=0
73+
repeat
74+
---@type Zenitha.MIDI.Event
75+
local event={tick=now}
76+
local dt
77+
dt,tData=VLQ(tData)
78+
now=now+dt
79+
80+
sec,tData=read(tData,1)
81+
event.type=sec:byte()
82+
if event.type>=0x90 and event.type<=0x9F then
83+
event.name="NoteStart"
84+
event.channel=event.type-0x90
85+
sec,tData=read(tData,2)
86+
event.note=sec:byte(1)
87+
event.velocity=sec:byte(2)
88+
print("NoteStart",event.note)
89+
elseif event.type>=0x80 and event.type<=0x8F then
90+
event.name="NoteEnd"
91+
event.channel=event.type-0x80
92+
sec,tData=read(tData,2)
93+
event.note=sec:byte(1)
94+
event.velocity=sec:byte(2)
95+
print("NoteEnd",event.note)
96+
elseif event.type>=0xB0 and event.type<=0xBF then
97+
event.name="ControlChange"
98+
event.channel=event.type-0xB0
99+
sec,tData=read(tData,2)
100+
event.control=sec:byte(1)
101+
event.value=sec:byte(2)
102+
print("ControlChange",event.control)
103+
elseif event.type>=0xC0 and event.type<=0xCF then
104+
event.name="ProgramChange"
105+
event.channel=event.type-0xC0
106+
sec,tData=read(tData,1)
107+
event.value=sec:byte()
108+
print("ProgramChange",event.value)
109+
elseif event.type>=0xE0 and event.type<=0xEF then
110+
event.name="PitchBend"
111+
event.channel=event.type-0xE0
112+
sec,tData=read(tData,2)
113+
event.value=STRING.binNum(sec)
114+
print("PitchBend",event.value)
115+
elseif event.type==0xFF then
116+
event.name="MetaEvent"
117+
event.subType,tData=VLQ(tData)
118+
local len
119+
len,tData=VLQ(tData)
120+
event.data,tData=read(tData,len)
121+
print("MetaEvent",event.subType)
122+
else
123+
print("UNK",event.type)
124+
end
125+
if event.name then
126+
table.insert(track,event)
127+
end
128+
until #tData==0
129+
table.insert(Song.tracks,track)
130+
end
131+
assert(#Song.tracks==Song.trackCount,"redundancy track")
132+
assert(#sData==0,"redundancy data")
133+
134+
Song.time=0
135+
Song.trackHeads=TABLE.new(1,Song.trackCount)
136+
Song.beatPerMinute=120
137+
138+
return setmetatable(Song,MIDI)
139+
end
140+
141+
function MIDI:seek(t)
142+
self.time=t
143+
for i=1,#self.trackHeads do
144+
local p=1
145+
self.trackHeads[i]=p
146+
end
147+
end
148+
149+
function MIDI:reset()
150+
self.time=0
151+
for i=1,#self.trackHeads do
152+
self.trackHeads[i]=1
153+
end
154+
end
155+
156+
function MIDI:doEvent(event)
157+
-- print(event.name)
158+
end
159+
160+
function MIDI:update(dt)
161+
self.time=self.time+dt
162+
local tick=self.time*self.beatPerMinute/60*self.tickPerQuarterNote
163+
for i=1,self.trackCount do
164+
local head=self.trackHeads[i]
165+
while true do
166+
local event=self.tracks[i][head]
167+
if event and tick>=event.tick then
168+
self:doEvent(event)
169+
head=head+1
170+
else
171+
break
172+
end
173+
end
174+
self.trackHeads[i]=head
175+
end
176+
end
177+
178+
return MIDI

0 commit comments

Comments
 (0)