Tone.js/Tone/instrument/MonoSynth.test.ts

89 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-05-03 14:10:40 +00:00
import { BasicTests } from "../../test/helper/Basic.js";
import { MonoSynth } from "./MonoSynth.js";
import { InstrumentTest } from "../../test/helper/InstrumentTests.js";
import { CompareToFile } from "../../test/helper/CompareToFile.js";
2019-10-28 22:12:20 +00:00
import { expect } from "chai";
2024-05-03 14:10:40 +00:00
import { Offline } from "../../test/helper/Offline.js";
2019-10-28 22:12:20 +00:00
describe("MonoSynth", () => {
BasicTests(MonoSynth);
InstrumentTest(MonoSynth, "C4");
2019-10-29 21:48:59 +00:00
it("matches a file", () => {
2024-05-03 15:09:28 +00:00
return CompareToFile(
() => {
const synth = new MonoSynth().toDestination();
synth.triggerAttackRelease("C4", 0.1, 0.05);
},
"monoSynth.wav",
0.01
);
2019-10-29 21:48:59 +00:00
});
2019-10-28 22:12:20 +00:00
context("API", () => {
it("can get and set oscillator attributes", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth();
2019-10-28 22:12:20 +00:00
monoSynth.oscillator.type = "triangle";
expect(monoSynth.oscillator.type).to.equal("triangle");
monoSynth.dispose();
});
it("can get and set envelope attributes", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth();
2019-10-28 22:12:20 +00:00
monoSynth.envelope.attack = 0.24;
expect(monoSynth.envelope.attack).to.equal(0.24);
monoSynth.dispose();
});
it("can get and set filter attributes", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth();
2019-10-28 22:12:20 +00:00
monoSynth.filter.Q.value = 0.4;
expect(monoSynth.filter.Q.value).to.be.closeTo(0.4, 0.001);
monoSynth.dispose();
});
it("can get and set filterEnvelope attributes", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth();
2019-10-28 22:12:20 +00:00
monoSynth.filterEnvelope.baseFrequency = 400;
expect(monoSynth.filterEnvelope.baseFrequency).to.equal(400);
monoSynth.dispose();
});
it("can be constructed with an options object", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth({
2019-10-28 22:12:20 +00:00
envelope: {
2024-05-03 15:09:28 +00:00
sustain: 0.3,
},
2019-10-28 22:12:20 +00:00
});
expect(monoSynth.envelope.sustain).to.equal(0.3);
monoSynth.dispose();
});
it("can get/set attributes", () => {
2019-10-29 21:48:59 +00:00
const monoSynth = new MonoSynth();
2019-10-28 22:12:20 +00:00
monoSynth.set({
envelope: {
2024-05-03 15:09:28 +00:00
decay: 0.24,
},
2019-10-28 22:12:20 +00:00
});
expect(monoSynth.get().envelope.decay).to.equal(0.24);
monoSynth.dispose();
});
it("is silent after triggerAttack if sustain is 0", async () => {
return await Offline(() => {
2019-10-29 21:48:59 +00:00
const synth = new MonoSynth({
2019-10-28 22:12:20 +00:00
envelope: {
attack: 0.1,
decay: 0.1,
sustain: 0,
2024-05-03 15:09:28 +00:00
},
2019-10-28 22:12:20 +00:00
}).toDestination();
synth.triggerAttack("C4", 0);
}, 0.5).then((buffer) => {
expect(buffer.getTimeOfLastSound()).to.be.closeTo(0.2, 0.01);
2019-10-28 22:12:20 +00:00
});
});
});
});