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

agent-voice-recognition

v1.0.0

Published

语音识别工具,支持实时语音转文字

Downloads

84

Readme

agent-voice-recognition

语音识别工具,支持实时语音转文字。

安装

npm install agent-voice-recognition

使用

基础用法

import VoiceRecognitionSDK from 'agent-voice-recognition';

// 创建语音识别实例
const voiceSDK = new VoiceRecognitionSDK({
  wsConfig: {
    wsUrl: 'wss://example.com/asr',
    wsDataCallback: (json) => {
      console.log('识别结果:', json.voice_text_str);
    },
    onOpen: () => {
      console.log('连接成功');
    },
    onError: (err) => {
      console.error('连接错误:', err);
    },
    onClose: () => {
      console.log('连接关闭');
    },
  }
});

// 初始化 WebSocket 连接
voiceSDK.initializeWebSocket();

// 设置音频数据回调
voiceSDK.setAudioDataCallback((audioData) => {
  // 发送音频数据到服务器
  voiceSDK.wsManager.sendOutData(audioData);
});

// 开始录音
await voiceSDK.startRecording();

// 停止录音
voiceSDK.stopRecording();

// 销毁实例
voiceSDK.destroy();

在 Vue 中使用

<script setup>
import { onMounted, onUnmounted } from 'vue';
import VoiceRecognitionSDK from 'agent-voice-recognition';

let voiceSDK = null;

onMounted(() => {
  voiceSDK = new VoiceRecognitionSDK({
    wsConfig: {
      wsUrl: 'wss://example.com/asr',
      wsDataCallback: (json) => {
        // 处理识别结果
        console.log(json);
      }
    }
  });

  voiceSDK.setAudioDataCallback((audioData) => {
    voiceSDK.wsManager.sendOutData(audioData);
  });
});

onUnmounted(() => {
  if (voiceSDK) {
    voiceSDK.destroy();
  }
});
</script>

API

VoiceRecognitionSDK 类

构造函数

new VoiceRecognitionSDK(config)

参数:

  • config (object) - 配置选项
    • wsConfig (object) - WebSocket 配置
      • wsUrl (string) - WebSocket URL
      • wsDataCallback (Function) - 数据回调
      • onOpen (Function) - 连接打开回调
      • onError (Function) - 错误回调
      • onClose (Function) - 连接关闭回调
      • onReconnect (Function) - 重连回调
      • onReconnectFailed (Function) - 重连失败回调
      • reconnectAttempts (number) - 最大重连次数,默认 3

方法

  • initializeWebSocket() - 初始化 WebSocket 连接
  • setAudioDataCallback(callback) - 设置音频数据回调
  • setWsDataCallback(callback) - 设置 WebSocket 数据回调
  • startRecording() - 开始录音
  • stopRecording() - 停止录音
  • destroy() - 销毁实例

特性

  • ✅ 实时语音转文字
  • ✅ WebSocket 实时传输
  • ✅ 自动重连机制
  • ✅ 心跳保活
  • ✅ PCM 音频格式转换
  • ✅ TypeScript 支持

注意事项

  1. 需要浏览器支持 getUserMedia API
  2. 需要浏览器支持 AudioWorklet API
  3. WebSocket 服务器需要支持音频数据接收和识别结果返回