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

@chnak/speak

v1.0.1

Published

Node.js 流媒体音频播放器,持久化 Speaker 实例

Readme

@chnak/speak

Node.js 流媒体音频播放器,持久化 Speaker 实例。

特性

  • 持久化 Speaker 实例,流畅播放音频
  • 自动缓冲队列管理
  • 错误自动恢复和重连
  • 支持流式音频缓冲区
  • 事件驱动架构

安装

npm install @chnak/speak

使用

ESM

import { Speaker } from '@chnak/speak';

const speaker = new Speaker('audio/L16;rate=32000', 2);

// 推送 PCM 音频缓冲区
speaker.push(audioBuffer);

// 监听事件
speaker.on('finish', () => console.log('播放完成'));
speaker.on('error', (err) => console.error('错误:', err));

// 查看状态
console.log(speaker.getState());  // { isPlaying: true, queueSize: 3 }

// 停止播放
speaker.end();

CommonJS

const { Speaker } = require('@chnak/speak');

const speaker = new Speaker('audio/L16;rate=32000', 2);
speaker.push(audioBuffer);

API

new Speaker(config?, channels?)

创建扬声器实例。

参数:

  • config - 音频配置字符串(如 'audio/L16;rate=32000')或 AudioParams 对象
  • channels - 声道数(默认:1)

配置字符串格式:

  • rate=<采样率> - 设置采样率(如 rate=32000
  • 示例:'audio/L16;rate=32000' = 16位 PCM,32kHz 采样率

speaker.push(buffer)

将 PCM 音频缓冲区推入播放队列。

参数:

  • buffer - Buffer(16位有符号 PCM)

返回: boolean - 是否成功

speaker.write(buffer)

push() 的别名。

speaker.end()

停止播放并清理资源。

speaker.getState()

获取当前状态。

返回: { isPlaying: boolean, queueSize: number }

speaker.hasPendingBuffers()

检查是否有待播放的缓冲区。

返回: boolean

speaker.getOptions()

获取音频配置。

返回: AudioParams

speaker.createSilenceBuffer(durationMs, sampleRate?, channels?)

创建静音缓冲区,用于测试或填充。

事件

| 事件 | 说明 | |------|------| | ready | 初始化完成 | | finish | 音频流结束 | | drain | 队列清空 | | end | 资源清理完成 | | error | 播放出错 |

类型定义

interface AudioParams {
  sampleRate: number;
  channels: number;
  bitDepth: number;
  signed: boolean;
}

interface SpeakerState {
  isPlaying: boolean;
  queueSize: number;
}

构建

npm run build    # 构建 ESM 和 CJS
npm run dev      # 监视模式
npm test         # 运行测试

许可证

MIT