function create_directory_tree(path, depth)
-- Enumerate subdirectories and files
local idx = 0
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
code
-- 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
idx = idx + 1
end
-- Enumerate subdirectories
idx = 0
while true do
local subdir = reaper.EnumerateSubdirectories(path, idx)
if not subdir then break end
local full_path = path .. "/" .. subdir
if reaper.ImGui_TreeNode(ctx, subdir) then
create_directory_tree(full_path, depth + 1)
reaper.ImGui_TreePop(ctx)
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
Assistant
The code you have provided is quite specific to the Reaper DAW (Digital Audio Workstation) and uses its Lua API to manage and preview audio files. Here's a quick breakdown with enhancements and optimized practices:
Proper comment headers:
Adding comments at the beginning of sections/functions to explain what they do is always beneficial for readability and maintenance.
Error Handling:
Always check for potential errors especially when dealing with file systems and external APIs.
Optimization:
Current snippet enumerates files and directories separately which is fine but could be optimized depending on the actual user need. You may also want to limit the recursion depth when enumerating subdirectories.
Here's an enhanced version of your function:
lua
-- Reaper Lua script to create a directory tree and preview audio files
-- Function to recursively create a directory tree UI
function create_directory_tree(path, depth)
-- Set a maximal depth to prevent infinite recursion
if depth > 10 then return end
-- Enumerate files in the current directory
local idx = 0
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
-- Check if the file matches audio extensions and optionally a 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
idx = idx + 1
end
-- Enumerate subdirectories in the current directory
idx = 0
while true do
local subdir = reaper.EnumerateSubdirectories(path, idx)
if not subdir then break end
local full_path = path .. "/" .. subdir
-- Check if the directory node is expanded
if reaper.ImGui_TreeNode(ctx, subdir) then
-- Recursively create tree for the subdirectory
create_directory_tree(full_path, depth + 1)
reaper.ImGui_TreePop(ctx)
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
-- Attempt to create a PCM_Source from the file
local source = reaper.PCM_Source_CreateFromFile(file)
-- Ensure source creation was successful
if source then
-- Create and play the preview object
current_preview = reaper.CF_CreatePreview(source)
local play_success = reaper.CF_Preview_Play(current_preview)
if not play_success then
reaper.ShowMessageBox("Failed to play preview: " .. file, "Error", 0)
end
else
reaper.ShowMessageBox("Failed to create source from file: " .. file, "Error", 0)
end
end
Explanation of Changes:
Recursion Depth Limitation: Added a depth check-in create_directory_tree to prevent potential infinite recursion.
Error Handling: Added error handling for source creation in the preview_file function. This is essential to make your script more robust and user-friendly.
Comments: Clarified the purpose of functions and processes with comments for better readability.
Feel free to adjust the depth limit or add more audio extensions to the match conditions as suited to your requirements. The above script assumes ctx and search_query are globally defined variables in your script's context.