Windows平台Node.js实现文本转语音TTS 【转】

Windows平台Node.js实现文本转语音TTS

最新有个需求,想在Windows平台上用Node.js实现文本转语音 (Text to Speech, TTS) 功能,实现过程中走了一些弯路,总结一下,做个记录。

因为不要求听起来多自然,也不考虑跨平台,所以就打算自己实现一下。PowerShell可以调用系统的语音API,代码如下:

Add-Type -AssemblyName System.speech;
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;
$speak.Rate = 5; # 语速,-10 ~ 10,-10最慢,10最快
$speak.Speak('别不信')
复制代码

在PowerShell中运行这段代码,就可以听到语速稍快的“别不信”(语音)了。

在Node.js中怎么用呢?由于PowerShell默认是GBK编码,考虑编码转换,如下:

const { exec } = require('child_process');
const iconv = require('iconv-lite');

exec(`powershell.exe Add-Type -AssemblyName System.speech; $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; $speak.Rate = 5; $speak.Speak([Console]::In.ReadLine()); exit`).stdin.end(iconv.encode('别不信', 'gbk'));

这样就用最简单的代码实现了TTS功能。

原文

个人技术博客 biebu.xin,原文链接——Windows平台Node.js实现文本转语音TTS

posted on 2020-08-10 11:53  马什么梅  阅读(972)  评论(0编辑  收藏  举报

导航