Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all bit32 functions with bitwise operations #19

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions midi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ local function parseVarInt(s, bits) -- parses multiple bytes as an integer
error("error parsing file")
end
bits = bits or 8
local mask = bit32.rshift(0xFF, 8 - bits)
local mask = 0xFF >> 8 - bits
local num = 0
for i = 1, s:len() do
num = num + bit32.lshift(bit32.band(s:byte(i), mask), (s:len() - i) * bits)
num = num + s:byte(i) & mask << (s:len() - i) * bits
end
return num
end
Expand Down Expand Up @@ -114,17 +114,17 @@ if format == 2 then
end

-- Figure out our time system and prepare accordingly.
local time = {division = bit32.band(0x8000, delta) == 0 and "tpb" or "fps"}
local time = {division = 0x8000 & delta == 0 and "tpb" or "fps"}
if time.division == "tpb" then
time.tpb = bit32.band(0x7FFF, delta)
time.tpb = 0x7FFF & delta
time.mspb = 500000
function time.tick()
return time.mspb / time.tpb
end
print(string.format("Time division is in %d ticks per beat.", time.tpb))
else
time.fps = bit32.band(0x7F00, delta)
time.tpf = bit32.band(0x00FF, delta)
time.fps = 0x7F00 & delta
time.tpf = 0x00FF & delta
function time.tick()
return 1000000 / (time.fps * time.tpf)
end
Expand Down Expand Up @@ -162,13 +162,13 @@ while true do
for i = 1, math.huge do
local part = read()
total = total .. part
if bit32.band(0x80, part:byte(1)) == 0 then
if 0x80 & part:byte(1) == 0 then
return parseVarInt(total, 7)
end
end
end
local function parseVoiceMessage(event)
local channel = bit32.band(0xF, event)
local channel = 0xF & event
local note = parseVarInt(read())
local velocity = parseVarInt(read())
return channel, note, velocity
Expand Down Expand Up @@ -196,7 +196,7 @@ while true do
error("corrupt file: could not find continuation of divided sysex event")
end
local event
if bit32.band(test, 0x80) == 0 then
if test & 0x80 == 0 then
if running == 0 then
error("corrupt file: invalid running status")
end
Expand All @@ -209,7 +209,7 @@ while true do
running = test
end
end
local status = bit32.band(0xF0, event)
local status = 0xF0 & event
if status == 0x80 then -- Note off.
local channel, note, velocity = parseVoiceMessage(event)
noteOff(cursor, channel, note, velocity)
Expand Down Expand Up @@ -382,4 +382,4 @@ for tick = 1, totalLength do
if instruments.flush then instruments.flush() end
end
if keyboard.isKeyDown(keyboard.keys.c) and keyboard.isControlDown() then os.exit() end
end
end