From 90474792a807096cfbf1c390947f5f0c65d1c34b Mon Sep 17 00:00:00 2001 From: david Date: Fri, 24 Apr 2020 22:22:48 -0500 Subject: [PATCH] add code --- VB.ahk | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 VB.ahk diff --git a/VB.ahk b/VB.ahk new file mode 100644 index 0000000..36230c3 --- /dev/null +++ b/VB.ahk @@ -0,0 +1,235 @@ +#NoEnv +#SingleInstance force +#MaxHotkeysPerInterval 99000000 +#HotkeyInterval 99000000 +#KeyHistory 0 +#UseHook +ListLines Off +SendMode Input ; Recommended for new scripts due to its superior speed and reliability. +SetTitleMatchMode RegEx +StringCaseSense Off +SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. + +OnExit("cleanup_before_exit") +SetFormat, Float, 0.3 +global VMR_FUNCTIONS := {} +global VMR_DLL_DRIVE := "C:" +global VMR_DLL_DIRPATH := "Program Files (x86)\VB\Voicemeeter" +global VMR_DLL_FILENAME_32 := "VoicemeeterRemote.dll" +global VMR_DLL_FILENAME_64 := "VoicemeeterRemote64.dll" +global VMR_DLL_FULL_PATH := VMR_DLL_DRIVE . "\" . VMR_DLL_DIRPATH . "\" +Sleep, 500 +if (A_Is64bitOS) { + VMR_DLL_FULL_PATH .= VMR_DLL_FILENAME_64 +} else { + VMR_DLL_FULL_PATH .= VMR_DLL_FILENAME_32 +} + +; == START OF EXECUTION == +; ======================== + +; Load the VoicemeeterRemote DLL: +; This returns a module handle +global VMR_MODULE := DllCall("LoadLibrary", "Str", VMR_DLL_FULL_PATH, "Ptr") +if (ErrorLevel || VMR_MODULE == 0) + die("Attempt to load VoiceMeeter Remote DLL failed.") + +; Populate VMR_FUNCTIONS +add_vmr_function("Login") +add_vmr_function("Logout") +add_vmr_function("RunVoicemeeter") +add_vmr_function("SetParameterFloat") +add_vmr_function("GetParameterFloat") +add_vmr_function("IsParametersDirty") + +; "Login" to Voicemeeter, by calling the function in the DLL named 'VBVMR_Login()'... +login_result := DllCall(VMR_FUNCTIONS["Login"], "Int") +if (ErrorLevel || login_result < 0) + die("VoiceMeeter Remote login failed.") + +; If the login returns 1, that apparently means that Voicemeeter isn't running, +; so we start it; pass 1 to run Voicemeeter, or 2 for Voicemeeter Banana: +if (login_result == 1) { + DllCall(VMR_FUNCTIONS["RunVoicemeeter"], "Int", 2, "Int") + if (ErrorLevel) + die("Attempt to run VoiceMeeter failed.") + Sleep 2000 +} + +; == HOTKEYS == +; ============= + +Volume_Up:: + If GetKeyState("Media_Stop") + { + cLvl := readParam("Strip[2]" . ".Gain") + if (cLvl != "") + { + cLvl += 2 + adjustVolLvl("Strip[2]" . ".Gain", cLvl) + } + Return + } + + b0M := Round(readParam("Bus[0]" . ".Mute")) + b1M := Round(readParam("Bus[1]" . ".Mute")) + + if !(b0M) + { + cLvl := readParam("Bus[0]" . ".Gain") + if (cLvl != "") + { + cLvl += 2 + adjustVolLvl("Bus[0]" . ".Gain", cLvl) + } + } + + if !(b1M) + { + cLvl := readParam("Bus[1]" . ".Gain") + if (cLvl != "") + { + cLvl += 2 + adjustVolLvl("Bus[1]" . ".Gain", cLvl) + } + } +return + +Volume_Down:: + If GetKeyState("Media_Stop") + { + cLvl := readParam("Strip[2]" . ".Gain") + if (cLvl != "") + { + cLvl -= 2 + adjustVolLvl("Strip[2]" . ".Gain", cLvl) + } + Return + } + + b0M := Round(readParam("Bus[0]" . ".Mute")) + b1M := Round(readParam("Bus[1]" . ".Mute")) + + if !(b0M) + { + cLvl := readParam("Bus[0]" . ".Gain") + if (cLvl != "") + { + cLvl -= 2 + adjustVolLvl("Bus[0]" . ".Gain", cLvl) + } + } + + if !(b1M) + { + cLvl := readParam("Bus[1]" . ".Gain") + if (cLvl != "") + { + cLvl -= 2 + adjustVolLvl("Bus[1]" . ".Gain", cLvl) + } + } +return + +Volume_Mute:: + If GetKeyState("Media_Stop") + { + b0M := Round(readParam("Bus[0]" . ".Mute")) + b1M := Round(readParam("Bus[1]" . ".Mute")) + cM := b0M + b1M + + if (cM = "2") + { + adjustMute("Bus[0]" . ".Mute", b0Ms) + adjustMute("Bus[1]" . ".Mute", b1Ms) + } else { + if !(b0M) + { + b0Ms := True + } else { + b0Ms := False + } + + if !(b1M) + { + b1Ms := True + } else { + b1Ms := False + } + adjustMute("Bus[0]" . ".Mute", "0") + adjustMute("Bus[1]" . ".Mute", "0") + } + Return + } + cM := Round(readParam("Bus[0]" . ".Mute")) + if (cM) + { + adjustMute("Bus[0]" . ".Mute", "1") + adjustMute("Bus[1]" . ".Mute", "0") + } + if !(cM) + { + adjustMute("Bus[0]" . ".Mute", "0") + adjustMute("Bus[1]" . ".Mute", "1") + } +return + + +; == Functions == +; =============== +readParam(loc){ + Loop + { + pDirty := DLLCall(VMR_FUNCTIONS["IsParametersDirty"]) ;Check if parameters have changed. + if (pDirty==0) ;0 = no new paramters. + break + else if (pDirty<0) ;-1 = error, -2 = no server + return "" + else ;1 = New parameters -> update your display. (this only applies if YOU have a display, couldn't find any code to update VM display which can get off sometimes) + if A_Index > 200 + return "" + sleep, 20 + } + tParamVal := 0.0 + NumPut(0.0, tParamVal, 0, "Float") + statusLvl := DllCall(VMR_FUNCTIONS["GetParameterFloat"], "AStr", loc, "Ptr", &tParamVal, "Int") + tParamVal := NumGet(tParamVal, 0, "Float") + if (statusLvl < 0) + return "" + else + return tParamVal +} + +adjustVolLvl(loc, tVol) { + if (tVol > 12.0) + tVol = 12.0 + else if (tVol < -60.0) + tVol = -60.0 + DllCall(VMR_FUNCTIONS["SetParameterFloat"], "AStr", loc, "Float", tVol, "Int") +} + +adjustMute(loc, tM) { + if (tM = 0) + tM = 1 + else + tM = 0 + DllCall(VMR_FUNCTIONS["SetParameterFloat"], "AStr", loc, "Float", tM, "Int") +} + +add_vmr_function(func_name) { + VMR_FUNCTIONS[func_name] := DllCall("GetProcAddress", "Ptr", VMR_MODULE, "AStr", "VBVMR_" . func_name, "Ptr") + if (ErrorLevel || VMR_FUNCTIONS[func_name] == 0) + die("Failed to register VMR function " . func_name . ".") +} + +cleanup_before_exit(exit_reason, exit_code) { + DllCall(VMR_FUNCTIONS["Logout"], "Int") + ; OnExit functions must return 0 to allow the app to exit. + return 0 +} + +die(die_string:="UNSPECIFIED FATAL ERROR.", exit_status:=254) { + MsgBox 16, FATAL ERROR, %die_string% + ExitApp exit_status +} +