Skip to content

Commit f11edd6

Browse files
committed
theres a slider now, what does it do??
1 parent 2bc61d0 commit f11edd6

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

index.html

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
const pointers = new Map();
1717
const pi = Math.PI;
1818
let currentBass = 220.0;
19+
let frozenBass = 220.0;
1920
let sampleBuf = undefined;
2021

2122
fetch("./guitar.wav").then(async r => {
@@ -30,41 +31,29 @@
3031
function chordFreq(semitones) {
3132
let k = currentBass * 2 ** (semitones / 12);
3233
console.log(k);
33-
if (k < 300) k *= 2;
34-
if (k > 600) k /= 2;
34+
if (k < 250) k *= 2;
35+
if (k > 650) k /= 2;
3536
return k;
3637
}
3738

3839
function noteNameToFrequency(note) {
39-
const i = " D EF G A BC".indexOf(note.charAt(0)) - 8;
40+
const base = Number(document.querySelector("#base").value);
41+
const i = ("A BC D EF G ".indexOf(note.charAt(0)) + 1200 - base) % 12 + base;
4042
const j = i + !!note.match(//) - !!note.match(//);
4143
return 220 * 2 ** (j / 12);
4244
}
4345

44-
function makeOsc(freq, gainValue) {
45-
if (true) {
46-
const osc = ctx.createBufferSource();
47-
const gain = ctx.createGain();
48-
gain.gain.value = gainValue;
49-
osc.buffer = sampleBuf;
50-
osc.connect(gain);
51-
osc.gainNode = gain;
52-
gain.connect(mix);
53-
// osc.frequency.value = freq;
54-
osc.playbackRate.value = freq / 110;
55-
osc.start(ctx.currentTime + Math.random()*0.04);
56-
return osc;
57-
58-
}
59-
const osc = ctx.createOscillator();
46+
function makeOsc(freq, gainValue, strumFactor) {
47+
const osc = ctx.createBufferSource();
6048
const gain = ctx.createGain();
6149
gain.gain.value = gainValue;
62-
osc.type = "triangle";
50+
osc.buffer = sampleBuf;
6351
osc.connect(gain);
6452
osc.gainNode = gain;
6553
gain.connect(mix);
66-
osc.frequency.value = freq;
67-
osc.start();
54+
// osc.frequency.value = freq;
55+
osc.playbackRate.value = freq / 110;
56+
osc.start(ctx.currentTime + Math.random() * strumFactor);
6857
return osc;
6958
}
7059

@@ -85,8 +74,12 @@
8574
const centerX = rect.left + rect.width / 2;
8675
const centerY = rect.top + rect.height / 2;
8776

77+
let isFrozen = document.getElementById("freeze").checked;
8878
let freq = noteNameToFrequency(e.target.innerText);
79+
if (!isFrozen) frozenBass = freq;
8980
currentBass = freq;
81+
if (isFrozen) freq = frozenBass;
82+
9083
const fourthLower = e.clientY > rect.top + rect.height * 0.65;
9184
e.target.style.background = fourthLower
9285
? "linear-gradient(to bottom, #a99 65%, #f80 65%)"
@@ -97,7 +90,7 @@
9790
centerY: centerY,
9891
note: e.target.innerText,
9992
target: e.target,
100-
oscs: [makeOsc(freq / 2, 0.3)],
93+
oscs: [makeOsc(freq / 2, 0.3, 0)],
10194
});
10295

10396
for (const v of pointers.values()) {
@@ -117,7 +110,7 @@
117110
const p = pointers.get(pointerId);
118111
if (!p) return;
119112
for (const osc of p.oscs) {
120-
osc.gainNode.gain.setTargetAtTime(0, ctx.currentTime, 0.005)
113+
osc.gainNode.gain.setTargetAtTime(0, ctx.currentTime, 0.01)
121114
osc.stop(ctx.currentTime + 0.2);
122115
}
123116
p.target.style.background = "";
@@ -142,23 +135,22 @@
142135
note: e.target.innerText,
143136
target: e.target,
144137
voicing: voicing,
145-
oscs: voicing.map(i => makeOsc(chordFreq(i), 0.06)),
138+
oscs: voicing.map(i => makeOsc(chordFreq(i), 0.06, 0.04)),
146139
});
147140
});
148141
b.addEventListener("pointermove", (e) => {
149142
const p = pointers.get(e.pointerId);
150143
if (!p) return;
151144
const detune = e.clientY - p.centerY;
152145
for (const osc of p.oscs) {
153-
osc.detune.value = detune * 0.5;
146+
osc.detune.value = detune * -0.5;
154147
}
155148
})
156149
b.addEventListener("pointerup", (e) => stop(e.pointerId));
157150
b.addEventListener(
158151
"pointerleave",
159152
(e) => stop(e.pointerId)
160153
);
161-
162154
}
163155
});
164156
</script>
@@ -211,12 +203,20 @@
211203
}
212204

213205
.chord-column {
206+
display: flex;
207+
flex-direction: column;
208+
justify-content: end;
209+
}
210+
211+
.middle {
214212
display: flex;
215213
flex-direction: column;
216214
justify-content: center;
215+
align-items: center;
217216
}
218217

219218
.bass {
219+
user-select: none;
220220
touch-action: none;
221221
background: rgba(0, 0, 0, 0.1);
222222
padding: 20px 40px;
@@ -267,10 +267,26 @@
267267
}
268268

269269
.refresh-link {
270+
user-select: none;
270271
position: absolute;
271272
top: 4px;
272273
right: 4px;
273274
}
275+
276+
input[type=range][orient=vertical]
277+
{
278+
writing-mode: bt-lr; /* IE */
279+
-webkit-appearance: slider-vertical; /* WebKit */
280+
width: 12px;
281+
height: 80%;
282+
padding: 0 5px;
283+
}
284+
285+
input[type=checkbox]
286+
{
287+
width: 20px;
288+
height: 20px;
289+
}
274290
</style>
275291
</head>
276292

@@ -300,13 +316,16 @@
300316
<div class="button bass-button">F&sharp;</div>
301317
</div>
302318
</div>
319+
<div class="middle">
320+
<input id="base" type="range" orient="vertical" min="-12" max="12" value="-8">
321+
<!-- <input id="freeze" type="checkbox"> -->
322+
</div>
303323
<div class="chords">
304324
<div class="chord-column">
305325
<div class="button chord-button" data-chord="7 11 14 16">Δ9</div>
306326
<div class="button chord-button" data-chord="7 10 14 15">m9</div>
307327
<div class="button chord-button" data-chord="7 10 12 17">7s</div>
308328
<div class="button chord-button" data-chord="6 10 12 16">&flat;5</div>
309-
310329
</div>
311330
<div class="chord-column">
312331
<div class="button chord-button" data-chord="7 11 12 16">Δ</div>

0 commit comments

Comments
 (0)