Spaces:
Sleeping
Sleeping
Ben Blaker commited on
feat(audio): pen-scratch + keycap-press sound as each word is written (#26)
Browse filesPlay a real pen-nib recording (trimmed ~0.37s clip, inlined as a data URI) on each word's handwriting reveal, alongside the keycap-press thock. Rides the sfx bus so the in-game mixer levels/mutes it. Verified in-browser (sample plays once per word, no console errors); 18 unit tests pass.
- app.py +7 -0
- static/game.js +29 -1
- static/pencil-scratch.mp3 +0 -0
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import pathlib
|
| 2 |
|
| 3 |
import gradio as gr
|
|
@@ -7,12 +8,18 @@ from levels import HOME_ID, LEVEL_ORDER, LEVELS
|
|
| 7 |
|
| 8 |
STATIC = pathlib.Path(__file__).parent / "static"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Everything the board needs to render and switch levels client-side: the
|
| 11 |
# play order, the boot board, and each level's client-facing slice.
|
| 12 |
GAME = {
|
| 13 |
"levels": [LEVELS[lid].client_value() for lid in LEVEL_ORDER],
|
| 14 |
"order": LEVEL_ORDER,
|
| 15 |
"home": HOME_ID,
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
HEAD = """
|
|
|
|
| 1 |
+
import base64
|
| 2 |
import pathlib
|
| 3 |
|
| 4 |
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
STATIC = pathlib.Path(__file__).parent / "static"
|
| 10 |
|
| 11 |
+
# The pen-scratch SFX, inlined as a data URI so it needs no static-file route
|
| 12 |
+
# (which is brittle inside HF Spaces' iframe). The client decodes it once into
|
| 13 |
+
# a Web Audio buffer; see sfx.scratch in game.js.
|
| 14 |
+
_SCRATCH = base64.b64encode((STATIC / "pencil-scratch.mp3").read_bytes()).decode()
|
| 15 |
+
|
| 16 |
# Everything the board needs to render and switch levels client-side: the
|
| 17 |
# play order, the boot board, and each level's client-facing slice.
|
| 18 |
GAME = {
|
| 19 |
"levels": [LEVELS[lid].client_value() for lid in LEVEL_ORDER],
|
| 20 |
"order": LEVEL_ORDER,
|
| 21 |
"home": HOME_ID,
|
| 22 |
+
"scratchAudio": "data:audio/mpeg;base64," + _SCRATCH,
|
| 23 |
}
|
| 24 |
|
| 25 |
HEAD = """
|
static/game.js
CHANGED
|
@@ -815,6 +815,7 @@
|
|
| 815 |
musicGain.connect(masterGain);
|
| 816 |
}
|
| 817 |
if (ac.state === "suspended") ac.resume();
|
|
|
|
| 818 |
return ac;
|
| 819 |
}
|
| 820 |
// Sounds often fire from gsap timelines (outside any user gesture), so unlock
|
|
@@ -862,6 +863,20 @@
|
|
| 862 |
src.start(t0);
|
| 863 |
}
|
| 864 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
const sfx = {
|
| 866 |
jump() {
|
| 867 |
tone({ type: "triangle", from: 280, to: 640, dur: 0.14, vol: 0.3 }); // springy boing
|
|
@@ -877,6 +892,19 @@
|
|
| 877 |
pop() {
|
| 878 |
tone({ type: "triangle", from: 900, to: 1400, dur: 0.07, vol: 0.14 }); // chip appears
|
| 879 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
click() {
|
| 881 |
thud({ dur: 0.03, vol: 0.3, freq: 2500 }); // UI button tap
|
| 882 |
},
|
|
@@ -1191,8 +1219,8 @@
|
|
| 1191 |
text.textContent = word;
|
| 1192 |
chip.appendChild(text);
|
| 1193 |
chipsEl.appendChild(chip);
|
| 1194 |
-
sfx.pop();
|
| 1195 |
const dur = 0.16 + word.length * 0.06; // writing pace scales with word length
|
|
|
|
| 1196 |
// The clip is just the left→right write-on; clear it once written so no
|
| 1197 |
// residual inset lingers. A leftover right inset is a % of the chip's box,
|
| 1198 |
// which for a 1-char tile (! ?) is too small to clear Caveat's forward
|
|
|
|
| 815 |
musicGain.connect(masterGain);
|
| 816 |
}
|
| 817 |
if (ac.state === "suspended") ac.resume();
|
| 818 |
+
loadScratch(); // decode the pen-scratch sample on first wake-up
|
| 819 |
return ac;
|
| 820 |
}
|
| 821 |
// Sounds often fire from gsap timelines (outside any user gesture), so unlock
|
|
|
|
| 863 |
src.start(t0);
|
| 864 |
}
|
| 865 |
|
| 866 |
+
// The pen-scratch cue is a real nib recording (trimmed to one stroke),
|
| 867 |
+
// decoded once into a buffer the first time the audio context wakes up.
|
| 868 |
+
let scratchBuf = null;
|
| 869 |
+
let scratchLoading = false;
|
| 870 |
+
function loadScratch() {
|
| 871 |
+
if (scratchBuf || scratchLoading || !ac || !props.value.scratchAudio) return;
|
| 872 |
+
scratchLoading = true;
|
| 873 |
+
fetch(props.value.scratchAudio)
|
| 874 |
+
.then((r) => r.arrayBuffer())
|
| 875 |
+
.then((b) => ac.decodeAudioData(b))
|
| 876 |
+
.then((buf) => (scratchBuf = buf))
|
| 877 |
+
.catch(() => (scratchLoading = false)); // let a later wake-up retry
|
| 878 |
+
}
|
| 879 |
+
|
| 880 |
const sfx = {
|
| 881 |
jump() {
|
| 882 |
tone({ type: "triangle", from: 280, to: 640, dur: 0.14, vol: 0.3 }); // springy boing
|
|
|
|
| 892 |
pop() {
|
| 893 |
tone({ type: "triangle", from: 900, to: 1400, dur: 0.07, vol: 0.14 }); // chip appears
|
| 894 |
},
|
| 895 |
+
scratch(dur = 0.4) {
|
| 896 |
+
if (!audio() || !scratchBuf) return; // sample still decoding → skip silently
|
| 897 |
+
const src = ac.createBufferSource();
|
| 898 |
+
src.buffer = scratchBuf;
|
| 899 |
+
// stretch the stroke toward the word's writing time (longer words →
|
| 900 |
+
// slower nib) without wild pitch shifts, plus jitter so no two are alike
|
| 901 |
+
const fit = Math.min(1.12, Math.max(0.78, scratchBuf.duration / dur));
|
| 902 |
+
src.playbackRate.value = fit * rand(0.97, 1.03);
|
| 903 |
+
const g = ac.createGain();
|
| 904 |
+
g.gain.value = 0.3; // sit under the keycap press — broadband noise reads loud
|
| 905 |
+
src.connect(g).connect(sfxGain); // ride the sfx bus so the mixer levels/mutes it
|
| 906 |
+
src.start();
|
| 907 |
+
},
|
| 908 |
click() {
|
| 909 |
thud({ dur: 0.03, vol: 0.3, freq: 2500 }); // UI button tap
|
| 910 |
},
|
|
|
|
| 1219 |
text.textContent = word;
|
| 1220 |
chip.appendChild(text);
|
| 1221 |
chipsEl.appendChild(chip);
|
|
|
|
| 1222 |
const dur = 0.16 + word.length * 0.06; // writing pace scales with word length
|
| 1223 |
+
sfx.scratch(dur); // pen scratches for as long as the word takes to write
|
| 1224 |
// The clip is just the left→right write-on; clear it once written so no
|
| 1225 |
// residual inset lingers. A leftover right inset is a % of the chip's box,
|
| 1226 |
// which for a 1-char tile (! ?) is too small to clear Caveat's forward
|
static/pencil-scratch.mp3
ADDED
|
Binary file (10.7 kB). View file
|
|
|