Skip to content

Commit

Permalink
fix: pitch/speed issue on devices with different sample rates (#98)
Browse files Browse the repository at this point in the history
This fixes an issue where different devices and browsers would result in pitch/speed discrepancies when playing back audio files.

The `sampleRate` option wasn't well supported, but is now implemented by all browsers: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/AudioContext#browser_compatibility

Co-authored-by: Tom Robinson <[email protected]>
  • Loading branch information
davwheat and t5r7 authored Nov 3, 2023
1 parent 0d14b93 commit 8bdb2d3
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/crunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Crunker {
* for the device being used.
*/
constructor({ sampleRate }: Partial<CrunkerConstructorOptions> = {}) {
this._context = this._createContext();
this._context = this._createContext(sampleRate);

sampleRate ||= this._context.sampleRate;

Expand All @@ -44,9 +44,9 @@ export default class Crunker {
*
* @internal
*/
private _createContext(): AudioContext {
private _createContext(sampleRate: number = 44_100): AudioContext {
window.AudioContext = window.AudioContext || (window as any).webkitAudioContext || (window as any).mozAudioContext;
return new AudioContext();
return new AudioContext({ sampleRate });
}

/**
Expand Down

0 comments on commit 8bdb2d3

Please sign in to comment.