cortico-world-agent-speed 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -0
- package/LICENSE +0 -14
- package/README.md +29 -11
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/activity.d.ts +15 -0
- package/activity.js +88 -3
- package/describe.json +806 -0
- package/dist/webui.css +2 -0
- package/dist/webui.css.map +7 -0
- package/dist/webui.js +2 -0
- package/dist/webui.js.map +7 -0
- package/host/app-store.cjs +68 -0
- package/host/cursor.cjs +67 -0
- package/host/electron-main.cjs +666 -0
- package/host/input-shape.cjs +63 -0
- package/host/mouse-lease.cjs +77 -0
- package/host/overlay.cjs +139 -0
- package/host/portrait.cjs +52 -0
- package/host/preload.cjs +60 -14
- package/host/renderer-gone.cjs +24 -0
- package/host/windows.cjs +251 -0
- package/package.json +27 -7
- package/scripts/build-webui.mjs +40 -0
- package/scripts/speech-bank/build.py +166 -0
- package/scripts/speech-bank/dicts.py +76 -0
- package/src/ENV_PROMPT.en.md +51 -0
- package/src/ENV_PROMPT.md +51 -0
- package/src/asr/address.ts +25 -0
- package/src/asr/hotkey.ts +335 -0
- package/src/asr/result.ts +51 -0
- package/src/asr/segmenter.ts +269 -0
- package/src/asr/sherpa.ts +280 -0
- package/src/asr/simplify.ts +38 -0
- package/src/asr/system-recognizer.ts +292 -0
- package/src/asr/system-sapi.ps1 +173 -0
- package/src/asr/transcription-api.ts +176 -0
- package/src/chat.ts +212 -0
- package/src/config.ts +481 -0
- package/src/cua/ENV_PROMPT.en.md +29 -0
- package/src/cua/ENV_PROMPT.md +29 -0
- package/src/cua/config.ts +88 -0
- package/src/cua/cua.ts +712 -0
- package/src/cua/engine/darwin.ts +410 -0
- package/src/cua/engine/display.ts +55 -0
- package/src/cua/engine/fail.ts +15 -0
- package/src/cua/engine/foreground.ts +8 -0
- package/src/cua/engine/image.ts +57 -0
- package/src/cua/engine/keys.ts +70 -0
- package/src/cua/engine/linux-keys.ts +29 -0
- package/src/cua/engine/linux.ts +343 -0
- package/src/cua/engine/mac-keys.ts +57 -0
- package/src/cua/engine/user-activity.ts +54 -0
- package/src/cua/engine/wait-idle.ts +31 -0
- package/src/cua/engine/win32.ts +299 -0
- package/src/cua/engine-child.ts +274 -0
- package/src/cua/engine-ipc.ts +140 -0
- package/src/cua/i18n/de.ts +53 -0
- package/src/cua/i18n/en.ts +63 -0
- package/src/cua/i18n/es-419.ts +53 -0
- package/src/cua/i18n/fr.ts +53 -0
- package/src/cua/i18n/index.ts +26 -0
- package/src/cua/i18n/it.ts +53 -0
- package/src/cua/i18n/ja.ts +53 -0
- package/src/cua/i18n/ko.ts +53 -0
- package/src/cua/i18n/pt-BR.ts +53 -0
- package/src/cua/i18n/ru.ts +59 -0
- package/src/cua/i18n/zh-Hant.ts +53 -0
- package/src/cua/i18n/zh.ts +59 -0
- package/src/cua/model-text.ts +252 -0
- package/src/cua/terminal.ts +31 -0
- package/src/cua/tools.ts +153 -0
- package/src/definition.ts +146 -0
- package/src/host-reason.ts +32 -0
- package/src/i18n/de.ts +409 -0
- package/src/i18n/en.ts +443 -0
- package/src/i18n/es-419.ts +409 -0
- package/src/i18n/fr.ts +409 -0
- package/src/i18n/index.ts +56 -0
- package/src/i18n/it.ts +409 -0
- package/src/i18n/ja.ts +409 -0
- package/src/i18n/ko.ts +409 -0
- package/src/i18n/pt-BR.ts +409 -0
- package/src/i18n/ru.ts +415 -0
- package/src/i18n/zh-Hant.ts +409 -0
- package/src/i18n/zh.ts +440 -0
- package/src/index.ts +15 -0
- package/src/model-text.ts +344 -0
- package/src/moves.ts +101 -0
- package/src/pack-import.ts +361 -0
- package/src/packs.ts +449 -0
- package/src/runtime/download.ts +29 -0
- package/src/runtime/store.ts +293 -0
- package/src/script.ts +202 -0
- package/src/self.ts +247 -0
- package/src/server.ts +479 -0
- package/src/speech/data/CMUDICT-LICENSE +33 -0
- package/src/speech/data/IPADIC-COPYING +71 -0
- package/src/speech/data/UNICODE-LICENSE +39 -0
- package/src/speech/data/en.tsv.gz +0 -0
- package/src/speech/data/ja-ipadic.tsv.gz +0 -0
- package/src/speech/data/ja-unihan.tsv.gz +0 -0
- package/src/speech/data/zh.json +1 -0
- package/src/speech/index.ts +204 -0
- package/src/speech/ipa.ts +83 -0
- package/src/speech/japanese.ts +105 -0
- package/src/speech/jukujikun.ts +22 -0
- package/src/speech/korean.ts +35 -0
- package/src/speech/lang.ts +151 -0
- package/src/speech/rules.ts +72 -0
- package/src/speech/syllable.ts +76 -0
- package/src/status.ts +164 -0
- package/src/tools.ts +110 -0
- package/src/webui/api-rows.css +2 -0
- package/src/webui/api-rows.ts +44 -0
- package/src/webui/client.ts +451 -0
- package/src/webui/dress-dialog.css +14 -0
- package/src/webui/dress-dialog.ts +117 -0
- package/src/webui/dress-panel.ts +160 -0
- package/src/webui/key-capture.ts +60 -0
- package/src/webui/shared/modal.css +13 -0
- package/src/webui/shared/modal.ts +77 -0
- package/src/webui/shared/problem.css +15 -0
- package/src/webui/shared/problem.ts +217 -0
- package/src/webui/shared/strings.ts +5 -0
- package/src/webui/style.css +50 -0
- package/src/window-host.ts +116 -0
- package/src/world.ts +2926 -0
- package/web/activity-motion.js +117 -0
- package/web/appearance.js +60 -0
- package/web/body-host.js +257 -0
- package/web/coo/coo.js +384 -0
- package/web/coo/figure.js +15 -0
- package/web/coo/figure.json +1224 -0
- package/web/dist/dress-dialog.css +1 -0
- package/web/dist/dress-dialog.js +1 -0
- package/web/drag-row.js +49 -0
- package/web/drag.js +149 -0
- package/web/dress.css +170 -0
- package/web/dress.html +37 -0
- package/web/dress.js +786 -0
- package/web/figure-frame.html +16 -0
- package/web/figure-frame.js +128 -0
- package/web/frame-loop.js +55 -0
- package/web/hints.js +77 -0
- package/web/i18n/de.js +170 -0
- package/web/i18n/en.js +177 -0
- package/web/i18n/es-419.js +170 -0
- package/web/i18n/fr.js +170 -0
- package/web/i18n/it.js +170 -0
- package/web/i18n/ja.js +170 -0
- package/web/i18n/ko.js +170 -0
- package/web/i18n/pt-BR.js +170 -0
- package/web/i18n/ru.js +170 -0
- package/web/i18n/zh-Hant.js +170 -0
- package/web/i18n/zh.js +175 -0
- package/web/i18n.js +54 -0
- package/{renderer/vendor → web}/kit/body.js +31 -58
- package/web/looks.js +70 -0
- package/web/mic-worklet.js +36 -0
- package/web/mic.js +117 -0
- package/web/motion-frames.js +80 -0
- package/web/motion-level.js +40 -0
- package/web/new-year-2027.svg +238 -0
- package/web/overlay.html +16 -0
- package/web/overlay.js +100 -0
- package/web/pet-app.js +3000 -0
- package/web/pet.css +642 -0
- package/web/pet.html +21 -0
- package/web/scale-slider.js +259 -0
- package/web/show-hide.js +49 -0
- package/web/sound.js +245 -0
- package/web/speech/zh.json +1 -0
- package/web/speech/zh.ogg +0 -0
- package/web/speech.js +197 -0
- package/web/stage.js +160 -0
- package/web/swap-motion.js +119 -0
- package/web/ui.js +195 -0
- package/host/launch.js +0 -187
- package/host/library.cjs +0 -638
- package/host/main.cjs +0 -389
- package/index.js +0 -93
- package/renderer/index.html +0 -22
- package/renderer/pet-sandbox.html +0 -11
- package/renderer/pet.css +0 -38
- package/renderer/pet.js +0 -251
- package/renderer/vendor/activity-motion.js +0 -101
- package/renderer/vendor/figure-frame.js +0 -115
- package/renderer/vendor/pack.css +0 -2
- /package/{renderer/vendor → web}/kit/rig.js +0 -0
package/web/pet.html
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN" data-theme="dark">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Cortico</title>
|
|
7
|
+
<link rel="stylesheet" href="/web/pet.css">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div class="floor" aria-hidden="true"></div>
|
|
11
|
+
<div class="stage" id="stage">
|
|
12
|
+
<!-- the body (its shadow and particles too), run by its figure pack in a sandboxed frame (body-host.js) -->
|
|
13
|
+
<div class="figure-layer" id="figureLayer"></div>
|
|
14
|
+
</div>
|
|
15
|
+
<div class="bubble" id="bubble" role="status" aria-live="polite" hidden></div>
|
|
16
|
+
<div class="menu" id="menu" role="menu" hidden></div>
|
|
17
|
+
<div class="tools" id="tools" hidden></div>
|
|
18
|
+
<div class="wheel" id="wheel" hidden></div>
|
|
19
|
+
<script type="module" src="/web/pet-app.js"></script>
|
|
20
|
+
</body>
|
|
21
|
+
</html>
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slider of the pet's pages and of the settings window (console/features/pet/index.ts, through pet-ui.d.ts):
|
|
3
|
+
* `min`–`soft` on the main stretch, then a short stretch past a divider for `soft`–`max`; with `soft` at `max` it is one
|
|
4
|
+
* plain stretch (the 外发光 and 音量 sliders). While the value is past the divider the short stretch widens (and the
|
|
5
|
+
* whole bar, as far as `room()` allows); back at `soft` or below it shrinks again, animated. A drag leaves what is left
|
|
6
|
+
* of the pointer as it is and grows the bar to the right only (`dragTo`), so the thumb stays under the pointer; the bar
|
|
7
|
+
* settles into its rest layout once let go. Steps are `step`, or .05 up to `soft` and .1 past it.
|
|
8
|
+
* Each page styles it by its root class: `.pet-scale` (pet.css, dress.css) or the settings window's `.companion-scale`
|
|
9
|
+
* (home.css); the parts are `.ps-main`, `.ps-tail`, `.ps-fill`, `.ps-divider` and `.ps-thumb` on both.
|
|
10
|
+
*/
|
|
11
|
+
import { progress } from './motion-frames.js';
|
|
12
|
+
|
|
13
|
+
/** The bar's width and its main stretch's share: at rest, and while the value is past the divider. */
|
|
14
|
+
const WIDTHS = { collapsed: { width: 280, main: .88 }, expanded: { width: 440, main: .45 } };
|
|
15
|
+
const ANIMATE_MS = 240;
|
|
16
|
+
/** Milliseconds after the last step by key or wheel that the knob still shows as held (`.stepping`, pet.css). */
|
|
17
|
+
const STEP_HOLD_MS = 300;
|
|
18
|
+
const FINE = .05, COARSE = .1;
|
|
19
|
+
/** The size range on screen (src/config.ts SCALE_MIN, SCALE_MAX) and the divider, as on the settings window's slider. */
|
|
20
|
+
export const SIZE = { min: .5, soft: 2, max: 10 };
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A page that sends a value to the World (a size, a transparency) and hears the World's back: `sent(v)` when it sends one;
|
|
24
|
+
* `heard(v, busy)` for each value the World pushes, `busy` while the page's own slider is held or a send is pending. A value
|
|
25
|
+
* heard while busy, or within `ms` of a send and unlike it, is older than that send and is held; the value held when the
|
|
26
|
+
* window ends without an echo is taken then (`take(v)`), so a value set elsewhere meanwhile is not lost.
|
|
27
|
+
*/
|
|
28
|
+
export function sizeEcho(take, ms = 1000) {
|
|
29
|
+
let sent = NaN, at = -Infinity, held = null, timer = 0;
|
|
30
|
+
const settle = () => { timer = 0; if (held === null) return; const v = held; held = null; take(v); };
|
|
31
|
+
return {
|
|
32
|
+
sent(v) {
|
|
33
|
+
sent = v; at = performance.now(); held = null;
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
timer = setTimeout(settle, ms);
|
|
36
|
+
},
|
|
37
|
+
heard(v, busy = false) {
|
|
38
|
+
if (busy || (v !== sent && performance.now() - at < ms)) { held = v; return; }
|
|
39
|
+
held = null;
|
|
40
|
+
take(v);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ease = (k) => (k < .5 ? 2 * k * k : 1 - (-2 * k + 2) ** 2 / 2);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* `o`: { min, soft, max, label, room() (pixels the bar may take), onInput(value) while it moves, onChange(value) once a
|
|
49
|
+
* drag or key press ends, step? (one step of the value), format?(value) (the value read aloud, a percentage by default),
|
|
50
|
+
* widths? (as WIDTHS), className? (the root's, `pet-scale` by default) }. Returns { el, value, active (being dragged),
|
|
51
|
+
* set(value), nudge(dir), relayout() }.
|
|
52
|
+
*/
|
|
53
|
+
export function scaleSlider(doc, o) {
|
|
54
|
+
const widths = o.widths ?? WIDTHS;
|
|
55
|
+
const el = doc.createElement('div');
|
|
56
|
+
el.className = o.className ?? 'pet-scale';
|
|
57
|
+
el.tabIndex = 0;
|
|
58
|
+
el.setAttribute('role', 'slider');
|
|
59
|
+
el.setAttribute('aria-label', o.label);
|
|
60
|
+
el.setAttribute('aria-valuemin', String(o.min));
|
|
61
|
+
el.setAttribute('aria-valuemax', String(o.max));
|
|
62
|
+
const part = (cls) => { const d = doc.createElement('div'); d.className = cls; el.append(d); return d; };
|
|
63
|
+
const main = part('ps-main'), tail = part('ps-tail'), fill = part('ps-fill'), divider = part('ps-divider'), thumb = part('ps-thumb');
|
|
64
|
+
|
|
65
|
+
let value = 1;
|
|
66
|
+
/** The bar at rest: 0 collapsed, 1 widened; eased from `from` toward `target` since `startedAt`. */
|
|
67
|
+
let k = 0, from = 0, target = 0, startedAt = 0;
|
|
68
|
+
/**
|
|
69
|
+
* While a drag holds the bar: the main stretch's width at the press, kept; the tail values past the divider are mapped
|
|
70
|
+
* on (`tail`), which only grows; and the tail as drawn (`shown`), catching up with it from `grownFrom` since `grownAt`.
|
|
71
|
+
*/
|
|
72
|
+
let held = null;
|
|
73
|
+
/** After a drag: the bar as it left it, shown less and less over the rest layout since `at`. */
|
|
74
|
+
let left = null;
|
|
75
|
+
let raf = 0;
|
|
76
|
+
|
|
77
|
+
/** No stretch past a divider: `soft` is the end. */
|
|
78
|
+
const single = o.soft >= o.max;
|
|
79
|
+
if (single) { tail.hidden = true; divider.hidden = true; }
|
|
80
|
+
const fine = o.step ?? FINE, coarse = o.step ?? COARSE;
|
|
81
|
+
const step = (v, up) => (v > o.soft || (up && v >= o.soft) ? coarse : fine);
|
|
82
|
+
const snap = (v) => {
|
|
83
|
+
const c = Math.min(o.max, Math.max(o.min, v));
|
|
84
|
+
const by = c <= o.soft ? fine : coarse;
|
|
85
|
+
return Number((Math.round(c / by) * by).toFixed(4));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** The bar at rest for `kk`: its width, its main stretch and the tail past the divider, in pixels. */
|
|
89
|
+
function rest(kk) {
|
|
90
|
+
const room = o.room() || widths.expanded.width;
|
|
91
|
+
const cw = Math.min(widths.collapsed.width, room), ew = Math.min(widths.expanded.width, room);
|
|
92
|
+
if (single) return { mainW: cw, tailW: 0 };
|
|
93
|
+
const e = ease(kk);
|
|
94
|
+
const width = cw + (ew - cw) * e;
|
|
95
|
+
const mainW = cw * widths.collapsed.main + (ew * widths.expanded.main - cw * widths.collapsed.main) * e;
|
|
96
|
+
return { mainW, tailW: width - mainW };
|
|
97
|
+
}
|
|
98
|
+
/** The bar now: `mainW` and `tailW` map values to pixels, `drawn` is the tail's drawn width. */
|
|
99
|
+
function layout(now = performance.now()) {
|
|
100
|
+
if (held) {
|
|
101
|
+
const t = progress(held.grownAt, ANIMATE_MS, now, doc.defaultView);
|
|
102
|
+
return { mainW: held.mainW, tailW: held.tail, drawn: held.grownFrom + (held.tail - held.grownFrom) * ease(t) };
|
|
103
|
+
}
|
|
104
|
+
const kk = from + (target - from) * progress(startedAt, ANIMATE_MS, now, doc.defaultView);
|
|
105
|
+
k = kk;
|
|
106
|
+
const r = rest(kk);
|
|
107
|
+
if (!left) return { ...r, drawn: r.tailW };
|
|
108
|
+
const b = 1 - ease(progress(left.at, ANIMATE_MS, now, doc.defaultView));
|
|
109
|
+
const mainW = r.mainW + (left.mainW - r.mainW) * b, tailW = r.tailW + (left.tailW - r.tailW) * b;
|
|
110
|
+
return { mainW, tailW, drawn: tailW };
|
|
111
|
+
}
|
|
112
|
+
const xOf = (v, l) => (v <= o.soft
|
|
113
|
+
? ((v - o.min) / (o.soft - o.min)) * l.mainW
|
|
114
|
+
: l.mainW + ((v - o.soft) / (o.max - o.soft)) * l.tailW);
|
|
115
|
+
const valueAt = (x, l) => (x <= l.mainW
|
|
116
|
+
? o.min + (Math.max(0, x) / l.mainW) * (o.soft - o.min)
|
|
117
|
+
// with no stretch past the divider (tailW 0), beyond the bar's end is its end, not 0/0
|
|
118
|
+
: l.tailW > 0 ? o.soft + (Math.min(l.tailW, x - l.mainW) / l.tailW) * (o.max - o.soft) : o.soft);
|
|
119
|
+
|
|
120
|
+
function draw(l = layout()) {
|
|
121
|
+
el.style.width = `${l.mainW + l.drawn}px`;
|
|
122
|
+
main.style.width = `${l.mainW}px`;
|
|
123
|
+
tail.style.left = divider.style.left = `${l.mainW}px`;
|
|
124
|
+
tail.style.width = `${l.drawn}px`;
|
|
125
|
+
const x = xOf(value, l);
|
|
126
|
+
fill.style.width = `${x}px`;
|
|
127
|
+
thumb.style.left = `${x}px`;
|
|
128
|
+
el.classList.toggle('past', value > o.soft);
|
|
129
|
+
el.setAttribute('aria-valuenow', String(value));
|
|
130
|
+
el.setAttribute('aria-valuetext', o.format?.(value) ?? `${Math.round(value * 100)}%`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether any of the bar's changes is still under way at `now`. */
|
|
134
|
+
function moving(now) {
|
|
135
|
+
const win = doc.defaultView;
|
|
136
|
+
if (held) return progress(held.grownAt, ANIMATE_MS, now, win) < 1;
|
|
137
|
+
return progress(startedAt, ANIMATE_MS, now, win) < 1 || (!!left && progress(left.at, ANIMATE_MS, now, win) < 1);
|
|
138
|
+
}
|
|
139
|
+
function tick(now) {
|
|
140
|
+
draw(layout(now));
|
|
141
|
+
if (left && !held && now - left.at >= ANIMATE_MS) left = null;
|
|
142
|
+
raf = moving(now) ? requestAnimationFrame(tick) : 0;
|
|
143
|
+
}
|
|
144
|
+
const animate = () => { if (!raf) raf = requestAnimationFrame(tick); };
|
|
145
|
+
/** At rest (no drag): the bar widens past the divider and narrows back at it. */
|
|
146
|
+
function settle() {
|
|
147
|
+
const want = value > o.soft ? 1 : 0;
|
|
148
|
+
if (want === target) return;
|
|
149
|
+
layout();
|
|
150
|
+
from = k; target = want; startedAt = performance.now();
|
|
151
|
+
animate();
|
|
152
|
+
}
|
|
153
|
+
function update(v, notify) {
|
|
154
|
+
const next = snap(v);
|
|
155
|
+
if (next === value) return;
|
|
156
|
+
value = next;
|
|
157
|
+
if (!held) settle();
|
|
158
|
+
draw();
|
|
159
|
+
if (notify) o.onInput(value);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A step by key or wheel moves the knob as a drag does: held (`.stepping`) until a moment after the last step, and
|
|
164
|
+
* gliding to each new place instead of jumping.
|
|
165
|
+
*/
|
|
166
|
+
let stepTimer = 0;
|
|
167
|
+
function stepTo(v) {
|
|
168
|
+
el.classList.add('stepping');
|
|
169
|
+
clearTimeout(stepTimer);
|
|
170
|
+
stepTimer = setTimeout(() => el.classList.remove('stepping'), STEP_HOLD_MS);
|
|
171
|
+
update(v, true);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A drag keeps what lies left of the pointer as it is: the main stretch keeps its width, and the tail grows to the
|
|
176
|
+
* right, once, when the pointer first goes past the divider; values past it map on the grown tail from that moment on,
|
|
177
|
+
* so the thumb stays under the pointer.
|
|
178
|
+
*/
|
|
179
|
+
function dragTo(x) {
|
|
180
|
+
if (!single && x > held.mainW) {
|
|
181
|
+
const room = o.room() || widths.expanded.width;
|
|
182
|
+
const want = Math.max(held.tail, Math.min(rest(1).tailW, room - held.mainW));
|
|
183
|
+
if (want > held.tail) {
|
|
184
|
+
const now = performance.now();
|
|
185
|
+
held.grownFrom = layout(now).drawn;
|
|
186
|
+
held.grownAt = now;
|
|
187
|
+
held.tail = want;
|
|
188
|
+
animate();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
update(valueAt(x, layout()), true);
|
|
192
|
+
}
|
|
193
|
+
const localX = (e) => e.clientX - el.getBoundingClientRect().left;
|
|
194
|
+
el.addEventListener('pointerdown', (e) => {
|
|
195
|
+
if (e.button !== 0) return;
|
|
196
|
+
el.setPointerCapture(e.pointerId);
|
|
197
|
+
el.focus({ preventScroll: true });
|
|
198
|
+
// a drag follows the pointer at once: no glide left over from a step a moment ago
|
|
199
|
+
clearTimeout(stepTimer);
|
|
200
|
+
el.classList.remove('stepping');
|
|
201
|
+
const l = layout();
|
|
202
|
+
held = { mainW: l.mainW, tail: l.tailW, grownFrom: l.drawn, grownAt: -Infinity };
|
|
203
|
+
left = null;
|
|
204
|
+
dragTo(localX(e));
|
|
205
|
+
});
|
|
206
|
+
// the bar as the drag left it settles into the rest layout for the value let go at
|
|
207
|
+
const end = () => {
|
|
208
|
+
if (!held) return;
|
|
209
|
+
const l = layout();
|
|
210
|
+
held = null;
|
|
211
|
+
left = { mainW: l.mainW, tailW: l.drawn, at: performance.now() };
|
|
212
|
+
k = from = target = value > o.soft ? 1 : 0;
|
|
213
|
+
animate();
|
|
214
|
+
draw();
|
|
215
|
+
o.onChange(value);
|
|
216
|
+
};
|
|
217
|
+
el.addEventListener('pointermove', (e) => {
|
|
218
|
+
if (!held) return;
|
|
219
|
+
// a move with no button down: the release was missed (let go over another window), and the drag is over
|
|
220
|
+
if (e.buttons === 0) { end(); return; }
|
|
221
|
+
dragTo(localX(e));
|
|
222
|
+
});
|
|
223
|
+
el.addEventListener('pointerup', end);
|
|
224
|
+
el.addEventListener('pointercancel', end);
|
|
225
|
+
// the page lost the pointer without a release reaching it: a drag left held would keep the pet window taking the mouse
|
|
226
|
+
el.addEventListener('lostpointercapture', end);
|
|
227
|
+
el.addEventListener('keydown', (e) => {
|
|
228
|
+
const keys = {
|
|
229
|
+
ArrowRight: () => value + step(value, true), ArrowUp: () => value + step(value, true),
|
|
230
|
+
ArrowLeft: () => value - step(value, false), ArrowDown: () => value - step(value, false),
|
|
231
|
+
PageUp: () => value + coarse * 5, PageDown: () => value - coarse * 5, Home: () => o.min, End: () => o.max,
|
|
232
|
+
};
|
|
233
|
+
const next = keys[e.key];
|
|
234
|
+
if (!next) return;
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
stepTo(next());
|
|
237
|
+
o.onChange(value);
|
|
238
|
+
});
|
|
239
|
+
draw();
|
|
240
|
+
/** Whether a value was set from outside yet: the first one, the value the page opens with, is drawn at rest at once. */
|
|
241
|
+
let placed = false;
|
|
242
|
+
return {
|
|
243
|
+
el,
|
|
244
|
+
get value() { return value; },
|
|
245
|
+
get active() { return !!held; },
|
|
246
|
+
/** A value set from outside: the knob moves there, and the bar widens or narrows as a step makes it. */
|
|
247
|
+
set(v) {
|
|
248
|
+
const next = snap(v);
|
|
249
|
+
if (!placed) { placed = true; value = next; k = from = target = value > o.soft ? 1 : 0; left = null; draw(); return; }
|
|
250
|
+
if (next === value) return;
|
|
251
|
+
value = next;
|
|
252
|
+
if (!held) settle();
|
|
253
|
+
draw();
|
|
254
|
+
},
|
|
255
|
+
/** One step up (`dir` 1) or down (-1), as an arrow key moves it; reported through onInput. */
|
|
256
|
+
nudge(dir) { stepTo(value + (dir > 0 ? step(value, true) : -step(value, false))); },
|
|
257
|
+
relayout: () => draw(),
|
|
258
|
+
};
|
|
259
|
+
}
|
package/web/show-hide.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Showing and hiding an element with an entrance and an exit its stylesheet animates: `entering` while it comes in
|
|
3
|
+
* (for `enterMs`), `leaving` while it goes (for the `ms` given to `hide`), then hidden. The pet's menu, hover buttons,
|
|
4
|
+
* look wheel and size slider, the pet's hover hints (hints.js) and, bundled, the settings window's hint boxes
|
|
5
|
+
* (console/base/ui/tip.ts) all go through it. At low motion (motion-level.js) it hides at once.
|
|
6
|
+
*
|
|
7
|
+
* `hiddenClass` hides by a class instead of the `hidden` attribute (the settings window's `.u-tip.hidden`).
|
|
8
|
+
*/
|
|
9
|
+
import { motionLevel } from './motion-level.js';
|
|
10
|
+
|
|
11
|
+
export function showHide(el, { enterMs = () => 0, hiddenClass = null } = {}) {
|
|
12
|
+
const win = el.ownerDocument.defaultView;
|
|
13
|
+
let enter = 0, leave = 0;
|
|
14
|
+
const setHidden = (hidden) => { if (hiddenClass) el.classList.toggle(hiddenClass, hidden); else el.hidden = hidden; };
|
|
15
|
+
const isHidden = () => (hiddenClass ? el.classList.contains(hiddenClass) : el.hidden);
|
|
16
|
+
const api = {
|
|
17
|
+
get hidden() { return isHidden(); },
|
|
18
|
+
get leaving() { return el.classList.contains('leaving'); },
|
|
19
|
+
/** Shows it from the start of its entrance, cutting short whatever it was doing. */
|
|
20
|
+
show() {
|
|
21
|
+
api.hideNow();
|
|
22
|
+
setHidden(false);
|
|
23
|
+
const ms = enterMs();
|
|
24
|
+
if (ms > 0) { el.classList.add('entering'); enter = win.setTimeout(() => el.classList.remove('entering'), ms); }
|
|
25
|
+
},
|
|
26
|
+
/** Shows it without an entrance; one leaving is called back where it is. */
|
|
27
|
+
reveal() { api.stay(); setHidden(false); },
|
|
28
|
+
/** Plays its exit for `ms`, then hides it; at once at low motion or when `now`. */
|
|
29
|
+
hide(ms, now = false) {
|
|
30
|
+
win.clearTimeout(enter); win.clearTimeout(leave);
|
|
31
|
+
el.classList.remove('entering');
|
|
32
|
+
if (now || motionLevel(win) === 'low' || isHidden()) { el.classList.remove('leaving'); setHidden(true); return; }
|
|
33
|
+
el.classList.add('leaving');
|
|
34
|
+
leave = win.setTimeout(() => { el.classList.remove('leaving'); setHidden(true); }, ms);
|
|
35
|
+
},
|
|
36
|
+
/** Hides it at once, cancelling an entrance or exit under way. */
|
|
37
|
+
hideNow() {
|
|
38
|
+
win.clearTimeout(enter); win.clearTimeout(leave);
|
|
39
|
+
el.classList.remove('entering', 'leaving');
|
|
40
|
+
setHidden(true);
|
|
41
|
+
},
|
|
42
|
+
/** Calls back an exit under way: it stays as it is. */
|
|
43
|
+
stay() {
|
|
44
|
+
win.clearTimeout(leave);
|
|
45
|
+
el.classList.remove('leaving');
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
return api;
|
|
49
|
+
}
|
package/web/sound.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pages' sounds: tones synthesized with Web Audio, and a figure pack's own audio files
|
|
3
|
+
* (`sounds` in its figure.json). The body in the figure frame asks for its sounds by name
|
|
4
|
+
* (`play`); the page plays them here, so the settings' switches by kind and the master switch
|
|
5
|
+
* hold for every pack.
|
|
6
|
+
*/
|
|
7
|
+
/** Which kind each sound belongs to; a kind can be silenced on its own (`sfx.configure`). */
|
|
8
|
+
export const SOUND_KINDS = {
|
|
9
|
+
move: ['step', 'skid', 'jump', 'land', 'whoosh', 'chirps', 'shake', 'nod', 'spin', 'shiver', 'dance', 'look'],
|
|
10
|
+
touch: ['grab', 'squeak', 'purr', 'poke'],
|
|
11
|
+
face: ['happy', 'wink', 'love', 'surprised', 'angry', 'sad', 'shy', 'yawn'],
|
|
12
|
+
snore: ['snore'],
|
|
13
|
+
talk: ['babble', 'blub'],
|
|
14
|
+
ui: ['tick', 'pop', 'sparkle', 'select', 'listenStart', 'listenEnd'],
|
|
15
|
+
alert: ['alert'],
|
|
16
|
+
};
|
|
17
|
+
/** The kinds a body's sounds are filed under; the rest belong to the page (talk, ui, alert). */
|
|
18
|
+
export const BODY_SOUND_KINDS = ['move', 'touch', 'face', 'snore'];
|
|
19
|
+
/** The smallest change of pan worth passing to the audio graph. */
|
|
20
|
+
const PAN_STEP = .01;
|
|
21
|
+
/** The farthest a sound leans to one side; at ±1 the far channel all but falls silent. */
|
|
22
|
+
const PAN_MAX = .5;
|
|
23
|
+
/** Seconds the fade of a pack's `restart` sound takes (playOwn). */
|
|
24
|
+
const RESTART_FADE = .03;
|
|
25
|
+
const KIND_OF = Object.fromEntries(Object.entries(SOUND_KINDS).flatMap(([kind, names]) => names.map((n) => [n, kind])));
|
|
26
|
+
/** `volume`: the master gain at 100%; the setting (`configure`) scales it. */
|
|
27
|
+
export function createSfx({ storageKey = 'cortico-pet.sound.v1', volume = .55 } = {}) {
|
|
28
|
+
let ctx = null, master = null, unlocked = false, on = true, noiseBuf = null, level = 1;
|
|
29
|
+
// left to right, -PAN_MAX to PAN_MAX: `pan` is what plays, `where` is Coo's place (setPan), heard while `byPosition`;
|
|
30
|
+
// a context without StereoPannerNode plays everything centred
|
|
31
|
+
let panner = null, pan = 0, where = 0, byPosition = true;
|
|
32
|
+
// kinds turned off, and seconds of snoring per sleep (0 = the whole sleep)
|
|
33
|
+
let muted = new Set(), snoreSeconds = 0;
|
|
34
|
+
// whether the window is shown: a window that does not throttle its page keeps document.hidden false while hidden
|
|
35
|
+
let shown = true;
|
|
36
|
+
try { on = localStorage.getItem(storageKey) !== 'off'; } catch (e) { /* default on */ }
|
|
37
|
+
const R = (a, b) => a + Math.random() * (b - a);
|
|
38
|
+
function ready() {
|
|
39
|
+
if (!on || !unlocked || !shown || document.hidden) return null;
|
|
40
|
+
if (!ctx) {
|
|
41
|
+
const C = window.AudioContext || window.webkitAudioContext;
|
|
42
|
+
if (!C) return null;
|
|
43
|
+
ctx = new C();
|
|
44
|
+
master = ctx.createGain(); master.gain.value = volume * level;
|
|
45
|
+
const comp = ctx.createDynamicsCompressor();
|
|
46
|
+
if (ctx.createStereoPanner) {
|
|
47
|
+
panner = ctx.createStereoPanner(); panner.pan.value = pan;
|
|
48
|
+
master.connect(panner); panner.connect(comp);
|
|
49
|
+
} else master.connect(comp);
|
|
50
|
+
comp.connect(ctx.destination);
|
|
51
|
+
}
|
|
52
|
+
if (ctx.state === 'suspended') ctx.resume();
|
|
53
|
+
for (const o of own.values()) decode(o);
|
|
54
|
+
return ctx;
|
|
55
|
+
}
|
|
56
|
+
function applyPan() {
|
|
57
|
+
const p = byPosition ? where : 0;
|
|
58
|
+
if (Math.abs(p - pan) < PAN_STEP) return;
|
|
59
|
+
pan = p;
|
|
60
|
+
panner?.pan.setTargetAtTime(p, ctx.currentTime, .03);
|
|
61
|
+
}
|
|
62
|
+
function tone({ type = 'sine', f0 = 440, f1 = f0, dur = .1, vol = .2, at = 0, attack = .005, vib = 0, vibRate = 0, filter = 0 }) {
|
|
63
|
+
const c = ready(); if (!c) return;
|
|
64
|
+
const t0 = c.currentTime + at, o = c.createOscillator(), g = c.createGain();
|
|
65
|
+
o.type = type;
|
|
66
|
+
o.frequency.setValueAtTime(f0, t0);
|
|
67
|
+
o.frequency.exponentialRampToValueAtTime(Math.max(20, f1), t0 + dur);
|
|
68
|
+
if (vib) {
|
|
69
|
+
const l = c.createOscillator(), lg = c.createGain();
|
|
70
|
+
l.frequency.value = vibRate; lg.gain.value = vib;
|
|
71
|
+
l.connect(lg); lg.connect(o.frequency); l.start(t0); l.stop(t0 + dur + .05);
|
|
72
|
+
}
|
|
73
|
+
g.gain.setValueAtTime(.0001, t0);
|
|
74
|
+
g.gain.exponentialRampToValueAtTime(vol, t0 + attack);
|
|
75
|
+
g.gain.exponentialRampToValueAtTime(.0001, t0 + dur);
|
|
76
|
+
let node = o;
|
|
77
|
+
if (filter) { const bq = c.createBiquadFilter(); bq.type = 'lowpass'; bq.frequency.value = filter; o.connect(bq); node = bq; }
|
|
78
|
+
node.connect(g); g.connect(master);
|
|
79
|
+
o.start(t0); o.stop(t0 + dur + .03);
|
|
80
|
+
}
|
|
81
|
+
function noise({ type = 'bandpass', f0 = 1000, f1 = f0, q = 1, dur = .2, vol = .15, at = 0, attack = .01 }) {
|
|
82
|
+
const c = ready(); if (!c) return;
|
|
83
|
+
if (!noiseBuf) {
|
|
84
|
+
noiseBuf = c.createBuffer(1, c.sampleRate, c.sampleRate);
|
|
85
|
+
const d = noiseBuf.getChannelData(0);
|
|
86
|
+
for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
|
|
87
|
+
}
|
|
88
|
+
const t0 = c.currentTime + at, s = c.createBufferSource(), bq = c.createBiquadFilter(), g = c.createGain();
|
|
89
|
+
s.buffer = noiseBuf; bq.type = type; bq.Q.value = q;
|
|
90
|
+
bq.frequency.setValueAtTime(f0, t0);
|
|
91
|
+
bq.frequency.exponentialRampToValueAtTime(f1, t0 + dur);
|
|
92
|
+
g.gain.setValueAtTime(.0001, t0);
|
|
93
|
+
g.gain.exponentialRampToValueAtTime(vol, t0 + attack);
|
|
94
|
+
g.gain.exponentialRampToValueAtTime(.0001, t0 + dur);
|
|
95
|
+
s.connect(bq); bq.connect(g); g.connect(master);
|
|
96
|
+
s.start(t0, Math.random() * .5); s.stop(t0 + dur + .03);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The pack's own sounds by name: kind, volume, `repeat` (the manifest's, absent for overlap), the file's bytes,
|
|
100
|
+
* the buffer once an audio context decoded them, and the plays of it not yet ended (`playing`: source and gain).
|
|
101
|
+
*/
|
|
102
|
+
let own = new Map();
|
|
103
|
+
/** The pack's voice: its `talk` sounds, which babble in place of the tone, and the pitch both are played at. */
|
|
104
|
+
let voice = { talk: [], pitch: 1 };
|
|
105
|
+
function decode(o) {
|
|
106
|
+
if (o.buffer || o.decoding || !ctx) return;
|
|
107
|
+
o.decoding = o.bytes.then((b) => ctx.decodeAudioData(b.slice(0))).then((buf) => { o.buffer = buf; }, (err) => console.error(`音效 ${o.name} 没能解码:`, err));
|
|
108
|
+
}
|
|
109
|
+
/** While the last play of `o` has not ended, a `finish` sound is not played and a `restart` sound fades that play out and starts after it. */
|
|
110
|
+
function playOwn(o, rate = 1) {
|
|
111
|
+
const c = ready(); if (!c) return;
|
|
112
|
+
// the first ask before the file is decoded goes unheard; the decode starts with the audio context
|
|
113
|
+
if (!o.buffer) { decode(o); return; }
|
|
114
|
+
if (o.repeat === 'finish' && o.playing.size) return;
|
|
115
|
+
let at = c.currentTime;
|
|
116
|
+
if (o.repeat === 'restart' && o.playing.size) {
|
|
117
|
+
for (const p of o.playing) {
|
|
118
|
+
p.g.gain.setValueAtTime(o.volume, at);
|
|
119
|
+
p.g.gain.linearRampToValueAtTime(0, at + RESTART_FADE);
|
|
120
|
+
p.src.stop(at + RESTART_FADE);
|
|
121
|
+
}
|
|
122
|
+
o.playing.clear();
|
|
123
|
+
at += RESTART_FADE;
|
|
124
|
+
}
|
|
125
|
+
const src = c.createBufferSource(), g = c.createGain(), p = { src, g };
|
|
126
|
+
src.buffer = o.buffer; src.playbackRate.value = rate; g.gain.value = o.volume;
|
|
127
|
+
src.connect(g); g.connect(master);
|
|
128
|
+
o.playing.add(p);
|
|
129
|
+
src.onended = () => o.playing.delete(p);
|
|
130
|
+
src.start(at);
|
|
131
|
+
}
|
|
132
|
+
const tones = {
|
|
133
|
+
step(run, i) {
|
|
134
|
+
const k = run ? 1.25 : 1;
|
|
135
|
+
tone({ f0: (i ? 520 : 440) * k, f1: (i ? 380 : 330) * k, dur: .06, vol: run ? .07 : .05 });
|
|
136
|
+
if (run) noise({ f0: 2500, q: .8, dur: .05, vol: .03 });
|
|
137
|
+
},
|
|
138
|
+
skid() { noise({ type: 'highpass', f0: 1800, f1: 900, dur: .22, vol: .08 }); },
|
|
139
|
+
jump() { tone({ type: 'triangle', f0: 200, f1: 720, dur: .24, vol: .22, vib: 40, vibRate: 22 }); },
|
|
140
|
+
land(hard) {
|
|
141
|
+
tone({ f0: hard ? 160 : 190, f1: 48, dur: hard ? .28 : .16, vol: hard ? .4 : .25 });
|
|
142
|
+
noise({ type: 'lowpass', f0: hard ? 700 : 500, f1: 120, dur: .12, vol: hard ? .25 : .12 });
|
|
143
|
+
if (hard) tone({ type: 'square', f0: 420, f1: 300, dur: .08, vol: .06, at: .02, filter: 1600 });
|
|
144
|
+
},
|
|
145
|
+
grab() { tone({ type: 'triangle', f0: 680, f1: 1500, dur: .14, vol: .18, vib: 60, vibRate: 30 }); },
|
|
146
|
+
squeak() { tone({ type: 'triangle', f0: R(900, 1200), f1: R(1300, 1700), dur: .09, vol: .08, vib: 40, vibRate: 35 }); },
|
|
147
|
+
whoosh() { noise({ f0: 300, f1: 2400, q: 1.4, dur: .38, vol: .22, attack: .08 }); },
|
|
148
|
+
chirps() { for (let i = 0; i < 3; i++) tone({ f0: 2300 + i * 120, f1: 3200, dur: .06, vol: .06, at: i * .11 }); },
|
|
149
|
+
shake() { for (let i = 0; i < 5; i++) tone({ type: 'square', f0: 180, f1: 160, dur: .05, vol: .05, at: i * .06, filter: 900 }); },
|
|
150
|
+
hmm() { tone({ type: 'triangle', f0: 330, f1: 360, dur: .12, vol: .08 }); tone({ type: 'triangle', f0: 392, f1: 470, dur: .16, vol: .08, at: .14 }); },
|
|
151
|
+
yawn() { tone({ type: 'triangle', f0: 520, f1: 240, dur: 1, vol: .1, vib: 12, vibRate: 5, filter: 1500, attack: .15 }); },
|
|
152
|
+
snore() {
|
|
153
|
+
noise({ type: 'lowpass', f0: 250, f1: 700, dur: .7, vol: .09, attack: .35 });
|
|
154
|
+
},
|
|
155
|
+
purr() { tone({ type: 'sawtooth', f0: 62, f1: 58, dur: .9, vol: .08, vib: 6, vibRate: 24, filter: 320, attack: .1 }); },
|
|
156
|
+
poke() { tone({ f0: 320, f1: 200, dur: .09, vol: .16 }); },
|
|
157
|
+
shiver() { for (let i = 0; i < 8; i++) tone({ type: 'square', f0: 900, f1: 820, dur: .03, vol: .035, at: i * .07, filter: 2400 }); },
|
|
158
|
+
// looking about hums like the thinking 'hmm', but counts as a motion sound
|
|
159
|
+
look() { tones.hmm(); },
|
|
160
|
+
dance() { [523, 659, 784, 659, 880].forEach((fr, i) => tone({ type: 'triangle', f0: fr, dur: .13, vol: .09, at: i * .15 })); },
|
|
161
|
+
nod() { tone({ type: 'triangle', f0: 520, f1: 440, dur: .07, vol: .08 }); tone({ type: 'triangle', f0: 520, f1: 440, dur: .07, vol: .08, at: .2 }); },
|
|
162
|
+
spin() { tone({ type: 'triangle', f0: 300, f1: 1200, dur: .3, vol: .12, vib: 30, vibRate: 18 }); },
|
|
163
|
+
happy() { tone({ type: 'triangle', f0: 660, f1: 700, dur: .1, vol: .14 }); tone({ type: 'triangle', f0: 990, f1: 1050, dur: .14, vol: .14, at: .09 }); },
|
|
164
|
+
wink() { tone({ f0: 1760, dur: .5, vol: .1 }); tone({ f0: 2637, dur: .45, vol: .06, at: .04 }); },
|
|
165
|
+
love() { tone({ f0: 480, f1: 820, dur: .1, vol: .14 }); tone({ f0: 600, f1: 1000, dur: .12, vol: .14, at: .13 }); },
|
|
166
|
+
surprised() { tone({ f0: 380, f1: 1500, dur: .2, vol: .16, vib: 15, vibRate: 12 }); },
|
|
167
|
+
angry() { tone({ type: 'sawtooth', f0: 120, f1: 95, dur: .5, vol: .12, vib: 12, vibRate: 14, filter: 700 }); },
|
|
168
|
+
sad() { tone({ type: 'triangle', f0: 440, f1: 392, dur: .3, vol: .13, vib: 8, vibRate: 6 }); tone({ type: 'triangle', f0: 392, f1: 262, dur: .55, vol: .13, at: .3, vib: 10, vibRate: 5 }); },
|
|
169
|
+
shy() { tone({ f0: 1300, f1: 1600, dur: .07, vol: .07 }); tone({ f0: 1450, f1: 1750, dur: .07, vol: .06, at: .1 }); },
|
|
170
|
+
tick() { tone({ f0: 1200, f1: 1000, dur: .03, vol: .05 }); },
|
|
171
|
+
pop() { tone({ f0: 240, f1: 720, dur: .07, vol: .14 }); },
|
|
172
|
+
sparkle() { [1568, 2093, 2637].forEach((fr, i) => tone({ f0: fr, dur: .25, vol: .06, at: i * .06 })); },
|
|
173
|
+
select() { tone({ type: 'triangle', f0: 520, f1: 1040, dur: .1, vol: .12 }); },
|
|
174
|
+
babble(ch) {
|
|
175
|
+
// the same character always gets the same syllable or tone, so a word sounds alike each time
|
|
176
|
+
const n = ch.codePointAt(0);
|
|
177
|
+
if (voice.talk.length) { playOwn(voice.talk[n % voice.talk.length], voice.pitch * R(.95, 1.05)); return; }
|
|
178
|
+
const fr = (330 + (n % 9) * 28) * voice.pitch;
|
|
179
|
+
tone({ type: 'square', f0: fr, f1: fr * R(.85, 1.1), dur: .05, vol: .05, filter: 1800 });
|
|
180
|
+
},
|
|
181
|
+
blub() { tone({ f0: R(500, 700), f1: R(900, 1200), dur: .05, vol: .05 }); },
|
|
182
|
+
listenStart() { tone({ f0: 880, dur: .12, vol: .1 }); tone({ f0: 1320, dur: .16, vol: .1, at: .1 }); },
|
|
183
|
+
// a two-note chime, louder and longer than the other cues, for a question that needs the person
|
|
184
|
+
alert() { tone({ type: 'triangle', f0: 1319, dur: .3, vol: .2 }); tone({ type: 'triangle', f0: 1047, dur: .55, vol: .2, at: .16 }); },
|
|
185
|
+
listenEnd() { tone({ f0: 1320, dur: .1, vol: .09 }); tone({ f0: 990, dur: .16, vol: .09, at: .09 }); },
|
|
186
|
+
};
|
|
187
|
+
const api = {
|
|
188
|
+
unlock() { unlocked = true; ready(); },
|
|
189
|
+
isOn: () => on,
|
|
190
|
+
set(v) { on = v; try { localStorage.setItem(storageKey, v ? 'on' : 'off'); } catch (e) { /* not persisted */ } },
|
|
191
|
+
/** Coo's place across its display, -1 (left) to 1 (right), held to ±PAN_MAX; changes under PAN_STEP are skipped. */
|
|
192
|
+
setPan(v) {
|
|
193
|
+
where = Math.max(-PAN_MAX, Math.min(PAN_MAX, v));
|
|
194
|
+
applyPan();
|
|
195
|
+
},
|
|
196
|
+
/** The window was shown or hidden (by hand or while something is full screen); hidden, nothing plays. */
|
|
197
|
+
setShown(v) { shown = v; },
|
|
198
|
+
/** The audio context and the gain a sound of `kind` plays into, or null while it would not be heard. */
|
|
199
|
+
output(kind) {
|
|
200
|
+
if (muted.has(kind)) return null;
|
|
201
|
+
const c = ready();
|
|
202
|
+
return c ? { ctx: c, master } : null;
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* `kinds`: kind → on (SOUND_KINDS); `snoreSeconds`: snoring stops this long into a sleep, 0 = never;
|
|
206
|
+
* `panByPosition`: sounds follow setPan, off = they ease back to the centre; `volume`: percent of the master gain, 0–100.
|
|
207
|
+
*/
|
|
208
|
+
configure({ kinds, snoreSeconds: s, panByPosition: b, volume: v } = {}) {
|
|
209
|
+
if (kinds) muted = new Set(Object.keys(SOUND_KINDS).filter((k) => kinds[k] === false));
|
|
210
|
+
if (typeof s === 'number' && s >= 0) snoreSeconds = s;
|
|
211
|
+
if (typeof b === 'boolean') { byPosition = b; applyPan(); }
|
|
212
|
+
if (typeof v === 'number' && Number.isFinite(v)) { level = Math.max(0, Math.min(100, v)) / 100; if (master) master.gain.value = volume * level; }
|
|
213
|
+
},
|
|
214
|
+
/**
|
|
215
|
+
* The body's sounds (`sounds` of the pack on screen, from `base`): each is fetched now and decoded once
|
|
216
|
+
* there is an audio context. `null` drops them. `v` is the pack's `voice` ({ pitch }); its `talk` sounds babble.
|
|
217
|
+
*/
|
|
218
|
+
usePack(base, sounds, v) {
|
|
219
|
+
own = new Map(Object.entries(sounds ?? {}).map(([name, d]) => {
|
|
220
|
+
const bytes = fetch(new URL(d.file, new URL(base, location.href))).then((r) => (r.ok ? r.arrayBuffer() : Promise.reject(new Error(`HTTP ${r.status}`))));
|
|
221
|
+
bytes.catch((err) => console.error(`音效 ${name}(${d.file})没能载入:`, err));
|
|
222
|
+
return [name, { name, kind: d.kind, volume: d.volume ?? 1, repeat: d.repeat, bytes, playing: new Set() }];
|
|
223
|
+
}));
|
|
224
|
+
if (ctx) for (const o of own.values()) decode(o);
|
|
225
|
+
const pitch = typeof v?.pitch === 'number' && v.pitch >= .5 && v.pitch <= 2 ? v.pitch : 1;
|
|
226
|
+
voice = { talk: [...own.values()].filter((o) => o.kind === 'talk'), pitch };
|
|
227
|
+
},
|
|
228
|
+
/**
|
|
229
|
+
* A sound the body asks for: the pack's own by that name (filed under the kind its manifest gives), else the
|
|
230
|
+
* synthesized tone, filed under `kind` when that is a body's kind (a face that borrows the 'pop' tone is a
|
|
231
|
+
* face sound) and under the tone's own kind otherwise. Snoring (args[0]: seconds asleep) stops after `snoreSeconds`.
|
|
232
|
+
*/
|
|
233
|
+
play(name, kind, ...args) {
|
|
234
|
+
const o = own.get(name);
|
|
235
|
+
const k = o ? o.kind : BODY_SOUND_KINDS.includes(kind) ? kind : KIND_OF[name];
|
|
236
|
+
if (muted.has(k)) return;
|
|
237
|
+
if (k === 'snore' && snoreSeconds > 0 && args[0] > snoreSeconds) return;
|
|
238
|
+
if (o) playOwn(o);
|
|
239
|
+
else if (Object.hasOwn(tones, name)) tones[name](...args);
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
// the page's own calls by name (sfx.pop()) play the tone, muted with its kind; a pack's sound never stands in for them
|
|
243
|
+
for (const name of Object.keys(tones)) api[name] = (...args) => { if (!muted.has(KIND_OF[name])) tones[name](...args); };
|
|
244
|
+
return api;
|
|
245
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"hz":364,"units":{"a":[0.06,0.1149],"ai":[0.2349,0.1149],"an":[0.4098,0.115],"ang":[0.5848,0.1149],"ao":[0.7597,0.1149],"ba":[0.9346,0.1148],"bai":[1.1094,0.1145],"ban":[1.2839,0.1148],"bang":[1.4586,0.1143],"bao":[1.633,0.1147],"bei":[1.8077,0.1145],"ben":[1.9822,0.1148],"beng":[2.157,0.1147],"bi":[2.3317,0.1146],"bian":[2.5064,0.1149],"biao":[2.6813,0.1149],"bie":[2.8562,0.1149],"bin":[3.0311,0.1149],"bing":[3.206,0.1147],"bo":[3.3807,0.115],"bu":[3.5557,0.1148],"ca":[3.7305,0.1149],"cai":[3.9053,0.1148],"can":[4.0801,0.115],"cang":[4.2552,0.1149],"cao":[4.4301,0.1149],"ce":[4.6049,0.115],"cen":[4.78,0.115],"ceng":[4.955,0.115],"cha":[5.1301,0.1148],"chai":[5.3049,0.115],"chan":[5.4799,0.115],"chang":[5.655,0.1148],"chao":[5.8298,0.1148],"che":[6.0045,0.1148],"chen":[6.1793,0.115],"cheng":[6.3543,0.1147],"chi":[6.529,0.114],"chong":[6.703,0.1149],"chou":[6.8779,0.1149],"chu":[7.0528,0.1145],"chua":[7.2273,0.105],"chuai":[7.3923,0.115],"chuan":[7.5674,0.115],"chuang":[7.7424,0.1148],"chui":[7.9172,0.1148],"chun":[8.092,0.1149],"chuo":[8.2669,0.1147],"ci":[8.4416,0.115],"cong":[8.6166,0.115],"cou":[8.7917,0.1145],"cu":[8.9661,0.1146],"cuan":[9.1408,0.115],"cui":[9.3158,0.1148],"cun":[9.4906,0.115],"cuo":[9.6656,0.115],"da":[9.8407,0.1145],"dai":[10.0152,0.1149],"dan":[10.1901,0.1144],"dang":[10.3645,0.1143],"dao":[10.5389,0.1149],"de":[10.7138,0.115],"den":[10.8887,0.1148],"deng":[11.0635,0.1149],"di":[11.2384,0.1148],"dian":[11.4132,0.1149],"diao":[11.5881,0.1149],"die":[11.763,0.1149],"ding":[11.938,0.1147],"diu":[12.1126,0.1149],"dong":[12.2875,0.1146],"dou":[12.4621,0.1149],"du":[12.637,0.1147],"duan":[12.8117,0.1147],"dui":[12.9864,0.1149],"dun":[13.1614,0.1149],"duo":[13.3362,0.1149],"e":[13.5111,0.115],"ei":[13.6861,0.1149],"en":[13.861,0.1148],"eng":[14.0358,0.1149],"er":[14.2106,0.1146],"fa":[14.3853,0.115],"fan":[14.5602,0.115],"fang":[14.7353,0.1149],"fei":[14.9102,0.1149],"fen":[15.0851,0.1149],"feng":[15.26,0.1149],"fiao":[15.435,0.1148],"fo":[15.6097,0.115],"fou":[15.7848,0.1149],"fu":[15.9596,0.114],"ga":[16.1336,0.115],"gai":[16.3086,0.1146],"gan":[16.4832,0.1146],"gang":[16.6578,0.1148],"gao":[16.8325,0.1147],"ge":[17.0073,0.115],"gei":[17.1823,0.115],"gen":[17.3573,0.1147],"geng":[17.532,0.115],"gong":[17.707,0.115],"gou":[17.8819,0.1149],"gu":[18.0568,0.1149],"gua":[18.2317,0.115],"guai":[18.4067,0.1149],"guan":[18.5816,0.115],"guang":[18.7567,0.115],"gui":[18.9317,0.115],"gun":[19.1066,0.115],"guo":[19.2817,0.115],"ha":[19.4567,0.115],"hai":[19.6318,0.115],"han":[19.8068,0.115],"hang":[19.9819,0.115],"hao":[20.1569,0.115],"he":[20.3319,0.1149],"hei":[20.5068,0.1149],"hen":[20.6817,0.115],"heng":[20.8568,0.115],"hong":[21.0318,0.115],"hou":[21.2069,0.115],"hu":[21.3819,0.1145],"hua":[21.5564,0.115],"huai":[21.7314,0.115],"huan":[21.9064,0.1149],"huang":[22.0812,0.1149],"hui":[22.2562,0.1147],"hun":[22.4309,0.1149],"huo":[22.6057,0.115],"ji":[22.7808,0.1146],"jia":[22.9554,0.1149],"jian":[23.1303,0.115],"jiang":[23.3053,0.115],"jiao":[23.4802,0.115],"jie":[23.6553,0.1148],"jin":[23.8301,0.1145],"jing":[24.0046,0.1145],"jiong":[24.1792,0.115],"jiu":[24.3542,0.115],"ju":[24.5293,0.1144],"juan":[24.7036,0.1149],"jue":[24.8785,0.1147],"jun":[25.0532,0.1147],"ka":[25.2279,0.115],"kai":[25.4029,0.1147],"kan":[25.5776,0.115],"kang":[25.7526,0.115],"kao":[25.9277,0.115],"ke":[26.1027,0.1146],"ken":[26.2774,0.115],"keng":[26.4524,0.115],"kong":[26.6274,0.1149],"kou":[26.8023,0.115],"ku":[26.9774,0.115],"kua":[27.1524,0.115],"kuai":[27.3274,0.1145],"kuan":[27.5019,0.115],"kuang":[27.6769,0.1147],"kui":[27.8516,0.1148],"kun":[28.0264,0.115],"kuo":[28.2015,0.1149],"la":[28.3764,0.1148],"lai":[28.5512,0.1148],"lan":[28.726,0.1148],"lang":[28.9009,0.1148],"lao":[29.0756,0.1148],"le":[29.2504,0.1148],"lei":[29.4252,0.1148],"leng":[29.6001,0.1148],"li":[29.7749,0.1148],"lia":[29.9497,0.1148],"lian":[30.1245,0.1149],"liang":[30.2994,0.1148],"liao":[30.4742,0.1149],"lie":[30.6491,0.1148],"lin":[30.8239,0.1149],"ling":[30.9987,0.1148],"liu":[31.1736,0.1148],"lo":[31.3484,0.1148],"long":[31.5232,0.1148],"lou":[31.6981,0.1148],"lu":[31.8729,0.1148],"luan":[32.0477,0.1148],"lun":[32.2225,0.1148],"luo":[32.3973,0.1148],"lv":[32.5721,0.1149],"lve":[32.747,0.1148],"ma":[32.9218,0.1148],"mai":[33.0966,0.1148],"man":[33.2714,0.1148],"mang":[33.4463,0.1148],"mao":[33.6211,0.1148],"me":[33.7959,0.1148],"mei":[33.9707,0.1148],"men":[34.1455,0.1148],"meng":[34.3203,0.1148],"mi":[34.4951,0.1148],"mian":[34.6699,0.1148],"miao":[34.8447,0.1148],"mie":[35.0195,0.1148],"min":[35.1943,0.1148],"ming":[35.3691,0.1148],"miu":[35.5439,0.1148],"mo":[35.7187,0.1148],"mou":[35.8935,0.1148],"mu":[36.0683,0.1148],"na":[36.2431,0.1148],"nai":[36.418,0.1148],"nan":[36.5928,0.1148],"nang":[36.7676,0.1148],"nao":[36.9424,0.1148],"ne":[37.1172,0.1146],"nei":[37.2919,0.1148],"nen":[37.4667,0.1148],"neng":[37.6415,0.1148],"ni":[37.8163,0.1148],"nian":[37.9911,0.1148],"niang":[38.166,0.1148],"niao":[38.3408,0.1148],"nie":[38.5156,0.1148],"nin":[38.6904,0.1148],"ning":[38.8652,0.1148],"niu":[39.04,0.1148],"nong":[39.2149,0.1148],"nou":[39.3897,0.1148],"nu":[39.5645,0.1148],"nuan":[39.7393,0.1148],"nun":[39.9141,0.1148],"nuo":[40.0889,0.1148],"nv":[40.2638,0.1148],"nve":[40.4385,0.1148],"o":[40.6134,0.1149],"ou":[40.7883,0.1148],"pa":[40.9631,0.1147],"pai":[41.1378,0.115],"pan":[41.3129,0.1148],"pang":[41.4876,0.1146],"pao":[41.6622,0.1148],"pei":[41.837,0.1144],"pen":[42.0114,0.1148],"peng":[42.1861,0.115],"pi":[42.3611,0.1145],"pian":[42.5356,0.115],"piao":[42.7106,0.115],"pie":[42.8857,0.1149],"pin":[43.0606,0.115],"ping":[43.2357,0.1146],"po":[43.4103,0.115],"pou":[43.5853,0.115],"pu":[43.7604,0.115],"qi":[43.9354,0.1144],"qia":[44.1098,0.115],"qian":[44.2848,0.115],"qiang":[44.4599,0.1148],"qiao":[44.6347,0.115],"qie":[44.8096,0.1145],"qin":[44.9841,0.1145],"qing":[45.1586,0.1146],"qiong":[45.3333,0.115],"qiu":[45.5083,0.1149],"qu":[45.6832,0.115],"quan":[45.8583,0.115],"que":[46.0333,0.115],"qun":[46.2084,0.115],"ran":[46.3834,0.115],"rang":[46.5585,0.1149],"rao":[46.7334,0.1148],"re":[46.9082,0.1149],"ren":[47.0831,0.1148],"reng":[47.2579,0.1148],"ri":[47.4327,0.1148],"rong":[47.6075,0.1148],"rou":[47.7823,0.115],"ru":[47.9574,0.115],"rua":[48.1324,0.115],"ruan":[48.3075,0.115],"rui":[48.4825,0.115],"run":[48.6575,0.115],"ruo":[48.8325,0.115],"sa":[49.0076,0.115],"sai":[49.1826,0.1149],"san":[49.3575,0.115],"sang":[49.5326,0.1148],"sao":[49.7074,0.115],"se":[49.8824,0.115],"sen":[50.0574,0.115],"seng":[50.2325,0.115],"sha":[50.4075,0.1149],"shai":[50.5824,0.115],"shan":[50.7575,0.115],"shang":[50.9325,0.1149],"shao":[51.1075,0.1149],"she":[51.2824,0.1148],"shen":[51.4572,0.1144],"sheng":[51.6316,0.1149],"shi":[51.8065,0.1143],"shou":[51.9808,0.115],"shu":[52.1557,0.1144],"shua":[52.3301,0.1146],"shuai":[52.5047,0.1148],"shuan":[52.6795,0.1148],"shuang":[52.8543,0.1146],"shui":[53.0289,0.1145],"shun":[53.2035,0.1149],"shuo":[53.3783,0.1145],"si":[53.5529,0.11],"song":[53.7229,0.1149],"sou":[53.8978,0.1149],"su":[54.0727,0.1147],"suan":[54.2474,0.115],"sui":[54.4225,0.1149],"sun":[54.5974,0.115],"suo":[54.7725,0.1147],"ta":[54.9471,0.115],"tai":[55.1222,0.115],"tan":[55.2972,0.1149],"tang":[55.4721,0.115],"tao":[55.6471,0.1145],"te":[55.8217,0.115],"teng":[55.9967,0.115],"ti":[56.1718,0.1145],"tian":[56.3462,0.115],"tiao":[56.5213,0.115],"tie":[56.6963,0.115],"ting":[56.8714,0.1148],"tong":[57.0462,0.1149],"tou":[57.221,0.1149],"tu":[57.396,0.1148],"tuan":[57.5708,0.115],"tui":[57.7458,0.1146],"tun":[57.9205,0.115],"tuo":[58.0955,0.1149],"wa":[58.2704,0.1149],"wai":[58.4453,0.1148],"wan":[58.62,0.1149],"wang":[58.795,0.1149],"wei":[58.9699,0.1149],"wen":[59.1447,0.1149],"weng":[59.3196,0.1148],"wo":[59.4943,0.1149],"wu":[59.6692,0.1148],"xi":[59.844,0.1148],"xia":[60.0187,0.1149],"xian":[60.1937,0.1149],"xiang":[60.3686,0.1149],"xiao":[60.5435,0.115],"xie":[60.7185,0.1148],"xin":[60.8934,0.115],"xing":[61.0684,0.1147],"xiong":[61.2431,0.1148],"xiu":[61.4178,0.115],"xu":[61.5928,0.105],"xuan":[61.7578,0.1148],"xue":[61.9326,0.1149],"xun":[62.1075,0.1146],"ya":[62.2821,0.1148],"yan":[62.4569,0.1149],"yang":[62.6319,0.1148],"yao":[62.8067,0.1148],"ye":[62.9815,0.1149],"yi":[63.1564,0.1149],"yin":[63.3312,0.115],"ying":[63.5062,0.1149],"yo":[63.6811,0.1148],"yong":[63.8559,0.1148],"you":[64.0308,0.1148],"yu":[64.2056,0.1146],"yuan":[64.3802,0.1148],"yue":[64.555,0.1148],"yun":[64.7298,0.1148],"za":[64.9046,0.115],"zai":[65.0796,0.115],"zan":[65.2546,0.115],"zang":[65.4297,0.1149],"zao":[65.6046,0.1149],"ze":[65.7795,0.115],"zei":[65.9545,0.1149],"zen":[66.1294,0.1148],"zeng":[66.3042,0.115],"zha":[66.4793,0.1148],"zhai":[66.654,0.1149],"zhan":[66.829,0.115],"zhang":[67.0039,0.1149],"zhao":[67.1788,0.115],"zhe":[67.3539,0.115],"zhen":[67.5289,0.1149],"zheng":[67.7038,0.1149],"zhi":[67.8788,0.114],"zhong":[68.0528,0.1148],"zhou":[68.2276,0.115],"zhu":[68.4026,0.1147],"zhua":[68.5773,0.1149],"zhuai":[68.7522,0.1146],"zhuan":[68.9268,0.1149],"zhuang":[69.1018,0.1149],"zhui":[69.2767,0.115],"zhun":[69.4517,0.1148],"zhuo":[69.6264,0.115],"zi":[69.8015,0.1148],"zong":[69.9762,0.1148],"zou":[70.151,0.115],"zu":[70.3261,0.115],"zuan":[70.5011,0.115],"zui":[70.6761,0.115],"zun":[70.8511,0.115],"zuo":[71.0262,0.115]}}
|
|
Binary file
|