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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@xmov/doubao-asr

v1.0.2

Published

豆包(字节跳动)实时语音识别 SDK

Readme

@xmov/doubao-asr

豆包(字节跳动)实时语音识别 SDK,专为数字人电话场景优化。

特性

  • 🎤 实时语音识别 - 支持实时音频流处理和识别
  • 🔗 WebSocket 持续连接 - 保持连接活跃,高效的 gzip 压缩通信
  • 🎯 句子级识别 - 提供句子开始、更新、结束事件
  • 🔄 持续监听 - 识别完一句话后自动继续监听下一句,无需重启
  • 🚫 手动停止 - 只有用户主动停止才关闭连接,忽略服务器完成信号
  • 📱 数字人电话优化 - 针对电话对话场景的模型优化
  • 💪 TypeScript 支持 - 完整的类型定义
  • 🚀 开箱即用 - 简单的 API 设计
  • 🛡️ 生产环境控制 - 可配置的生产环境禁用选项

安装

npm install @xmov/doubao-asr
# 或
pnpm add @xmov/doubao-asr

快速开始

基础用法

import { createDoubaoASR, DoubaoRecognizer } from '@xmov/doubao-asr'

// 创建并启动识别器
const recognizer = await createDoubaoASR(
  {
    wsUrl: 'wss://your-proxy-server.com/asr',
    appKey: 'your-app-key',
    accessKey: 'your-access-key'
  },
  {
    onRecognitionResultChange: (event) => {
      console.log('实时识别结果:', event.result.voice_text_str)
    },
    onSentenceEnd: (event) => {
      console.log('句子结束:', event.result.voice_text_str)
    },
    onError: (event) => {
      console.error('识别错误:', event.error)
    }
  }
)

// 停止识别
await recognizer.stop()

// 销毁资源
recognizer.destroy()

手动控制

import { DoubaoRecognizer } from '@xmov/doubao-asr'

const recognizer = new DoubaoRecognizer(
  {
    wsUrl: 'wss://openspeech.bytedance.com/api/v3/sauc/bigmodel_async',
    appKey: 'your-app-key',
    accessKey: 'your-access-key',
    engineModel: '16k_zh',
    sampleRate: 16000
  },
  {
    onRecognitionStart: () => {
      console.log('识别开始')
    },
    onSentenceBegin: (event) => {
      console.log('句子开始:', event.result.voice_text_str)
    },
    onRecognitionResultChange: (event) => {
      // 实时字幕更新
      updateSubtitle(event.result.voice_text_str)
    },
    onSentenceEnd: (event) => {
      // 句子完成,可以进行后续处理
      processFinalText(event.result.voice_text_str)
    },
    onRecognitionComplete: () => {
      console.log('识别完成')
    },
    onError: (event) => {
      console.error('错误:', event.error.message)
    }
  }
)

// 手动启动
await recognizer.start()

// 检查状态
console.log('当前状态:', recognizer.currentState)

配置选项

interface DoubaoASRConfig {
  /** WebSocket URL (代理或直接连接) */
  wsUrl?: string
  /** 应用Key (推荐使用) */
  appKey?: string
  /** 访问Key (推荐使用) */
  accessKey?: string
  /** 应用ID (向后兼容) */
  appId?: string
  /** Token (向后兼容) */
  token?: string
  /** 资源ID */
  resourceId?: string
  /** 连接ID */
  connectId?: string
  /** 采样率,默认 16000 */
  sampleRate?: number
  /** 引擎模型,默认 '16k_zh' */
  engineModel?: string
  /** VAD静音阈值(毫秒) */
  vadSilenceTime?: number
  /** 心跳间隔(毫秒) */
  keepAliveMs?: number
  /** 是否在生产环境禁用ASR */
  disableInProduction?: boolean
}

事件回调

interface DoubaoASRCallbacks {
  /** 错误回调 */
  onError?: (event: DoubaoErrorEvent) => void
  /** 识别开始回调 */
  onRecognitionStart?: (event: DoubaoRecognitionStartEvent) => void
  /** 识别结果变化回调 - 实时字幕更新 */
  onRecognitionResultChange?: (event: DoubaoRecognitionResultEvent) => void
  /** 识别完成回调 */
  onRecognitionComplete?: (event: DoubaoRecognitionCompleteEvent) => void
  /** 句子开始回调 */
  onSentenceBegin?: (event: DoubaoSentenceBeginEvent) => void
  /** 句子结束回调 - 最终文本 */
  onSentenceEnd?: (event: DoubaoSentenceEndEvent) => void
}

状态管理

识别器具有以下状态:

  • IDLE - 空闲状态
  • CONNECTING - 连接中
  • CONNECTED - 已连接
  • LISTENING - 监听中
  • STOPPING - 停止中
  • STOPPED - 已停止
  • ERROR - 错误状态
import { RecognizerState } from '@xmov/doubao-asr'

console.log('当前状态:', recognizer.currentState)

if (recognizer.currentState === RecognizerState.LISTENING) {
  // 识别器正在监听
}

最佳实践

1. 错误处理

const recognizer = new DoubaoRecognizer(config, {
  onError: (event) => {
    console.error('ASR错误:', event.error.message)

    // 根据错误类型进行处理
    if (event.error.message.includes('网络')) {
      // 网络错误,可以尝试重连
      setTimeout(() => {
        recognizer.start().catch(console.error)
      }, 5000)
    }
  }
})

2. 资源管理

// 组件卸载时确保清理资源
useEffect(() => {
  return () => {
    recognizer.destroy()
  }
}, [])

3. 生产环境控制

const recognizer = new DoubaoRecognizer({
  // 在生产环境自动禁用 ASR
  disableInProduction: true,
  ...otherConfig
})

4. 实时字幕显示

let currentText = ''

const recognizer = new DoubaoRecognizer(config, {
  onSentenceBegin: () => {
    currentText = ''
  },
  onRecognitionResultChange: (event) => {
    // 实时更新字幕
    currentText = event.result.voice_text_str
    updateSubtitle(currentText)
  },
  onSentenceEnd: (event) => {
    // 句子完成,保存最终文本
    const finalText = event.result.voice_text_str
    saveFinalText(finalText)
    currentText = ''
  }
})

开发和构建

# 安装依赖
pnpm install

# 开发模式(监听文件变化)
pnpm dev

# 构建
pnpm build

# 类型检查
pnpm typecheck

# 运行测试
pnpm test

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!