-- Store the reference to the currently playing preview
local current_preview = nil
-- Variables for search functionality
local search_query = ""
-- Function to create a directory tree and search for files recursively
function create_directory_tree(path, depth)
-- Enumerate subdirectories and files
local idx = 0
while true do
local subdir = reaper.EnumerateSubdirectories(path, idx)
if not subdir then break end
code
local full_path = path .. "/" .. subdir
-- Check if the directory matches the search query
if search_query == "" or subdir:lower():find(search_query:lower(), 1, true) then
if 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
-- 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
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
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
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
-- Adjust the indentation spacing
-- 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()
This script uses Reaper's API functions and ImGui (Immediate Mode GUI library) to create a media explorer within Reaper's environment. It allows you to navigate through directories, search for audio files, and preview the selected files within Reaper. Here's a breakdown of the key components of this script:
Variables:
current_preview: Stores the reference to the currently playing audio preview.
search_query: Holds the user's input for file searching.
create_directory_tree function:
Recursively enumerates subdirectories and files within a given path.
Directory and file names are matched against the search_query.
If a file (wav or mp3) is selected, it triggers the preview_file function.
preview_file function:
Stops any currently playing preview.
Creates a PCM_Source from the given file and plays it using Reaper's preview functions.
Displays an error message if preview playback fails.
main function:
Initializes the ImGui window.
Displays a search input box.
Calls the create_directory_tree function to build the directory tree.
Uses reaper.defer to keep the script running in the background.
Initialization:
Creates an ImGui context and initiates the main function call.
Improvements and Considerations
Error Handling: Ensure robust error handling for cases such as missing directories, unsupported file formats, or failures in ImGui operations.
Performance: For large directories, consider adding optimizations such as caching results or incrementally loading directory content to improve responsiveness.
Customization: Allow the base path of the directory to be configurable via user input or project settings instead of hardcoding.
Running the Script
Ensure Reaper and its SWS extension are installed to access necessary API functions.
Copy the script into Reaper's script editor (Alt+E > New or Load) and run it.
Here's the complete script for easy reference:
lua
-- Store the reference to the currently playing preview
local current_preview = nil
-- Variables for search functionality
local search_query = ""
-- Function to create a directory tree and search for files recursively
function create_directory_tree(path, depth)
-- Enumerate subdirectories and files
local 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 matches the search query
if search_query == "" or subdir:lower():find(search_query:lower(), 1, true) then
if 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
-- 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
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
-- Adjust the indentation spacing
-- 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()
This script provides a basic yet functional media explorer interface within Reaper for audio file previewing. You can build upon it by adding more features (e.g., drag-and-drop support, more detailed file info, etc.) to enhance its functionality further.