Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(audio): master-bus spectrum analysis for visualizers

yhh8bba4bf1447e
diff --git a/sdk/src/audio/Audio.ts b/sdk/src/audio/Audio.ts
index 62e9ec51a..4b116d3e7 100644
--- a/sdk/src/audio/Audio.ts
+++ b/sdk/src/audio/Audio.ts
@@ -202,6 +202,13 @@ export class AudioAPI {
return this.bufferCache_.get(url);
}
+ /** Fill `out` with the master output's frequency spectrum (0-255 per bin,
+ * low→high) for visualizers. Returns false on backends without analysis
+ * (e.g. WeChat) — callers should treat that as silence. */
+ getSpectrum(out: Uint8Array): boolean {
+ return this.backend_.getFrequencyData?.(out) ?? false;
+ }
+
dispose(): void {
this.disposed_ = true;
for (const id of this.fadeAnimIds_) {
diff --git a/sdk/src/audio/PlatformAudioBackend.ts b/sdk/src/audio/PlatformAudioBackend.ts
index fff019ff6..d0660e014 100644
--- a/sdk/src/audio/PlatformAudioBackend.ts
+++ b/sdk/src/audio/PlatformAudioBackend.ts
@@ -51,4 +51,8 @@ export interface PlatformAudioBackend {
suspend(): void;
resume(): void;
dispose(): void;
+ /** Fill `out` with the master output's frequency magnitudes (0-255 per bin,
+ * low→high), returning true. Optional: backends without analysis (e.g.
+ * WeChat) omit it, and callers treat a missing method as silence. */
+ getFrequencyData?(out: Uint8Array): boolean;
}
diff --git a/sdk/src/audio/WebAudioBackend.ts b/sdk/src/audio/WebAudioBackend.ts
index b357b5ba2..482934bdc 100644
--- a/sdk/src/audio/WebAudioBackend.ts
+++ b/sdk/src/audio/WebAudioBackend.ts
@@ -144,6 +144,7 @@ export class WebAudioBackend implements PlatformAudioBackend {
private nextBufferId_ = 0;
private nextHandleId_ = 0;
private resumeHandler_: (() => void) | null = null;
+ private analyser_: AnalyserNode | null = null;
get mixer(): AudioMixer | null {
return this.mixer_;
@@ -158,6 +159,13 @@ export class WebAudioBackend implements PlatformAudioBackend {
this.mixer_ = new AudioMixer(this.context_, options.mixerConfig);
this.pool_ = new AudioPool(this.context_, options.initialPoolSize);
+ // Tap the master bus for spectrum analysis (a side branch — the analyser
+ // is a sink, so it never alters the audio reaching the destination).
+ this.analyser_ = this.context_.createAnalyser();
+ this.analyser_.fftSize = 128;
+ this.analyser_.smoothingTimeConstant = 0.7;
+ this.mixer_.master.node.connect(this.analyser_);
+
if (this.context_.state === 'suspended') {
const resume = () => {
this.context_!.resume();
@@ -272,6 +280,12 @@ export class WebAudioBackend implements PlatformAudioBackend {
return handle;
}
+ getFrequencyData(out: Uint8Array): boolean {
+ if (!this.analyser_) return false;
+ this.analyser_.getByteFrequencyData(out);
+ return true;
+ }
+
suspend(): void {
this.context_?.suspend();
}
@@ -287,6 +301,7 @@ export class WebAudioBackend implements PlatformAudioBackend {
document.removeEventListener('keydown', this.resumeHandler_);
this.resumeHandler_ = null;
}
+ this.analyser_ = null;
this.pool_ = null;
this.mixer_ = null;
this.buffers_.clear();
diff --git a/sdk/tests/audio-api.test.ts b/sdk/tests/audio-api.test.ts
index 3c481a247..c0b59999b 100644
--- a/sdk/tests/audio-api.test.ts
+++ b/sdk/tests/audio-api.test.ts
@@ -195,6 +195,24 @@ describe('AudioAPI', () => {
});
});
+ describe('getSpectrum', () => {
+ it('returns false on a backend without analysis (silent fallback)', () => {
+ // the default mock backend omits getFrequencyData (like WeChat)
+ expect(audio.getSpectrum(new Uint8Array(8))).toBe(false);
+ });
+
+ it('forwards to the backend analyser and fills the array', () => {
+ const withAnalyser = createMockBackend();
+ withAnalyser.getFrequencyData = vi.fn((o: Uint8Array) => { o.fill(200); return true; });
+ const a = new AudioAPI(withAnalyser, createMockMixer());
+
+ const out = new Uint8Array(8);
+ expect(a.getSpectrum(out)).toBe(true);
+ expect(withAnalyser.getFrequencyData).toHaveBeenCalledWith(out);
+ expect(out.every(v => v === 200)).toBe(true);
+ });
+ });
+
describe('dispose', () => {
it('should stop BGM and dispose backend', async () => {
await audio.preload('bgm.mp3');