-- Variables for search functionality
local search_query = ""
-- Function to check if a directory or its subdirectories contain matching files
function directory_has_matching_files(path)
local idx = 0
code
-- Enumerate files first to check if there are matching files in the directory
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$") or file:match("%.mp3$")) and (search_query == "" or file:lower():find(search_query:lower(), 1, true)) then
return true
end
end
idx = idx + 1
end
-- Enumerate subdirectories
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_has_matching_files(full_path) then
return true
end
end
idx = idx + 1
end
return false
end
-- Function to create a directory tree and search for files recursively
function create_directory_tree(path, depth)
-- Enumerate subdirectories and files
local idx = 0
code
Assistant
-- Enumerate subdirectories
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_has_matching_files(full_path) and reaper.ImGui_TreeNode(ctx, subdir) then
create_directory_tree(full_path, depth + 1)
reaper.ImGui_TreePop(ctx)
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$") 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
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
code
-- 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)
code
if is_open then
-- Search input box
local changed, text = reaper.ImGui_InputText(ctx, "Search", search_query, 256)
if changed then
search_query = text
end
-- Call the function to create the directory tree and search for files
create_directory_tree("E:\\Sample Packs\\COLLECTION", 0)
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
ctx = reaper.ImGui_CreateContext("ctx")
main()
Your code aims to create a simple media explorer in Reaper that allows you to search for audio files within a directory and its subdirectories. Here's an organized breakdown of the code and some potential improvements or additions you could consider:
Code Breakdown
Global Variables & Initial Setup
current_preview keeps track of the currently previewed file.
search_query stores the user's search input.
Function Definitions
directory_has_matching_files(path): Checks if a directory or its subdirectories contain any files that match the search query and have .wav or .mp3 extensions.
create_directory_tree(path, depth): Recursively creates a directory tree in the ImGui window. It also ensures only directories/files matching the search criteria are displayed and provides file selection for preview.
preview_file(file): Plays the selected audio file in Reaper, stopping any current preview first.
Main Function
main(): The main function which sets up the ImGui window and triggers the directory tree creation.
lua
local current_preview = nil
-- Variables for search functionality
local search_query = ""
-- Function to check if a directory or its subdirectories contain matching files
function directory_has_matching_files(path)
local idx = 0
-- Enumerate files first to check if there are matching files in the directory
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$") or file:match("%.mp3$")) and (search_query == "" or file:lower():find(search_query:lower(), 1, true)) then
return true
end
end
idx = idx + 1
end
-- Enumerate subdirectories
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_has_matching_files(full_path) then
return true
end
end
idx = idx + 1
end
return false
end
-- Function to create a directory tree and search for files recursively
function create_directory_tree(path, depth)
-- Enumerate subdirectories and files
local idx = 0
-- Enumerate subdirectories
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_has_matching_files(full_path) and reaper.ImGui_TreeNode(ctx, subdir) then
create_directory_tree(full_path, depth + 1)
reaper.ImGui_TreePop(ctx)
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$") 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
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
end
-- Call the function to create the directory tree and search for files
create_directory_tree("E:\\Sample Packs\\COLLECTION", 0)
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
-- Create the ImGui context and start the main function
ctx = reaper.ImGui_CreateContext("ctx")
main()
Suggestions for Improvement:
Search Filters Enhancements:
Consider adding more filters, such as filtering by file size, date modified, etc., to make the search function more versatile.
Add support for other audio file formats if needed (.flac, .ogg, etc.).
Clear Search & Reset:
Implement a "Clear Search" button to reset the search query easily.
lua
if reaper.ImGui_Button(ctx, "Clear Search") then
search_query = ""
end
Error Handling:
Incorporate more robust error handling for edge cases, such as permissions issues when accessing directories.
Performance Optimization:
Cache the directory structure to avoid re-scanning on every change/query to improve performance, especially for large directories.
Preview Control Improvements:
Implement additional controls for the preview, such as pause, stop or volume control.
By implementing these improvements, you can extend the functionality and usability of your media explorer script in Reaper.