Appearance
火山引擎浏览器端文本转语音(TTS)工具库,支持流式文本输入、SSML 发音规则转换、iOS 兼容处理和音频播放控制。
安装
bash
pnpm add @mingto/huoshan-tts快速开始
typescript
import huoshanTts from '@mingto/huoshan-tts'
huoshanTts.config({
ttsRequestBaseUrl: 'wss://audio.workbrain.cn/tts',
})
const ttsInstance = huoshanTts.create({
voice_type: 'zh_female_daimengchuanmei_moon_bigtts',
})
ttsInstance
.on('audioFirstStart', () => {
console.log('音频首次播放')
})
.on('appError', (error) => {
console.error('应用错误', error)
})
.on('appFinish', () => {
console.log('应用已结束')
})
ttsInstance.start()
ttsInstance.send('你好呀。')
ttsInstance.end()API
huoshanTts.config(systemConfig)
配置系统参数,返回当前模块对象,支持链式调用。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| ttsRequestBaseUrl | string | wss://audio.workbrain.cn/tts | TTS WebSocket 请求地址 |
huoshanTts.create(businessParams?, ssmlConfig?)
创建 TTS 控制器实例。
typescript
const ttsInstance = huoshanTts.create(
{
voice_type: 'zh_female_daimengchuanmei_moon_bigtts',
text_type: 'ssml',
},
{
pronunciationRules: [
{
content: '筠连',
alphabet: 'py',
ph: 'jvn1 lian2',
},
],
}
)businessParams
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| voice_type | string | zh_female_daimengchuanmei_moon_bigtts | 音色类型 |
| text_type | plain / ssml | plain | 文本类型 |
| speed_ratio | number | 1 | 语速,约 [0.2, 3] |
| volume_ratio | number | 1 | 音量,约 [0.1, 3] |
| pitch_ratio | number | 1 | 音高,约 [0.1, 3] |
| language | string | cn | 语言代码 |
| provider | string | 0 | 供应商 ID;100 会强制使用纯文本,iOS 下使用 URL 输出 |
| cache | boolean | false | 是否使用缓存 |
| stream | boolean | true | 是否流式返回 |
| encoding | string | iOS 下为 pcm | 音频编码,可由运行环境或 provider 自动调整 |
| output_format | string | iOS 下为 base64 | 输出格式,可由运行环境或 provider 自动调整 |
ssmlConfig
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| pronunciationRules | Array<{ content: string; alphabet: 'py' | 'ipa'; ph: string }> | [] | 发音规则,仅在 text_type: 'ssml' 时参与转换 |
实例方法
| 方法 | 说明 |
|---|---|
| start() | 激活应用,准备接收文本 |
| send(text) | 发送文本进行合成;仅在执行中生效 |
| end() | 通知文本输入结束,不会立即停止剩余音频播放 |
| finish() | 停止所有处理器并重置状态 |
| mute() | 静音当前音频输出 |
| unmute() | 恢复音频输出 |
| pause() | 暂停播放 |
| resume() | 恢复播放 |
| on(eventName, callback) | 订阅应用事件,返回当前实例 |
应用事件
| 事件名 | 说明 |
|---|---|
| audioFirstStart | 音频首次播放时触发 |
| appError | 应用发生错误时触发 |
| appFinish | 应用结束或销毁时触发 |
流式文本示例
typescript
const textList = ['第一段文本,', '第二段文本。']
const ttsInstance = huoshanTts.create({
voice_type: 'zh_female_daimengchuanmei_moon_bigtts',
})
ttsInstance.start()
textList.forEach((text, index) => {
ttsInstance.send(text)
if (index === textList.length - 1) {
ttsInstance.end()
}
})