Skip to content

Instantly share code, notes, and snippets.

@SorraTheOrc
Last active October 16, 2024 05:13
Show Gist options
  • Save SorraTheOrc/7a42f6619ef44e5171a9e77c93ba0277 to your computer and use it in GitHub Desktop.
Save SorraTheOrc/7a42f6619ef44e5171a9e77c93ba0277 to your computer and use it in GitHub Desktop.
Reaper Script to insert a number of midi notes - useful for SFX generation
--[[
* ReaScript Name: Insert x MIDI notes, one every y seconds, starting at the current playhead position
* Screenshot:
* Author: Wizards Code
* Author URI: httpL//www.thewizardscode.com
* Repository:
* Repository URI:
* Licence: MIT
* REAPER: 7.0 (and likely earlier)
* Version: 0.1
--]]
--[[
* Changelog:
* v0.1.1 (2024-10-15) Added ability to randomize the notes
* v0.1 (2024-04-16)
+ Initial Release
--]]
-- Prompt the user for the number of triggers, the repeat interval, and whether to play or record on completion
retval, user_input = reaper.GetUserInputs("Number of Triggers, Repeat Interval, Note Length, MIDI Note and note range", 5,
"Enter number of triggers:,Enter repeat interval in seconds:,Enter note length in seconds:,Enter MIDI note:,Note range (+/-)",
"3,3,2,72,0")
if retval then
numberOfTriggers, repeatIntervalInSeconds, noteLengthInSeconds, midiNote, noteRange = user_input:match("([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)")
numberOfTriggers = tonumber(numberOfTriggers)
repeatIntervalInSeconds = tonumber(repeatIntervalInSeconds)
noteLengthInSeconds = tonumber(noteLengthInSeconds)
midiNote = tonumber(midiNote)
noteRange = tonumber(noteRange)
else
return
end
channel = 0
selected = false
muted = false
minVelocity = 80
maxVelocity = 120
startPos = 0
endPos = 120
track = reaper.GetSelectedTrack(0, 0)
if track == nil then
reaper.ShowMessageBox("No track selected", "Error", 0)
return
end
playheadPosition = reaper.GetCursorPosition()
for i = 0, numberOfTriggers - 1 do
item = reaper.CreateNewMIDIItemInProj(track, playheadPosition + i * repeatIntervalInSeconds, playheadPosition + i * repeatIntervalInSeconds + noteLengthInSeconds)
take = reaper.GetTake(item, 0)
if take == nil then
reaper.ShowMessageBox("No take found in the item", "Error", 0)
return
end
velocity = math.random(minVelocity, maxVelocity)
local ppqpos = reaper.MIDI_GetPPQPosFromProjTime(take, playheadPosition + i * repeatIntervalInSeconds)
local noteLengthInPPQ = reaper.TimeMap2_timeToQN(0, noteLengthInSeconds) * 960
local noteEnd = ppqpos + noteLengthInPPQ
local finalNote = midiNote + math.random(-noteRange, noteRange)
reaper.MIDI_InsertNote(take, selected, muted, ppqpos, noteEnd, channel, finalNote, velocity, false)
reaper.MIDI_Sort(take)
end
-- Create a loop around the inserted notes
-- reaper.GetSet_LoopTimeRange(true, true, playheadPosition, playheadPosition + numberOfTriggers * repeatIntervalInSeconds, false)
reaper.CSurf_OnPlay()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment