-- folders are represented as tree nodes
-- items are selectables
-- play functionality with volume knob and autoplay
-- drag and drop functionalities
-- drop to the arrange view (priority)
-- drop to the media explorer
-- search bar functionality
-- Store the reference to the currently playing preview
local current_preview = nil
-- Variables for search functionality
local search_query = ""
local last_search_query = ""
local search_cache = {}
local debounce_timer = 0
-- Function to check if a directory contains any matching files
function directory_contains_matching_files(path)
if search_cache[path] ~= nil then
return search_cache[path]
end
code
local idx = 0
local contains_matches = false
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
idx = idx + 1
end
if not contains_matches then
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_contains_matching_files(full_path) then
contains_matches = true
break
end
end
idx = idx + 1
end
end
search_cache[path] = contains_matches
return contains_matches
end
-- Function to create a directory tree and search for files recursively
function create_directory_tree(path, depth)
if depth > 5 then return end -- Limit search depth
code
Assistant
-- 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
-- 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()
To create a more complete and functional Media Explorer with ImGui, there are still a few things we might want to consider:
Volume Control:
Implementing volume control for the previews.
Autoplay:
Add an option for autoplay functionality when selecting an item.
Drag and Drop:
Enable drag and drop for files into the arrange view or media explorer.
UI Improvement:
Adding more UI components to make it more user-friendly.
Below is the script with added volume control and basic workspace for drag and drop functionality.
lua
-- Define the script context globally
ctx = reaper.ImGui_CreateContext("Media Explorer")
-- Define default volume
local volume = 1.0
-- Store the reference to the currently playing preview
local current_preview = nil
-- Variables for search functionality
local search_query = ""
local last_search_query = ""
local search_cache = {}
local debounce_timer = 0
-- Function to check if a directory contains any matching files
function directory_contains_matching_files(path)
if search_cache[path] ~= nil then
return search_cache[path]
end
local idx = 0
local contains_matches = false
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
contains_matches = true
break
end
idx = idx + 1
end
if not contains_matches then
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_contains_matching_files(full_path) then
contains_matches = true
break
end
end
idx = idx + 1
end
end
search_cache[path] = contains_matches
return contains_matches
end
-- Function to create a directory tree and search for files 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
-- Implementing the drag and drop functionality
if reaper.ImGui_BeginDragDropSource(ctx) then
reaper.ImGui_SetDragDropPayload(ctx, "DND_DEMO", file)
reaper.ImGui_Text(ctx, file)
reaper.ImGui_EndDragDropSource(ctx)
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)
reaper.CF_SetPreviewVolume(current_preview, volume)
-- 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
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
-- Volume control
changed, volume = reaper.ImGui_VSliderFloat(ctx, "Volume", volume, 0.0, 1.0)
if changed and current_preview then
reaper.CF_SetPreviewVolume(current_preview, volume)
end
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
main()
Key Changes and Additions:
Volume Slider: Added a vertical volume slider to control the playback volume of the previewed media.
Search Functionality: Modified the condition to ensure the search results update on query change.
Drag and Drop: Added basic drag and drop functionality within the create_directory_tree() function. You can expand this as per your needs.
UI Layout and Elements:
Search Bar: Allows user to search through media files within the directory.
Volume Slider: Controls the volume of the currently playing media preview.
Directory and File Listing: Displays media files and directories. Select a file to preview it.
Further Enhancements:
Autoplay: Integrate autoplay functionality triggered by item selection.
Improved Drag and Drop: Enhance the drag and drop to interact with the arrange view or media explorer pane.
UI Styling: Improve styling and layout using ImGui functionalities to make the tool more user-friendly.
This ensures a functional media explorer where users can easily search, preview, and drag media items.