npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

xunfei-lat

v1.0.1

Published

讯飞LAT 语音转文本

Downloads

8

Readme

🎤 基于讯飞语音识别 API 的实时语音转文字 JavaScript SDK

特性

  • 🎯 实时语音识别 - 支持实时音频流识别
  • 🔄 智能音量监听 - 自动检测音量变化并启动识别
  • 🌐 WebSocket 连接 - 基于 WebSocket 的实时通信
  • 🎛️ 灵活配置 - 支持多种语言、领域和音频格式
  • 📦 TypeScript 支持 - 完整的类型定义
  • 🔧 模块化设计 - 采用处理器链模式,易于扩展

安装

npm install xunfei-lat
# 或
pnpm install xunfei-lat
# 或
yarn add xunfei-lat

快速开始

1. 配置系统参数

首先需要在讯飞开放平台申请相关密钥:

import xunfeiLat from 'xunfei-lat'

// 配置系统参数
xunfeiLat.config({
  API_SECRET: 'your_api_secret',
  APPID: 'your_appid',
  API_KEY: 'your_api_key'
})

2. 创建实例

// 创建语音识别实例
const latInstance = xunfeiLat.create()

3. 监听事件

// 监听识别结果
latInstance.on('appResultText', (text) => {
  console.log('最终识别结果:', text)
})

// 监听实时识别过程
latInstance.on('appResponseText', (text) => {
  console.log('实时识别结果:', text)
})

// 监听识别完成
latInstance.on('appFinish', () => {
  console.log('识别完成')
})

4. 开始识别

// 手动开始录音识别
latInstance.start()

API 参考

配置参数

SystemConfig(系统配置)

| 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | API_SECRET | string | ✅ | 讯飞平台申请的密钥 | | APPID | string | ✅ | 讯飞平台申请的应用ID | | API_KEY | string | ✅ | 讯飞平台申请的API密钥 |

BusinessParams(业务参数)

| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | language | 'zh_cn' \| 'en_us' | 'zh_cn' | 识别语言 | | domain | string | 'iat' | 应用领域 | | accent | 'mandarin' | 'mandarin' | 方言类型 | | vad_eos | number | 3600000 | 静默检测时间(ms) | | dwa | 'wpgs' | 'wpgs' | 动态修正 | | ptt | 0 \| 1 | 1 | 标点符号添加 | | rlang | 'zh-cn' \| 'zh-hk' | 'zh-cn' | 字体类型 | | nunum | 0 \| 1 | 1 | 数字格式 |

SectionDelayParams(延迟控制参数)

| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | autoControl | boolean | true | 是否自动控制延迟 | | initialDelay | number | 3500 | 初始延迟时间(ms) | | subsequentDelay | number | 3000 | 后续延迟时间(ms) |

实例方法

start(): void

手动开始录音和语音识别。


watch(): void

启动智能音量监听模式。当检测到音量大于20%且系统处于离线状态时自动开始识别。


finish(): void

结束当前的录音和识别过程。


on(eventName: string, callback: Function): void

监听事件。

事件

| 事件名 | 参数 | 说明 | |--------|------|------| | appResultText | text: string | 最终识别结果 | | appResponseText | text: string | 实时识别过程中的文本 | | appFinish | - | 识别完成 |

完整示例

import xunfeiLat from 'xunfei-lat'

// 1. 配置系统参数
xunfeiLat.config({
  API_SECRET: 'your_api_secret',
  APPID: 'your_appid',
  API_KEY: 'your_api_key'
})

// 2. 创建实例
const latInstance = xunfeiLat.create({}, {
  autoControl: false
})

// 3. 监听事件
latInstance.on('appResultText', (text) => {
  console.log('识别完成:', text)
})

latInstance.on('appResponseText', (text) => {
  console.log('实时结果:', text)
})

latInstance.on('appFinish', () => {
  console.log('识别结束')
})

// 4. 开始识别
const startBtn = document.createElement('button')
startBtn.id = 'startBtn'
startBtn.textContent = '开始识别'
document.body.appendChild(startBtn)

const stopBtn = document.createElement('button')
stopBtn.id = 'stopBtn'
stopBtn.textContent = '结束识别'
document.body.appendChild(stopBtn)

startBtn.addEventListener('click', () => {
  latInstance.start()
})

stopBtn.addEventListener('click', () => {
  latInstance.end()
})

常见问题

Q: 为什么无法录音?

A: 请确保:

  1. 使用 HTTPS 协议访问
  2. 浏览器已授权麦克风权限
  3. 设备有可用的音频输入设备

许可证

ISC