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

-- Store the reference to the currently playing preview local currentpreview = nil --…

  1. User

    -- 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) 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
    
        -- Ignore hidden files and directories
        if not file:match("^%.") then
            -- Check if the file matches the search query
            if (file:match("%.wav$") then
                contains_matches = true
                break
            end
        end
    
        idx = idx + 1
    end
    
    if not contains_matches then
        idx = 0
        while true do
            local subdir = reaper.EnumerateSubdirectories(path, idx)
            if not subdir then break end
    
            -- Ignore hidden files and directories
            if not subdir:match("^%.") then
                local full_path = path .. "/" .. subdir
                if directory_contains_matching_files(full_path) then
                    contains_matches = true
                    break
                end
            end
    
            idx = idx + 1
        end
    end
    
    search_cache[path] = contains_matches
    return contains_matches
    

    end

    -- Function to create a directory tree and search for files recursively function create_directory_tree(path, depth) if depth > 5 then return end -- Limit search depth

    -- Enumerate subdirectories
    local idx = 0
    while true do
        local subdir = reaper.EnumerateSubdirectories(path, idx)
        if not subdir then break end
    
        -- Ignore hidden files and directories
        if not subdir:match("^%.") then
            local full_path = path .. "/" .. subdir
    
            -- Check if the subdir contains matching files
            if directory_contains_matching_files(full_path) then
                if reaper.ImGui_TreeNode(ctx, subdir) then
                    create_directory_tree(full_path, depth + 1)
                    reaper.ImGui_TreePop(ctx)
                end
            end
        end
    
        idx = idx + 1
    end
    
    -- Enumerate files
    idx = 0
    while true do
        local file = reaper.EnumerateFiles(path, idx)
        if not file then break end
    
        -- Ignore hidden files and directories
        if not file:match("^%.") then
            -- Check if the file matches the search query
            if (file:match("%.wav$") then
                if reaper.ImGui_Selectable(ctx, file) then
                    -- Preview the selected file
                    preview_file(path .. "/" .. file)
                end
            end
        end
    
        idx = idx + 1
    end
    

    end

    -- Function to preview a selected file within Reaper function preview_file(file) -- Stop any currently playing preview if current_preview then reaper.CF_Preview_StopAll() current_preview = nil end

    -- Create a PCM_Source from the file
    local source = reaper.PCM_Source_CreateFromFile(file)
    
    -- Create a preview object
    current_preview = reaper.CF_CreatePreview(source)
    
    -- Start the preview
    local play_success = reaper.CF_Preview_Play(current_preview)
    if not play_success then
        reaper.ShowMessageBox("Failed to play preview: " .. file, "Error", 0)
    end
    

    end

    function main() reaper.ImGui_SetNextWindowDockID(ctx, -7) local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer", true)

    if is_open then
        -- Search input box
        local changed, text = reaper.ImGui_InputText(ctx, "Search", search_query, 256)
        if changed then
            search_query = text
            debounce_timer = reaper.time_precise() + 0.3  -- 300 ms debounce
        end
    
        if reaper.time_precise() > debounce_timer then
            if search_query ~= last_search_query then
                search_cache = {}  -- Clear cache when the search query changes
                last_search_query = search_query
            end
    
            -- Call the function to create the directory tree and search for files
            create_directory_tree("E:\\Sample Packs\\COLLECTION", 0)
        end
    
        reaper.defer(main)
    end
    
    reaper.ImGui_End(ctx)
    

    end

    ctx = reaper.ImGui_CreateContext("ctx") main()

  2. Assistant

    Your code for a media explorer in Reaper using ReaImGui and Lua looks great! It focuses on searching WAV files in a directory and its subdirectories with some neat functionalities like previewing files and managing a search cache.

    Here’s a breakdown and a few improvements or fixes to consider:

    1. Corrections on Syntax Errors:

      • In directory_contains_matching_files, you have a misplaced parenthesis:
        if (file:match("%.wav$") then
        
        should be:
        if file:match("%.wav$") then
        
      • Similarly, in create_directory_tree, you should remove the extra parenthesis:
        if (file:match("%.wav$") then
        
        should be:
        if file:match("%.wav$") then
        
    2. Debouncing Improvement:

      • The debounce timer is a good addition to avoid multiple rapid executions. Make sure to handle cases when the search query becomes empty, resetting the search_cache.
    3. GUI Improvement:

      • Ensure you handle the ImGui context and window properly by checking is_open and ImGui_End.
    4. Consistency and Readability:

      • Consider using a standardized pattern to avoid hidden (dot-prefixed) files and folders.
      • Handle errors or edge cases (e.g., non-existent directories).

    Here's the adjusted script with these points in mind:

    -- 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)
        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
    
            -- Ignore hidden files and directories
            if not file:match("^%.") then
                -- Check if the file matches the search query
                if file:match("%.wav$") then
                    contains_matches = true
                    break
                end
            end
            idx = idx + 1
        end
    
        if not contains_matches then
            idx = 0
            while true do
                local subdir = reaper.EnumerateSubdirectories(path, idx)
                if not subdir then break end
    
                -- Ignore hidden files and directories
                if not subdir:match("^%.") then
                    local full_path = path .. "/" .. subdir
                    if directory_contains_matching_files(full_path) then
                        contains_matches = true
                        break
                    end
                end
                idx = idx + 1
            end
        end
    
        search_cache[path] = contains_matches
        return contains_matches
    end
    
    -- Function to create a directory tree and search for files recursively
    function create_directory_tree(path, depth)
        if depth > 5 then return end  -- Limit search depth
    
        -- Enumerate subdirectories
        local idx = 0
        while true do
            local subdir = reaper.EnumerateSubdirectories(path, idx)
            if not subdir then break end
    
            -- Ignore hidden files and directories
            if not subdir:match("^%.") then
                local full_path = path .. "/" .. subdir
    
                -- Check if the subdir contains matching files
                if directory_contains_matching_files(full_path) then
                    if reaper.ImGui_TreeNode(ctx, subdir) then
                        create_directory_tree(full_path, depth + 1)
                        reaper.ImGui_TreePop(ctx)
                    end
                end
            end
            idx = idx + 1
        end
    
        -- Enumerate files
        idx = 0
        while true do
            local file = reaper.EnumerateFiles(path, idx)
            if not file then break end
    
            -- Ignore hidden files and directories
            if not file:match("^%.") then
                -- Check if the file matches the search query
                if file:match("%.wav$") then
                    if reaper.ImGui_Selectable(ctx, file) then
                        -- Preview the selected file
                        preview_file(path .. "/" .. file)
                    end
                end
            end
            idx = idx + 1
        end
    end
    
    -- Function to preview a selected file within Reaper
    function preview_file(file)
        -- Stop any currently playing preview
        if current_preview then
            reaper.CF_Preview_StopAll()
            current_preview = nil
        end
    
        -- Create a PCM_Source from the file
        local source = reaper.PCM_Source_CreateFromFile(file)
    
        -- Create a preview object
        current_preview = reaper.CF_CreatePreview(source)
    
        -- Start the preview
        local play_success = reaper.CF_Preview_Play(current_preview)
        if not play_success then
            reaper.ShowMessageBox("Failed to play preview: " .. file, "Error", 0)
        end
    end
    
    function main()
        reaper.ImGui_SetNextWindowDockID(ctx, -7)
        local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer", true)
    
        if is_open then
            -- Search input box
            local changed, text = reaper.ImGui_InputText(ctx, "Search", search_query, 256)
            if changed then
                search_query = text
                debounce_timer = reaper.time_precise() + 0.3  -- 300 ms debounce
            end
    
            if reaper.time_precise() > debounce_timer then
                if search_query ~= last_search_query then
                    search_cache = {}  -- Clear cache when the search query changes
                    last_search_query = search_query
                end
    
                -- Call the function to create the directory tree and search for files
                create_directory_tree("E:\\Sample Packs\\COLLECTION", 0)
            end
    
            reaper.ImGui_End(ctx)
        end
    
        reaper.defer(main)
    end
    
    ctx = reaper.ImGui_CreateContext("ctx")
    main()
    

    Key Points:

    • I corrected the misplaced parentheses in if statements.
    • Ensured GUI handling by proper wrapping with ImGui_Begin and ImGui_End.
    • The debounce logic will ensure efficient handling of the search input.

    Your script is now more robust and easier to follow. Remember to test it within the Reaper environment to ensure all functionalities work as expected.