2019-06-19 14:15:15 +00:00
|
|
|
// import APITest from "helper/APITest";
|
|
|
|
import { expect } from "chai";
|
2024-05-03 18:31:14 +00:00
|
|
|
import { Offline } from "./Offline.js";
|
|
|
|
import { OutputAudio } from "./OutputAudio.js";
|
|
|
|
import { connectFrom, connectTo } from "./Connect.js";
|
2019-06-19 14:15:15 +00:00
|
|
|
|
|
|
|
export function SourceTests(Constr, args?): void {
|
|
|
|
context("Source Tests", () => {
|
|
|
|
it("can connect the output", () => {
|
|
|
|
const instance = new Constr(args);
|
|
|
|
instance.connect(connectTo());
|
|
|
|
instance.dispose();
|
|
|
|
});
|
|
|
|
|
2019-08-08 18:13:47 +00:00
|
|
|
it("has no input", () => {
|
2019-06-19 14:15:15 +00:00
|
|
|
const instance = new Constr(args);
|
|
|
|
// has no input
|
2019-08-08 18:13:47 +00:00
|
|
|
expect(instance.numberOfInputs).to.equal(0);
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts and stops", () => {
|
|
|
|
return Offline(() => {
|
|
|
|
const instance = new Constr(args);
|
|
|
|
expect(instance.state).to.equal("stopped");
|
|
|
|
instance.start(0).stop(0.2);
|
|
|
|
return (time) => {
|
|
|
|
if (time >= 0 && time < 0.2) {
|
|
|
|
expect(instance.state).to.equal("started");
|
|
|
|
} else if (time > 0.2) {
|
|
|
|
expect(instance.state).to.equal("stopped");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, 0.3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("makes a sound", () => {
|
|
|
|
return OutputAudio(() => {
|
|
|
|
const instance = new Constr(args);
|
2019-07-25 15:32:56 +00:00
|
|
|
instance.toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-14 22:25:58 +00:00
|
|
|
it("invokes the onstop method the source is stopped", () => {
|
2019-08-10 15:51:35 +00:00
|
|
|
let wasInvoked = false;
|
|
|
|
return Offline(() => {
|
|
|
|
const instance = new Constr(args);
|
|
|
|
instance.toDestination();
|
2024-05-03 18:31:14 +00:00
|
|
|
instance.onstop = () => (wasInvoked = true);
|
2019-08-10 15:51:35 +00:00
|
|
|
instance.start(0).stop(0.1);
|
|
|
|
}, 0.2).then(() => {
|
|
|
|
expect(wasInvoked).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
2019-06-19 14:15:15 +00:00
|
|
|
|
|
|
|
it("be scheduled to start in the future", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const instance = new Constr(args).toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start(0.1);
|
|
|
|
}, 0.3).then((buffer) => {
|
|
|
|
buffer.forEach((sample, time) => {
|
|
|
|
if (sample > 0) {
|
|
|
|
expect(time).to.be.at.least(0.099);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("makes no sound if it is started and then stopped with a time at or before the start time", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const instance = new Constr(args).toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start(0.1).stop(0.05);
|
|
|
|
}, 0.3).then((buffer) => {
|
|
|
|
expect(buffer.isSilent()).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can be muted", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const instance = new Constr(args).toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start(0);
|
|
|
|
instance.mute = true;
|
|
|
|
}, 0.3).then((buffer) => {
|
|
|
|
expect(buffer.isSilent()).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("be scheduled to stop in the future", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const instance = new Constr(args).toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start(0).stop(0.2);
|
|
|
|
}, 0.3).then((buffer) => {
|
|
|
|
buffer.forEach((sample, time) => {
|
|
|
|
if (time > 0.2) {
|
|
|
|
expect(sample).to.equal(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can be restarted", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const instance = new Constr(args).toDestination();
|
2019-06-19 14:15:15 +00:00
|
|
|
instance.start(0).stop(0.2);
|
|
|
|
instance.restart(0.1);
|
|
|
|
instance.stop(0.25);
|
|
|
|
}, 0.32).then((buffer) => {
|
|
|
|
expect(buffer.getRmsAtTime(0)).to.be.gt(0);
|
|
|
|
expect(buffer.getRmsAtTime(0.1)).to.be.gt(0);
|
|
|
|
expect(buffer.getRmsAtTime(0.2)).to.be.gt(0);
|
|
|
|
expect(buffer.getRmsAtTime(0.23)).to.be.gt(0);
|
|
|
|
expect(buffer.getRmsAtTime(0.3)).to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
2024-05-03 18:31:14 +00:00
|
|
|
|
2019-11-13 19:09:13 +00:00
|
|
|
it("calling restart before calling start has no effect", () => {
|
|
|
|
return Offline(() => {
|
|
|
|
const instance = new Constr(args).toDestination();
|
|
|
|
instance.restart(0.1);
|
|
|
|
}, 0.2).then((buffer) => {
|
|
|
|
expect(buffer.isSilent()).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2019-06-19 14:15:15 +00:00
|
|
|
});
|
|
|
|
}
|