converting helpers to ts

This commit is contained in:
tambien 2019-06-19 10:15:15 -04:00
parent ff157bf4ad
commit 63ad175b6d
3 changed files with 241 additions and 0 deletions

106
Tone/source/Noise.test.ts Normal file
View file

@ -0,0 +1,106 @@
import { BasicTests } from "test/helper/Basic";
import { Noise } from "./Noise";
import {expect} from "chai";
import {SourceTests} from "test/helper/SourceTests";
import {OutputAudio} from "test/helper/OutputAudio";
import {CompareToFile} from "test/helper/CompareToFile";
describe("Noise", () => {
// run the common tests
BasicTests(Noise);
SourceTests(Noise);
it("matches a file", () => {
return CompareToFile(() => {
const noise = new Noise().toMaster();
noise.start(0.1).stop(0.2);
}, "noise.wav", 9);
});
context("Get/Set", () => {
it("can be constructed with an options object", () => {
const noise = new Noise({
type: "brown",
});
expect(noise.type).to.equal("brown");
noise.dispose();
});
it("can set the playbackRate in the constructor", () => {
const noise = new Noise({
playbackRate: 2,
});
expect(noise.playbackRate).to.equal(2);
noise.dispose();
});
it("can set the playbackRate after the noise is started", () => {
return OutputAudio(() => {
const noise = new Noise().toMaster();
noise.start();
noise.playbackRate = 3;
expect(noise.playbackRate).to.equal(3);
});
});
});
context("Type", () => {
it("can be set to 3 noise types", () => {
const noise = new Noise();
const types = ["white", "brown", "pink"];
types.forEach(type => {
// @ts-ignore
noise.type = type;
expect(noise.type).to.equal(type);
});
noise.dispose();
});
it("cant set invalid type", () => {
const noise = new Noise();
expect(() => {
// @ts-ignore
noise.type = "else";
}).to.throw(Error);
noise.dispose();
});
it("outputs white noise", () => {
return OutputAudio(() => {
const noise = new Noise("white");
noise.toMaster();
noise.start();
});
});
it("outputs pink noise", () => {
return OutputAudio(() => {
const noise = new Noise("pink");
noise.toMaster();
noise.start();
});
});
it("outputs brown noise", () => {
return OutputAudio(() => {
const noise = new Noise("brown");
noise.toMaster();
noise.start();
});
});
it("can set the type after the noise is started", () => {
return OutputAudio(() => {
const noise = new Noise();
noise.toMaster();
noise.start();
noise.type = "brown";
});
});
});
});

View file

@ -0,0 +1,8 @@
import { expect } from "chai";
import { Offline } from "./Offline";
export function OutputAudio(callback) {
return Offline(callback, 0.1).then((buffer) => {
expect(buffer.isSilent()).to.equal(false);
});
}

127
test/helper/SourceTests.ts Normal file
View file

@ -0,0 +1,127 @@
// import APITest from "helper/APITest";
import { expect } from "chai";
// import Source from "Tone/source/Source";
// import OutputAudioStereo from "helper/OutputAudioStereo";
// import Test from "helper/Test";
import { Offline } from "test/helper/Offline";
import { OutputAudio } from "test/helper/OutputAudio";
import { connectFrom, connectTo } from "./Connect";
export function SourceTests(Constr, args?): void {
context("Source Tests", () => {
// it("extends Tone.Source", () => {
// const instance = new Constr(args);
// expect(instance).to.be.an.instanceof(Source);
// instance.dispose();
// });
it("can connect the output", () => {
const instance = new Constr(args);
instance.connect(connectTo());
instance.dispose();
});
it.skip("has no input", () => {
const instance = new Constr(args);
// has no input
expect(() => {
connectFrom().connect(instance);
}).throws(Error);
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);
instance.toMaster();
instance.start();
});
});
// it("produces sound in both channels", () => {
// return OutputAudioStereo(() => {
// const instance = new Constr(args);
// instance.toMaster();
// instance.start();
// });
// });
it("be scheduled to start in the future", () => {
return Offline(() => {
const instance = new Constr(args).toMaster();
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(() => {
const instance = new Constr(args).toMaster();
instance.start(0.1).stop(0.05);
}, 0.3).then((buffer) => {
expect(buffer.isSilent()).to.equal(true);
});
});
it("can be muted", () => {
return Offline(() => {
const instance = new Constr(args).toMaster();
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(() => {
const instance = new Constr(args).toMaster();
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(() => {
const instance = new Constr(args).toMaster();
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);
});
});
});
}