2019-07-18 18:07:25 +00:00
|
|
|
import { expect } from "chai";
|
2024-05-03 18:31:14 +00:00
|
|
|
import { BasicTests } from "../../test/helper/Basic.js";
|
|
|
|
import { CompareToFile } from "../../test/helper/CompareToFile.js";
|
|
|
|
import { InstrumentTest } from "../../test/helper/InstrumentTests.js";
|
|
|
|
import { MonophonicTest } from "../../test/helper/MonophonicTests.js";
|
|
|
|
import { Offline } from "../../test/helper/Offline.js";
|
|
|
|
import { Frequency } from "../core/type/Frequency.js";
|
|
|
|
import { Synth } from "./Synth.js";
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
describe("Synth", () => {
|
|
|
|
BasicTests(Synth);
|
|
|
|
InstrumentTest(Synth, "C4");
|
2019-08-12 04:13:52 +00:00
|
|
|
MonophonicTest(Synth, "C4");
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
it("matches a file basic", () => {
|
2024-05-03 18:31:14 +00:00
|
|
|
return CompareToFile(
|
|
|
|
() => {
|
|
|
|
const synth = new Synth().toDestination();
|
|
|
|
synth.triggerAttackRelease("C4", 0.1, 0.05);
|
|
|
|
},
|
|
|
|
"synth_basic.wav",
|
|
|
|
0.3
|
|
|
|
);
|
2019-07-18 18:07:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("matches a file melody", () => {
|
2024-05-03 18:31:14 +00:00
|
|
|
return CompareToFile(
|
|
|
|
() => {
|
|
|
|
const synth = new Synth().toDestination();
|
|
|
|
synth.triggerAttack("C4", 0);
|
|
|
|
synth.triggerAttack("E4", 0.1, 0.5);
|
|
|
|
synth.triggerAttackRelease("G4", 0.5, 0.3);
|
|
|
|
synth.triggerAttackRelease("B4", 0.5, 0.5, 0.2);
|
|
|
|
},
|
|
|
|
"synth_melody.wav",
|
|
|
|
0.3
|
|
|
|
);
|
2019-07-18 18:07:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
context("API", () => {
|
|
|
|
it("can get and set oscillator attributes", () => {
|
|
|
|
const simple = new Synth();
|
|
|
|
simple.oscillator.type = "triangle";
|
|
|
|
expect(simple.oscillator.type).to.equal("triangle");
|
|
|
|
simple.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can get and set envelope attributes", () => {
|
|
|
|
const simple = new Synth();
|
|
|
|
simple.envelope.attack = 0.24;
|
|
|
|
expect(simple.envelope.attack).to.equal(0.24);
|
|
|
|
simple.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can be constructed with an options object", () => {
|
|
|
|
const simple = new Synth({
|
2019-09-14 22:12:44 +00:00
|
|
|
envelope: {
|
|
|
|
sustain: 0.3,
|
2019-07-18 18:07:25 +00:00
|
|
|
},
|
|
|
|
oscillator: {
|
|
|
|
type: "sine",
|
|
|
|
},
|
2019-09-14 22:12:44 +00:00
|
|
|
volume: -5,
|
2019-07-18 18:07:25 +00:00
|
|
|
});
|
|
|
|
expect(simple.envelope.sustain).to.equal(0.3);
|
|
|
|
expect(simple.oscillator.type).to.equal("sine");
|
|
|
|
expect(simple.volume.value).to.be.closeTo(-5, 0.1);
|
|
|
|
simple.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can get/set attributes", () => {
|
|
|
|
const simple = new Synth();
|
|
|
|
simple.set({
|
|
|
|
envelope: {
|
|
|
|
decay: 0.24,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(simple.get().envelope.decay).to.equal(0.24);
|
|
|
|
simple.dispose();
|
|
|
|
});
|
|
|
|
|
2019-09-17 16:21:27 +00:00
|
|
|
it("can get does not include omited oscillator attributes", () => {
|
|
|
|
const simple = new Synth();
|
|
|
|
expect(simple.get().oscillator).to.not.have.key("frequency");
|
|
|
|
expect(simple.get().oscillator).to.not.have.key("detune");
|
|
|
|
expect(Object.keys(simple.get().oscillator)).to.include("type");
|
|
|
|
simple.dispose();
|
|
|
|
});
|
|
|
|
|
2019-07-18 18:07:25 +00:00
|
|
|
it("can be trigged with a Tone.Frequency", () => {
|
|
|
|
return Offline(() => {
|
2019-07-25 15:32:56 +00:00
|
|
|
const synth = new Synth().toDestination();
|
2019-07-18 18:07:25 +00:00
|
|
|
synth.triggerAttack(Frequency("C4"), 0);
|
|
|
|
}).then((buffer) => {
|
|
|
|
expect(buffer.isSilent()).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is silent after triggerAttack if sustain is 0", () => {
|
|
|
|
return Offline(() => {
|
|
|
|
const synth = new Synth({
|
2019-09-14 22:12:44 +00:00
|
|
|
envelope: {
|
|
|
|
attack: 0.1,
|
|
|
|
decay: 0.1,
|
|
|
|
sustain: 0,
|
2019-07-18 18:07:25 +00:00
|
|
|
},
|
2019-07-25 15:32:56 +00:00
|
|
|
}).toDestination();
|
2019-07-18 18:07:25 +00:00
|
|
|
synth.triggerAttack("C4", 0);
|
|
|
|
}, 0.5).then((buffer) => {
|
|
|
|
expect(buffer.getTimeOfLastSound()).to.be.closeTo(0.2, 0.01);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-13 23:27:34 +00:00
|
|
|
context("Transport sync", () => {
|
|
|
|
it("is silent until the transport is started", () => {
|
|
|
|
return Offline(({ transport }) => {
|
|
|
|
const synth = new Synth().sync().toDestination();
|
|
|
|
synth.triggerAttackRelease("C4", 0.5);
|
|
|
|
transport.start(0.5);
|
|
|
|
}, 1).then((buffer) => {
|
|
|
|
expect(buffer.getTimeOfFirstSound()).is.closeTo(0.5, 0.1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("stops when the transport is stopped", () => {
|
|
|
|
return Offline(({ transport }) => {
|
|
|
|
const synth = new Synth({
|
|
|
|
envelope: {
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.sync()
|
|
|
|
.toDestination();
|
2021-10-13 23:27:34 +00:00
|
|
|
synth.triggerAttackRelease("C4", 0.5);
|
|
|
|
transport.start(0.5).stop(1);
|
|
|
|
}, 1.5).then((buffer) => {
|
|
|
|
expect(buffer.getTimeOfLastSound()).is.closeTo(1, 0.1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("goes silent at the loop boundary", () => {
|
|
|
|
return Offline(({ transport }) => {
|
|
|
|
const synth = new Synth({
|
|
|
|
envelope: {
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.sync()
|
|
|
|
.toDestination();
|
2021-10-13 23:27:34 +00:00
|
|
|
synth.triggerAttackRelease("C4", 0.8, 0.5);
|
|
|
|
transport.loopEnd = 1;
|
|
|
|
transport.loop = true;
|
|
|
|
transport.start();
|
|
|
|
}, 2).then((buffer) => {
|
|
|
|
expect(buffer.getRmsAtTime(0)).to.be.closeTo(0, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(0.6)).to.be.closeTo(0.2, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(1.1)).to.be.closeTo(0, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(1.6)).to.be.closeTo(0.2, 0.05);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can unsync", () => {
|
|
|
|
return Offline(({ transport }) => {
|
|
|
|
const synth = new Synth({
|
|
|
|
envelope: {
|
|
|
|
sustain: 1,
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.sync()
|
|
|
|
.toDestination()
|
|
|
|
.unsync();
|
2021-10-13 23:27:34 +00:00
|
|
|
synth.triggerAttackRelease("C4", 1, 0.5);
|
|
|
|
transport.start().stop(1);
|
|
|
|
}, 2).then((buffer) => {
|
|
|
|
expect(buffer.getRmsAtTime(0)).to.be.closeTo(0, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(0.6)).to.be.closeTo(0.6, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(1.4)).to.be.closeTo(0.6, 0.05);
|
|
|
|
expect(buffer.getRmsAtTime(1.6)).to.be.closeTo(0, 0.05);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-18 18:07:25 +00:00
|
|
|
context("Portamento", () => {
|
|
|
|
it("can play notes with a portamento", () => {
|
|
|
|
return Offline(() => {
|
|
|
|
const synth = new Synth({
|
2019-09-14 22:12:44 +00:00
|
|
|
portamento: 0.1,
|
2019-07-18 18:07:25 +00:00
|
|
|
});
|
|
|
|
expect(synth.portamento).to.equal(0.1);
|
2019-07-25 15:32:56 +00:00
|
|
|
synth.frequency.toDestination();
|
2019-07-18 18:07:25 +00:00
|
|
|
synth.triggerAttack(440, 0);
|
|
|
|
synth.triggerAttack(880, 0.1);
|
|
|
|
}, 0.2).then((buffer) => {
|
|
|
|
buffer.forEach((val, time) => {
|
|
|
|
if (time < 0.1) {
|
|
|
|
expect(val).to.be.closeTo(440, 1);
|
|
|
|
} else if (time < 0.2) {
|
|
|
|
expect(val).to.within(440, 880);
|
|
|
|
} else {
|
|
|
|
expect(val).to.be.closeTo(880, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|