2024-05-03 18:31:14 +00:00
|
|
|
import { FrequencyEnvelope } from "./FrequencyEnvelope.js";
|
|
|
|
import { BasicTests } from "../../../test/helper/Basic.js";
|
|
|
|
import { Offline } from "../../../test/helper/Offline.js";
|
|
|
|
import { connectFrom, connectTo } from "../../../test/helper/Connect.js";
|
|
|
|
import { Envelope } from "./Envelope.js";
|
2019-10-26 15:43:00 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
|
|
|
describe("FrequencyEnvelope", () => {
|
|
|
|
BasicTests(FrequencyEnvelope);
|
|
|
|
|
|
|
|
context("FrequencyEnvelope", () => {
|
|
|
|
it("has an output connections", () => {
|
2019-10-28 16:19:33 +00:00
|
|
|
const freqEnv = new FrequencyEnvelope();
|
2019-10-26 15:43:00 +00:00
|
|
|
freqEnv.connect(connectTo());
|
|
|
|
connectFrom().connect(freqEnv);
|
|
|
|
freqEnv.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("extends Envelope", () => {
|
2019-10-28 16:19:33 +00:00
|
|
|
const freqEnv = new FrequencyEnvelope();
|
2019-10-26 15:43:00 +00:00
|
|
|
expect(freqEnv).to.be.instanceOf(Envelope);
|
|
|
|
freqEnv.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can get and set values an Objects", () => {
|
2019-10-28 16:19:33 +00:00
|
|
|
const freqEnv = new FrequencyEnvelope();
|
|
|
|
const values = {
|
2019-10-26 15:43:00 +00:00
|
|
|
attack: 0,
|
|
|
|
release: "4n",
|
|
|
|
baseFrequency: 20,
|
2024-05-03 18:31:14 +00:00
|
|
|
octaves: 4,
|
2019-10-26 15:43:00 +00:00
|
|
|
};
|
|
|
|
freqEnv.set(values);
|
|
|
|
expect(freqEnv.get()).to.contain.keys(Object.keys(values));
|
|
|
|
expect(freqEnv.baseFrequency).to.equal(20);
|
|
|
|
expect(freqEnv.octaves).to.equal(4);
|
|
|
|
freqEnv.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can take parameters as both an object and as arguments", () => {
|
2019-11-17 18:09:19 +00:00
|
|
|
const env0 = new FrequencyEnvelope({
|
2019-10-26 15:43:00 +00:00
|
|
|
attack: 0,
|
|
|
|
decay: 0.5,
|
|
|
|
sustain: 1,
|
2024-05-03 18:31:14 +00:00
|
|
|
exponent: 3,
|
2019-10-26 15:43:00 +00:00
|
|
|
});
|
|
|
|
expect(env0.attack).to.equal(0);
|
|
|
|
expect(env0.decay).to.equal(0.5);
|
|
|
|
expect(env0.sustain).to.equal(1);
|
|
|
|
expect(env0.exponent).to.equal(3);
|
|
|
|
env0.dispose();
|
2019-11-17 18:09:19 +00:00
|
|
|
const env1 = new FrequencyEnvelope(0.1, 0.2, 0.3);
|
2019-10-26 15:43:00 +00:00
|
|
|
expect(env1.attack).to.equal(0.1);
|
|
|
|
expect(env1.decay).to.equal(0.2);
|
|
|
|
expect(env1.sustain).to.equal(0.3);
|
2019-10-31 13:56:09 +00:00
|
|
|
env1.exponent = 2;
|
|
|
|
expect(env1.exponent).to.equal(2);
|
2019-10-26 15:43:00 +00:00
|
|
|
env1.dispose();
|
|
|
|
});
|
|
|
|
|
2020-11-02 18:47:24 +00:00
|
|
|
it("can set a negative octave", () => {
|
|
|
|
const freqEnv = new FrequencyEnvelope();
|
|
|
|
freqEnv.octaves = -2;
|
|
|
|
freqEnv.dispose();
|
|
|
|
});
|
|
|
|
|
2019-10-26 15:43:00 +00:00
|
|
|
it("goes to the scaled range", async () => {
|
2019-10-28 16:19:33 +00:00
|
|
|
const e = {
|
2019-10-26 15:43:00 +00:00
|
|
|
attack: 0.01,
|
|
|
|
decay: 0.4,
|
2024-05-03 18:31:14 +00:00
|
|
|
sustain: 1,
|
2019-10-26 15:43:00 +00:00
|
|
|
};
|
|
|
|
const buffer = await Offline(() => {
|
2024-05-03 18:31:14 +00:00
|
|
|
const freqEnv = new FrequencyEnvelope(
|
|
|
|
e.attack,
|
|
|
|
e.decay,
|
|
|
|
e.sustain
|
|
|
|
);
|
2019-10-26 15:43:00 +00:00
|
|
|
freqEnv.baseFrequency = 200;
|
|
|
|
freqEnv.octaves = 3;
|
|
|
|
freqEnv.attackCurve = "exponential";
|
|
|
|
freqEnv.toDestination();
|
|
|
|
freqEnv.triggerAttack(0);
|
|
|
|
}, 0.3);
|
|
|
|
buffer.forEach((sample, time) => {
|
|
|
|
if (time < e.attack) {
|
|
|
|
expect(sample).to.be.within(200, 1600);
|
|
|
|
} else if (time < e.attack + e.decay) {
|
|
|
|
expect(sample).to.be.closeTo(1600, 10);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|