Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
208 lines (138 loc) · 4.95 KB
/
Copy pathscript.js
File metadata and controls
208 lines (138 loc) · 4.95 KB
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
const fundamentalInput = document.getElementById("fundamental");
const multiplierInput = document.getElementById("multiplier");
const volumeSlider = document.getElementById("volume");
const outputDisplay = document.getElementById("outputFrequency");
const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const fundMinus = document.getElementById("fundMinus");
const fundPlus = document.getElementById("fundPlus");
const multMinus = document.getElementById("multMinus");
const multPlus = document.getElementById("multPlus");
let audioContext = null;
let oscillator = null;
let gainNode = null;
let holdTimeout;
let holdInterval;
function getVolume() {
return Math.pow(Number(volumeSlider.value) / 100, 2);
}
let volume = getVolume();
// ------------------------------ press and hold
function setupSpinner(button, input, direction) {
const change = () => changeValue(input, direction);
button.addEventListener("click", (e) => {
// Prevent the click after a long press from adding one more step
if (button.dataset.held === "true") {
button.dataset.held = "false";
e.preventDefault();
return;
}
change();
});
const startHold = () => {
button.dataset.held = "false";
holdTimeout = setTimeout(() => {
button.dataset.held = "true";
change();
holdInterval = setInterval(change, 50); // 20 times/sec
}, 400);
};
const stopHold = () => {
clearTimeout(holdTimeout);
clearInterval(holdInterval);
};
button.addEventListener("mousedown", startHold);
button.addEventListener("touchstart", startHold);
button.addEventListener("mouseup", stopHold);
button.addEventListener("mouseleave", stopHold);
button.addEventListener("touchend", stopHold);
button.addEventListener("touchcancel", stopHold);
}
function getFrequency() {
return Number(fundamentalInput.value) * Number(multiplierInput.value);
}
function updateDisplay() {
const freq = getFrequency();
outputDisplay.textContent = freq.toFixed(2) + " Hz" ; // was freq + " Hz"
if (oscillator) {
oscillator.frequency.setValueAtTime(
freq,
audioContext.currentTime
);
}
}
function updateVolume() {
volume = getVolume();
if (gainNode) {
const currentGain = gainNode.gain.value;
gainNode.gain.cancelScheduledValues(audioContext.currentTime);
gainNode.gain.setValueAtTime(currentGain, audioContext.currentTime);
gainNode.gain.linearRampToValueAtTime(
volume,
audioContext.currentTime + 0.05
);
}
}
function formatValue(input, value) {
const decimals = (input.step.split(".")[1] || "").length;
return value.toFixed(decimals);
}
function changeValue(input, amount) {
const step = Number(input.step) || 1;
const min = Number(input.min) || 0;
let value = Number(input.value);
value += amount * step;
value = Math.max(min, value);
input.value = formatValue(input, value);
updateDisplay();
}
function startTone() {
if (oscillator) return;
if (!audioContext) {
audioContext = new AudioContext();
}
oscillator = audioContext.createOscillator();
gainNode = audioContext.createGain();
oscillator.type = "sine";
oscillator.frequency.value = getFrequency();
volume = getVolume();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
// Start silent
gainNode.gain.setValueAtTime(0, audioContext.currentTime);
oscillator.start();
// Fade in
gainNode.gain.linearRampToValueAtTime(
volume,
audioContext.currentTime + 0.02
);
}
function stopTone() {
if (!oscillator) return;
gainNode.gain.cancelScheduledValues(audioContext.currentTime);
gainNode.gain.setValueAtTime(
gainNode.gain.value,
audioContext.currentTime
);
gainNode.gain.linearRampToValueAtTime(
0,
audioContext.currentTime + 0.22
);
oscillator.stop(audioContext.currentTime + 0.22);
oscillator.onended = () => {
oscillator.disconnect();
gainNode.disconnect();
oscillator = null;
gainNode = null;
};
}
fundamentalInput.addEventListener("input", updateDisplay);
multiplierInput.addEventListener("input", updateDisplay);
volumeSlider.addEventListener("input", updateVolume);
startButton.addEventListener("click", startTone);
stopButton.addEventListener("click", stopTone);
setupSpinner(fundMinus, fundamentalInput, -1);
setupSpinner(fundPlus, fundamentalInput, 1);
setupSpinner(multMinus, multiplierInput, -1);
setupSpinner(multPlus, multiplierInput, 1);
updateDisplay();