首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell - Voice Synth保存输出

Powershell - Voice Synth保存输出
EN

Stack Overflow用户
提问于 2018-04-21 18:39:35
回答 2查看 481关注 0票数 0

我想帮助理解如何将variable.method输出的结果保存到另一个变量中。

我想测试创建一首歌曲,但我被困在如何继续。

目前,语音会自动输出,但不会保存在变量中。

这是一个合乎逻辑的原因吗?我只学习了一个月的powershell。

代码语言:javascript
复制
Add-Type -AssemblyName System.Speech

$speak = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer

$man = $speak.Speak("Male Voice") # Outputs the Audio to host, but it does not store the result into Variable
$speak.SelectVoice("Microsoft Zira Desktop")
$woman = $speak.Speak("Female Voice")# Same as above

function song {
$man
$woman
song
}
EN

回答 2

Stack Overflow用户

发布于 2018-04-21 19:24:34

Speak方法没有返回值

public void ( string textToSpeak )

但是,您可以通过SetOutputToWaveFile将音频输入设置为文件,然后使用SoundPlayer立即播放音频,以获得更好的用户体验。

代码语言:javascript
复制
Add-Type -AssemblyName System.Speech

$soundFilePath = "Test.wav"

# Save the Audio to a file
[System.Speech.Synthesis.SpeechSynthesizer] $voice = $null
Try {
    $voice = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
    $voice.SetOutputToWaveFile($soundFilePath)

    $voice.Speak("Male Voice")
    $voice.SelectVoice("Microsoft Zira Desktop")
    $voice.Speak("Female Voice")
} Finally {
    # Make sure the wave file is closed
    if ($voice) {
        $voice.Dispose()
    }
}


# Load the saved audio and play it back
[System.Media.SoundPlayer] $player = $null
Try {
    $player = New-Object -TypeName System.Media.SoundPlayer($soundFilePath)
    $player.PlaySync()
} Finally {
    if ($player) {
        $player.Dispose()
    }
}

如果您只需要内存中的音频,而不想对任何内容进行后处理,那么可以将数据写入MemoryStream

代码语言:javascript
复制
Add-Type -AssemblyName System.Speech

[System.IO.MemoryStream] $memoryStream = $null;

Try {
    $memoryStream = New-Object -TypeName System.IO.MemoryStream
    [System.Speech.Synthesis.SpeechSynthesizer] $voice = $null
    Try {
        $voice = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
        $voice.SetOutputToWaveStream($memoryStream)

        $voice.Speak("Male Voice")
        $voice.SelectVoice("Microsoft Zira Desktop")
        $voice.Speak("Female Voice")
    } Finally {
        if ($voice) {
            $voice.Dispose()
        }
    }


    # Load the saved audio and play it back
    [System.Media.SoundPlayer] $player = $null
    Try {
        $memoryStream.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
        $player = New-Object -TypeName System.Media.SoundPlayer($memoryStream)
        $player.PlaySync()
    } Finally {
        if ($player) {
            $player.Dispose()
        }
    }

} Finally {
    if ($memoryStream) {
        $memoryStream.Dispose()
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-04-21 20:50:07

您不能将语音输出放入一个变量中,因为它不是函数返回的值,而是一个“副作用”。

但不需要存储音频。只存储文本并在每次运行脚本时生成语音输出要有效得多。

编辑:我使用了.SpeakAsyncsleep来更好地控制时间。调整延迟数,使其适合每条线路的流量。

代码语言:javascript
复制
Add-Type -AssemblyName System.Speech

$male = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$male.SelectVoice("Microsoft David Desktop")

$female = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$female.SelectVoice("Microsoft Zira Desktop")

function male([string] $text, [int] $duration) {
    $male.SpeakAsync($text) | Out-Null
    sleep -Milliseconds $duration
}

function female([string] $text, [int] $duration) {
    $female.SpeakAsync($text) | Out-Null
    sleep -Milliseconds $duration
}

function song {
    male 'hello' 500
    female 'goodbye' 500
}

song
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49954898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档