From 8bdb2d3c52973b69f0e5caa8577d09e8b72d6967 Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Fri, 3 Nov 2023 20:58:28 +0100 Subject: [PATCH] fix: pitch/speed issue on devices with different sample rates (#98) 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 --- src/crunker.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crunker.ts b/src/crunker.ts index 1303dd7..27e277b 100644 --- a/src/crunker.ts +++ b/src/crunker.ts @@ -32,7 +32,7 @@ export default class Crunker { * for the device being used. */ constructor({ sampleRate }: Partial = {}) { - this._context = this._createContext(); + this._context = this._createContext(sampleRate); sampleRate ||= this._context.sampleRate; @@ -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 }); } /**