Functions/GenXdev.Console/Suspend-TextToSpeech.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Console
Original cmdlet filename : Suspend-TextToSpeech.ps1
Original author : René Vaessen / GenXdev
Version : 3.29.2026
################################################################################
Copyright (c) 2026 René Vaessen / GenXdev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>


###############################################################################

<#
.SYNOPSIS
Pauses any ongoing text-to-speech output across all synthesizer instances.

.DESCRIPTION
Pauses audio playback on whichever GenXdev speech synthesizer is currently
producing sound — queued speech (Start-TextToSpeech), force-interrupted
speech (Start-TextToSpeech -Force), or the default instance. While paused,
new Start-TextToSpeech calls (without -Force) will still queue normally but
won't start playing. Start-TextToSpeech -Force will speak immediately but
will NOT auto-resume the paused speech afterward. Use Resume-TextToSpeech
to continue playback.

.EXAMPLE
PS C:\> say "This is a long story about PowerShell and GenXdev and all
the amazing things you can do with it" ; Suspend-TextToSpeech
Starts speaking and immediately pauses it.

.EXAMPLE
PS C:\> Suspend-TextToSpeech; say "Urgent!" -Force; Resume-TextToSpeech
Pauses ongoing speech, interrupts with urgent message, then resumes.

.NOTES
This cmdlet is commonly used in conjunction with Start-TextToSpeech (alias:
say), Resume-TextToSpeech, and Stop-TextToSpeech (alias: sst) for speech
control.
#>

function Suspend-TextToSpeech {

    [CmdletBinding(SupportsShouldProcess = $true)]
    [Alias("pausespeech")]
    param()

    begin {

        Microsoft.PowerShell.Utility\Write-Verbose 'Initiating speech pause request'
    }

    process {

        try {
            if ($PSCmdlet.ShouldProcess('Text-to-speech output', 'Pause')) {
                # Pause all three synthesizer instances — whichever
                # is actually playing will pause; the other two are
                # harmless no-ops. Sets Misc.IsUserPaused = true so
                # that -Force won't auto-resume.
                [GenXdev.Helpers.Misc]::PauseAllSpeech()

                Microsoft.PowerShell.Utility\Write-Verbose 'Successfully paused all speech output'
            }
        }
        catch {
            Microsoft.PowerShell.Utility\Write-Verbose 'Error occurred while attempting to pause speech'
        }
    }

    end {
    }
}