From 62ad51c6b830186b8085ec3fc9a8612cf59c235f Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 16 Dec 2022 17:16:02 +0000 Subject: [PATCH] examples now using relative import path obs example now using obs-powershell module. README updated. --- examples/cli/CLI.ps1 | 14 ++---- examples/nextbus/GoTo-NextBus.ps1 | 4 +- examples/obs/README.md | 3 +- examples/obs/Vm-Obs-Sync.ps1 | 83 +++++++++++++++++++------------ 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/examples/cli/CLI.ps1 b/examples/cli/CLI.ps1 index 5ee926b..7ce1ce7 100644 --- a/examples/cli/CLI.ps1 +++ b/examples/cli/CLI.ps1 @@ -1,11 +1,11 @@ param( [switch]$interactive, [switch]$output, - [String]$kind="banana", + [String]$kind = "banana", [String[]]$script = @() ) -Import-Module Voicemeeter +Import-Module ..\..\lib\Voicemeeter.psm1 function get-value { param([object]$vmr, [string]$line) @@ -39,7 +39,7 @@ function msgHandler { function read-hostuntilempty { param([object]$vmr) - while (($line = Read-Host) -cne[string]::Empty) { msgHandler -vmr $vmr -line $line } + while (($line = Read-Host) -cne [string]::Empty) { msgHandler -vmr $vmr -line $line } } @@ -47,11 +47,7 @@ function main { [object]$vmr try { - switch ($kind) { - "basic" { $vmr = Get-RemoteBasic } - "banana" { $vmr = Get-RemoteBanana } - "potato" { $vmr = Get-RemotePotato } - } + $vmr = Connect-Voicemeeter -Kind $kind if ($interactive) { "Press to exit" | Write-Host @@ -62,7 +58,7 @@ function main { msgHandler -vmr $vmr -line $_ } } - finally { $vmr.Logout() } + finally { Disconnect-Voicemeeter } } if ($MyInvocation.InvocationName -ne '.') { main } diff --git a/examples/nextbus/GoTo-NextBus.ps1 b/examples/nextbus/GoTo-NextBus.ps1 index 4274930..187440e 100644 --- a/examples/nextbus/GoTo-NextBus.ps1 +++ b/examples/nextbus/GoTo-NextBus.ps1 @@ -7,7 +7,7 @@ Credits go to @bobsupercow #> -Import-Module Voicemeeter +Import-Module ..\..\lib\Voicemeeter.psm1 try { $vmr = Get-RemotePotato @@ -20,7 +20,7 @@ try { $bus = $_ # 2) if (-not $bus.mute) { - "bus " + $bus.index + " is unmuted... muting it" | Write-Host + "bus " + $bus.index + " is unmuted... muting it" | Write-Host $unmutedIndex = $buses.IndexOf($bus) $bus.mute = $true diff --git a/examples/obs/README.md b/examples/obs/README.md index 2a0cfa0..4a9d1fe 100644 --- a/examples/obs/README.md +++ b/examples/obs/README.md @@ -5,11 +5,12 @@ Demonstrates how to sync Voicemeeter states with OBS scene switches. ## Requirements - [OBS Studio 28+](https://obsproject.com/) -- [OBSWebSocket for Powershell](https://github.com/onyx-and-iris/OBSWebSocket-Powershell) +- [OBS-Powershell](https://github.com/StartAutomating/obs-powershell) ## Use This example assumes the following: + - OBS connection info saved in `config.psd1`, placed next to `Vm-Obs-Sync.ps1`: ```psd1 diff --git a/examples/obs/Vm-Obs-Sync.ps1 b/examples/obs/Vm-Obs-Sync.ps1 index ddb9ed7..40654fd 100644 --- a/examples/obs/Vm-Obs-Sync.ps1 +++ b/examples/obs/Vm-Obs-Sync.ps1 @@ -1,26 +1,43 @@ -Import-Module Voicemeeter -Import-Module OBSWebSocket +Import-Module ..\..\lib\Voicemeeter.psm1 +Import-Module obs-powershell $VerbosePreference = "Continue" -$info = @{ - START = "Toggling Strip 0 mute" - BRB = "Setting Strip 0 gain to -8.3" - END = "Setting Strip 0 mono to `$false" - LIVE = "Setting Strip 0 color_x to 0.3" -} +function CurrentProgramSceneChanged { + param([System.Object]$data) + Write-Host "Switched to scene", $data.sceneName -function CurrentProgramSceneChanged($data) { - "Switched to scene " + $data.sceneName | Write-Host - - switch ($data.SceneName) { - "START" { $vmr.strip[0].mute = !$vmr.strip[0].mute } - "BRB" { $vmr.strip[0].gain = -8.3 } - "END" { $vmr.strip[0].mono = $true } - "LIVE" { $vmr.strip[0].color_x = 0.3 } + switch ($data.sceneName) { + "START" { + $vmr.strip[0].mute = !$vmr.strip[0].mute + "Toggling Strip 0 mute" + } + "BRB" { + $vmr.strip[0].gain = -8.3 + "Setting Strip 0 gain to -8.3" + } + "END" { + $vmr.strip[0].mono = $true + "Setting Strip 0 mono to `$false" + } + "LIVE" { + $vmr.strip[0].color_x = 0.3 + "Setting Strip 0 color_x to 0.3" + } default { "Expected START, BRB, END or LIVE scene" | Write-Warning; return } } - $info[$data.SceneName] | Write-Host +} + +function ExitStarted { + param([System.Object]$data) + "OBS closing has begun!" | Write-Host + break +} + +function eventHandler($data) { + if (Get-Command $data.eventType -ErrorAction SilentlyContinue) { + & $data.eventType -data $data.eventData + } } function ConnFromFile { @@ -29,22 +46,26 @@ function ConnFromFile { } function main { - try { - $vmr = Get-RemoteBasic - $conn = ConnFromFile - $r_client = Get-OBSRequest -hostname $conn.hostname -port $conn.port -pass $conn.password - $resp = $r_client.getVersion() - "obs version:" + $resp.obsVersion | Write-Host - "websocket version:" + $resp.obsWebSocketVersion | Write-Host + $vmr = Connect-Voicemeeter -Kind "basic" - $e_client = Get-OBSEvent -hostname $conn.hostname -port $conn.port -pass $conn.password - $callbacks = @("CurrentProgramSceneChanged", ${function:CurrentProgramSceneChanged}) - $e_client.Register($callbacks) - } finally { - $r_client.TearDown() - $e_client.TearDown() - $vmr.Logout() + $conn = ConnFromFile + $job = Watch-OBS -WebSocketURI "ws://$($conn.host):$($conn.port)" -WebSocketToken $conn.password + + try { + while ($true) { + Receive-Job -Job $job | ForEach-Object { + $data = $_.MessageData + + if ($data.op -eq 5) { + eventHandler($data.d) + } + } + } } + finally { + Disconnect-OBS + Disconnect-Voicemeeter + } } if ($MyInvocation.InvocationName -ne '.') { main }