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

[Sound] New pitch accessor #31

Open
beefchimi opened this issue Mar 12, 2023 · 0 comments
Open

[Sound] New pitch accessor #31

beefchimi opened this issue Mar 12, 2023 · 0 comments
Labels
✨ Feature New addition to the codebase (feature, utility, component, etc.)

Comments

@beefchimi
Copy link
Owner

beefchimi commented Mar 12, 2023

We should be able to adjust the pitch of each Sound. This probably should be restricted to Sound and unavailable on Stack or Earwurm.

Unlike playbackRate, there does not appear to be a simple solution to changing pitch. While there is a detune property on a AudioBufferSourceNode, apparently it is:

  1. Not supported in Safari.
  2. Appears to be the same as playbackRate but for cents instead of a "multiplier".

This will need to be investigated, as current solutions seem to indicate a new for a AudioWorklet:

Implementation

this._pitch = 1;

get pitch() {
    return this._pitch;
}

set pitch(value: number) {
    const oldPitch = this._pitch;
    const newPitch = clamp(tokens.minPitch, value, tokens.maxPitch);

    if (oldPitch === newPitch) return;

    this._pitch = newPatch;
    this.emit('pitch', newPitch);

    // Do something to actually change sound pitch...
}

Usage

const sound1 = await stack.prepare();
// Should be `1` by default.
const currentPitch = sound1.pitch;

// If using a Setter:
sound1.pitch = 2;

// Otherwise, if we prefer a method:
sound1.setPitch(2, 1000);

const sound2 = await stack.prepare({pitch: 2});
const sound3 = new Sound(...props, {pitch: 2});

Key points

@beefchimi beefchimi added the ✨ Feature New addition to the codebase (feature, utility, component, etc.) label Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature New addition to the codebase (feature, utility, component, etc.)
Projects
None yet
Development

No branches or pull requests

1 participant