gpt4 book ai didi

node.js - 如何更改 Node 模块 "say"的音频输出设备

转载 作者:行者123 更新时间:2023-12-03 12:38:44 25 4
gpt4 key购买 nike

我正在制作一个将文本输出到语音的程序,用户需要能够更改输出设备(例如更改为 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
})
}
}

然后我可以传入一个音频 channel ,比如
tts = new TTS("CABLE Input (VB-Audio Virtual Cable)", 0);
tts.speak("Hello there");
它会在我想要的 channel 中输出 TTS

最佳答案

一些浏览器支持内置的 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>
这是你想要的 ?

关于node.js - 如何更改 Node 模块 "say"的音频输出设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64830979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com