Old 07-27-2021, 08:39 AM   #1
ferropop
Human being with feelings
 
ferropop's Avatar
 
Join Date: Jan 2016
Location: Los Angeles, CA
Posts: 3,126
Default Create Env Points at Razor Edges

I've tried and failed at this seemingly-simple task.

Can anyone lend a hand? Simply want an action that creates 2 envelope points - one at each edgepoint of the razor edit.

Thank you !
ferropop is online now   Reply With Quote
Old 07-28-2021, 10:51 PM   #2
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 2,009
Default

Code:
 
function GetEnvelopePointsInRange(envelopeTrack, areaStart, areaEnd)
    local envelopePoints = {}

    for i = 1, reaper.CountEnvelopePoints(envelopeTrack) do
        local retval, time, value, shape, tension, selected = reaper.GetEnvelopePoint(envelopeTrack, i - 1)

        if time >= areaStart and time <= areaEnd then --point is in range
            envelopePoints[#envelopePoints + 1] = {
                id = i-1 ,
                time = time,
                value = value,
                shape = shape,
                tension = tension,
                selected = selected
            }
        end
    end

    return envelopePoints
end

function GetItemsInRange(track, areaStart, areaEnd)
    local items = {}
    local itemCount = reaper.CountTrackMediaItems(track)
    for k = 0, itemCount - 1 do 
        local item = reaper.GetTrackMediaItem(track, k)
        local pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
        local length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH")
        local itemEndPos = pos+length

        --check if item is in area bounds
        if (itemEndPos > areaStart and itemEndPos <= areaEnd) or
            (pos >= areaStart and pos < areaEnd) or
            (pos <= areaStart and itemEndPos >= areaEnd) then
                table.insert(items,item)
        end
    end

    return items
end

function GetRazorEdits()
    local trackCount = reaper.CountTracks(0)
    local areaMap = {}
    for i = 0, trackCount - 1 do
        local track = reaper.GetTrack(0, i)
        local ret, area = reaper.GetSetMediaTrackInfo_String(track, 'P_RAZOREDITS', '', false)
        if area ~= '' then
            --PARSE STRING
            local str = {}
            for j in string.gmatch(area, "%S+") do
                table.insert(str, j)
            end
        
            --FILL AREA DATA
            local j = 1
            while j <= #str do
                --area data
                local areaStart = tonumber(str[j])
                local areaEnd = tonumber(str[j+1])
                local GUID = str[j+2]
                local isEnvelope = GUID ~= '""'

                --get item/envelope data
                local items = {}
                local envelopeName, envelope
                local envelopePoints
                
                if not isEnvelope then
                    items = GetItemsInRange(track, areaStart, areaEnd)
                else
                    envelope = reaper.GetTrackEnvelopeByChunkName(track, GUID:sub(2, -2))
                    local ret, envName = reaper.GetEnvelopeName(envelope)

                    envelopeName = envName
                    envelopePoints = GetEnvelopePointsInRange(envelope, areaStart, areaEnd)
                end

                local areaData = {
                    areaStart = areaStart,
                    areaEnd = areaEnd,
                    
                    track = track,
                    items = items,
                    
                    --envelope data
                    isEnvelope = isEnvelope,
                    envelope = envelope,
                    envelopeName = envelopeName,
                    envelopePoints = envelopePoints,
                    GUID = GUID:sub(2, -2)
                }

                table.insert(areaMap, areaData)

                j = j + 3
            end
        end
    end

    return areaMap
end

function print(arg)
    reaper.ShowConsoleMsg(tostring(arg).."\n")
end

function AddPoint(env, time)
    local samplerate = reaper.GetSetProjectInfo( 0, 'PROJECT_SRATE', 0, false )
    local retval, value, dVdS, ddVdS, dddVdS = reaper.Envelope_Evaluate( env, time, samplerate, 1 )
    -- TODO here it should check if there is already a point at time if there is delete
    reaper.InsertEnvelopePoint(env, time, value, 0, 0, false, false)
end

local re_table = GetRazorEdits()
for k,re in pairs(re_table) do
    if re.isEnvelope == true then
        AddPoint(re.envelope ,re.areaStart)
        AddPoint(re.envelope ,re.areaEnd)
    end
end
using BirdBird functions https://forum.cockos.com/showthread.php?t=241604

PS: I leaved a todo for you in the code, inside the AddPoint function it should check if there is a point at the time before inserting and if there is delete or not insert. Currently it is putting point over point. So probably you want to correct that. Shouldn't be hard try using reaper.GetEnvelopePointByTime( envelope, time ) EDIT: I did here just a reminder: GetEnvelopePointByTime() will always return the index of the point in time. OR the previous point if there is not a point there. So always check the answer against the time you want to add the point

Last edited by daniellumertz; 07-28-2021 at 10:58 PM.
daniellumertz is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.