Skip to content

Instantly share code, notes, and snippets.

@wastee
Created September 28, 2021 00:24
Show Gist options
  • Save wastee/d7017cd36deea6bf4754586d5b71baa2 to your computer and use it in GitHub Desktop.
Save wastee/d7017cd36deea6bf4754586d5b71baa2 to your computer and use it in GitHub Desktop.
REAPER 使用 reascript 添加 fx + 点击 fx 窗口示范
-- fx_name 为 vst 文件名字,避免 vst 和 vst3 重名
fx_name = "Ozone 9 Match EQ.dll"
-- fx_name_query 为用于查询有无添加,查询不能写 vst 文件名字
fx_name_query = "Ozone 9 Match EQ"
-- 按钮的坐标
button_x = 685
button_y = 385
-- 播放的秒数
second = 2.22
-- 点击插件状态
click_status = "stop"
-- 插件窗口 classname
fx_window_classname = "iZ_OZONE9MEQ_v9100"
has_api = "yes"
-- 检查 js reascript 是否安装
if reaper.JS_Window_Find == nil then
has_api = reaper.MB("缺少 js reascript api。\n请自行安装 或 到 Reapack 搜索安装。", "错误", 0)
end
-- 打印函数
function print(text)
-- reaper.ClearConsole()
text = tostring(text)
reaper.ShowConsoleMsg(text)
reaper.ShowConsoleMsg("\n")
end
-- 左键点击函数
function left_click(hwnd, x, y)
reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONUP", 0, 0, x, y)
end
-- 播放函数
function playback()
-- cursor 初始位置
start_at = reaper.GetCursorPositionEx(0)
-- 定义起始和结束
stop_at = start_at + second
playing_at = reaper.GetPlayPositionEx(0)
play_status = reaper.GetPlayStateEx()
if click_status == "clicked" and play_status == 0 then
-- 开始播放
reaper.Main_OnCommand(1007, 0)
elseif playing_at > stop_at then
-- 停止播放
reaper.Main_OnCommand(1016, 0)
click_status = "finish"
return
end
-- 后台运行,相当于 while
reaper.defer(playback)
end
function run()
is_open = false
track_idx = 0
track_count = reaper.CountSelectedTracks(0)
play_state = false
-- 停止播放
reaper.Main_OnCommand(1016, 0)
-- 只能有一个轨道 且 只添加一个VST
if track_count == 1 then
track = reaper.GetSelectedTrack(0, track_idx)
-- 根据 fx_name_query 查询 fx 是否已经添加
fx_idx = reaper.TrackFX_GetByName(track, fx_name_query, 0)
if (fx_idx == -1) then
-- 若无添加,根据实际 vst 文件名字(fx_name)添加
fx_idx = reaper.TrackFX_GetByName(track, fx_name, 1)
end
is_open = reaper.TrackFX_GetOpen(track, fx_idx)
-- 检查 fx 窗口是否为打开
if (is_open == false) then
-- 发送点击事件,点击指定位置
-- 浮动显示 fx 窗口
reaper.TrackFX_Show(track, fx_idx, 3)
-- 让这个窗口在最上层(不必要)
-- reaper.JS_Window_SetFocus(fx_window)
-- 获取 fx 窗口的 HWND
fx_window = reaper.JS_Window_Find(fx_name_query, 0)
-- 获取实际屏幕的 x 和 y
-- real_x, real_y = reaper.JS_Window_ClientToScreen(fx_window, button_x, button_y)
if fx_window then
arr = reaper.new_array({}, 1024)
reaper.JS_Window_ArrayAllChild(fx_window, arr)
childs = arr.table()
-- 获取 button HWDN
for j = 1, #childs do
hwnd = reaper.JS_Window_HandleFromAddress(childs[j])
if reaper.JS_Window_GetClassName(hwnd):match(fx_window_classname) then
button = hwnd
break
end
end
-- 点击
if click_status == "stop" then
left_click(hwnd, button_x, button_y)
click_status = "clicked"
end
playback()
-- 点击
function click_to_stop()
if click_status == "finish" then
left_click(hwnd, button_x, button_y)
return
end
-- 后台运行,相当于 while
reaper.defer(click_to_stop)
end
if click_status == "clicked" then
click_to_stop()
end
end
else
-- 隐藏已经显示的 fx 窗口
reaper.TrackFX_Show(track, fx_idx, 2)
end
end
end
-- 如果有 js reascript 则运行
if has_api == "yes" then
run()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment