【发布时间】: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