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
38 changes: 38 additions & 0 deletions Tone/signal/ToneConstantSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,44 @@ describe("ToneConstantSource", () => {
source.dispose();
});

it("schedules a future start when the audio context is resumed", async () => {
const context = new Context();
expect(context.state).to.equal("suspended");

const originalCreateConstantSource = context.createConstantSource;
const nativeSource = context.rawContext.createConstantSource();
const startTimes: number[] = [];
let source: ToneConstantSource | undefined;

context.createConstantSource = () =>
({
connect: () => nativeSource,
disconnect: () => {},
numberOfOutputs: 1,
offset: nativeSource.offset,
start: (time = 0) => {
startTimes.push(time);
},
stop: () => {},
}) as unknown as ConstantSourceNode;

try {
source = new ToneConstantSource({
context,
});

source.start(1);

await context.resume();

expect(startTimes).to.deep.equal([1]);
} finally {
context.createConstantSource = originalCreateConstantSource;
source?.dispose();
context.dispose();
}
});

it("context can be suspended again", async () => {
const context = new Context();
expect(context.state).to.equal("suspended");
Expand Down
7 changes: 5 additions & 2 deletions Tone/signal/ToneConstantSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ export class ToneConstantSource<
this._source = this.context.createConstantSource();
connect(this._source, this._gainNode);
this.offset?.setParam(this._source.offset);
if (this.state === "started") {
this._source.start(0);
const isStarted = this._startTime !== -1;
const isStoppedBeforeStart =
this._stopTime !== -1 && this._stopTime < this._startTime;
if (isStarted && !isStoppedBeforeStart) {
this._source.start(this._startTime);
}
}

Expand Down
Loading