Skip to content

Speakers.getSampleRate() wastefully creates/destroys AudioContext #613

@bfirsh

Description

@bfirsh

Summary

browser/speakers.js:21-29:

getSampleRate() {
  if (!window.AudioContext) return 44100;
  let myCtx = new window.AudioContext();
  let sampleRate = myCtx.sampleRate;
  myCtx.close();
  return sampleRate;
}

Creates and immediately destroys an AudioContext just to read the sample rate. This is wasteful and can trigger browser autoplay warnings.

Fix

Cache the sample rate, or defer reading it until start() is called and the AudioContext is created anyway:

getSampleRate() {
  if (this.audioCtx) return this.audioCtx.sampleRate;
  return 44100; // Default until start() is called
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: browserBrowser integration (browser/)difficulty: easySmall, localized change (1-2 lines)enhancementNew feature or capabilitypriority: lowMinor inaccuracy or code quality issue

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions