param(
    [string]$TextFile,
    [string]$VoiceName = "",
    [int]$Rate = 0,
    [int]$Volume = 100
)

Add-Type -AssemblyName System.Speech

$synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$synth.Rate = $Rate
$synth.Volume = $Volume

if ($VoiceName -and $VoiceName.Trim().Length -gt 0) {
    try {
        $synth.SelectVoice($VoiceName)
    } catch {
        Write-Host "Voice not found, using Windows default voice: $VoiceName"
    }
}

$text = Get-Content -Raw -Path $TextFile

if ($text -and $text.Trim().Length -gt 0) {
    $synth.Speak($text)
}

$synth.Dispose()

try {
    Remove-Item -Force $TextFile
} catch {}
