Skip to content
TheChatArchiveThe Chat Archive
gpt-4-turbo-2024-04-09· OpenAIAi2 WildChat-4.8M (AI2)

In reaper api lua, how would i go about finding the last touched parameter of an fx chain?

  1. User

    In reaper api lua, how would i go about finding the last touched parameter of an fx chain?

  2. Assistant

    In REAPER, using the ReaScript API with Lua to find the last touched FX parameter involves using specific functions designed to interact with the track and FX chain. Below, I provide a script example that demonstrates how to retrieve the last touched FX parameter on any track.

    This Lua script will identify the last touched FX parameter, and output in the REAPER console the track number, FX number, FX name, parameter number, and parameter name:

    function main()
        -- Get the last touched FX and its last touched parameter
        local retval, trackid, fxid, paramid = reaper.GetLastTouchedFX()
    
        if retval then
            -- Convert track ID to MediaTrack object
            local track = reaper.CSurf_TrackFromID(trackid, false)
            
            if track then
                -- Get the FX name
                local _, fxName = reaper.TrackFX_GetFXName(track, fxid, "")
                
                -- Get the parameter name
                local _, paramName = reaper.TrackFX_GetParamName(track, fxid, paramid, "")
                
                -- Print information to console
                local trackNumber = reaper.GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER")
                reaper.ShowConsoleMsg("Track: " .. tonumber(trackNumber) .. "\n")
                reaper.ShowConsoleMsg("FX ID: " .. fxid .. " (" .. fxName .. ")\n")
                reaper.ShowConsoleMsg("Parameter ID: " .. paramid .. " (" .. paramName .. ")\n")
            else
                reaper.ShowConsoleMsg("Track not found.")
            end
        else
            reaper.ShowConsoleMsg("No FX parameter was touched recently.")
        end
    end
    
    -- Run the script
    main()
    

    Explanation of the Script

    1. GetLastTouchedFX(): This function returns four values:

      • retval, which is true if an FX parameter was recently touched.
      • trackid (integer), ID of the track that contains the touched FX.
      • fxid (integer), index of the FX chain where the parameter was touched.
      • paramid (integer), index of the touched parameter in the FX.
    2. CSurf_TrackFromID(trackid, isMCP): Converts trackid to a MediaTrack object.

    3. TrackFX_GetFXName(track, fxid, ""): Retrieves the name of the FX that was touched.

    4. TrackFX_GetParamName(track, fxid, paramid, ""): Fetches the name of the parameter that was touched.

    5. ShowConsoleMsg(): Outputs gathered information to REAPER's console, which is helpful for debugging and verification.

    How to Run This Script

    1. Copy the script above.
    2. Open REAPER.
    3. Go to "Actions" > "New Action…" > "New ReaScript…".
    4. Paste the script into the script editor.
    5. Save the file with a .lua extension.
    6. Run the script from the Actions list to see the last touched parameter on the console.

    Make sure to test the script while working on a project with an FX chain, and after manually touching/modifying an FX parameter to update the internal "last touched" status in REAPER.

  3. User

    how would i get the last touched parameter of an fx chain of a certain track (i have it's index)

  4. Assistant

    To get the last touched FX parameter of a specific track when you already know the track index, you will need to modify the Lua script slightly. Essentially, you will be comparing the desired track index with the track index of the last touched FX. If they match, you will retrieve and report the details of the FX parameter. Here’s how you can achieve that:

    function get_last_touched_fx_parameter(track_index)
        -- Get the last touched FX and its parameter
        local retval, touched_trackid, fxid, paramid = reaper.GetLastTouchedFX()
    
        if retval then
            -- Get the track corresponding to the track index
            local track = reaper.GetTrack(0, track_index - 1)  -- -1 because Lua is zero-based index
    
            -- Check if the track index matches the trackid derived from GetLastTouchedFX()
            if reaper.CSurf_TrackToID(track, false) == touched_trackid then
                -- Get the FX name
                local _, fxName = reaper.TrackFX_GetFXName(track, fxid, "")
    
                -- Get the parameter name
                local _, paramName = reaper.TrackFX_GetParamName(track, fxid, paramid, "")
    
                -- Output information
                reaper.ShowConsoleMsg(string.format("Track Index: %d\nFX ID: %d (%s)\nParameter ID: %d (%s)\n",
                                                    track_index, fxid, fxName, paramid, paramName))
            else
                reaper.ShowConsoleMsg("Last touched FX is not on the specified track.\n")
            end
        else
            reaper.ShowConsoleMsg("No FX parameter was touched recently.\n")
        end
    end
    
    -- Replace '1' with your specific track index as needed
    get_last_touched_fx_parameter(1)
    

    Explanation of the Script:

    1. GetLastTouchedFX(): Same as before, it checks which FX was last touched.
    2. Track Index Comparison: The script gets the track using GetTrack() with the desired track index. Then it confirms that this track was the one with the last touched FX using CSurf_TrackToID() to get the track ID from the track object for comparison.
    3. Extracting and Showing FX and Parameter Names: If the touched FX is on the intended track, the script retrieves and shows the names of the FX and the parameter.

    Differences from the Original Script:

    • The function now takes a track_index as an argument to specify which track to look on.
    • There's a check to ensure the last touched FX was on the provided track.

    Note:

    • The script assumes track indexing starting from 1 as standard in LUA API scripts, which matches user expectation from REAPER's UI, but internally it is corrected for zero-based indexing.
    • Make sure the track index is valid for your project; indices that are out of range will cause the script to not function correctly.

    How to Run This Script:

    Follow the same steps as outlined previously to run this script in REAINER. Make sure to replace '1' in get_last_touched_fx_parameter(1) with the index of the track you are interested in.