Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

feat(player): add accessible volume control

yzrceefe0922f51
diff --git a/.claude/skills/papermorph/assets/engine/engine.css b/.claude/skills/papermorph/assets/engine/engine.css
index 32acb76..6cf70a8 100644
--- a/.claude/skills/papermorph/assets/engine/engine.css
+++ b/.claude/skills/papermorph/assets/engine/engine.css
@@ -106,6 +106,20 @@ button[hidden] { display: none; }
.seg.ask { background: #2d4a57; }
.seg.ask i { background: var(--quiz); }
.step { width: 300px; flex: none; color: var(--dim); font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right; }
+.volume { display: flex; align-items: center; gap: 8px; flex: none; font-size: 19px; color: var(--dim); }
+.volume input { width: 120px; height: 44px; margin: 0; accent-color: var(--chalk); cursor: pointer; }
+.volume input:focus-visible { outline: 3px solid var(--task); outline-offset: 2px; }
+/* Keep the control strip usable without scaling down its touch targets. */
+@media (max-width: 700px) {
+ #frame { top: calc(50% - 50px); overflow: visible; }
+ #bar { width: calc(1600px * var(--k)); height: auto; flex-wrap: wrap; gap: 6px; padding: 8px;
+ transform: scale(calc(1 / var(--k))); transform-origin: left top; border-radius: 0 0 12px 12px; }
+ #bar .icon { width: 36px; height: 40px; }
+ .step { display: none; }
+ .segs, .volume { order: 1; }
+ .segs { min-width: 80px; margin: 0; }
+ .volume { font-size: 16px; }
+}
.sound-note { position: absolute; left: 24px; top: 20px; margin: 0; padding: 8px 18px; border-radius: 12px; background: rgba(15, 22, 19, .85); color: var(--dim); font-size: 19px; }
.sound-note[hidden] { display: none; }
.blanks .brow { padding: 8px 0; border-bottom: 1px solid #2e433b; }
diff --git a/.claude/skills/papermorph/assets/engine/engine.js b/.claude/skills/papermorph/assets/engine/engine.js
index 568d6bd..ec78183 100644
--- a/.claude/skills/papermorph/assets/engine/engine.js
+++ b/.claude/skills/papermorph/assets/engine/engine.js
@@ -72,6 +72,9 @@ document.body.insertAdjacentHTML('afterbegin', `<div id="frame">
</button>
<div class="segs" id="segs"></div>
<div class="step" id="stepName"></div>
+ <label class="volume" for="volume">Volume
+ <input id="volume" type="range" min="0" max="1" step="0.05" value="1" title="Volume (0 = muted)">
+ </label>
<button class="icon txt" id="bHelp" aria-label="Keyboard shortcuts (?)" title="Keyboard shortcuts (?)">?</button>
<button class="icon cc" id="bCC" aria-pressed="false" aria-label="Captions (C)" title="Captions (C)">CC</button>
<button class="icon" id="bFull" aria-label="Full screen">
@@ -1479,6 +1482,7 @@ function start(i, play) {
evalTo(0);
const a = P.audio = new Audio(`audio/${CHAPTER.language || 'en'}/${BEATS[i].id}.mp3`);
a.preload = 'auto';
+ a.volume = +$('volume').value;
a.onerror = () => { if (P.audio === a) soundFailed(); };
const b = BEATS[i];
if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });
@@ -1564,6 +1568,7 @@ $('stage').addEventListener('click', e => {
if (!b.ask && !e.target.closest('.qlayer')) togglePlay();
});
$('bPlay').onclick = togglePlay;
+$('volume').oninput = () => { if (P.audio) P.audio.volume = +$('volume').value; };
$('bBack').onclick = () => { hideCover(); back1(); };
$('bRestart').onclick = restart;
$('bCC').onclick = () => setCaptions(!captions);
@@ -1581,8 +1586,8 @@ document.addEventListener('keydown', e => {
if (e.altKey || e.metaKey || e.ctrlKey) return;
const k = e.key;
if (!$('help').hidden) { if (k === 'Escape' || k === '?') { e.preventDefault(); toggleHelp(); } return; }
- if (e.target.matches?.('input')) { // typing an answer: only Enter (check) and Escape (leave the box) are ours
- if (k === 'Enter' && P.keys) { e.preventDefault(); P.keys(e); }
+ if (e.target.matches?.('input')) { // Keep native input keys; only answer boxes use Enter to check.
+ if (k === 'Enter' && e.target.type !== 'range' && P.keys) { e.preventDefault(); P.keys(e); }
else if (k === 'Escape') e.target.blur();
return;
}
@@ -1604,7 +1609,7 @@ document.addEventListener('keydown', e => {
// Mouse clicks should not leave focus on a control, so Enter and Space keep meaning "check" and "play".
$('frame').addEventListener('mousedown', e => { if (e.target.closest('button, [tabindex]')) e.preventDefault(); });
$('bHelp').onclick = toggleHelp;
-const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, innerHeight / 1000));
+const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, (innerHeight - (innerWidth <= 700 ? 120 : 0)) / 1000));
addEventListener('resize', fit);
fit();
diff --git a/.claude/skills/papermorph/scripts/check_volume.py b/.claude/skills/papermorph/scripts/check_volume.py
new file mode 100644
index 0000000..4384d53
--- /dev/null
+++ b/.claude/skills/papermorph/scripts/check_volume.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python3
+"""Real-audio volume regression for both books and the Skill chapter template.
+
+Serve site on 8767, then: uv run --with playwright .claude/skills/papermorph/scripts/check_volume.py
+BASE_URL overrides http://127.0.0.1:8767. No files are generated.
+"""
+import asyncio
+import json
+import os
+from pathlib import Path
+from urllib.parse import urlparse
+
+from playwright.async_api import async_playwright
+
+SKILL = Path(__file__).resolve().parents[1]
+SITE = SKILL.parents[2] / "site"
+BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8767").rstrip("/")
+
+
+async def template(route):
+ name = urlparse(route.request.url).path.split("/__volume_template__/")[1]
+ if name == "ch01/":
+ await route.fulfill(path=SKILL / "assets/templates/chapter.html")
+ elif name.startswith("lib/"):
+ await route.fulfill(path=SKILL / "assets/engine" / Path(name).name)
+ elif name.endswith("timings.js"):
+ timing = {"dur": 30, "marks": {"sub": 1, "note": 2, "w1": 3, "w2": 4}, "cues": []}
+ await route.fulfill(content_type="text/javascript", body="const TIMINGS = " + json.dumps(
+ {key: timing for key in ("intro", "idea", "q1", "wrap", "final1", "finish")}))
+ else:
+ # Template has no narrated assets; exercise it with an existing, real MP3.
+ await route.fulfill(path=SITE / "math-notebook/ch01/audio/en/intro.mp3")
+
+
+async def check(browser, path):
+ page = await browser.new_page(viewport={"width": 1440, "height": 900})
+ errors = []
+ page.on("pageerror", lambda error: errors.append(str(error)))
+ page.on("console", lambda message: message.type == "error" and errors.append(message.text))
+ page.on("response", lambda response: response.status >= 400 and errors.append(response.url))
+ page.on("requestfailed", lambda request: request.failure != "net::ERR_ABORTED" and errors.append(request.url))
+ if path.startswith("__volume_template__"):
+ await page.route("**/__volume_template__/**", template)
+ await page.goto(f"{BASE}/{path}")
+ await page.wait_for_function("typeof P !== 'undefined' && P.audio && P.audio.readyState >= 2")
+ slider = page.get_by_role("slider", name="Volume", exact=True)
+ assert await slider.count() == 1
+ assert await page.evaluate("P.audio.volume") == 1
+ await slider.fill("0.4")
+ assert await page.evaluate("P.audio.volume") == .4
+ await page.locator("#cover").click()
+ await page.wait_for_function("P.audio.currentTime > .2 && P.t > .1")
+
+ for value in ("0", "0.5", "1"):
+ await slider.fill(value)
+ assert await page.evaluate("P.audio.volume") == float(value)
+ before = await page.evaluate("P.audio.currentTime")
+ await page.wait_for_function("P.audio.currentTime > " + str(before + .1))
+ await page.locator("#bPlay").click()
+ paused = await page.evaluate("P.audio.currentTime")
+ await page.wait_for_timeout(200)
+ assert await page.evaluate("P.audio.paused && !P.playing")
+ assert abs(await page.evaluate("P.audio.currentTime") - paused) < .05
+ await slider.fill(f"{1 - float(value):g}")
+ assert await page.evaluate("P.audio.volume") == 1 - float(value)
+ assert await page.evaluate("P.audio.paused && !P.playing")
+ await slider.fill(value)
+ await page.locator("#bPlay").click()
+ await page.wait_for_function("P.audio.currentTime > " + str(paused + .1))
+ await page.locator("#segs button").nth(1).click()
+ await page.wait_for_function("P.i === 1 && P.audio.currentTime > .1")
+ assert await page.evaluate("P.audio.volume") == float(value)
+ # Also cover automatic completion, not just manual seeking.
+ await page.evaluate("P.audio.currentTime = P.audio.duration; P.t = P.end")
+ await page.wait_for_function("P.i === 2")
+ assert await page.evaluate("P.audio.volume") == float(value)
+ await page.locator("#bRestart").click()
+ await page.wait_for_function("P.i === 0 && P.audio.currentTime > .1")
+ assert await page.evaluate("P.audio.volume") == float(value)
+
+ await page.evaluate("window.volumeAudio = P.audio")
+ await slider.focus()
+ for key, value in (("Home", 0), ("ArrowRight", .05), ("ArrowUp", .1),
+ ("ArrowLeft", .05), ("End", 1), ("ArrowDown", .95)):
+ await slider.press(key)
+ assert abs(await page.evaluate("P.audio.volume") - value) < .001
+ assert await page.evaluate("P.i === 0 && P.playing && P.audio === window.volumeAudio"), "range keys must not navigate, restart or pause"
+ await page.evaluate("seek(BEATS.findIndex(b => b.ask), false)")
+ await slider.focus()
+ await slider.press("Home")
+ await slider.press("Enter")
+ assert await page.evaluate("P.i > 0 && P.audio.volume === 0 && Object.keys(SCORE).length === 0"), "range Enter must not submit a question"
+ await page.locator("#bRestart").click()
+
+ for width, height in ((390, 844), (320, 568), (700, 390), (1440, 900)):
+ await page.set_viewport_size({"width": width, "height": height})
+ await page.wait_for_timeout(100)
+ assert await page.evaluate("""[...document.querySelectorAll('#bar > *, #volume')].every(e => {
+ if (getComputedStyle(e).display === 'none') return true;
+ const r = e.getBoundingClientRect();
+ return r.x >= 0 && r.y >= 0 && r.right <= innerWidth + 1 && r.bottom <= innerHeight + 1;
+ })"""), f"controls clipped at {width}x{height}"
+ if width <= 700:
+ bounds = await slider.bounding_box()
+ assert bounds["width"] >= 100 and bounds["height"] >= 40
+ await slider.click(position={"x": bounds["width"] / 2, "y": bounds["height"] / 2})
+ assert .3 < await page.evaluate("P.audio.volume") < .7
+ assert not errors, errors
+ print(f"PASS {path}: real audio, mute/mid/full, pause/resume, steps/restart, keyboard, narrow layout")
+ await page.close()
+
+
+async def main():
+ async with async_playwright() as playwright:
+ browser = await playwright.chromium.launch(args=["--autoplay-policy=no-user-gesture-required"])
+ for path in ("elementary-algebra/ch01/", "math-notebook/ch01/", "__volume_template__/ch01/"):
+ await check(browser, path)
+ await browser.close()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/site/elementary-algebra/lib/engine.css b/site/elementary-algebra/lib/engine.css
index 32acb76..6cf70a8 100644
--- a/site/elementary-algebra/lib/engine.css
+++ b/site/elementary-algebra/lib/engine.css
@@ -106,6 +106,20 @@ button[hidden] { display: none; }
.seg.ask { background: #2d4a57; }
.seg.ask i { background: var(--quiz); }
.step { width: 300px; flex: none; color: var(--dim); font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right; }
+.volume { display: flex; align-items: center; gap: 8px; flex: none; font-size: 19px; color: var(--dim); }
+.volume input { width: 120px; height: 44px; margin: 0; accent-color: var(--chalk); cursor: pointer; }
+.volume input:focus-visible { outline: 3px solid var(--task); outline-offset: 2px; }
+/* Keep the control strip usable without scaling down its touch targets. */
+@media (max-width: 700px) {
+ #frame { top: calc(50% - 50px); overflow: visible; }
+ #bar { width: calc(1600px * var(--k)); height: auto; flex-wrap: wrap; gap: 6px; padding: 8px;
+ transform: scale(calc(1 / var(--k))); transform-origin: left top; border-radius: 0 0 12px 12px; }
+ #bar .icon { width: 36px; height: 40px; }
+ .step { display: none; }
+ .segs, .volume { order: 1; }
+ .segs { min-width: 80px; margin: 0; }
+ .volume { font-size: 16px; }
+}
.sound-note { position: absolute; left: 24px; top: 20px; margin: 0; padding: 8px 18px; border-radius: 12px; background: rgba(15, 22, 19, .85); color: var(--dim); font-size: 19px; }
.sound-note[hidden] { display: none; }
.blanks .brow { padding: 8px 0; border-bottom: 1px solid #2e433b; }
diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.js
index 2e2f2c2..967e062 100644
--- a/site/elementary-algebra/lib/engine.js
+++ b/site/elementary-algebra/lib/engine.js
@@ -72,6 +72,9 @@ document.body.insertAdjacentHTML('afterbegin', `<div id="frame">
</button>
<div class="segs" id="segs"></div>
<div class="step" id="stepName"></div>
+ <label class="volume" for="volume">Volume
+ <input id="volume" type="range" min="0" max="1" step="0.05" value="1" title="Volume (0 = muted)">
+ </label>
<button class="icon txt" id="bHelp" aria-label="Keyboard shortcuts (?)" title="Keyboard shortcuts (?)">?</button>
<button class="icon cc" id="bCC" aria-pressed="false" aria-label="Captions (C)" title="Captions (C)">CC</button>
<button class="icon" id="bFull" aria-label="Full screen">
@@ -1479,6 +1482,7 @@ function start(i, play) {
evalTo(0);
const a = P.audio = new Audio(`audio/en/${BEATS[i].id}.mp3`);
a.preload = 'auto';
+ a.volume = +$('volume').value;
a.onerror = () => { if (P.audio === a) soundFailed(); };
const b = BEATS[i];
if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });
@@ -1564,6 +1568,7 @@ $('stage').addEventListener('click', e => {
if (!b.ask && !e.target.closest('.qlayer')) togglePlay();
});
$('bPlay').onclick = togglePlay;
+$('volume').oninput = () => { if (P.audio) P.audio.volume = +$('volume').value; };
$('bBack').onclick = () => { hideCover(); back1(); };
$('bRestart').onclick = restart;
$('bCC').onclick = () => setCaptions(!captions);
@@ -1581,8 +1586,8 @@ document.addEventListener('keydown', e => {
if (e.altKey || e.metaKey || e.ctrlKey) return;
const k = e.key;
if (!$('help').hidden) { if (k === 'Escape' || k === '?') { e.preventDefault(); toggleHelp(); } return; }
- if (e.target.matches?.('input')) { // typing an answer: only Enter (check) and Escape (leave the box) are ours
- if (k === 'Enter' && P.keys) { e.preventDefault(); P.keys(e); }
+ if (e.target.matches?.('input')) { // Keep native input keys; only answer boxes use Enter to check.
+ if (k === 'Enter' && e.target.type !== 'range' && P.keys) { e.preventDefault(); P.keys(e); }
else if (k === 'Escape') e.target.blur();
return;
}
@@ -1604,7 +1609,7 @@ document.addEventListener('keydown', e => {
// Mouse clicks should not leave focus on a control, so Enter and Space keep meaning "check" and "play".
$('frame').addEventListener('mousedown', e => { if (e.target.closest('button, [tabindex]')) e.preventDefault(); });
$('bHelp').onclick = toggleHelp;
-const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, innerHeight / 1000));
+const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, (innerHeight - (innerWidth <= 700 ? 120 : 0)) / 1000));
addEventListener('resize', fit);
fit();
diff --git a/site/math-notebook/lib/engine.css b/site/math-notebook/lib/engine.css
index 32acb76..6cf70a8 100644
--- a/site/math-notebook/lib/engine.css
+++ b/site/math-notebook/lib/engine.css
@@ -106,6 +106,20 @@ button[hidden] { display: none; }
.seg.ask { background: #2d4a57; }
.seg.ask i { background: var(--quiz); }
.step { width: 300px; flex: none; color: var(--dim); font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right; }
+.volume { display: flex; align-items: center; gap: 8px; flex: none; font-size: 19px; color: var(--dim); }
+.volume input { width: 120px; height: 44px; margin: 0; accent-color: var(--chalk); cursor: pointer; }
+.volume input:focus-visible { outline: 3px solid var(--task); outline-offset: 2px; }
+/* Keep the control strip usable without scaling down its touch targets. */
+@media (max-width: 700px) {
+ #frame { top: calc(50% - 50px); overflow: visible; }
+ #bar { width: calc(1600px * var(--k)); height: auto; flex-wrap: wrap; gap: 6px; padding: 8px;
+ transform: scale(calc(1 / var(--k))); transform-origin: left top; border-radius: 0 0 12px 12px; }
+ #bar .icon { width: 36px; height: 40px; }
+ .step { display: none; }
+ .segs, .volume { order: 1; }
+ .segs { min-width: 80px; margin: 0; }
+ .volume { font-size: 16px; }
+}
.sound-note { position: absolute; left: 24px; top: 20px; margin: 0; padding: 8px 18px; border-radius: 12px; background: rgba(15, 22, 19, .85); color: var(--dim); font-size: 19px; }
.sound-note[hidden] { display: none; }
.blanks .brow { padding: 8px 0; border-bottom: 1px solid #2e433b; }
diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.js
index 84cb026..2200961 100644
--- a/site/math-notebook/lib/engine.js
+++ b/site/math-notebook/lib/engine.js
@@ -72,6 +72,9 @@ document.body.insertAdjacentHTML('afterbegin', `<div id="frame">
</button>
<div class="segs" id="segs"></div>
<div class="step" id="stepName"></div>
+ <label class="volume" for="volume">Volume
+ <input id="volume" type="range" min="0" max="1" step="0.05" value="1" title="Volume (0 = muted)">
+ </label>
<button class="icon txt" id="bHelp" aria-label="Keyboard shortcuts (?)" title="Keyboard shortcuts (?)">?</button>
<button class="icon cc" id="bCC" aria-pressed="false" aria-label="Captions (C)" title="Captions (C)">CC</button>
<button class="icon" id="bFull" aria-label="Full screen">
@@ -1484,6 +1487,7 @@ function start(i, play) {
evalTo(0);
const a = P.audio = new Audio(`audio/${CHAPTER.language || 'en'}/${BEATS[i].id}.mp3`);
a.preload = 'auto';
+ a.volume = +$('volume').value;
a.onerror = () => { if (P.audio === a) soundFailed(); };
const b = BEATS[i];
if (b.ask) b.ask(() => { if (P.i === i) { evalTo(Infinity); start(i + 1, true); } });
@@ -1569,6 +1573,7 @@ $('stage').addEventListener('click', e => {
if (!b.ask && !e.target.closest('.qlayer')) togglePlay();
});
$('bPlay').onclick = togglePlay;
+$('volume').oninput = () => { if (P.audio) P.audio.volume = +$('volume').value; };
$('bBack').onclick = () => { hideCover(); back1(); };
$('bRestart').onclick = restart;
$('bCC').onclick = () => setCaptions(!captions);
@@ -1586,8 +1591,8 @@ document.addEventListener('keydown', e => {
if (e.altKey || e.metaKey || e.ctrlKey) return;
const k = e.key;
if (!$('help').hidden) { if (k === 'Escape' || k === '?') { e.preventDefault(); toggleHelp(); } return; }
- if (e.target.matches?.('input')) { // typing an answer: only Enter (check) and Escape (leave the box) are ours
- if (k === 'Enter' && P.keys) { e.preventDefault(); P.keys(e); }
+ if (e.target.matches?.('input')) { // Keep native input keys; only answer boxes use Enter to check.
+ if (k === 'Enter' && e.target.type !== 'range' && P.keys) { e.preventDefault(); P.keys(e); }
else if (k === 'Escape') e.target.blur();
return;
}
@@ -1609,7 +1614,7 @@ document.addEventListener('keydown', e => {
// Mouse clicks should not leave focus on a control, so Enter and Space keep meaning "check" and "play".
$('frame').addEventListener('mousedown', e => { if (e.target.closest('button, [tabindex]')) e.preventDefault(); });
$('bHelp').onclick = toggleHelp;
-const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, innerHeight / 1000));
+const fit = () => document.documentElement.style.setProperty('--k', Math.min(innerWidth / 1640, (innerHeight - (innerWidth <= 700 ? 120 : 0)) / 1000));
addEventListener('resize', fit);
fit();