Skip to content
Open
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
9 changes: 3 additions & 6 deletions src/browser/speakers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,10 @@ export default class Speakers {
}

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

// start() is async because audioWorklet.addModule() returns a promise.
Expand Down
29 changes: 29 additions & 0 deletions test/speakers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from "node:test";
import assert from "node:assert/strict";

import Speakers from "../src/browser/speakers.js";

test("getSampleRate returns default without creating AudioContext", () => {
let constructions = 0;
globalThis.window = {
AudioContext: class {
constructor() {
constructions++;
this.sampleRate = 48000;
}
},
};

const speakers = new Speakers({ onBufferUnderrun: () => {} });

assert.equal(speakers.getSampleRate(), 44100);
assert.equal(constructions, 0);
});

test("getSampleRate uses active audio context sample rate", () => {
globalThis.window = {};
const speakers = new Speakers({ onBufferUnderrun: () => {} });
speakers.audioCtx = { sampleRate: 48000 };

assert.equal(speakers.getSampleRate(), 48000);
});