mirror of
https://github.com/onyx-and-iris/voicemeeter-api-powershell.git
synced 2026-08-17 01:24:23 +00:00
319 lines
8.8 KiB
PowerShell
319 lines
8.8 KiB
PowerShell
. $PSScriptRoot\errors.ps1
|
|
. $PSScriptRoot\binding.ps1
|
|
|
|
function Login {
|
|
param(
|
|
[string]$kindId
|
|
)
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Login()
|
|
if ($retval -notin @(0, 1, -2)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Login')
|
|
}
|
|
|
|
switch ($retval) {
|
|
0 {
|
|
WaitForConnection
|
|
}
|
|
1 {
|
|
if ($kindId -eq 'none') {
|
|
'Logged in to Voicemeeter Remote.' | Write-Verbose
|
|
}
|
|
else {
|
|
'Logged in to Voicemeeter Remote but audio engine and GUI not launched. Launching engine now.' | Write-Verbose
|
|
RunVm -kindId $kindId
|
|
}
|
|
}
|
|
-2 {
|
|
throw [LoginError]::new('Login may only be called once per session.')
|
|
}
|
|
}
|
|
}
|
|
|
|
function WaitForConnection {
|
|
$timeout = New-TimeSpan -Seconds 2
|
|
$sw = [diagnostics.stopwatch]::StartNew()
|
|
$exception = $null
|
|
do {
|
|
Start-Sleep -m 100
|
|
try {
|
|
'Successfully connected to Voicemeeter [' + $(VmType).ToUpper() + '] Version ' + $(VmVersion) | Write-Verbose
|
|
$exception = $null
|
|
break
|
|
}
|
|
catch [CAPIError] {
|
|
$exception = $_
|
|
$exception | Write-Debug
|
|
}
|
|
} while ($sw.elapsed -lt $timeout)
|
|
|
|
if ($null -ne $exception) {
|
|
throw [VMRemoteError]::new('Timeout waiting to connect to Voicemeeter.')
|
|
}
|
|
|
|
while (P_Dirty -or M_Dirty) { Start-Sleep -m 1 }
|
|
}
|
|
|
|
function Logout {
|
|
Start-Sleep -m 100
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Logout()
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Logout')
|
|
}
|
|
if ($retval -eq 0) { 'Sucessfully logged out' | Write-Verbose }
|
|
}
|
|
|
|
function Run {
|
|
param(
|
|
[int]$id
|
|
)
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([Int64]$id)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_RunVoicemeeter')
|
|
}
|
|
}
|
|
|
|
function RunVm {
|
|
param(
|
|
[string]$kindId
|
|
)
|
|
$kinds = @{
|
|
'basic' = $(if ([Environment]::Is64BitOperatingSystem) { 4 } else { 1 })
|
|
'banana' = $(if ([Environment]::Is64BitOperatingSystem) { 5 } else { 2 })
|
|
'potato' = $(if ([Environment]::Is64BitOperatingSystem) { 6 } else { 3 })
|
|
}
|
|
Run -id $kinds[$kindId]
|
|
WaitForConnection
|
|
}
|
|
|
|
function RunApp {
|
|
param(
|
|
[string]$appId
|
|
)
|
|
$apps = @{
|
|
'devicecheck' = 10
|
|
'macrobuttons' = 11
|
|
'streamerview' = 12
|
|
'busmatrix8' = 13
|
|
'busgeq15' = 14
|
|
'vban2midi' = 15
|
|
'cablecontrolpanel' = 20
|
|
'auxcontrolpanel' = 21
|
|
'vaio3controlpanel' = 22
|
|
'vaiocontrolpanel' = 23
|
|
}
|
|
Run -id $apps[$appId]
|
|
}
|
|
|
|
function P_Dirty {
|
|
$retval = [Voicemeeter.Remote]::VBVMR_IsParametersDirty()
|
|
if ($retval -notin @(0, 1)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_IsParametersDirty')
|
|
}
|
|
[bool]$retval
|
|
}
|
|
|
|
function M_Dirty {
|
|
$retval = [Voicemeeter.Remote]::VBVMR_MacroButton_IsDirty()
|
|
if ($retval -notin @(0, 1)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_MacroButton_IsDirty')
|
|
}
|
|
[bool]$retval
|
|
}
|
|
|
|
function VmType {
|
|
New-Variable -Name ptr -Value 0
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterType([ref]$ptr)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_GetVoicemeeterType')
|
|
}
|
|
switch ($ptr) {
|
|
1 { return 'basic' }
|
|
2 { return 'banana' }
|
|
3 { return 'potato' }
|
|
}
|
|
}
|
|
|
|
function VmVersion {
|
|
New-Variable -Name ptr -Value 0
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterVersion([ref]$ptr)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_GetVoicemeeterVersion')
|
|
}
|
|
$v1 = ($ptr -band 0xFF000000) -shr 24
|
|
$v2 = ($ptr -band 0x00FF0000) -shr 16
|
|
$v3 = ($ptr -band 0x0000FF00) -shr 8
|
|
$v4 = $ptr -band 0x000000FF
|
|
"$v1.$v2.$v3.$v4"
|
|
}
|
|
|
|
|
|
function Param_Get {
|
|
param(
|
|
[string]$PARAM, [bool]$IS_STRING = $false
|
|
)
|
|
Start-Sleep -m 30
|
|
while (P_Dirty) { Start-Sleep -m 1 }
|
|
|
|
if ($IS_STRING) {
|
|
$BYTES = [System.Byte[]]::new(512)
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterStringA($PARAM, $BYTES)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_GetParameterStringA')
|
|
}
|
|
[System.Text.Encoding]::ASCII.GetString($BYTES).Trim([char]0)
|
|
}
|
|
else {
|
|
New-Variable -Name ptr -Value 0.0
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterFloat($PARAM, [ref]$ptr)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_GetParameterFloat')
|
|
}
|
|
[single]$ptr
|
|
}
|
|
}
|
|
|
|
function Param_Set {
|
|
param(
|
|
[string]$PARAM, [Object]$VALUE
|
|
)
|
|
if ($VALUE -is [string]) {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterStringA($PARAM, $VALUE)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_SetParameterStringA')
|
|
}
|
|
}
|
|
else {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterFloat($PARAM, $VALUE)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_SetParameterFloat')
|
|
}
|
|
}
|
|
}
|
|
|
|
function MB_Set {
|
|
param(
|
|
[int64]$ID, [single]$SET, [int64]$MODE
|
|
)
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_SetStatus($ID, $SET, $MODE)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_MacroButton_SetStatus')
|
|
}
|
|
}
|
|
|
|
function MB_Get {
|
|
param(
|
|
[int64]$ID, [int64]$MODE
|
|
)
|
|
Start-Sleep -m 50
|
|
while (M_Dirty) { Start-Sleep -m 1 }
|
|
|
|
New-Variable -Name ptr -Value 0.0
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_GetStatus($ID, [ref]$ptr, $MODE)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_MacroButton_GetStatus')
|
|
}
|
|
[int]$ptr
|
|
}
|
|
|
|
function Param_Set_Multi {
|
|
param(
|
|
[hashtable]$HASH
|
|
)
|
|
foreach ($key in $HASH.keys) {
|
|
$classobj, $m2, $m3 = $key.Split('_')
|
|
if ($m2 -match '^\d+$') { $index = [int]$m2 } else { $index = [int]$m3 }
|
|
|
|
foreach ($h in $HASH[$key].GetEnumerator()) {
|
|
$property = $h.Name
|
|
$value = $h.Value
|
|
if ($value -in ('False', 'True')) { [System.Convert]::ToBoolean($value) }
|
|
|
|
switch ($classobj) {
|
|
'strip' { $this.strip[$index].$property = $value }
|
|
'bus' { $this.bus[$index].$property = $value }
|
|
{ ($_ -eq 'button') -or ($_ -eq 'mb') } { $this.button[$index].$property = $value }
|
|
'vban' { $this.vban.$m2[$index].$property = $value }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function Set_By_Script {
|
|
param(
|
|
[string]$script
|
|
)
|
|
if ($script.Length -gt 48000) {
|
|
throw [VMRemoteError]::new('Script size cannot be larger than 48kB')
|
|
}
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameters($script)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_SetParameters')
|
|
}
|
|
}
|
|
|
|
function Get_Level {
|
|
param(
|
|
[int64]$MODE, [int64]$INDEX
|
|
)
|
|
New-Variable -Name ptr -Value 0.0
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_GetLevel($MODE, $INDEX, [ref]$ptr)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_GetLevel')
|
|
}
|
|
[float]$ptr
|
|
}
|
|
|
|
function Device_Count {
|
|
param(
|
|
[bool]$IS_OUT = $false
|
|
)
|
|
if ($IS_OUT) {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Output_GetDeviceNumber()
|
|
if ($retval -lt 0) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Output_GetDeviceNumber')
|
|
}
|
|
}
|
|
else {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Input_GetDeviceNumber()
|
|
if ($retval -lt 0) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Input_GetDeviceNumber')
|
|
}
|
|
}
|
|
$retval
|
|
}
|
|
|
|
function Device_Desc {
|
|
param(
|
|
[int]$INDEX, [bool]$IS_OUT = $false
|
|
)
|
|
$driver = 0
|
|
$name = [System.Byte[]]::new(512)
|
|
$hardwareid = [System.Byte[]]::new(512)
|
|
|
|
if ($IS_OUT) {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Output_GetDeviceDescA($INDEX, [ref]$driver, $name, $hardwareid)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Output_GetDeviceDescA')
|
|
}
|
|
}
|
|
else {
|
|
$retval = [int][Voicemeeter.Remote]::VBVMR_Input_GetDeviceDescA($INDEX, [ref]$driver, $name, $hardwareid)
|
|
if ($retval -notin @(0)) {
|
|
throw [CAPIError]::new($retval, 'VBVMR_Input_GetDeviceDescA')
|
|
}
|
|
}
|
|
|
|
$drivers = @{
|
|
1 = 'mme'
|
|
3 = 'wdm'
|
|
4 = 'ks'
|
|
5 = 'asio'
|
|
}
|
|
|
|
[PSCustomObject]@{
|
|
Driver = $drivers[$driver]
|
|
Name = [System.Text.Encoding]::ASCII.GetString($name).Trim([char]0)
|
|
HardwareId = [System.Text.Encoding]::ASCII.GetString($hardwareid).Trim([char]0)
|
|
IsOutput = $IS_OUT
|
|
}
|
|
} |