-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.mjs
103 lines (88 loc) · 2.78 KB
/
script.mjs
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
/* eslint no-unused-vars: [ "error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" } ] */
/* eslint no-trailing-spaces: [ "error", { "skipBlankLines": true } ] */
import wav from "./wav.mjs";
const amplitude = (() => {
const baseAmplitude = 0.4;
return (portion) => {
if(portion < 0.1){
return portion * 10 * baseAmplitude;
}
else if(portion > 0.9){
return (portion * -10 + 10) * baseAmplitude;
}
return baseAmplitude;
};
})(),
sine = (() => {
const sampleRate = 44100,
length = 500,
totalSamples = Math.floor(sampleRate * length / 1000);
return (numerator, denominator) => {
const frequency = document.getElementById("zeroNote").valueAsNumber * numerator / denominator,
wave = Array.from({
length: totalSamples
}, (_, i) => amplitude(i / totalSamples) * Math.sin(frequency * 2 * Math.PI * i / sampleRate)),
lastSign = Math.sign(wave[wave.length - 1]);
while(wave.length > 0 && Math.sign(wave[wave.length - 1]) === lastSign){
wave.pop();
}
return wave;
};
})(),
ratioSelector = {
"1": 0,
"-1": 1
},
aggregateRatio = (ratio, {dataset: {base}, value: exponent}) => {
const exponentSign = Math.sign(exponent);
if(ratioSelector.hasOwnProperty(exponentSign)){
ratio[ratioSelector[exponentSign]] *= base ** Math.abs(exponent);
}
return ratio;
},
getRatio = () => Array.from(document.querySelectorAll("sup > input[type=number]")).reduce(aggregateRatio, [
1,
1
]),
createExponentMultiplier = (value) => {
const exponent = document.createElement("sup"),
power = document.createElement("span");
exponent.appendChild(Object.assign(document.createElement("input"), {
type: "number",
value: "0",
step: "1",
size: "3"
})).dataset.base = value;
power.append(`× ${value}`, exponent);
return [
" ",
power
];
},
generateIntervals = () => {
const primes = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29
];
document.getElementById("intervals")
.append("Interval ratio: 1", ...primes.flatMap(createExponentMultiplier));
};
document.getElementById("play").addEventListener("click", () => new Audio(wav(sine(...getRatio()))).play());
document.getElementById("intervals").addEventListener("change", () => {
const ratio = getRatio(),
[
numerator,
denominator
] = ratio,
frequencyString = (document.getElementById("zeroNote").valueAsNumber * numerator / denominator).toFixed(3);
document.getElementById("play").value = `▶ Play: ${ratio.join("/")} (${frequencyString} Hz)`;
});
generateIntervals();