Loading of files. Versioning of format.
This commit is contained in:
parent
18615c1fce
commit
ff2ad8cfb6
|
@ -1,5 +1,5 @@
|
|||
DRUMSHEET
|
||||
tempo,130
|
||||
DRUMSHEET,1,2019-10-14
|
||||
bpm,130
|
||||
factor,4
|
||||
len,32
|
||||
INSTRUMENTS
|
||||
|
|
|
|
@ -5,6 +5,7 @@ require "midiwire"
|
|||
-- now, this can actually play Blue monday!
|
||||
-- see https://www.youtube.com/watch?v=Y_hk5P1X7sc&t=164s
|
||||
|
||||
VERSION = '2019-10-14'
|
||||
LEN = 33
|
||||
step = 1
|
||||
mout = midiwire.open_out(0)
|
||||
|
@ -37,13 +38,17 @@ pat_save = iup.button{title="save"}
|
|||
matrix = iup.matrix{numcol=32, numlin=9, widthdef=10}
|
||||
matrix.resizematrix = "YES"
|
||||
|
||||
function print_instruments()
|
||||
for i=1,32 do
|
||||
matrix:setcell(0,i,i)
|
||||
end
|
||||
for i=1,#instruments do
|
||||
matrix:setcell(i,0,instruments[i][1])
|
||||
end
|
||||
end
|
||||
|
||||
matrix:setcell(0,0,"DRUMsheet")
|
||||
for i=1,32 do
|
||||
matrix:setcell(0,i,i)
|
||||
end
|
||||
for i=1,#instruments do
|
||||
matrix:setcell(i,0,instruments[i][1])
|
||||
end
|
||||
print_instruments()
|
||||
|
||||
playbtn = iup.button{title="play"}
|
||||
stopbtn = iup.button{title="stop"}
|
||||
|
@ -128,17 +133,21 @@ function factor:valuechanged_cb()
|
|||
end
|
||||
end
|
||||
|
||||
function adjust_len(len)
|
||||
LEN = len + 1
|
||||
for i=1,LEN do
|
||||
matrix:setcell(0, i, i)
|
||||
end
|
||||
if LEN<33 then
|
||||
for i=LEN,32 do
|
||||
matrix:setcell(0, i, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function len:valuechanged_cb()
|
||||
if tonumber(len.value) then
|
||||
LEN = tonumber(len.value) + 1
|
||||
for i=1,LEN do
|
||||
matrix:setcell(0, i, i)
|
||||
end
|
||||
if LEN<33 then
|
||||
for i=LEN,32 do
|
||||
matrix:setcell(0, i, "")
|
||||
end
|
||||
end
|
||||
adjust_len(tonumber(len.value))
|
||||
matrix.redraw="YES"
|
||||
end
|
||||
end
|
||||
|
@ -213,6 +222,8 @@ end
|
|||
|
||||
-- saving & loading
|
||||
|
||||
FILEFORMAT_VERSION = 1
|
||||
|
||||
function _pat_write(fh, patno)
|
||||
fh:write("PAT,"..patno.."\n")
|
||||
for i=1,#patterns[patno] do
|
||||
|
@ -224,8 +235,8 @@ function pat_save:action()
|
|||
local fname, status = iup.GetFile("./*.csv")
|
||||
if status > -1 then
|
||||
local fh = io.open(fname, "w+")
|
||||
fh:write("DRUMSHEET\n")
|
||||
fh:write("tempo,"..bpm.value.."\n")
|
||||
fh:write("DRUMSHEET," .. FILEFORMAT_VERSION .. "," .. VERSION .. "\n")
|
||||
fh:write("bpm,"..bpm.value.."\n")
|
||||
fh:write("factor,"..factor.value.."\n")
|
||||
fh:write("len,"..len.value.."\n")
|
||||
fh:write("INSTRUMENTS\n")
|
||||
|
@ -241,6 +252,92 @@ function pat_save:action()
|
|||
end
|
||||
end
|
||||
|
||||
function explode(div,str) -- credit: http://richard.warburton.it
|
||||
if (div=='') then return false end
|
||||
local pos,arr = 0,{}
|
||||
-- for each divider found
|
||||
for st,sp in function() return string.find(str,div,pos,true) end do
|
||||
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
|
||||
pos = sp + 1 -- Jump past current divider
|
||||
end
|
||||
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
|
||||
return arr
|
||||
end
|
||||
|
||||
function sheet_read(fname)
|
||||
local data = { instruments={}, patterns = {} }
|
||||
local mode = ""
|
||||
local is_header = {bpm=true, factor=true, len=true}
|
||||
local i = 0
|
||||
local patno = 0
|
||||
for line in io.lines(fname) do
|
||||
local fields = explode(',', line)
|
||||
-- reads header:
|
||||
if mode=="DRUMSHEET" and is_header[fields[1]] then
|
||||
data[fields[1]] = tonumber(fields[2])
|
||||
end
|
||||
|
||||
-- switch to PAT mode in INSTRUMENTS
|
||||
if mode=="INSTRUMENTS" and fields[1]=="PAT" then
|
||||
mode = "PAT"
|
||||
end
|
||||
|
||||
-- reads instruments:
|
||||
if mode=="INSTRUMENTS" and fields[1]~="PAT" then
|
||||
i = i + 1
|
||||
data.instruments[i] = {fields[1], tonumber(fields[2])}
|
||||
end
|
||||
|
||||
-- read patterns:
|
||||
if mode=="PAT" and fields[1]=="PAT" then
|
||||
patno = tonumber(fields[2])
|
||||
data.patterns[patno] = {}
|
||||
i = 0
|
||||
elseif mode=="PAT" then
|
||||
i = i + 1
|
||||
data.patterns[patno][i] = fields
|
||||
end
|
||||
|
||||
if mode=="" and fields[1]=="DRUMSHEET" then
|
||||
-- todo - kontrolovat verzi drumsheetu
|
||||
if not fields[2] or not tonumber(fields[2]) then
|
||||
return nil, "File version not found!"
|
||||
end
|
||||
if tonumber(fields[2])>FILEFORMAT_VERSION then
|
||||
return nil, "File too new!"
|
||||
end
|
||||
mode = "DRUMSHEET"
|
||||
|
||||
end
|
||||
if mode=="DRUMSHEET" and fields[1]=="INSTRUMENTS" then
|
||||
mode = "INSTRUMENTS"
|
||||
i = 0
|
||||
end
|
||||
end
|
||||
return data
|
||||
end
|
||||
|
||||
function pat_load:action()
|
||||
local fname, status = iup.GetFile("./*.csv")
|
||||
if status > -1 then
|
||||
local data, error = sheet_read(fname)
|
||||
if error then
|
||||
iup.Message("File not loaded", error)
|
||||
else
|
||||
bpm.value = data.bpm
|
||||
factor.value = data.factor
|
||||
len.value = data.len
|
||||
adjust_time(data.bpm, data.factor)
|
||||
adjust_len(data.len)
|
||||
instruments = data.instruments
|
||||
print_instruments()
|
||||
patterns = data.patterns
|
||||
pat_recall(1)
|
||||
matrix.redraw = "YES"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- main program
|
||||
|
||||
|
@ -249,4 +346,4 @@ dlg:showxy( iup.CENTER, iup.CENTER )
|
|||
|
||||
if (iup.MainLoopLevel()==0) then
|
||||
iup.MainLoop()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue