From eff8ff81c1da1b5e312eecc675eb23c7f146918a Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 13 Oct 2025 02:58:08 +0200 Subject: [PATCH] Add option dialog to human^wbrutalize MIDI script --- share/scripts/brutalize_midi.lua | 77 ++++++++++++++++++++++++++++++++ share/scripts/midi_brutalize.lua | 41 ----------------- 2 files changed, 77 insertions(+), 41 deletions(-) create mode 100644 share/scripts/brutalize_midi.lua delete mode 100644 share/scripts/midi_brutalize.lua diff --git a/share/scripts/brutalize_midi.lua b/share/scripts/brutalize_midi.lua new file mode 100644 index 0000000000..799de78a03 --- /dev/null +++ b/share/scripts/brutalize_midi.lua @@ -0,0 +1,77 @@ +ardour { ["type"] = "EditorAction", name = "Brutalize MIDI", + license = "MIT", + author = "Ardour Team", + description = [[Randomize MIDI Note position (de-quantize).]] +} + +function factory () return function () + + -- Ask user about max randomness to introduce + local dialog_options = { + { type = "label", align="left", title = "Brutalize MIDI" }, + { + type = "dropdown", key = "divisor", title="Max randomness to introduce:", values = + { + ["8th note"] = 2, + ["16th note"] = 4, + ["16th triplett (1/24)"] = 6, + ["32nd"] = 8, + ["32nd triplett (1/48)"] = 12, + ["64th"] = 16 + }, + default = "16th note" + }, + { + type = "dropdown", key = "rand", title="Move Notes..", values = + { + ["only forward in time"] = function () return math.random() end, -- 0 .. +1 + ["only backward in time"] = function () return math.random() - 2; end, -- -1 .. 0 + ["either way"] = function () return 2 * math.random() - 1; end -- -1 .. +1 + }, + default = "either way" + } + } + local rv = LuaDialog.Dialog ("Select Automation State", dialog_options):run() + if not rv then return end + + -- calclate max distance in 'ticks' + local ticks_per_beat = Temporal.Beats (1, 0):to_ticks (); + local max_distance = ticks_per_beat / rv['divisor'] + + -- iterate over all selected regions + local sel = Editor:get_selection () + for r in sel.regions:regionlist ():iter () do + local mr = r:to_midiregion () + -- skip non MIDI regions + if mr:isnil () then goto continue end + + -- get MIDI Model of the region + local mm = mr:midi_source(0):model () + -- Prepare Undo command + local midi_command = mm:new_note_diff_command ("MIDI Note Brutalize") + + -- Iterate over all notes of the MIDI region + for note in ARDOUR.LuaAPI.note_list (mm):iter () do + -- note is-a https://manual.ardour.org/lua-scripting/class_reference/#Evoral:NotePtr + -- get current position .. + local old_pos = note:time () + + -- ..generate random offset.. + local tickdiff = math.floor (rv['rand']() * max_distance); + print (old_pos:get_beats (), old_pos:get_ticks (), tickdiff) + + -- .. and calculate new position. + local new_pos = Temporal.Beats (old_pos:get_beats (), old_pos:get_ticks () + tickdiff) + + -- now modify the note (but don't allow to move a note before the session start [1|1|0]) + if old_pos ~= new_pos and new_pos > Temporal.Beats (0, 0) then + local new_note = ARDOUR.LuaAPI.new_noteptr (note:channel (), new_pos, note:length (), note:note (), note:velocity ()) + midi_command:remove (note) + midi_command:add (new_note) + end + end + -- apply changes, and save undo + mm:apply_command (Session, midi_command) + ::continue:: + end +end end diff --git a/share/scripts/midi_brutalize.lua b/share/scripts/midi_brutalize.lua deleted file mode 100644 index 70faf4572b..0000000000 --- a/share/scripts/midi_brutalize.lua +++ /dev/null @@ -1,41 +0,0 @@ -ardour { ["type"] = "EditorAction", name = "MIDI Brutalize", - license = "MIT", - author = "Ardour Team", - description = [[Randomize MIDI Note position (de-quantize).]] -} - -function factory () return function () -local sel = Editor:get_selection () --- iterate over all selected regions -for r in sel.regions:regionlist ():iter () do - local mr = r:to_midiregion () - if mr:isnil () then goto continue end - - local ticks_per_beat = Temporal.Beats (1, 0):to_ticks (); - local max_shift = ticks_per_beat / 4.0 - - -- get MIDI Model - local mm = mr:midi_source(0):model () - -- Prepare Undo command - local midi_command = mm:new_note_diff_command ("MIDI Note Brutalize") - - -- Iterate over all notes of the MIDI region - for note in ARDOUR.LuaAPI.note_list (mm):iter () do - -- note is-a https://manual.ardour.org/lua-scripting/class_reference/#Evoral:NotePtr - local old_pos = note:time () - - -- shift +/- 1/16th note - local tickdiff = math.floor ((math.random() - 0.5) * max_shift) - --print (old_pos:get_beats (), old_pos:get_ticks (), tickdiff) - - local new_pos = Temporal.Beats (old_pos:get_beats (), old_pos:get_ticks () + tickdiff) - if old_pos ~= new_pos and new_pos > Temporal.Beats (0, 0) then - local new_note = ARDOUR.LuaAPI.new_noteptr (note:channel (), new_pos, note:length (), note:note (), note:velocity ()) - midi_command:remove (note) - midi_command:add (new_note) - end - end - mm:apply_command (Session, midi_command) - ::continue:: -end -end end