首页 > 解决方案 > 如何更改节点模块“say”的音频输出设备

问题描述

我正在制作一个将文本输出到语音的程序,用户需要能够更改输出设备(例如虚拟音频电缆)。目前我正在使用https://www.npmjs.com/package/say来制作演讲,例如

const say = require("say");
say.speak("Hello World");

但我不知道如何选择音频输出。

到目前为止,我发现的内容毫无用处,我猜主要是因为我不知道搜索答案的正确术语,不管是这样的:

我首先发现了navigator.MediaDevices,然后我发现了如何制作音频元素并通过setSinkId更改该元素的音频设备,然后我意识到这些东西可能(?)无关紧要,因为 say 模块似乎使用播放声音PowerShell 命令。我什至尝试在 app volume device preferences(img)中更改 powershell 的输出设备,但这似乎无济于事。

我现在很困惑,所以我很感激任何帮助。我不打算使用 Say 作为一个模块,一开始它似乎很容易使用。


编辑:

我已经解决的解决方法是制作自己的 TTS 课程并使用SpVoice
我有类似的东西:

const childProcess = require('child_process');
class TTS {
    constructor(channel, speed) {
        this.speed = speed;
        this.channel = channel;
        this.baseCommand = "$speak = New-Object -ComObject SAPI.SPVoice;"
    }
    speak(text){
        var command = this.baseCommand +
            `$speak.AudioOutput = foreach ($o in $speak.GetAudioOutputs()) {if ($o.getDescription() -eq '${this.channel}') {$o; break;}}; `
            + "$speak.Speak([Console]::In.ReadToEnd());"

        this.child = childProcess.spawn('powershell', [command], {shell: true})
        this.child.stdin.setEncoding('ascii')
        this.child.stdin.end(text);
        this.child.addListener('exit', (code, signal) => {
            if (code === null || signal !== null) {
              console.log(new Error(`error [code: ${code}] [signal: ${signal}]`))
            }
            this.child = null
        })
    }
}

然后我可以传入一个音频通道,比如

tts = new TTS("CABLE Input (VB-Audio Virtual Cable)", 0);
tts.speak("Hello there");

它会在我想要的频道中输出 TTS

标签: node.jspowershellelectrontext-to-speechplayback

解决方案


一些浏览器支持内置的 SpeechSynthesis API。

将以下代码保存在“test.html”文件中,并在 chrome Web 浏览器中打开该文件以测试语音 api。

<script>

    //---------------- SpeechAPI Start ------------------------
    
    function speak(message) {
      try {
        
        var utterance= new SpeechSynthesisUtterance("");
        utterance.lang='en-GB'; 
        utterance.text=message;
        window.speechSynthesis.speak(utterance);

      }catch(e){
        console.log('Exception in speak : ' + e)
      }
    }
    
    //---------------- SpeechAPI End ------------------------

</script>

<button onclick="speak('Hello, how are you doing?');">Press to speak</button>

这是你想要的 ?


推荐阅读