poulhoi

phoi_Create sampled notes from selected midi item.lua

Dec 28th, 2020 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. -- USER VARIABLES
  2. maxOcts = 12 -- maximum amount of octaves to sample
  3. promptForSettings = true -- set to true to prompt user if settings need to change, otherwise set to false
  4.  
  5. -- NAME
  6. local scriptName = ({reaper. get_action_context()})[2]:match("([^/\\]+)%.lua$") -- generate default scriptName from file
  7.  
  8. --- FUNCTIONS
  9.  
  10. function msg(msg)
  11.   reaper.ShowConsoleMsg(tostring(msg) .. "\n")
  12. end
  13.  
  14. function toCSV(tt)
  15.   -- Convert from table to CSV string
  16.  
  17.   local function escapeCSV(s) --Used to escape "'s by toCSV
  18.     if string.find(s, '[,"]') then
  19.       s = '"' .. string.gsub(s, '"', '""') .. '"'
  20.     end
  21.     return s
  22.   end
  23.      
  24.   local s = ""
  25.  
  26.   for _,p in ipairs(tt) do  
  27.     s = s .. "," .. escapeCSV(p)
  28.   end
  29.  
  30.   return string.sub(s, 2)      -- remove first comma
  31. end
  32.  
  33. function fromCSV(vals_csv, outputTypes)
  34.   local t = {}
  35.   local i = 0
  36.   for line in vals_csv:gmatch("[^" .. "," .. "]*") do
  37.     i = i + 1
  38.        
  39.     if #outputTypes == 1 and outputTypes[1] == "number" then
  40.       t[i] = tonumber(line)
  41.     elseif outputTypes[i] == "number" then
  42.       t[i] = tonumber(line)
  43.     else
  44.       t[i] = line
  45.     end
  46.   end
  47.   return t
  48. end
  49.  
  50. function multiUserInput(outputTypes, windowName, numberOfParams, paramNames, defaultVals, saveVals, valsPersist, valsKeySuffix)
  51.   --[[ returns retval, input values as a table
  52.   Examples:
  53.  
  54.   retval, inputs = multiUserInput({"number"}, "Test", 3, {"Threshold", "Ratio", "Output"}, {-18, 4, 6}, true, false, "")
  55.   if retval then msg(inputs[1]) end
  56.  
  57.   Note: outputTypes can take a 1-element table, which will apply that value type to all inputs.
  58.     Otherwise specify output type for each value
  59.   --]]
  60.  
  61.   paramNames_csv = toCSV(paramNames)
  62.  
  63.   if saveVals and reaper.HasExtState("Main", scriptName .. "_Vals" .. valsKeySuffix) then -- restore previous values on run if saveVals is true and the values exist
  64.     defaultVals_csv = reaper.GetExtState("Main", scriptName .. "_Vals" .. valsKeySuffix)
  65.   else
  66.     defaultVals_csv = toCSV(defaultVals)
  67.   end
  68.  
  69.  
  70.  
  71.   retval, retvals_csv = reaper.GetUserInputs(tostring(windowName), numberOfParams, paramNames_csv, defaultVals_csv)
  72.  
  73.   if retval and saveVals then
  74.     reaper.SetExtState("Main", scriptName .. "_Vals" .. valsKeySuffix, retvals_csv, valsPersist) -- save new defaults
  75.   end
  76.  
  77.   if retval then
  78.     vals = fromCSV(retvals_csv, outputTypes)
  79.   else
  80.     vals = nil
  81.   end
  82.  
  83.   return retval, vals
  84. end
  85.  
  86. function error(type)
  87.   if type == 0 then
  88.     msgStr = "Error. Invalid input values. Choose a number of octaves between 1 and " .. maxOcts ..
  89.     " and a number of samples per octave between 1 and 12"
  90.   elseif type == 1 then
  91.     msgStr = "Error. Select a midi item."
  92.   end
  93.   msg(msgStr)
  94. end
  95.  
  96.  
  97. ----- END OF FUNCTIONS
  98.  
  99. function main()
  100.   reaper.Undo_BeginBlock()
  101.  
  102.   if promptForSettings then
  103.     prompt = reaper.ShowMessageBox( "Are your current render settings correct? Click 'No' to change settings", scriptName, 4 ) -- returns 6 for yes, 7 for no
  104.     if prompt == 7 then
  105.       reaper.Main_OnCommand(40015, 0)
  106.       return
  107.     end
  108.   end
  109.  
  110.   local success, retvals_t = multiUserInput({"number"}, scriptName, 2, {"No. of octaves up", "No. of samples per octave"}, {4, 4}, true, false, "")
  111.  
  112.   if success then
  113.  
  114.     -- select only first item if midi item, else abort
  115.     item = reaper.GetSelectedMediaItem(0, 0) -- first item
  116.     isMidi = reaper.TakeIsMIDI( reaper.GetActiveTake(item) )
  117.  
  118.     if not isMidi then
  119.       error(1)
  120.       return
  121.     end
  122.  
  123.     for k = 0, reaper.CountSelectedMediaItems(0) - 1 do
  124.       reaper.SetMediaItemSelected(reaper.GetSelectedMediaItem(0, k), false)
  125.     end
  126.  
  127.     reaper.SetMediaItemSelected(item, true)
  128.    
  129.  
  130.  
  131.  
  132.     local octs, sampsPerOct = retvals_t[1], retvals_t[2]
  133.     --msg(octs .. " and " .. sampsPerOct)
  134.  
  135.     if octs < 1 or octs > maxOcts or sampsPerOct < 1 or sampsPerOct > 12 then
  136.       error(0)
  137.       return
  138.     end
  139.  
  140.     local transp = math.floor(12 / sampsPerOct) -- calculate amount of semitones to transpose each sample
  141.     local samps = octs * sampsPerOct
  142.  
  143.     for i = 1, samps do
  144.       reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWSMARKERLIST10"), 0) -- delete all regions
  145.  
  146.       for j = 1, transp do
  147.         reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_UP_SEMI"), 0) -- transpose midi item up one semitone
  148.       end
  149.  
  150.       reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_NAME"), 0) -- name midi item from note
  151.       reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWS_REGIONSFROMITEMS"), 0) -- create region named from item
  152.       reaper.Main_OnCommand(42230, 0) -- render using most recent settings
  153.    
  154.     end
  155.  
  156.     -- clean up
  157.     for i = 1, samps * transp do
  158.       reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_DN_SEMI"), 0) -- transpose midi item down one semitone
  159.     end
  160.     reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWSMARKERLIST10"), 0) -- delete all regions
  161.     reaper.Main_OnCommand(reaper.NamedCommandLookup("_FNG_MIDI_NAME"), 0) -- name midi item from note
  162.  
  163.   end
  164.  
  165.  
  166.  
  167.   reaper.Undo_EndBlock(scriptName, -1)
  168. end
  169.  
  170. reaper.PreventUIRefresh(1)
  171. main()
  172. reaper.PreventUIRefresh(-1)
  173. reaper.UpdateArrange()
  174. -- reaper.defer(function() end)   -- Prevent undo if necessary
  175.  
Add Comment
Please, Sign In to add comment