-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathautomode.ts
47 lines (42 loc) · 1.08 KB
/
automode.ts
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
// SPDX-License-Identifier: Apache-2.0
import { weightedRandom } from "@thi.ng/random";
import { map, range } from "@thi.ng/transducers";
import { updateAudio } from "./audio";
import { NUM_BINS } from "./config";
import { DB, updateSpectrumBin } from "./state";
const weights = [
0,
...map(
(x) => 1 - Math.pow((x - 1) / NUM_BINS, 0.15) * 0.99,
range(1, NUM_BINS)
),
];
const rnd = weightedRandom([...range(0, NUM_BINS)], weights);
const startAutoMode = () => {
let i = 0;
DB.resetIn(
["auto"],
setInterval(() => {
let { decay, attenuate, interval } = DB.value;
attenuate = 1 + attenuate;
DB.swapIn(["bins"], (buf: number[]) =>
buf.map((x, i) => (x * decay) / attenuate ** i)
);
if (i % interval === 0) {
const bin = rnd();
updateSpectrumBin(
bin,
Math.random() * (1 - Math.pow((bin - 1) / NUM_BINS, 0.8))
);
}
updateAudio();
i++;
}, 16)
);
};
const stopAutoMode = () => {
clearInterval(DB.value.auto);
DB.resetIn(["auto"], null);
};
export const toggleAutoMode = () =>
DB.value.auto == null ? startAutoMode() : stopAutoMode();