diff --git a/examples/nextbus/GoTo-NextBus.ps1 b/examples/nextbus/GoTo-NextBus.ps1 index eb90efb..9b135f9 100644 --- a/examples/nextbus/GoTo-NextBus.ps1 +++ b/examples/nextbus/GoTo-NextBus.ps1 @@ -1,10 +1,10 @@ <# - 1) Loop through an array of bus objects. - 2) Mute first unmuted bus - 3) If next bus in array exists, unmute it, otherwise clear unmuted variable. - 4) If every bus in array is muted, unmute the first bus specified in array. - - Credits go to @bobsupercow +.SYNOPSIS + Rotates through specified Voicemeeter buses, unmuting one at a time. +.DESCRIPTION + This script connects to Voicemeeter Potato and allows the user to rotate through a set + of buses (1, 2, 4, and 6). When the user presses Enter, the next bus in the sequence is unmuted, + while all other specified buses are muted. The user can exit the rotation by typing 'Q'. #> [cmdletbinding()] @@ -12,36 +12,58 @@ param() Import-Module ..\..\lib\Voicemeeter.psm1 +<# + A class that accepts a list of Voicemeeter buses and unmutes them one at a time in a round-robin fashion +#> +class BusRotator { + [object]$vmr = $null + [int]$CurrentIndex = -1 + [object[]]$Buses + + BusRotator([object]$vmr, [object[]]$buses) { + $this.vmr = $vmr + $this.Buses = $buses + } + + hidden [object] GetNextBus() { + # Mute all buses in the list + foreach ($bus in $this.Buses) { + $bus.mute = $true + } + + # Determine the next bus to unmute + $this.CurrentIndex = ($this.CurrentIndex + 1) % $this.Buses.Count + + return $this.Buses[$this.CurrentIndex] + } + + [object] MuteNextBus() { + $nextBus = $this.GetNextBus() + $nextBus.mute = $false + return $nextBus + } +} + + try { $vmr = Connect-Voicemeeter -Kind 'potato' - $buses = @($vmr.bus[1], $vmr.bus[2], $vmr.bus[4], $vmr.bus[6]) - "Buses in selection: $($buses)" - $unmutedIndex = $null - - # 1) - 'Cycling through bus selection to check for first unmuted Bus...' | Write-Host - foreach ($bus in $buses) { - # 2) - if (-not $bus.mute) { - "Bus $($bus.index) is unmuted... muting it" | Write-Host - $unmutedIndex = $buses.IndexOf($bus) - $bus.mute = $true - - # 3) - if ($buses[++$unmutedIndex]) { - "Unmuting Bus $($buses[$unmutedIndex].index)" | Write-Host - $buses[$unmutedIndex].mute = $false - break - } - else { Clear-Variable unmutedIndex } - } + # Mute all buses initially + foreach ($bus in $vmr.bus) { + $bus.mute = $true } - # 4) - if ($null -eq $unmutedIndex) { - $buses[0].mute = $false - "Unmuting Bus $($buses[0].index)" | Write-Host + + $busesToRotate = @( + $vmr.bus[1], + $vmr.bus[2], + $vmr.bus[4], + $vmr.bus[6] + ) + + $rotator = [BusRotator]::new($vmr, $busesToRotate) + while ((Read-Host "Press Enter to rotate buses or type 'Q' to quit.") -ne 'Q') { + $nextBus = $rotator.MuteNextBus() + Write-Host "Bus $nextBus is now unmuted." } - } finally { Disconnect-Voicemeeter }