Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/dev/core/src/AudioV2/abstractAudio/audioEngineV2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Nullable } from "../../types";
import type { IAudioParameterRampOptions } from "../audioParameter";
import type { AbstractAudioNode, AbstractNamedAudioNode } from "./abstractAudioNode";
import type { AbstractSound } from "./abstractSound";
import type { AbstractSoundSource, ISoundSourceOptions } from "./abstractSoundSource";
import type { AudioBus, IAudioBusOptions } from "./audioBus";
import type { IMainAudioBusOptions, MainAudioBus } from "./mainAudioBus";
Expand Down Expand Up @@ -51,6 +52,7 @@ export type AudioEngineV2State = "closed" | "interrupted" | "running" | "suspend
export abstract class AudioEngineV2 {
/** Not owned, but all items should be in `_nodes` container, too, which is owned. */
private readonly _mainBuses = new Set<MainAudioBus>();
private readonly _sounds = new Array<AbstractSound>();

/** Owned top-level sound and bus nodes. */
private readonly _nodes = new Set<AbstractNamedAudioNode>();
Expand Down Expand Up @@ -132,6 +134,13 @@ export abstract class AudioEngineV2 {
this._parameterRampDuration = Math.max(0, value);
}

/**
* The list of static and streaming sounds created by the audio engine.
*/
public get sounds(): ReadonlyArray<AbstractSound> {
return this._sounds;
}

/**
* Creates a new audio bus.
* @param name - The name of the audio bus.
Expand Down Expand Up @@ -213,6 +222,7 @@ export abstract class AudioEngineV2 {

this._mainBuses.clear();
this._nodes.clear();
this._sounds.length = 0;

this._defaultMainBus = null;
}
Expand Down Expand Up @@ -275,6 +285,19 @@ export abstract class AudioEngineV2 {
protected _removeNode(node: AbstractNamedAudioNode): void {
this._nodes.delete(node);
}

protected _addSound(sound: AbstractSound): void {
this._sounds.push(sound);
this._addNode(sound);
}

protected _removeSound(sound: AbstractSound): void {
const index = this._sounds.indexOf(sound);
if (index !== -1) {
this._sounds.splice(index, 1);
}
this._removeNode(sound);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/dev/core/src/AudioV2/webAudio/webAudioEngine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Observable } from "../../Misc/observable";
import type { Nullable } from "../../types";
import type { AbstractNamedAudioNode } from "../abstractAudio/abstractAudioNode";
import type { AbstractSound } from "../abstractAudio/abstractSound";
import type { AbstractSoundSource, ISoundSourceOptions } from "../abstractAudio/abstractSoundSource";
import type { AudioBus, IAudioBusOptions } from "../abstractAudio/audioBus";
import type { AudioEngineV2State, IAudioEngineV2Options } from "../abstractAudio/audioEngineV2";
Expand Down Expand Up @@ -413,6 +414,16 @@ export class _WebAudioEngine extends AudioEngineV2 {
super._removeNode(node);
}

/** @internal */
public override _addSound(sound: AbstractSound): void {
super._addSound(sound);
}

/** @internal */
public override _removeSound(sound: AbstractSound): void {
super._removeSound(sound);
}

/** @internal */
public _addUpdateObserver(callback: () => void): void {
if (!this._updateObservable) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class _WebAudioStaticSound extends StaticSound implements IWebAudioSuperN
this.play();
}

this.engine._addNode(this);
this.engine._addSound(this);
}

/** @internal */
Expand Down Expand Up @@ -141,7 +141,7 @@ export class _WebAudioStaticSound extends StaticSound implements IWebAudioSuperN

this._subGraph.dispose();

this.engine._removeNode(this);
this.engine._removeSound(this);
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class _WebAudioStreamingSound extends StreamingSound implements IWebAudio
this.play(options);
}

this.engine._addNode(this);
this.engine._addSound(this);
}

/** @internal */
Expand Down Expand Up @@ -127,7 +127,7 @@ export class _WebAudioStreamingSound extends StreamingSound implements IWebAudio

this._subGraph.dispose();

this.engine._removeNode(this);
this.engine._removeSound(this);
}

/** @internal */
Expand Down
31 changes: 31 additions & 0 deletions packages/tools/tests/test/audioV2/audioEngineV2.sounds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { InitAudioV2Tests } from "./utils/audioV2.utils";

import { expect, test } from "@playwright/test";

InitAudioV2Tests();

test.describe("AudioEngineV2 sounds", () => {
test("Sounds count should be 1 when sound is created", async ({ page }) => {
const soundCount = await page.evaluate(async () => {
const audioEngine = await AudioV2Test.CreateAudioEngineAsync();
await AudioV2Test.CreateSoundAsync(audioTestConfig.pulseTrainSoundFile, { spatialPosition: new BABYLON.Vector3(0, 0, -1) });

return audioEngine.sounds.length;
});

expect(soundCount).toBe(1);
});

test("Sounds count should be 0 when sound is disposed", async ({ page }) => {
const soundCount = await page.evaluate(async () => {
const audioEngine = await AudioV2Test.CreateAudioEngineAsync();
const sound = await AudioV2Test.CreateSoundAsync(audioTestConfig.pulseTrainSoundFile, { spatialPosition: new BABYLON.Vector3(0, 0, -1) });

sound.dispose();

return audioEngine.sounds.length;
});

expect(soundCount).toBe(0);
});
});