Skip to content

Commit

Permalink
Changed recurrent ipairs() loops to numerical for-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroAlvesV committed Dec 30, 2017
1 parent 70b5639 commit 463325d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/LuaMidi/NoteEvent.lua
Expand Up @@ -180,7 +180,8 @@ function NoteEvent.new(fields)
local note_on, note_off
if not self.sequential then
for j=1, self.repetition do
for i, p in ipairs(self.pitch) do
for i=1, #self.pitch do
local p = self.pitch[i]
local fields = {}
local data
if i == 1 then
Expand All @@ -195,7 +196,8 @@ function NoteEvent.new(fields)
note_on = NoteOnEvent.new(fields)
self.data = Util.table_concat(self.data, note_on.data)
end
for i, p in ipairs(self.pitch) do
for i=1, #self.pitch do
local p = self.pitch[i]
local fields = {}
local data
if i == 1 then
Expand All @@ -213,7 +215,8 @@ function NoteEvent.new(fields)
end
else
for j=1, self.repetition do
for i, p in ipairs(self.pitch) do
for i=1, #self.pitch do
local p = self.pitch[i]
local fields = {}
if i > 1 then
rest_duration = 0
Expand Down
21 changes: 14 additions & 7 deletions src/LuaMidi/Writer.lua
Expand Up @@ -14,8 +14,6 @@ local Constants = require('LuaMidi.Constants')
local Chunk = require('LuaMidi.Chunk')
local MetaEvent = require('LuaMidi.MetaEvent')

local unpack = unpack or table.unpack

local Writer = {}

-------------------------------------------------
Expand All @@ -39,7 +37,8 @@ function Writer.new(tracks)
tracks = tracks,
}
self.build_track = function(track)
for _, event in ipairs(track.events) do
for i=1, #track.events do
local event = track.events[i]
if event.type == 'note' then
event.build_data()
end
Expand All @@ -59,7 +58,8 @@ function Writer.new(tracks)
type = Constants.HEADER_CHUNK_TYPE,
data = chunk_data,
})
for _, track in ipairs(new_tracks) do
for i=1, #new_tracks do
local track = new_tracks[i]
track:add_events(MetaEvent.new({data = Constants.META_END_OF_TRACK_ID}))
track = self.build_track(track)
self.data[#self.data+1] = track
Expand Down Expand Up @@ -101,7 +101,8 @@ end
-------------------------------------------------
function Writer:build_file()
local build = {}
for _, elem in ipairs(self.data) do
for i=1, #self.data do
local elem = self.data[i]
build = Util.table_concat(build, elem.type)
build = Util.table_concat(build, elem.size)
build = Util.table_concat(build, elem.data)
Expand All @@ -121,7 +122,8 @@ end
function Writer:stdout(show_index)
local buffer = self:build_file()
print('{')
for i, byte in ipairs(buffer) do
for i=1, #buffer do
local byte = buffer[i]
io.write(' ')
if show_index then io.write(i..' - ') end
io.write(byte)
Expand Down Expand Up @@ -150,7 +152,12 @@ function Writer:save_MIDI(title, directory)
end
end
local file = io.open(title, 'wb')
local buffer = string.char(unpack(self:build_file()))
local bytes = self:build_file()
local buffer = ""
for i=1, #bytes do
local byte = bytes[i]
buffer = buffer..string.char(byte)
end
file:write(buffer)
file:close()
end
Expand Down

0 comments on commit 463325d

Please sign in to comment.