@ audio/ 发音器

在节点或浏览器中输出音频数据给扬声器.
使用量
import speaker from '@audio/speaker'
let write = speaker({
sampleRate: 44100,
channels: 2,
bitDepth: 16,
// bufferSize: 50, // ring buffer ms (default 50)
// backend: 'miniaudio', // force backend
})
write(pcmBuffer, (err) => {
// ready for next chunk
})
write(null) // end playback
设备
import speaker, { devices } from '@audio/speaker'
await devices()
// [{ id: 'BuiltInSpeakerDevice', name: 'MacBook Pro Speakers', default: true }, …]
let write = speaker({ device: 'headphones' }) // by id, by name, or by part of its name when only one holds it
ID是平台的自有: CoreAudio的UID,WASAPI的端点id,ALSA或PulseAudio名称. 不存在错误的设备( E)code: 'ENODEVICE'),从没有其他设备. 在浏览器中, devices() 读取 enumerateDevices() 财务报告和审定财务报表 device 成为上下文的汇( e)setSinkId) (中文(简体) ).
可同步执行
直接控制一个可同步源 :
import speaker from '@audio/speaker'
await speaker.from(audioSource, { sampleRate: 44100, channels: 2 })
流线
import SpeakerWritable from '@audio/speaker/stream'
source.pipe(SpeakerWritable({ sampleRate: 44100, channels: 2 }))
后端
按顺序尝试,第一个成功.
| 后端 | 怎么样 | 延迟 | 安装 |
|---|---|---|---|
miniaudio | N- API 附加包装 微型电台h | 低级 | 预建通过 @audio/speaker-* 软件包 |
process | 管道 PCM 到ffplay/ sox/ aplay | 高级 | 系统工具必须安装 |
null | 沉默并保持定时合同 | —— 说 | 内置( CI/ 无头倒置) |
webaudio | AudioWorklet, 块向后播放( 只浏览器) | 低级 | 内建 |
在PulseAudio的空槽上(无头 Linux, CI),任意一流, pacat''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''起起'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 闲去后立即取出: pactl unload-module module-suspend-on-idle && pactl load-module module-suspend-on-idle timeout=0。 。 。 .
API 密码
write = speaker(opts?)
返回汇函数。 选项 :
sampleRate——默认44100channels——默认2bitDepth—— 说8, (中文(简体) ),16(违约),24, (中文(简体) ),32bufferSize——环缓冲区以毫秒计,默认值50(浏览器: 音频队列在回调写入前, 默认)100(中文(简体) )device由 id 或 name 输出(devices()),默认系统backend——强制特定后端context——浏览器:要播放的 AudioContext ;默认情况下,页面每个样本率和设备共享一个(write.context(中文(简体) )
write(buffer, cb?)
写入PCM数据. 接受 Buffer, (中文(简体) ), Uint8Array,或 AudioBuffer。 准备下个块时, 回击起火 .
write(null)
结束播放。 Flushes 留下音频,然后关闭设备.
write.flush(cb?)
等待缓冲音频完成播放.
write.close()
立即关闭音频设备.
write.backend
活动后端的名称( N)'miniaudio', (中文(简体) ), 'process', (中文(简体) ), 'null', (中文(简体) ), 'webaudio') (中文(简体) ).
大楼
npm run build # compile native addon locally
npm test # run tests
出版
# JS-only change (no native code changed):
npm version patch && git push && git push --tags
npm publish
# Native code changed — rebuild platform packages:
npm version patch && git push && git push --tags
gh run watch # wait for CI
rm -rf artifacts
gh run download --dir artifacts \
-n speaker-darwin-arm64 -n speaker-darwin-x64 \
-n speaker-linux-x64 -n speaker-linux-arm64 -n speaker-win32-x64
# (fallback) If darwin-x64 CI is unavailable, cross-compile locally
npx node-gyp@latest rebuild --arch=x64
mkdir -p artifacts/speaker-darwin-x64
cp build/Release/speaker.node artifacts/speaker-darwin-x64/
for pkg in packages/speaker-*/; do
cp artifacts/$(basename $pkg)/speaker.node $pkg/
(cd $pkg && npm publish)
done
npm publish
许可证
麻省理工学院