UNPKG

2.4 kBPlain TextView Raw
1import { BasicTests } from "../../test/helper/Basic.js";
2import { MonoSynth } from "./MonoSynth.js";
3import { InstrumentTest } from "../../test/helper/InstrumentTests.js";
4import { CompareToFile } from "../../test/helper/CompareToFile.js";
5import { expect } from "chai";
6import { Offline } from "../../test/helper/Offline.js";
7
8describe("MonoSynth", () => {
9 BasicTests(MonoSynth);
10 InstrumentTest(MonoSynth, "C4");
11
12 it("matches a file", () => {
13 return CompareToFile(
14 () => {
15 const synth = new MonoSynth().toDestination();
16 synth.triggerAttackRelease("C4", 0.1, 0.05);
17 },
18 "monoSynth.wav",
19 0.01
20 );
21 });
22
23 context("API", () => {
24 it("can get and set oscillator attributes", () => {
25 const monoSynth = new MonoSynth();
26 monoSynth.oscillator.type = "triangle";
27 expect(monoSynth.oscillator.type).to.equal("triangle");
28 monoSynth.dispose();
29 });
30
31 it("can get and set envelope attributes", () => {
32 const monoSynth = new MonoSynth();
33 monoSynth.envelope.attack = 0.24;
34 expect(monoSynth.envelope.attack).to.equal(0.24);
35 monoSynth.dispose();
36 });
37
38 it("can get and set filter attributes", () => {
39 const monoSynth = new MonoSynth();
40 monoSynth.filter.Q.value = 0.4;
41 expect(monoSynth.filter.Q.value).to.be.closeTo(0.4, 0.001);
42 monoSynth.dispose();
43 });
44
45 it("can get and set filterEnvelope attributes", () => {
46 const monoSynth = new MonoSynth();
47 monoSynth.filterEnvelope.baseFrequency = 400;
48 expect(monoSynth.filterEnvelope.baseFrequency).to.equal(400);
49 monoSynth.dispose();
50 });
51
52 it("can be constructed with an options object", () => {
53 const monoSynth = new MonoSynth({
54 envelope: {
55 sustain: 0.3,
56 },
57 });
58 expect(monoSynth.envelope.sustain).to.equal(0.3);
59 monoSynth.dispose();
60 });
61
62 it("can get/set attributes", () => {
63 const monoSynth = new MonoSynth();
64 monoSynth.set({
65 envelope: {
66 decay: 0.24,
67 },
68 });
69 expect(monoSynth.get().envelope.decay).to.equal(0.24);
70 monoSynth.dispose();
71 });
72
73 it("is silent after triggerAttack if sustain is 0", async () => {
74 return await Offline(() => {
75 const synth = new MonoSynth({
76 envelope: {
77 attack: 0.1,
78 decay: 0.1,
79 sustain: 0,
80 },
81 }).toDestination();
82 synth.triggerAttack("C4", 0);
83 }, 0.5).then((buffer) => {
84 expect(buffer.getTimeOfLastSound()).to.be.closeTo(0.2, 0.01);
85 });
86 });
87 });
88});