@mt-utils/xunfei-tts
v1.0.12
Published
讯飞TTS 文本转语音
Downloads
270
Readme
@mt-utils/xunfei-tts
讯飞TTS 文本转语音库,用于将文本转换为语音输出。
特点
- 提供灵活的参数配置,包括语速、音量、音高等。
- 支持使用默认参数或部分覆盖以简化配置流程。
- 支持WebSocket通信,实现实时的文本到语音转换。
- 支持Web Worker,进行音频数据的转换处理,不阻塞主线程。
安装
pnpm install @mt-utils/xunfei-tts
使用方法
引入库
import { createMtTts } from '@mt-utils/xunfei-tts'
创建实例
您可以选择使用默认的 ttsOptions
,或者提供部分参数来覆盖默认设置。
const systemOptions = {
API_SECRET: 'your_api_secret',
APPID: 'your_appid',
API_KEY: 'your_api_key'
}
// 创建mtTts实例,使用默认ttsOptions
const mtTts = createMtTts(systemOptions)
// 或者创建mtTts实例,提供部分ttsOptions来覆盖默认值
const ttsOptions = {
speed: 70
}
const mtTts = createMtTts(systemOptions, ttsOptions)
发送文本并播放
send
方法现在接受两个参数:待转换的文本 text
和一个可选的 options
对象。options
对象的属性会与默认的 SendOptions
合并。
// 发送文本并播放,默认isAppend为true
mtTts.send('你好,欢迎使用讯飞TTS服务!')
// 发送文本并播放
mtTts.send('这是一条更长的文本示例,将被自动分割。', {
isAppend: false
})
参数说明
text
: 字符串 - 待转换的文本。options
: 对象 - 发送文本时的配置选项,包括:isAppend
: 布尔值 - 是否将末尾未计入此次分段落的段落,主动追加到段落队列中,默认值为 false。/** * isAppend:false的情况 * 在进行分割段落时,会分割为:["你好,"],剩余的文本内容“欢迎使用讯”会交给下次的send方法 */ mtTts.send('你好,欢迎使用讯', { isAppend: false }) /** * 首先拼接上次未完全分段的部分“欢迎使用讯”,然后再分割为本内容为["欢迎使用讯飞TTS服务!"] */ mtTts.send('飞TTS服务!') // -----------------两种情况---------------- /** * isAppend:true的情况 * 在进行分割段落时,会分割为:["你好,","剩余的文本内容"] */ mtTts.send('你好,欢迎使用讯', { isAppend: true }) /** * 分割为本内容为["讯飞TTS服务!"] */ mtTts.send('飞TTS服务!')
可用方法
stop
: 终止TTS转换和语音播放。
监听事件
mtTts.on('appStart', () => {
console.log('TTS服务已启动')
})
mtTts.on('appStop', () => {
console.log('TTS服务已停止')
})
配置选项
SystemOptions(必传)
API_SECRET
: 您的API密钥。APPID
: 您的应用ID。API_KEY
: 您的API密钥。
TtsOptions(可选值)
speed
: 语速,默认为50,取值范围0-100。voice
: 音量,默认为50,取值范围0-100。pitch
: 音高,默认为50,取值范围0-100。vcn
: 发音人名称,默认为 'xiaoyan'。bgs
: 合成音频的背景音设置,默认为0(无背景音),取值0或1。tte
: 文本编码格式,默认为 "UTF8"。
SendOptions 配置项
isAppend
: 默认值为true,在进行文本分割的时,分割到最后的时候,是否将文本追加到文本分割器中。
事件
appStart
: 当TTS服务启动时触发。appStop
: 当TTS服务停止时触发。audioFirstStart
: 当音频首次开始播放时触发。
## 注意
明途内部工具库,并不负责改BUG,请勿随便下载