idx = 0
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
code
-- Ignore hidden files and directories
if not file:match("^%.") then
-- Check if the file matches the search query
if (file:match("%.wav$") or file:match("%.mp3$")) and (search_query == "" or file:lower():find(search_query:lower(), 1, true)) then
if reaper.ImGui_Selectable(ctx, file) then
-- Preview the selected file
preview_file(path .. "/" .. file)
end
end
end
Assistant
It looks like you're working with the Lua scripting interface of REAPER (a digital audio workstation) and using the ImGui library to create a file browser/dialog for audio files. This script enumerates the files in a given directory and allows the user to interact with them, filtering by hidden files and search queries, and providing functionality for previewing audio files.
Here's a more complete snippet that should help you achieve that functionality:
lua
local reaper = reaper -- Ensure REAPER functions are available
local path = "YOUR_DIRECTORY_PATH" -- Set the directory path containing audio files
local search_query = "" -- If you have an input field for a search query, set this variable accordingly
local function preview_file(filepath)
-- Function to preview the selected file
-- You might implement REAPER's preview functionality here
reaper.ShowConsoleMsg("Previewing file: " .. filepath .. "\n")
end
-- Run script in defer loop to keep the ImGui context alive
reaper.defer(function()
-- Initialize ImGui context if needed
local ctx = reaper.ImGui_CreateContext('Audio Files Browser')
-- Create and show the window
reaper.ImGui_SetNextWindowSize(ctx, 400, 300, reaper.ImGui_Cond_FirstUseEver)
reaper.ImGui_Begin(ctx, 'Audio Files Browser', true)
-- Input box for search query
local _, new_search_query = reaper.ImGui_InputText(ctx, "Search", search_query)
if new_search_query ~= nil then
search_query = new_search_query
end
local idx = 0
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
-- Increment index for next iteration
idx = idx + 1
-- Ignore hidden files (starting with a dot) and directories
if not file:match("^%.") then
-- Check if the file matches the search query and is an audio file
if (file:match("%.wav$") or file:match("%.mp3$")) and (search_query == "" or file:lower():find(search_query:lower(), 1, true)) then
if reaper.ImGui_Selectable(ctx, file) then
-- Preview the selected file
preview_file(path .. "/" .. file)
end
end
end
end
-- End the window
reaper.ImGui_End(ctx)
-- Keep the script running by deferring it again
reaper.defer(function() end)
end)
Explanation of the Code:
Initialization:
Define the path to the directory containing your audio files.
search_query variable is for inputting a search term.
ImGui Context:
Create an ImGui context using reaper.ImGui_CreateContext.
Set up the window with specific dimensions using reaper.ImGui_SetNextWindowSize.
Search Input:
Use reaper.ImGui_InputText to create an input box for the search query.
File Enumeration and Filtering:
Enumerate files in the specified directory using reaper.EnumerateFiles.
Filter out hidden files (those starting with a dot) and directories.
Further filter files based on file type (e.g., .wav or .mp3) and the search query.
File Selection and Preview:
Use reaper.ImGui_Selectable to create selectable items for each file.
When a file is selected, call the preview_file function to preview it.
Defer Loop:
The script is wrapped in reaper.defer() to keep the ImGui context and the script running in REAPER's defer loop.
Feel free to adjust the path, search query handling, and the preview_file function to match your specific needs in REAPER.