glyphix-utils
v2.0.0
Published
Glyphix JsApp 常用工具模块。
Readme
glyphix-utils
Glyphix JsApp 常用工具模块。
API 服务开通
腾讯云服务密钥创建 腾讯 TTS API 开通 腾讯一句话识别
安装
yarn add glyphix-utils -SASR — 语音识别
将音频文件转为文字文本。
Google Cloud Speech-to-Text
import { GoogleRecognition } from 'glyphix-utils';
const result = await new GoogleRecognition().createRecognition(
'/path/to/audio.mp3',
{
secretKey: '<Google API Key>',
config: {
encoding: 'MP3',
sampleRateHertz: 16000,
languageCode: 'cmn-Hans-CN',
},
},
);| 状态码 | 常量 | 说明 |
|--------|------|------|
| 0 | AsrCode.SUCCESS | 识别成功 |
| 1 | AsrCode.EMPTY_RESULT | 无识别结果 |
| 1002 | AsrCode.REQUEST_FAILED | 网络请求失败 |
| 1003 | AsrCode.API_ERROR | API 返回错误 |
| 9999 | AsrCode.UNKNOWN_ERROR | 未知错误 |
语言代码:cmn-Hans-CN(普通话)en-US(英语)yue-Hant-HK(粤语)
腾讯云语音识别
import { TencentRecognition } from 'glyphix-utils';
const result = await new TencentRecognition().createRecognition(
'/path/to/audio.pcm',
{
secretId: '<SecretId>',
secretKey: '<SecretKey>',
config: {
voiceFormat: 'pcm',
serviceType: '16k_zh', // 16k_zh | 8k_zh | 16k_en
},
},
);支持的音频格式:wav pcm ogg-opus speex silk mp3 m4a aac amr
TTS — 语音合成
将文字合成为音频文件。
Google Cloud Text-to-Speech
import { googleTTS } from 'glyphix-utils';
await googleTTS({
secretKey: '<Google API Key>',
text: 'hello world',
language: 'en-US',
saveFilename: '/path/to/output.mp3',
format: 'MP3',
});腾讯云语音合成
import { tencentTTS } from 'glyphix-utils';
await tencentTTS({
secretId: '<SecretId>',
secretKey: '<SecretKey>',
text: '你好世界',
saveFilename: 'internal://files/audio.mp3',
// 以下可选
config: {
voiceType: 0, // 音色 ID
codec: 'mp3', // wav | mp3 | pcm
speed: 0, // 语速 [-2, 2]
volume: 0, // 音量 [0, 10]
sampleRate: 16000, // 8000 | 16000
},
});文本长度限制:中文最大 150 字,英文最大 500 字母,超出直接抛错。
EventEmitter
事件总线,用于模块间数据通信。
在页面中监听事件,建议在
OnHide中取消监听,避免页面隐藏后回调异常。
import { EventEmitter } from 'glyphix-utils';
const emitter = new EventEmitter();
// on 返回 id,可用于 off 取消监听
const id = emitter.on('test', (msg) => console.debug(msg));
// 仅触发一次
emitter.once('test-once', (msg) => console.debug('once', msg));
// 取消指定监听
emitter.off('test', id);
// 取消某类型所有监听
emitter.offAll('test');节流与防抖
import { throttled, debounce } from 'glyphix-utils';
// 节流:delay ms 内只执行一次
const fn = throttled((args) => console.debug(args), 1000);
// 防抖:wait ms 后执行,重复触发则重新计时
const fn2 = debounce((args) => console.debug(args), 5000);