Skip to content
The Chat Archive
gpt-4o-2024-05-13· OpenAI WildChat-4.8M (AI2)

-- Required Reaper and ImGui libraries -- Constants for the debounce delay (in seconds)…

  1. User

    -- Required Reaper and ImGui libraries

    -- Constants for the debounce delay (in seconds) local DEBOUNCE_DELAY = 0.3

    -- Store the reference to the currently playing preview local current_preview = nil

    -- Variables for search functionality local search_query = "" local last_search_query = "" local search_cache = {} local debounce_timer = 0

    -- Function to check if a directory contains any matching files function directory_contains_matching_files(path, query) -- Check the cache first to avoid redundant file operations if search_cache[path] ~= nil then return search_cache[path] end

    local idx = 0
    local contains_matches = false
    while true do
        local file = reaper.EnumerateFiles(path, idx)
        if not file then break end
    
        if string.match(file:lower(), query:lower()) then
            contains_matches = true
            break
        end
    
        idx = idx + 1
    end
    
    search_cache[path] = contains_matches
    return contains_matches
    

    end

    -- Function to handle media playback function handle_playback(item_path, volume) if current_preview ~= nil then reaper.Main_OnCommand(current_preview, 0) -- Stop previous preview end

    -- Start new preview
    current_preview = reaper.Main_OnCommand(reaper.NamedCommandLookup(‘_S&M_PREVIEW’), 0)
    reaper.CSurf_SetPlayState(1) -- Play
    reaper.SetMediaTrackInfo_Value(current_preview, "D_VOL", volume)
    

    end

    -- Function to render the media explorer function render_media_explorer() -- Draw the search bar search_query = ImGui.InputText("Search", search_query, 256)

    -- Add debounce logic to minimize searches
    if search_query ~= last_search_query then
        debounce_timer = DEBOUNCE_DELAY
    end
    
    if debounce_timer > 0 then
        debounce_timer = debounce_timer - ImGui.GetIO().DeltaTime
    else
        last_search_query = search_query
        -- Execute search logic here
    end
    
    -- Draw folders as tree nodes
    if ImGui.TreeNode("Media Folder 1") then
        -- Check if directory contains matching files
        if directory_contains_matching_files("path/to/media_folder_1", search_query) then
            local idx = 0
            while true do
                local file = reaper.EnumerateFiles("path/to/media_folder_1", idx)
                if not file then break end
    
                -- Render matching files as selectables
                if string.match(file:lower(), search_query:lower()) then
                    if ImGui.Selectable(file) then
                        handle_playback("path/to/media_folder_1/" … file, 1.0) -- Default volume is 1.0
                    end
                end
    
                idx = idx + 1
            end
        end
        ImGui.TreePop()
    end
    

    end

    -- Main loop to render the entire interface reaper.defer(function() -- Initialize ImGui ImGui.Begin("Media Explorer")

    -- Render media explorer UI
    render_media_explorer()
    
    -- End of ImGui
    ImGui.End()
    
    -- Repeat the main loop
    reaper.defer(main)
    

    end)

  2. Assistant

    To create a functional media explorer extension for Reaper using Lua and ImGui, we need to ensure we're correctly handling Reaper's API and ImGui's workflows. Here's a revised version of the provided script with key fixes to ensure it works as expected.

    Key Fixes and Improvements:

    1. Correct use of Lua syntax (like .. for string concatenation).
    2. Proper handling of Reaper command and media preview APIs.
    3. Structure for managing the main loop with reaper.defer.

    Make sure to have necessary libraries (Reaper and ImGui) installed and properly set up.

    Here is the corrected version of your script:

    -- Required Reaper and ImGui libraries
    reaper = reaper -- Make sure REAPER API is available, this line might be redundant in some environments
    local imgui = require 'imgui' -- Make sure you've installed ImGui Lua bindings
    
    -- Constants for the debounce delay (in seconds)
    local DEBOUNCE_DELAY = 0.3
    
    -- Store the reference to the currently playing preview
    local current_preview = -1
    
    -- Variables for search functionality
    local search_query = ""
    local last_search_query = ""
    local search_cache = {}
    local debounce_timer = 0
    
    -- Function to check if a directory contains any matching files
    function directory_contains_matching_files(path, query)
        -- Check the cache first to avoid redundant file operations
        if search_cache[path] ~= nil then
            return search_cache[path]
        end
    
        local idx = 0
        local contains_matches = false
        while true do
            local file = reaper.EnumerateFiles(path, idx)
            if not file then break end
    
            if string.match(file:lower(), query:lower()) then
                contains_matches = true
                break
            end
    
            idx = idx + 1
        end
    
        search_cache[path] = contains_matches
        return contains_matches
    end
    
    -- Function to handle media playback
    function handle_playback(item_path, volume)
        if current_preview >= 0 then
            reaper.StopPreview(current_preview) -- Stop previous preview
        end
    
        -- Start new preview
        local preview_id = reaper.Preview(item_path, 0, volume, 0, 1, -1)
        current_preview = preview_id
    end
    
    -- Function to render the media explorer
    function render_media_explorer()
        -- Draw the search bar
        local changed, new_search_query = imgui.InputText("Search", search_query, 256)
        if changed then
            search_query = new_search_query
            debounce_timer = DEBOUNCE_DELAY
        end
    
        if debounce_timer > 0 then
            debounce_timer = debounce_timer - imgui.GetIO().DeltaTime
        else
            last_search_query = search_query
        end
    
        -- Draw folders as tree nodes
        if imgui.TreeNode("Media Folder 1") then
            -- Check if directory contains matching files
            if directory_contains_matching_files("path/to/media_folder_1", last_search_query) then
                local idx = 0
                while true do
                    local file = reaper.EnumerateFiles("path/to/media_folder_1", idx)
                    if not file then break end
    
                    -- Render matching files as selectables
                    if string.match(file:lower(), last_search_query:lower()) then
                        if imgui.Selectable(file) then
                            handle_playback("path/to/media_folder_1/" .. file, 1.0) -- Default volume is 1.0
                        end
                    end
    
                    idx = idx + 1
                end
            end
            imgui.TreePop()
        end
    end
    
    -- Main loop to render the entire interface
    function main()
        -- Initialize ImGui
        imgui.Begin("Media Explorer")
    
        -- Render media explorer UI
        render_media_explorer()
    
        -- End of ImGui
        imgui.End()
    
        -- Repeat the main loop
        reaper.defer(main)
    end
    
    -- Start main loop
    main()
    

    Important Notes:

    1. ImGui Library: Ensure that the ImGui Lua bindings are correctly installed and you're requiring the right module.
    2. Reaper API Functions: Functions like reaper.StopPreview and reaper.Preview might need adjusting based on the actual Reaper API you're using.
    3. Correct Paths: Replace "path/to/media_folder_1" with the actual path to your media files.

    This script initializes ImGui and sets up a frame where you can input a search query, which will then filter the displayed media files from a given directory.