-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ground2.js
145 lines (123 loc) · 3.71 KB
/
Ground2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function Ground2(color) {
this.color = color;
let currentChordRoot = 1;
this.reverb = new Tone.FeedbackDelay(0.4, 0.5);
const reverbChannel = new Tone.Channel();
reverbChannel.receive("ground-2-send");
reverbChannel.chain(this.reverb, OUTPUT_NODE);
this.outputNode = new Tone.Gain(2);
this.outputNode.chain(OUTPUT_NODE);
const musicScale = teoria.note("D").scale("minor");
const notes = musicScale.notes();
const updateChordLoop = new Tone.Loop((time) => {
if (Math.random() > 0.9) {
currentChordRoot = Math.floor(random(0, 8));
}
}, 1);
updateChordLoop.start(0);
const getChord = () => [
musicScale.get(currentChordRoot),
musicScale.get(currentChordRoot + 2),
musicScale.get(currentChordRoot + 4),
musicScale.get(currentChordRoot + 6),
];
this.getSynth = (waveform) => {
return new Tone.MonoSynth({
volume: -Infinity,
oscillator: { type: waveform },
envelope: { attack: 0.01 },
filter: {
type: "lowpass",
frequency: 20000,
rolloff: -12,
Q: 1,
gain: 0,
},
filterEnvelope: {
attack: 0.1,
baseFrequency: 20000,
decay: 0.2,
exponent: 2,
octaves: 3,
release: 2,
sustain: 0.5,
},
});
};
this.renderGround = ({ x, y }, ctx_) => {
const ctx = ctx_ || window;
ctx.beginShape();
const [h, s, l] = this.color;
ctx.fill(h, s, l + noise(x, y) * 6);
ctx.vertex(x, y + 100);
ctx.vertex(x + 90, y + 100);
ctx.vertex(x + 50, y - 20);
ctx.endShape(CLOSE);
};
this.getDetune = (noiseVal) => {
const val = noiseVal * 0.6;
return val - val / 2;
};
this.getPart = ({ sentence, synth, synthOutput, getState }) => {
const { growSpeed } = getState();
let sendAmount = -60;
let filterFreq = 50;
const sendChannel = new Tone.Channel();
sendChannel.send("ground-2-send", sendAmount);
sendChannel.connect(reverbChannel);
synthOutput.connect(sendChannel);
synth.set({
portamento: random(0, 1),
envelope: { attack: 0.4 },
filter: { type: "lowpass", frequency: filterFreq, gain: 1, q: 15 },
filterEnvelope: { baseFrequency: filterFreq },
});
const chordIndex = Math.floor(random(0, getChord().length));
const octave = Math.floor(random(1, 4));
let currLetterIndex = 0;
let currNoteIndex = Math.floor(
random(0, musicScale.notes().length * 3) / 2
);
const part = new Tone.Loop((time) => {
const note = getChord()[chordIndex];
const { isGrowing, isDoneGrowing, maxLetters } = getState();
synth.triggerAttack((note.fq() * 2) / octave, time + 0.1, 0.1);
if (isDoneGrowing) {
sendAmount += 5;
sendAmount = constrain(sendAmount, -60, 0);
sendChannel.send("ground-2-send", sendAmount);
}
if (!isGrowing) {
const currLetter = sentence[currLetterIndex];
if (currLetterIndex === 0) {
// currNoteIndex = 0;
}
if (currLetter === "-") {
currNoteIndex--;
}
if (currLetter === "+") {
currNoteIndex++;
}
currLetterIndex++;
currLetterIndex = currLetterIndex % maxLetters;
} else {
// while being watered
filterFreq *= 2;
filterFreq = constrain(filterFreq, 20, 20000);
synth.set({
filter: { type: "lowpass", frequency: filterFreq },
filterEnvelope: { baseFrequency: filterFreq },
});
if (!isDoneGrowing) {
// synth.triggerAttackRelease(
// getRandomNoteInChord().toString(),
// time + 0.1,
// 0.1
// );
}
}
}, 1 / growSpeed);
part.start();
return part;
};
}