Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spatial audio init issues #16279

Merged
merged 2 commits into from
Mar 12, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ import type { AudioSubNode } from "./audioSubNode";
export abstract class _AbstractAudioSubGraph {
private _createSubNodePromises: { [key: string]: Promise<_AbstractAudioSubNode> } = {};
private _isDisposed = false;
private _subNodes: { [key: string]: AbstractNamedAudioNode } = {};
private _subNodes: { [key: string]: _AbstractAudioSubNode } = {};

/**
* Executes the given callback with the named sub node, creating the sub node if needed.
*
* Note that `callback` is executed synchronously if the sub node already exists, otherwise the sub node is created
* asynchronously before `callback` is executed.
*
* @param name The name of the sub node
* @param callback The function to call with the named sub node
*
Expand All @@ -41,10 +38,16 @@ export abstract class _AbstractAudioSubGraph {
return;
}

const promise = this._createSubNodePromises[name] ?? this.createAndAddSubNode(name);
this._createSubNodePromisesResolved().then(() => {
const node = this.getSubNode(name);
if (node) {
callback(node as T);
return;
}

promise.then((node) => {
callback(node as T);
this.createAndAddSubNode(name).then((node) => {
callback(node as T);
});
});
}

Expand Down Expand Up @@ -127,7 +130,7 @@ export abstract class _AbstractAudioSubGraph {
return Promise.all(Object.values(this._createSubNodePromises));
}

private _addSubNode(node: AbstractNamedAudioNode): void {
private _addSubNode(node: _AbstractAudioSubNode): void {
if (this._isDisposed) {
node.dispose();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ export abstract class _SpatialAudio extends AbstractSpatialAudio {
super();

const subNode = _GetSpatialAudioSubNode(subGraph);
if (!subNode) {
throw new Error("Sub node not found");
}
if (subNode) {
this._position = subNode.position.clone();
this._rotation = subNode.rotation.clone();
this._rotationQuaternion = subNode.rotationQuaternion.clone();
} else {
this._position = _SpatialAudioDefaults.position.clone();
this._rotation = _SpatialAudioDefaults.rotation.clone();
this._rotationQuaternion = _SpatialAudioDefaults.rotationQuaternion.clone();

this._position = subNode.position.clone();
this._rotation = subNode.rotation.clone();
this._rotationQuaternion = subNode.rotationQuaternion.clone();
subGraph.createAndAddSubNode(AudioSubNode.SPATIAL);
}

this._subGraph = subGraph;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export abstract class _WebAudioBaseSubGraph extends _AbstractAudioSubGraph {
const hasAnalyzerOptions = _HasAudioAnalyzerOptions(options);

if (hasAnalyzerOptions) {
this.createAndAddSubNode(AudioSubNode.ANALYZER);
await this.createAndAddSubNode(AudioSubNode.ANALYZER);
}

this.createAndAddSubNode(AudioSubNode.VOLUME);
await this.createAndAddSubNode(AudioSubNode.VOLUME);

await this._createSubNodePromisesResolved();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export abstract class _WebAudioBusAndSoundSubGraph extends _WebAudioBaseSubGraph

/** @internal */
public override async init(options: Partial<IWebAudioBusAndSoundSubGraphOptions>): Promise<void> {
super.init(options);
await super.init(options);

let hasSpatialOptions = false;
let hasStereoOptions = false;

if ((hasSpatialOptions = _HasSpatialAudioOptions(options))) {
this.createAndAddSubNode(AudioSubNode.SPATIAL);
await this.createAndAddSubNode(AudioSubNode.SPATIAL);
}
if ((hasStereoOptions = _HasStereoAudioOptions(options))) {
this.createAndAddSubNode(AudioSubNode.STEREO);
await this.createAndAddSubNode(AudioSubNode.STEREO);
}

await this._createSubNodePromisesResolved();
Expand Down
Loading