-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformulas.html
226 lines (214 loc) · 9.97 KB
/
formulas.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Formulas</title>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
input {
width: 100%;
box-sizing: border-box;
}
table {
width: 100%;
}
html {
font-family: sans-serif;
}
</style>
</head>
<body>
<table id="theTable">
<tr>
<th style="width: 250px;">Name</th>
<th style="width: 150px;">Value</th>
<th>Slider</th>
</tr>
</table>
<br>
<br>
<a href="index.html">(My Other Stuff)</a>
<script>
const values = [
{ friendlyName: "Midi Note", min: 0, max: 128, default: 69 },
{ friendlyName: "Frequency", min: 0, max: 20000, default: 440 },
{ friendlyName: "Wavelenth In Samples", min: 0, max: 6000, default: 44100 / 440 },
{ friendlyName: "Wavelenth In ms", min: 0, max: 100, default: (44100 / 440 / 44100) * 1000 },
{ friendlyName: "Sample Rate", min: 0, max: 192000, default: 44100 },
{ friendlyName: "Audio Buffer Size", min: 0, max: 2048, default: 128 },
{ friendlyName: "Audio Buffer Size 2 Exponent", min: 0, max: 11, default: 7 },
{ friendlyName: "Audio Buffer Latency ms", min: 0, max: 50, default: 128 / 44100 * 1000 },
{ friendlyName: "Audio Buffer Latency Feet", min: 0, max: 50, default: (128 / 44100) * 1116.437 },
{ friendlyName: "Decibels", min: -100, max: 0, default: -20 },
{ friendlyName: "Gain", min: 0, max: 1, default: Math.pow(10, -20 / 20) },
{ friendlyName: "Q", min: 0.2, max: 144, default: 0.707106781186548 },
{ friendlyName: "Bandwidth", min: 0.01, max: 4, default: 1.8999686269529905 },
{ friendlyName: "Degrees Fahrenheit", min: -40, max: 500, default: 32 },
{ friendlyName: "Degrees Celsius", min: -40, max: 260, default: 0 },
{ friendlyName: "Percent Humidity", min: 0, max: 100, default: 100 },
{ friendlyName: "Dew Point Fahrenheit", min: 14, max: 77, default: 32 },
{ friendlyName: "Dew Point Celsius", min: -10, max: 25, default: 0 },
]
for (item of values)
item.name = item.friendlyName.replaceAll(" ", "");
const table = document.querySelector("#theTable");
for (item of values) {
var mn = item.name === `MidiNote`;
table.innerHTML += `
<tr>
<td ${mn?"rowspan=2":""} >${item.friendlyName}</td>
<td><input type="number" id="${item.name}Box" step="any" value="${item.default}" min="${item.min}" max="${item.max}" data-value-name="${item.name}"></td>
<td ${mn?"rowspan=2":""}>
<input type="range" id="${item.name}Slider" step="any" value="${item.default}" min="${item.min}" max="${item.max}" data-value-name="${item.name}">
</td>
</tr>${mn?`
<tr>
<td style="height: 25px;vertical-align:bottom;"><span id="midiout"></span></td>
</tr>`:""}`;
}
for (item of values) {
let box = document.querySelector(`#${item.name}Box`);
let slider = document.querySelector(`#${item.name}Slider`);
slider.addEventListener("input", (event) => {
box.value = event.target.value;
UpdateThings(event.target);
});
box.addEventListener("input", (event) => {
slider.value = event.target.value;
UpdateThings(event.target);
});
}
UpdateThings(document.querySelector(`#MidiNoteSlider`));
function UpdateThings(target) {
var state = {};
for (item of values) {
state[item.name] = document.querySelector(`#${item.name}Slider`).value;
}
if (target.dataset.valueName == `SampleRate`) {
state.WavelenthInSamples = state.SampleRate / state.Frequency;
state.AudioBufferLatencyms = state.AudioBufferSize / state.SampleRate * 1000;
state.AudioBufferLatencyFeet = state.AudioBufferLatencyms / 1000 * 1116.437;
}
if (target.dataset.valueName == `Frequency`) {
state.MidiNote = Math.log2(state.Frequency / 440) * 12 + 69;
state.WavelenthInSamples = state.SampleRate / state.Frequency;
state.WavelenthInms = state.WavelenthInSamples / state.SampleRate * 1000;
}
if (target.dataset.valueName == `WavelenthInSamples`) {
state.Frequency = state.SampleRate / state.WavelenthInSamples;
state.MidiNote = Math.log2(state.Frequency / 440) * 12 + 69;
state.WavelenthInSamples = state.SampleRate / state.Frequency;
state.WavelenthInms = state.WavelenthInSamples / state.SampleRate * 1000;
}
if (target.dataset.valueName == `MidiNote`) {
state.Frequency = Math.pow(2, (state.MidiNote - 69) / 12) * 440;
state.WavelenthInSamples = state.SampleRate / state.Frequency;
state.WavelenthInms = state.WavelenthInSamples / state.SampleRate * 1000;
}
if (target.dataset.valueName == `AudioBufferSize2Exponent`) {
state.AudioBufferSize = Math.pow(2, state.AudioBufferSize2Exponent);
state.AudioBufferLatencyms = state.AudioBufferSize / state.SampleRate * 1000;
state.AudioBufferSize2Exponent = Math.log2(state.AudioBufferSize);
state.AudioBufferLatencyFeet = state.AudioBufferLatencyms / 1000 * 1116.437;
}
if (target.dataset.valueName == `AudioBufferSize`) {
state.AudioBufferLatencyms = state.AudioBufferSize / state.SampleRate * 1000;
state.AudioBufferSize2Exponent = Math.log2(state.AudioBufferSize);
state.AudioBufferLatencyFeet = state.AudioBufferLatencyms / 1000 * 1116.437;
}
if (target.dataset.valueName == `AudioBufferLatencyms`) {
state.AudioBufferSize = state.AudioBufferLatencyms / 1000 * state.SampleRate;
state.AudioBufferSize2Exponent = Math.log2(state.AudioBufferSize);
state.AudioBufferLatencyFeet = state.AudioBufferLatencyms / 1000 * 1116.437;
}
if (target.dataset.valueName == `AudioBufferLatencyFeet`) {
state.AudioBufferLatencyms = state.AudioBufferLatencyFeet/1116.437*1000;
state.AudioBufferSize = state.AudioBufferLatencyms / 1000 * state.SampleRate;
state.AudioBufferSize2Exponent = Math.log2(state.AudioBufferSize);
}
if (target.dataset.valueName == `Decibels`) {
state.Gain = Math.pow(10, state.Decibels / 20);
}
if (target.dataset.valueName == `Gain`) {
state.Decibels = Math.log10(state.Gain) * 20;
}
if (target.dataset.valueName == `WavelenthInms`) {
state.WavelenthInSamples = state.WavelenthInms * state.SampleRate / 1000;
state.MidiNote = Math.log2(state.Frequency / 440) * 12 + 69;
state.Frequency = state.SampleRate / state.WavelenthInSamples;
}
if (target.dataset.valueName == `Q`) {
state.Bandwidth = Math.log((2*state.Q*state.Q+1)/(2*state.Q*state.Q) + Math.sqrt(Math.pow(2*(2*state.Q*state.Q+1)/(2*state.Q*state.Q),2)/4-1))/Math.log(2);
}
if (target.dataset.valueName == `Bandwidth`) {
state.Q = Math.pow(2, state.Bandwidth/2)/(Math.pow(2, state.Bandwidth)-1);
}
if (target.dataset.valueName == `DegreesFahrenheit`) {
state.DegreesCelsius = (state.DegreesFahrenheit - 32) * 5/9;
state.DewPointCelsius = Math.pow(state.PercentHumidity / 100, 1 / 8) * (112 + 0.9 * state.DegreesCelsius) + 0.1 * state.DegreesCelsius - 112;
state.DewPointFahrenheit = state.DewPointCelsius * 9/5 + 32;
}
if (target.dataset.valueName == `DegreesCelsius`) {
state.DegreesFahrenheit = state.DegreesCelsius * 9/5 + 32;
state.DewPointCelsius = Math.pow(state.PercentHumidity / 100, 1 / 8) * (112 + 0.9 * state.DegreesCelsius) + 0.1 * state.DegreesCelsius - 112;
state.DewPointFahrenheit = state.DewPointCelsius * 9/5 + 32;
}
if (target.dataset.valueName == `PercentHumidity`) {
state.DewPointCelsius = Math.pow(state.PercentHumidity / 100, 1 / 8) * (112 + 0.9 * state.DegreesCelsius)
+ 0.1 * state.DegreesCelsius
- 112;
state.DewPointFahrenheit = state.DewPointCelsius * 9/5 + 32;
}
if (target.dataset.valueName == `DewPointCelsius`) {
state.PercentHumidity = Math.pow((state.DewPointCelsius-(0.1*state.DegreesCelsius-112))/(112+0.9*state.DegreesCelsius),8)*100;
state.DewPointFahrenheit = state.DewPointCelsius * 9/5 + 32;
}
if (target.dataset.valueName == `DewPointFahrenheit`) {
state.DewPointCelsius = (state.DewPointFahrenheit - 32) * 5/9;
state.PercentHumidity = Math.pow((state.DewPointCelsius-(0.1*state.DegreesCelsius-112))/(112+0.9*state.DegreesCelsius),8)*100;
}
var intNote = Math.floor(state.MidiNote);
document.querySelector(`#midiout`).innerText = `Note ${intNote} is ${NoteName(intNote)}`;
for (item of values) {
document.querySelector(`#${item.name}Slider`).value = state[item.name];
document.querySelector(`#${item.name}Box`).value = state[item.name];
}
}
function NoteName(argument) {
switch (argument % 12) {
case 0:
return `C`;
case 1:
return `C♯/D♭`;
case 2:
return `D`;
case 3:
return `D♯/E♭`;
case 4:
return `E`;
case 5:
return `F`;
case 6:
return `F♯/G♭`;
case 7:
return `G`;
case 8:
return `G♯/A♭`;
case 9:
return `A`;
case 10:
return `A♯/B♭`;
case 11:
return `B`;
}
return ``;
}
</script>
</body>
</html>