Skip to content

Instantly share code, notes, and snippets.

@X-Raym
Last active March 6, 2017 21:20
Show Gist options
  • Select an option

  • Save X-Raym/b282dcdef6328bd310462944d5bb8cae to your computer and use it in GitHub Desktop.

Select an option

Save X-Raym/b282dcdef6328bd310462944d5bb8cae to your computer and use it in GitHub Desktop.
Move to next pitch position in first selected items
reaper.PreventUIRefresh(1)
console = true
threshold = 0.01 -- arbitrary
reaper.ClearConsole()
function Msg(val)
if console then
reaper.ShowConsoleMsg(tostring(val).."\n")
end
end
function GetSourceItemFrequencyAtPosition2( take, pos, nch, ns, buf )
local rv = reaper.GetMediaItemTake_Peaks(take,1000.0,pos,nch,ns,115,buf)
if rv & (1<<24) and (rv&0xfffff) > 0 then
local spl = buf[nch*ns*2 + 1]
local freq = tonumber(string.format("%d",spl&0x7fff))
local tonal = tonumber(string.format("%f",(spl>>15)/16384.0))
return freq, tonal
end
end
local reaper = reaper
item = reaper.GetSelectedMediaItem(0,0)
if item then
take = reaper.GetActiveTake(item,0)
if take and not reaper.TakeIsMIDI(take) then
pos = reaper.GetCursorPosition()
itemstart = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
itemend = itemstart + (reaper.GetMediaItemInfo_Value(item, "D_LENGTH"))
if itemstart <= pos and pos < itemend then
source = reaper.GetMediaItemTake_Source(take)
nch = reaper.GetMediaSourceNumChannels(source)
ns = 1
buf = reaper.new_array(ns*3*nch)
startfreq = GetSourceItemFrequencyAtPosition2( take, pos, nch, ns, buf )
semup = startfreq*1.05946309436 -- up a semitone
semdown = startfreq*0.94387431268 -- down a semitone
Msg("Source")
Msg(startfreq)
Msg("Target")
while pos < itemend do
pos = pos + threshold
newfreq, tonali = GetSourceItemFrequencyAtPosition2( take, pos, nch, ns, buf )
Msg(newfreq)
if (newfreq >= semup or newfreq <= semdown) and (tonali > 0.8) then
reaper.SetEditCurPos(pos, 1, 0)
break
end
if pos >= itemend then break end
end
end
end
end
reaper.PreventUIRefresh(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment