【问题标题】:How could I change the audio output device of node module "say"如何更改节点模块“say”的音频输出设备
【发布时间】:2020-11-15 05:02:44
【问题描述】:

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

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

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

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

我首先发现了 navigator.MediaDevices,然后我发现了如何制作音频元素并通过 setSinkId 更改该元素的音频设备,然后我意识到这些东西可能(?)与 say 模块无关似乎使用 powershell 命令播放声音。我什至尝试在应用程序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.js powershell electron text-to-speech playback


    【解决方案1】:

    部分浏览器支持内置的 SpeechSynthesis API。

    将以下代码保存在“test.html”文件中,并在 chrome 网络浏览器中打开该文件以测试语音 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>
    

    这就是你要找的吗?

    【讨论】:

    • 感谢您的回复,但不,我的问题不在于如何将文本生成语音,而在于为其选择输出通道。
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2023-03-03
    • 2021-07-27
    相关资源
    最近更新 更多