Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- USER VARIABLES
- maxOcts = 12 -- maximum amount of octaves to sample
- promptForSettings = true -- set to true to prompt user if settings need to change, otherwise set to false
- -- NAME
- local scriptName = ({reaper. get_action_context()})[2]:match("([^/\\]+)%.lua$") -- generate default scriptName from file
- --- FUNCTIONS
- function msg(msg)
- reaper.ShowConsoleMsg(tostring(msg) .. "\n")
- end
- function toCSV(tt)
- -- Convert from table to CSV string
- local function escapeCSV(s) --Used to escape "'s by toCSV
- if string.find(s, '[,"]') then
- s = '"' .. string.gsub(s, '"', '""') .. '"'
- end
- return s
- end
- local s = ""
- for _,p in ipairs(tt) do
- s = s .. "," .. escapeCSV(p)
- end
- return string.sub(s, 2) -- remove first comma
- end
- function fromCSV(vals_csv, outputTypes)
- local t = {}
- local i = 0
- for line in vals_csv:gmatch("[^" .. "," .. "]*") do
- i = i + 1
- if #outputTypes == 1 and outputTypes[1] == "number" then
- t[i] = tonumber(line)
- elseif outputTypes[i] == "number" then
- t[i] = tonumber(line)
- else
- t[i] = line
- end
- end
- return t
- end
- function multiUserInput(outputTypes, windowName, numberOfParams, paramNames, defaultVals, saveVals, valsPersist, valsKeySuffix)
- --[[ returns retval, input values as a table
- Examples:
- retval, inputs = multiUserInput({"number"}, "Test", 3, {"Threshold", "Ratio", "Output"}, {-18, 4, 6}, true, false, "")
- if retval then msg(inputs[1]) end
- Note: outputTypes can take a 1-element table, which will apply that value type to all inputs.
- Otherwise specify output type for each value
- --]]
- paramNames_csv = toCSV(paramNames)
- if saveVals and reaper.HasExtState("Main", scriptName .. "_Vals" .. valsKeySuffix) then -- restore previous values on run if saveVals is true and the values exist
- defaultVals_csv = reaper.GetExtState("Main", scriptName .. "_Vals" .. valsKeySuffix)
- else
- defaultVals_csv = toCSV(defaultVals)
- end
- retval, retvals_csv = reaper.GetUserInputs(tostring(windowName), numberOfParams, paramNames_csv, defaultVals_csv)
- if retval and saveVals then
- reaper.SetExtState("Main", scriptName .. "_Vals" .. valsKeySuffix, retvals_csv, valsPersist) -- save new defaults
- end
- if retval then
- vals = fromCSV(retvals_csv, outputTypes)
- else
- vals = nil
- end
- return retval, vals
- end
- function error(type)
- if type == 0 then
- msgStr = "Error. Invalid input values. Choose a number of octaves between 1 and " .. maxOcts ..
- " and a number of samples per octave between 1 and 12"
- elseif type == 1 then
- msgStr = "Error. Select a midi item."
- end
- msg(msgStr)
- end
- ----- END OF FUNCTIONS
- function main()
- reaper.Undo_BeginBlock()
- if promptForSettings then
- prompt = reaper.ShowMessageBox( "Are your current render settings correct? Click 'No' to change settings", scriptName, 4 ) -- returns 6 for yes, 7 for no
- if prompt == 7 then
- reaper.Main_OnCommand(40015, 0)
- return
- end
- end
- local success, retvals_t = multiUserInput({"number"}, scriptName, 2, {"No. of octaves up", "No. of samples per octave"}, {4, 4}, true, false, "")
- if success then
- -- select only first item if midi item, else abort
- item = reaper.GetSelectedMediaItem(0, 0) -- first item
- isMidi = reaper.TakeIsMIDI( reaper.GetActiveTake(item) )
- if not isMidi then
- error(1)
- return
- end
- for k = 0, reaper.CountSelectedMediaItems(0) - 1 do
- reaper.SetMediaItemSelected(reaper.GetSelectedMediaItem(0, k), false)
- end
- reaper.SetMediaItemSelected(item, true)
- local octs, sampsPerOct = retvals_t[1], retvals_t[2]
- --msg(octs .. " and " .. sampsPerOct)
- if octs < 1 or octs > maxOcts or sampsPerOct < 1 or sampsPerOct > 12 then
- error(0)
- return
- end
- local transp = math.floor(12 / sampsPerOct) -- calculate amount of semitones to transpose each sample
- local samps = octs * sampsPerOct
- for i = 1, samps do
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWSMARKERLIST10"), 0) -- delete all regions
- for j = 1, transp do
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_UP_SEMI"), 0) -- transpose midi item up one semitone
- end
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_NAME"), 0) -- name midi item from note
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWS_REGIONSFROMITEMS"), 0) -- create region named from item
- reaper.Main_OnCommand(42230, 0) -- render using most recent settings
- end
- -- clean up
- for i = 1, samps * transp do
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_DN_SEMI"), 0) -- transpose midi item down one semitone
- end
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWSMARKERLIST10"), 0) -- delete all regions
- reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_NAME"), 0) -- name midi item from note
- end
- reaper.Undo_EndBlock(scriptName, -1)
- end
- reaper.PreventUIRefresh(1)
- main()
- reaper.PreventUIRefresh(-1)
- reaper.UpdateArrange()
- -- reaper.defer(function() end) -- Prevent undo if necessary
Add Comment
Please, Sign In to add comment