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

sophontalk-services

v0.0.15

Published

SophonTalk 的核心服务库,提供了音频特征提取、数据集加载等功能。

Readme

SophonTalk Services

SophonTalk 的核心服务库,提供了音频特征提取、数据集加载等功能。

安装

npm install sophontalk-services
# 或者
pnpm add sophontalk-services

使用方法

特征提取 (FeatureExtractor)

FeatureExtractor 使用 Web Worker 在后台线程中处理音频数据,提取 Mel 频谱特征。

import { FeatureExtractor } from 'sophontalk-services';

// 1. 创建实例
const extractor = new FeatureExtractor();

// 2. 准备音频数据 (AudioBuffer)
// 例如从 AudioContext 解码得到
const audioContext = new AudioContext();
const response = await fetch('path/to/audio.wav');
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);

// 3. 提取特征
try {
  const result = await extractor.process(audioBuffer);
  console.log('特征数据:', result.features);
  console.log('维度信息:', result.dimensions);
} catch (error) {
  console.error('特征提取失败:', error);
}

// 4. 销毁实例 (不再使用时)
extractor.dispose();

数据集工具 (Dataset)

提供了加载数据集和一些辅助计算函数。

import { loadDataset, calculatePingPongState, getBorder } from 'sophontalk-services';

// 加载数据集
// 默认加载 /complete_dataset.json 和 /processed_images.zip
// 也可以指定 URL
try {
  const { dataset, zipBuffer } = await loadDataset({
    jsonUrl: '/path/to/dataset.json',
    zipUrl: '/path/to/images.zip'
  });
  console.log('数据集信息:', dataset.dataset_info);
} catch (error) {
  console.error('加载数据集失败:', error);
}

// 计算 Ping-Pong 循环播放的下一帧索引
const { nextIndex, nextDirection } = calculatePingPongState(currentIndex, numFrames, direction);

// 获取裁剪边框大小
const border = getBorder(96); // 支持 96 或 128

推理引擎 (InferenceEngine)

InferenceEngine 封装了 ONNX Runtime 推理的 Web Worker 逻辑。

import { InferenceEngine } from 'sophontalk-services';

// 1. 创建实例并设置回调
const engine = new InferenceEngine({
  onReady: () => console.log('模型加载完成'),
  onStart: (totalFrames) => console.log('开始推理,总帧数:', totalFrames),
  onFrame: (frame, index) => {
    // frame 是 ImageBitmap,使用后请尽快绘制并关闭以释放内存
    // ctx.drawImage(frame, 0, 0);
    // frame.close();
  },
  onDone: () => console.log('推理完成'),
  onError: (err) => console.error('推理错误:', err)
});

// 2. 初始化 (需要加载好的资源)
// dataset, zipBuffer 来自 loadDataset
// blendingMask 是 ImageBitmap
engine.init({
  modelPath: '/model.onnx',
  dataset: dataset,
  zipBuffer: zipBuffer,
  blendingMask: blendingMask,
  wasmPaths: '/' // 可选,指定 onnxruntime wasm 文件路径
});

// 3. 运行推理 (需要 FeatureExtractor 的输出)
engine.run({
  audioFeatures: features,
  audioDimensions: dimensions
});

// 4. 停止或销毁
engine.stop();
engine.terminate();

构建

本项目使用 Vite 进行构建。

# 安装依赖
pnpm install

# 构建库文件
pnpm build:lib

构建产物位于 dist-lib 目录下。

  • dist-lib/sophontalk-services.js: ESM 格式入口
  • dist-lib/sophontalk-services.cjs: CJS 格式入口
  • dist-lib/types: 类型定义文件

依赖

本库依赖以下包,请确保在使用环境中安装:

  • vue (虽然标记为外部依赖,但部分类型可能引用)
  • onnxruntime-web
  • jszip
  • fft.js
  • @alexanderolsen/libsamplerate-js