function create_directory_tree(path, depth)
if depth > 5 then return end -- Limit search depth
code
-- 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$") 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
Assistant
-- 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
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()
This script serves as a Media Explorer for Reaper, displaying directories and allowing for previewing of audio files. The explorer uses Dear ImGui for the UI and allows recursive browsing up to a specified depth. Here's a detailed breakdown of what each part of the script does:
Global Variables
ctx: An ImGui context, created at the end of the script.
search_query: A string for holding user search input.
last_search_query: Keeps track of the previous search query to detect changes.
debounce_timer: Implements a debounce mechanism to delay search query processing (300 milliseconds).
current_preview: Holds the reference to the currently playing file, stopping it before playing a new one.
Functions
create_directory_tree(path, depth)
Recursively generates a tree structure representing directories and files.
Parameters:
path: The root directory path to start searching from.
depth: The current depth of the recursion.
Limits the recursion to a depth of 5 to prevent excessive traversal.
Uses reaper.EnumerateSubdirectories to list subdirectories and reaper.EnumerateFiles to list files.
Files and directories starting with . (usually hidden in Unix-based systems) are ignored.
Files matching .wav or .mp3 are displayed if they match the search query.
preview_file(file)
Handles the previewing of an audio file.
Parameters:
file: The full path of the file to preview.
Stops any currently playing preview using reaper.CF_Preview_StopAll.
Creates a PCM_Source from the file and starts a preview using reaper.CF_Preview_Play.
Shows an error message if preview playback fails.
main()
The main function for the UI and user interaction.
Sets up the ImGui window with reaper.ImGui_SetNextWindowDockID.
Displays a search input box using reaper.ImGui_InputText.
If the text changes, a debounce timer resets to delay search execution.
Clears the cache and updates if the search query changes after the debounce period.
Calls create_directory_tree to display directories and files that match the search criteria.
Uses reaper.defer to continuously update the UI.
start the application
The script creates an ImGui context (ctx) and calls main() to start the application.
Complete Script
lua
Missing Function directory_contains_matching_files
You need to define the function directory_contains_matching_files which is referenced but not defined in the provided script. Its purpose is likely to check if a directory contains files matching the specified criteria. Here's a possible implementation:
lua
function directory_contains_matching_files(path)
-- Enumerate files within directory
local idx = 0
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
if (file:match("%.wav$") or file:match("%.mp3$")) and (search_query == "" or file:lower():find(search_query:lower(), 1, true)) then
return true
end
idx = idx + 1
end
return false
end
Additional Notes
Ensure the Reaper API functions (reaper.ImGui_*, reaper.EnumerateSubdirectories, etc.) are available and functioning as expected.
Depending on your version of Reaper and the extensions loaded, some functionality might differ or might need additional setup.
Not yet included in a published release.
-- Global variables
search_query = ""
last_search_query = ""
debounce_timer = 0
search_cache = {}
current_preview = nil
ctx = reaper.ImGui_CreateContext("Media Explorer")
-- Function to create directory tree 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$") 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
-- Main function to create UI and handle search
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
main()