Skip to content

Commit 455c36e

Browse files
authored
Add sound activeInstancesCount property (#17368)
Partially addresses feature request: https://forum.babylonjs.com/t/allow-to-reject-sound-plays-if-maxinstances-number-is-reached/60433
1 parent 774f661 commit 455c36e

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

packages/dev/core/src/AudioV2/abstractAudio/abstractSound.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ export abstract class AbstractSound extends AbstractSoundSource {
6767
super(name, engine, AudioNodeType.HAS_INPUTS_AND_OUTPUTS); // Inputs are for instances.
6868
}
6969

70+
/**
71+
* The number of active instances of the sound that are currently playing.
72+
*/
73+
public get activeInstancesCount(): number {
74+
return this._instances.size;
75+
}
76+
7077
/**
7178
* Whether the sound should start playing automatically. Defaults to `false`.
7279
*/

packages/dev/core/src/AudioV2/abstractAudio/abstractSoundInstance.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,9 @@ export abstract class _AbstractSoundInstance extends AbstractAudioNode {
6060

6161
this._state = value;
6262
this.onStateChangedObservable.notifyObservers(this);
63+
64+
if (this._state === SoundState.Stopped) {
65+
this.onEndedObservable.notifyObservers(this);
66+
}
6367
}
6468
}

packages/dev/core/src/AudioV2/webAudio/webAudioStreamingSound.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class _WebAudioStreamingSoundInstance extends _StreamingSoundInstance implements
239239

240240
if (restart) {
241241
this._mediaElement.pause();
242-
this._setState(SoundState.Stopped);
242+
this._state = SoundState.Stopped;
243243
}
244244

245245
this._options.startOffset = value;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { EvaluateTestAsync } from "../utils/abstractSound.utils";
2+
import { SoundType } from "../utils/audioV2.utils";
3+
4+
import { expect, test } from "@playwright/test";
5+
6+
export const AddSharedAbstractSoundActiveInstancesCountTests = (soundType: SoundType) => {
7+
test.describe(`${soundType} activeInstancesCount`, () => {
8+
test("The `activeInstancesCount` property should equal 1 while playing", async ({ page }) => {
9+
const result = await EvaluateTestAsync(page, soundType, async ({ soundType }) => {
10+
await AudioV2Test.CreateAudioEngineAsync(soundType);
11+
const sound = await AudioV2Test.CreateAbstractSoundAsync(soundType, audioTestConfig.pulsed3CountSoundFile);
12+
13+
sound.play();
14+
await AudioV2Test.WaitAsync(1);
15+
16+
return sound.activeInstancesCount;
17+
});
18+
19+
expect(result).toBe(1);
20+
});
21+
22+
test("The `activeInstancesCount` property should equal 0 when stopped", async ({ page }) => {
23+
const result = await EvaluateTestAsync(page, soundType, async ({ soundType }) => {
24+
await AudioV2Test.CreateAudioEngineAsync(soundType);
25+
const sound = await AudioV2Test.CreateAbstractSoundAsync(soundType, audioTestConfig.pulsed3CountSoundFile);
26+
27+
sound.play();
28+
await AudioV2Test.WaitAsync(1);
29+
sound.stop();
30+
31+
return sound.activeInstancesCount;
32+
});
33+
34+
expect(result).toBe(0);
35+
});
36+
37+
test("The `activeInstancesCount` property should equal 2 when played twice", async ({ page }) => {
38+
const result = await EvaluateTestAsync(page, soundType, async ({ soundType }) => {
39+
await AudioV2Test.CreateAudioEngineAsync(soundType);
40+
const sound = await AudioV2Test.CreateAbstractSoundAsync(soundType, audioTestConfig.pulsed3CountSoundFile);
41+
42+
sound.play();
43+
sound.play();
44+
await AudioV2Test.WaitAsync(1);
45+
46+
return sound.activeInstancesCount;
47+
});
48+
49+
expect(result).toBe(2);
50+
});
51+
52+
test("The `activeInstancesCount` property should equal 0 when played twice and stopped", async ({ page }) => {
53+
const result = await EvaluateTestAsync(page, soundType, async ({ soundType }) => {
54+
await AudioV2Test.CreateAudioEngineAsync(soundType);
55+
const sound = await AudioV2Test.CreateAbstractSoundAsync(soundType, audioTestConfig.pulsed3CountSoundFile);
56+
57+
sound.play();
58+
sound.play();
59+
await AudioV2Test.WaitAsync(1);
60+
sound.stop();
61+
62+
return sound.activeInstancesCount;
63+
});
64+
65+
expect(result).toBe(0);
66+
});
67+
});
68+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { AddSharedAbstractSoundActiveInstancesCountTests } from "./shared/abstractSound.activeInstancesCount";
2+
import { InitAudioV2Tests } from "./utils/audioV2.utils";
3+
4+
InitAudioV2Tests();
5+
AddSharedAbstractSoundActiveInstancesCountTests("StaticSound");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { AddSharedAbstractSoundActiveInstancesCountTests } from "./shared/abstractSound.activeInstancesCount";
2+
import { InitAudioV2Tests } from "./utils/audioV2.utils";
3+
4+
InitAudioV2Tests();
5+
AddSharedAbstractSoundActiveInstancesCountTests("StreamingSound");

0 commit comments

Comments
 (0)