node.js - 如何更改节点模块“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
解决方案
一些浏览器支持内置的 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>
这是你想要的 ?
推荐阅读
- javascript - 使用 chrome 在服务器上运行 chutzpah
- python - 如何使用 jupyterhub 编译和运行 openmpi 代码
- c# - 在 WPF 应用程序中使用 .NET Standard 程序集中的内容文件
- ggplot2 - ggplot中轴刻度标签的突出显示/多色子字符串
- c# - 类型化的 HttpClient 仅适用于 WebApplicationFactory 的 CreateClient()
- c# - 使用“Keyboard.IsKey ...”实现键盘快捷键会重新缩放窗口,为什么?
- python - 如何获取我刚刚查询和使用的 sqlite 表的行的 blog_id?(在 Python 文件中)
- in-app-purchase - 用户想通过华为IAP订阅产品,但无法访问支付页面
- python-3.x - 如何在给定的数据框中用额外的值分配前 2、中 2 和后 2 值
- javascript - RGB 到 HSL Hue 用纯 JS 旋转效果