Skip to content

Change static sound playbackRate and pitch settings API #16396

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

Merged
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
42 changes: 19 additions & 23 deletions packages/dev/core/src/AudioV2/abstractAudio/staticSound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export interface IStaticSoundOptionsBase {
*
*/
loopStart: number;
}

/**
* Options stored in a static sound.
* @internal
*/
export interface IStaticSoundStoredOptions extends IAbstractSoundStoredOptions, IStaticSoundOptionsBase {
/**
* The pitch of the sound, in cents. Defaults to `0`.
* - Can be combined with {@link playbackRate}.
Expand All @@ -37,14 +44,6 @@ export interface IStaticSoundOptionsBase {
playbackRate: number;
}

/** @internal */
export interface IStaticSoundPlayOptionsBase {
/**
* The time to wait before playing the sound, in seconds. Defaults to `0`.
*/
waitTime: number;
}

/**
* Options for creating a static sound.
*/
Expand All @@ -53,7 +52,12 @@ export interface IStaticSoundOptions extends IAbstractSoundOptions, IStaticSound
/**
* Options for playing a static sound.
*/
export interface IStaticSoundPlayOptions extends IAbstractSoundPlayOptions, IStaticSoundOptionsBase, IStaticSoundPlayOptionsBase {}
export interface IStaticSoundPlayOptions extends IAbstractSoundPlayOptions, IStaticSoundOptionsBase {
/**
* The time to wait before playing the sound, in seconds. Defaults to `0`.
*/
waitTime: number;
}

/**
* Options for stopping a static sound.
Expand All @@ -65,12 +69,6 @@ export interface IStaticSoundStopOptions {
waitTime: number;
}

/**
* Options stored in a static sound.
* @internal
*/
export interface IStaticSoundStoredOptions extends IAbstractSoundStoredOptions, IStaticSoundOptionsBase {}

/**
* Abstract class representing a static sound.
*
Expand Down Expand Up @@ -145,9 +143,9 @@ export abstract class StaticSound extends AbstractSound {
public set pitch(value: number) {
this._options.pitch = value;

const instance = this._getNewestInstance() as _StaticSoundInstance;
if (instance) {
instance.pitch = value;
const it = this._instances.values();
for (let instance = it.next(); !instance.done; instance = it.next()) {
instance.value.pitch = value;
}
}

Expand All @@ -162,9 +160,9 @@ export abstract class StaticSound extends AbstractSound {
public set playbackRate(value: number) {
this._options.playbackRate = value;

const instance = this._getNewestInstance() as _StaticSoundInstance;
if (instance) {
instance.playbackRate = value;
const it = this._instances.values();
for (let instance = it.next(); !instance.done; instance = it.next()) {
instance.value.playbackRate = value;
}
}

Expand All @@ -185,8 +183,6 @@ export abstract class StaticSound extends AbstractSound {
options.loop ??= this.loop;
options.loopStart ??= this.loopStart;
options.loopEnd ??= this.loopEnd;
options.pitch ??= this.pitch;
options.playbackRate ??= this.playbackRate;
options.startOffset ??= this.startOffset;
options.volume ??= 1;
options.waitTime ??= 0;
Expand Down
12 changes: 2 additions & 10 deletions packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,13 @@ class _WebAudioStaticSoundInstance extends _StaticSoundInstance implements IWebA

/** @internal */
public set pitch(value: number) {
this._options.pitch = value;
if (this._sourceNode) {
this.engine._setAudioParam(this._sourceNode.detune, value);
}
}

/** @internal */
public set playbackRate(value: number) {
this._options.playbackRate = value;
if (this._sourceNode) {
this.engine._setAudioParam(this._sourceNode.playbackRate, value);
}
Expand Down Expand Up @@ -384,12 +382,6 @@ class _WebAudioStaticSoundInstance extends _StaticSoundInstance implements IWebA
if (options.loopEnd !== undefined) {
this._options.loopEnd = options.loopEnd;
}
if (options.pitch !== undefined) {
this._options.pitch = options.pitch;
}
if (options.playbackRate !== undefined) {
this._options.playbackRate = options.playbackRate;
}
if (options.startOffset !== undefined) {
this._options.startOffset = options.startOffset;
}
Expand Down Expand Up @@ -514,11 +506,11 @@ class _WebAudioStaticSoundInstance extends _StaticSoundInstance implements IWebA
}

const node = this._sourceNode;
node.detune.value = this._options.pitch;
node.detune.value = this._sound.pitch;
node.loop = this._options.loop;
node.loopEnd = this._options.loopEnd;
node.loopStart = this._options.loopStart;
node.playbackRate.value = this._options.playbackRate;
node.playbackRate.value = this._sound.playbackRate;
}

private _onEngineStateChanged = () => {
Expand Down
Loading