Skip to content

Commit

Permalink
Moved most of the code to rsmfs namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilcaream committed Aug 20, 2022
1 parent 1dddb2e commit 2ee4639
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 150 deletions.
4 changes: 0 additions & 4 deletions com.nilcaream.rsmfs.xrnx/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ rsmfs.log = function(s,...)
print("RSMFS " .. string.format(s, ...))
end

function log(s, ...)
print("RSMFS " .. string.format(s, ...))
end

rsmfs.log("Renoise Simple Midi File Support")
28 changes: 15 additions & 13 deletions com.nilcaream.rsmfs.xrnx/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,43 @@ end
function mid_import(filename)

if rsmfs.options.show_for_each_file then
rsmfs.options.show()
if rsmfs.options.show() == "Cancel" then
return
end
end

log("-------- Loading " .. filename)
rsmfs.log("-------- Loading " .. filename)

local midi = load_file(filename)
local score = MIDI.midi2score(midi)
local stats = MIDI.score2stats(score)

log("Tracks: " .. stats.ntracks)
log("Total ticks: " .. stats.nticks)
log("Ticks per beat: " .. score[1])
rsmfs.log("Tracks: " .. stats.ntracks)
rsmfs.log("Total ticks: " .. stats.nticks)
rsmfs.log("Ticks per beat: " .. score[1])

local raw_track_lines = { }

log("-------- Raw midi input")
rsmfs.log("-------- Raw midi input")

for itrack = 2, #score do
for _, event in ipairs(score[itrack]) do

-- TODO check if this is always sorted by start_time; filter by channel
-- TODO check if this is always sorted by start_time; filter by channel if needed
if event[1] == "note" then
local midi_note = MidiNote:new(event)
log(midi_note:__tostring())
Track.add_raw(midi_note, raw_track_lines)
local midi_note = rsmfs.midi_note:new(event)
rsmfs.log(midi_note:__tostring())
rsmfs.track.add_raw(midi_note, raw_track_lines)
end
end
end

local workplace = Workplace:new()
local workplace = rsmfs.workplace:new()
workplace:prepare(#raw_track_lines)

for index, value in ipairs(raw_track_lines) do
log("-------- Note column " .. index)
local track = Track:new(value, score[1])
rsmfs.log("-------- Note column " .. index)
local track = rsmfs.track:new(value, score[1])
track:print()
workplace:update(track, index)
end
Expand Down
19 changes: 9 additions & 10 deletions com.nilcaream.rsmfs.xrnx/midi-note.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
require("common")

MidiNote = {}
MidiNote.__index = MidiNote
rsmfs.midi_note = {}
rsmfs.midi_note.__index = rsmfs.midi_note
rsmfs.midi_note.notes_sharp = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }

function MidiNote:new(event)
function rsmfs.midi_note:new(event)
local instance = {
note_number = event[5],
note = to_note_string(event[5]),
note = rsmfs.midi_note.to_note_string(event[5]),
start_time = event[2],
end_time = event[2] + event[3],
duration = event[3],
channel = event[4],
velocity = event[6]
}
setmetatable(instance, MidiNote)
setmetatable(instance, rsmfs.midi_note)
return instance
end

function MidiNote:__tostring()
function rsmfs.midi_note:__tostring()
return string.format("CH: %s, start: % 5d, end: % 5d, note: %3s (% 3d), velocity: % 3d", self.channel, self.start_time, self.end_time, self.note, self.note_number, self.velocity)
end

-- =========================================

local notes_sharp = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }

-- https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies
function to_note_string(midi_note_number, _dictionary)
rsmfs.midi_note.to_note_string = function(midi_note_number, dictionary)
midi_note_number = midi_note_number + 12 * rsmfs.options.octave + rsmfs.options.transposition
dictionary = dictionary or rsmfs.midi_note.notes_sharp

local dictionary = _dictionary or notes_sharp
local note_index = midi_note_number % #dictionary
local octave = math.floor(midi_note_number / #dictionary) - 1
local note = dictionary[note_index + 1]
Expand Down
213 changes: 107 additions & 106 deletions com.nilcaream.rsmfs.xrnx/options.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
require("common")

rsmfs.options = {
init = function()
renoise.tool():add_menu_entry {
name = "Main Menu:Tools:Renoise Simple Midi File Support",
invoke = rsmfs.options.show
}
end,

add_note_columns = true,
remove_note_columns = true,
increase_number_of_lines = true,
Expand All @@ -16,111 +9,119 @@ rsmfs.options = {
transposition = 0,
octave = 1,
show_for_each_file = true,
}

show = function(on_action)
log("-------- Options")

local vb = renoise.ViewBuilder()

local DIALOG_MARGIN = renoise.ViewBuilder.DEFAULT_DIALOG_MARGIN
local CONTENT_SPACING = renoise.ViewBuilder.DEFAULT_CONTROL_SPACING
local CONTENT_MARGIN = renoise.ViewBuilder.DEFAULT_CONTROL_MARGIN
local DEFAULT_CONTROL_HEIGHT = renoise.ViewBuilder.DEFAULT_CONTROL_HEIGHT
local DEFAULT_DIALOG_BUTTON_HEIGHT = renoise.ViewBuilder.DEFAULT_DIALOG_BUTTON_HEIGHT
local DEFAULT_MINI_CONTROL_HEIGHT = renoise.ViewBuilder.DEFAULT_MINI_CONTROL_HEIGHT
local TEXT_ROW_WIDTH = 140

local add_checkbox = function(text, value, on_change)
return vb:row {
vb:text {
width = TEXT_ROW_WIDTH,
text = text
},
vb:checkbox {
value = value,
notifier = function(new_value)
rsmfs.log("%s = %s", text, tostring(new_value))
if on_change ~= nil then
on_change(new_value)
end
end,
}
rsmfs.options.init = function()
renoise.tool():add_menu_entry {
name = "Main Menu:Tools:Renoise Simple Midi File Support",
invoke = rsmfs.options.show
}
end

rsmfs.options.show = function(on_action)
rsmfs.log("-------- Options")

local vb = renoise.ViewBuilder()

local DIALOG_MARGIN = renoise.ViewBuilder.DEFAULT_DIALOG_MARGIN
local CONTENT_SPACING = renoise.ViewBuilder.DEFAULT_CONTROL_SPACING
local CONTENT_MARGIN = renoise.ViewBuilder.DEFAULT_CONTROL_MARGIN
local DEFAULT_CONTROL_HEIGHT = renoise.ViewBuilder.DEFAULT_CONTROL_HEIGHT
local DEFAULT_DIALOG_BUTTON_HEIGHT = renoise.ViewBuilder.DEFAULT_DIALOG_BUTTON_HEIGHT
local DEFAULT_MINI_CONTROL_HEIGHT = renoise.ViewBuilder.DEFAULT_MINI_CONTROL_HEIGHT
local TEXT_ROW_WIDTH = 140

local add_checkbox = function(text, value, on_change)
return vb:row {
vb:text {
width = TEXT_ROW_WIDTH,
text = text
},
vb:checkbox {
value = value,
notifier = function(new_value)
rsmfs.log("%s = %s", text, tostring(new_value))
if on_change ~= nil then
on_change(new_value)
end
end,
}
end

local add_valuebox = function(text, value, min, max, on_change)
return vb:row {
vb:text {
width = TEXT_ROW_WIDTH,
text = text
},
vb:valuebox {
min = min,
max = max,
value = value,
notifier = function(new_value)
rsmfs.log("%s = %s", text, tostring(new_value))
if on_change ~= nil then
on_change(new_value)
end
end,
}
}
end

local dialog_content = vb:column {
margin = DIALOG_MARGIN,
spacing = CONTENT_SPACING,

vb:row {
spacing = 4 * CONTENT_SPACING,

vb:column {
spacing = CONTENT_SPACING,

add_checkbox("Add note columns", rsmfs.options.add_note_columns, function(v)
rsmfs.options.add_note_columns = v
end),
add_checkbox("Remove note columns", rsmfs.options.remove_note_columns, function(v)
rsmfs.options.remove_note_columns = v
end),
add_checkbox("Increase number of lines", rsmfs.options.increase_number_of_lines, function(v)
rsmfs.options.increase_number_of_lines = v
end),
add_checkbox("Decrease number of lines", rsmfs.options.decrease_number_of_lines, function(v)
rsmfs.options.decrease_number_of_lines = v
end),

vb:space { height = DEFAULT_CONTROL_HEIGHT },

add_valuebox("Resolution", rsmfs.options.resolution, 1, 16, function(v)
rsmfs.options.resolution = v
end),
add_valuebox("Octave correction", rsmfs.options.octave, -4, 4, function(v)
rsmfs.options.octave = v
end),
add_valuebox("Transposition", rsmfs.options.transposition, -4, 4, function(v)
rsmfs.options.transposition = v
end),

vb:space { height = DEFAULT_CONTROL_HEIGHT },

add_checkbox("Show for each file", rsmfs.options.show_for_each_file, function(v)
rsmfs.options.show_for_each_file = v
end),
}
}
end

local add_valuebox = function(text, value, min, max, on_change)
return vb:row {
vb:text {
width = TEXT_ROW_WIDTH,
text = text
},
vb:valuebox {
min = min,
max = max,
value = value,
notifier = function(new_value)
rsmfs.log("%s = %s", text, tostring(new_value))
if on_change ~= nil then
on_change(new_value)
end
end,
}
}
end

local action = renoise.app():show_custom_prompt(
"Renoise Simple Midi File Support", dialog_content, { "OK" }
)
local dialog_content = vb:column {
margin = DIALOG_MARGIN,
spacing = CONTENT_SPACING,

vb:row {
spacing = 4 * CONTENT_SPACING,

vb:column {
spacing = CONTENT_SPACING,

add_checkbox("Add note columns", rsmfs.options.add_note_columns, function(v)
rsmfs.options.add_note_columns = v
end),
add_checkbox("Remove note columns", rsmfs.options.remove_note_columns, function(v)
rsmfs.options.remove_note_columns = v
end),
add_checkbox("Increase number of lines", rsmfs.options.increase_number_of_lines, function(v)
rsmfs.options.increase_number_of_lines = v
end),
add_checkbox("Decrease number of lines", rsmfs.options.decrease_number_of_lines, function(v)
rsmfs.options.decrease_number_of_lines = v
end),

vb:space { height = DEFAULT_CONTROL_HEIGHT },

add_valuebox("Resolution", rsmfs.options.resolution, 1, 16, function(v)
rsmfs.options.resolution = v
end),
add_valuebox("Octave correction", rsmfs.options.octave, -4, 4, function(v)
rsmfs.options.octave = v
end),
add_valuebox("Transposition", rsmfs.options.transposition, -4, 4, function(v)
rsmfs.options.transposition = v
end),

vb:space { height = DEFAULT_CONTROL_HEIGHT },

add_checkbox("Show for each file", rsmfs.options.show_for_each_file, function(v)
rsmfs.options.show_for_each_file = v
end),
}
}
}

rsmfs.log(action)
local action = renoise.app():show_custom_prompt(
"Renoise Simple Midi File Support", dialog_content, { "OK" }
)

if on_action ~= nil then
on_action(action)
end
rsmfs.log(action)

if on_action ~= nil then
on_action(action)
end
}

return action
end
14 changes: 7 additions & 7 deletions com.nilcaream.rsmfs.xrnx/track.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require("common")

Track = {}
Track.__index = Track
rsmfs.track = {}
rsmfs.track.__index = rsmfs.track

function Track:new(_table, ticks_per_beat)
function rsmfs.track:new(_table, ticks_per_beat)
local resolution = rsmfs.options.resolution / 4
local tick = ticks_per_beat / (renoise.song().transport.lpb * resolution)

Expand All @@ -17,19 +17,19 @@ function Track:new(_table, ticks_per_beat)
velocity = value.velocity
})
end
setmetatable(instance, Track)
setmetatable(instance, rsmfs.track)
return instance
end

function Track:print()
function rsmfs.track:print()
for index, value in ipairs(self) do
log("start: %6.2f, end: %6.2f, note: %3s, velocity: % 3d", value.start_position, value.end_position, value.note, value.velocity)
rsmfs.log("start: %6.2f, end: %6.2f, note: %3s, velocity: % 3d", value.start_position, value.end_position, value.note, value.velocity)
end
end

-- =========================================

function Track.add_raw(midi_note, tracks)
function rsmfs.track.add_raw(midi_note, tracks)
for track_index = 1, 16 do
if tracks[track_index] == nil then
table.insert(tracks, {})
Expand Down

0 comments on commit 2ee4639

Please sign in to comment.