Snippet: Instrument Definitions

Snippet for basic parser of cakewalk instrument definitions. I’m not a programmer so feel free to point out any bad syntax :)

It currently supports Bank Select method 3, and should provide all data needed to browse patches via midi messages.

[details=“Click to view contents”] ```
banks = { }
file_data = { } – for better and more efficient handling of the files data?
file_data_delimiters = { } – for finding delimiters using table.find
synth = { }

function extract(input, method)
if method == 1 then – method 1 filter recognizes the patch value later being used to derive control messages for bank change
return string.sub(input, string.find(input, “%[”)+1, string.find(input, “%]”)-1)
elseif method == 2 then – returns msb from patch value
return math.floor(input/128)
elseif method == 3 then – returns lsb from patch value
return input%128
elseif method == 4 then – returns the name of the bank
return string.sub(input, string.find(input, “=”)+1, string.len(input))
elseif method == 5 then – returns the ID of a patch
return string.sub(input, 0, string.find(input, “=”)-1)
elseif method == 6 then – returns a sane name of the patch
return string.sub(input, string.find(input, “=”)+1, string.len(input))
elseif method == 7 then – returns the name of the controller
return string.sub(input, string.find(input, “-”)+1, string.len(input))
elseif method == 8 then – returns the value of the controller
return string.sub(input, 0, string.find(input, “=”)-1)
end
end

function file_to_table(file)
local i = 0
for line in io.lines(file) do
if string.sub(line, 1, 1) == “;” then
elseif string.len(line) > 1 then
i = i+1
file_data[i] = line end
end
end

function file_to_table_delimiters(file)
local i = 0
for line in io.lines(file) do
if string.sub(line, 1, 1) == “;” then
elseif string.len(line) > 1 then
i = i+1
if string.sub(line, 1, 1) == “.” then file_data_delimiters[i] = “delimiter”
elseif string.sub(line, 1, 1) == “[” then file_data_delimiters[i] = “delimiter”
else file_data_delimiters[i] = line
end
end
end
end

function bank_setup(file_data)
local i = 0
for line in ipairs(file_data) do
if string.find(file_data[line], “.Patch”) then local patch_pos = i end
if string.find(file_data[line], “Patch%[”) and string.len(file_data[line]) > 20 then
local bank_name = extract(file_data[line], 4)
local bank_msb = extract(extract(file_data[line], 1),2)
local bank_lsb = extract(extract(file_data[line], 1),3)
i = i+1
banks[i] = { bank=bank_name, msb=bank_msb, lsb=bank_lsb, patches=patches_setup("[" … bank_name … “]”) }
end
end
return banks
end

function cc_setup()
local cc = { }
local cc_startline = table.find(file_data, “.Controller Names”) + 2
local cc_endline = table.find(file_data_delimiters, “delimiter”, cc_startline) - 1
local u = cc_startline
local i = 0
for u = cc_startline, cc_endline do
i = i + 1
cc[tonumber(extract(file_data[u], 8))] = extract(file_data[u], 7)
end
return cc
end

function patches_setup(bank)
local patches = { }
local bank_startline = table.find(file_data, bank) + 1
local bank_endline = table.find(file_data_delimiters, “delimiter”, bank_startline)
local u = bank_startline
local i = 0
for u = bank_startline, bank_endline-1 do
i = i + 1
patches[tonumber(extract(file_data[u], 5))] = extract(file_data[u], 6)
end
return patches
end

function synth_setup()
file_to_table(“c:/Roland JV-2080.ins”)
file_to_table_delimiters(“c:/Roland JV-2080.ins”)
synth = { banks = bank_setup(file_data), controllers = cc_setup() }
end

synth_setup()

rprint(synth)

[/details]

Good work! I can’t see any syntax errors from looking at it, how well is this working?

I have only tried it with roland jv-2080 and korg triton definitions, and it interprets the most crucial data into an array. I will have to check more carefully if files differ when it comes to empty lines and stuff that i decided to search for as delimiters. The file format is badly documented.

I will add support for other Bank select methods than 3.

Regarding the programming I probably haven’t optimized the use of for loops+iterators since I’m not very familiar with programing.