@flare-engine/音频
网络音频形状的运行时间音频 —— AudioManager (净值/暂停/恢复), SFXPool (前解码缓冲器+多声盖), Music (单行道有十字架), createChannels (主 / sfx / 音乐增益搅拌器), renderSynth (程序SFX改为PCM). 生命周期是手动的:呼叫 suspend()编号 :resume() 从自己的背景/前景处理器(见下文).
安装
bun add @flare-engine/audio
原生:配对 react-native-audio-api (Web Audio API on nural.). 互联网档案馆的存檔,存档日期2013-12-11. 这是一个可选的对等者,所以
将它安装在一个在本地播放音频的应用程序中( namebun add react-native-audio-api; 计 数
软件包从不导入。 在网页上,浏览器 AudioContext 不需要安装 .
使用量
import { AudioManager, SFXPool, createChannels } from '@flare-engine/audio';
import type { AudioContextLike, AudioBufferLike } from '@flare-engine/audio';
// AudioContextLike is the engine's interface — obtain a real one from
// `new AudioContext()` (react-native-audio-api on native, the Web Audio global on web).
declare const ctx: AudioContextLike;
declare const shootBuffer: AudioBufferLike;
// One AudioContext, created lazily via the manager's factory.
const manager = new AudioManager();
manager.init(() => ctx);
// master ← sfx / music gain mixer.
const channels = createChannels(ctx);
// SFX pool: cap simultaneous voices, route through the sfx channel.
const sfx = new SFXPool(8);
sfx.connect(ctx, channels.get('sfx')!.node);
// Register pre-decoded buffers (decode with react-native-audio-api), then play.
sfx.register('shoot', shootBuffer);
sfx.play('shoot', { volume: 0.8, rate: 1.25 });
sfx.play('hit', { delay: 0.35 }); // start 350 ms from now on the audio clock
综合SFX
renderSynth 将一个描述的声音(正弦/正弦/锯齿/噪音、ADSR信封、线频扫描)变成一个呼叫者拥有的单声道 Float32Array· ; fillPCMBuffer 复制到创建的上下文中.
import { fillPCMBuffer, renderSynth, synthSampleCount } from '@flare-engine/audio';
import type { AudioContextLike, SFXPool } from '@flare-engine/audio';
declare const ctx: AudioContextLike;
declare const sfx: SFXPool;
const rate = 44100;
const n = synthSampleCount(0.15, rate);
const pcm = new Float32Array(n);
renderSynth({ wave: 'square', frequency: 880, endFrequency: 220, duration: 0.15 }, rate, pcm);
const buffer = ctx.createBuffer?.(1, n, rate); // optional on AudioContextLike
if (buffer) {
fillPCMBuffer(buffer, pcm);
sfx.register('blip', buffer);
}
生命周期(人工)
没有自动的背景/前景处理——没有订阅 AppState 或者说 visibilitychange 给你的 驱动 suspend()编号 :resume() 从您自己的生命周期事件:
import { AppState } from 'react-native';
import { AudioManager } from '@flare-engine/audio';
declare const manager: AudioManager;
AppState.addEventListener('change', (state) => {
if (state === 'active') manager.resume();
else manager.suspend();
});
在网络上订阅 visibilitychange数字 :
import { AudioManager } from '@flare-engine/audio';
declare const manager: AudioManager;
document.addEventListener('visibilitychange', () => {
document.hidden ? manager.suspend() : manager.resume();
});
见 API 规格。 。 。 .
许可证
阿帕奇 - 2.0