@liberation-data/desk 0.14.0 → 0.15.0

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.
Files changed (69) hide show
  1. package/README.md +70 -7
  2. package/dist/core/index.d.ts +2 -0
  3. package/dist/core/index.d.ts.map +1 -1
  4. package/dist/core/index.js +1 -0
  5. package/dist/core/index.js.map +1 -1
  6. package/dist/core/look.d.ts +12 -0
  7. package/dist/core/look.d.ts.map +1 -0
  8. package/dist/core/look.js +26 -0
  9. package/dist/core/look.js.map +1 -0
  10. package/dist/css/dock.css +123 -3
  11. package/dist/css/looks.css +258 -0
  12. package/dist/css/tokens.css +5 -1
  13. package/dist/desk.css +387 -4
  14. package/dist/fx/fx.d.ts +29 -0
  15. package/dist/fx/fx.d.ts.map +1 -0
  16. package/dist/fx/fx.js +119 -0
  17. package/dist/fx/fx.js.map +1 -0
  18. package/dist/fx/index.d.ts +7 -0
  19. package/dist/fx/index.d.ts.map +1 -0
  20. package/dist/fx/index.js +4 -0
  21. package/dist/fx/index.js.map +1 -0
  22. package/dist/fx/measure.d.ts +18 -0
  23. package/dist/fx/measure.d.ts.map +1 -0
  24. package/dist/fx/measure.js +37 -0
  25. package/dist/fx/measure.js.map +1 -0
  26. package/dist/fx/sounds.d.ts +108 -0
  27. package/dist/fx/sounds.d.ts.map +1 -0
  28. package/dist/fx/sounds.js +148 -0
  29. package/dist/fx/sounds.js.map +1 -0
  30. package/dist/react/Desktop.d.ts.map +1 -1
  31. package/dist/react/Desktop.js +44 -9
  32. package/dist/react/Desktop.js.map +1 -1
  33. package/dist/react/Dock.d.ts +11 -1
  34. package/dist/react/Dock.d.ts.map +1 -1
  35. package/dist/react/Dock.js +4 -3
  36. package/dist/react/Dock.js.map +1 -1
  37. package/dist/react/context.d.ts +9 -1
  38. package/dist/react/context.d.ts.map +1 -1
  39. package/dist/react/context.js +15 -3
  40. package/dist/react/context.js.map +1 -1
  41. package/dist/react/index.d.ts +4 -2
  42. package/dist/react/index.d.ts.map +1 -1
  43. package/dist/react/index.js +2 -1
  44. package/dist/react/index.js.map +1 -1
  45. package/dist/react/shell.d.ts +4 -1
  46. package/dist/react/shell.d.ts.map +1 -1
  47. package/dist/react/shell.js +2 -2
  48. package/dist/react/shell.js.map +1 -1
  49. package/dist/react/soundsToggle.d.ts +14 -0
  50. package/dist/react/soundsToggle.d.ts.map +1 -0
  51. package/dist/react/soundsToggle.js +12 -0
  52. package/dist/react/soundsToggle.js.map +1 -0
  53. package/llms.txt +44 -14
  54. package/package.json +7 -2
  55. package/src/core/index.ts +2 -0
  56. package/src/core/look.ts +43 -0
  57. package/src/css/dock.css +123 -3
  58. package/src/css/looks.css +257 -0
  59. package/src/css/tokens.css +5 -1
  60. package/src/fx/fx.ts +145 -0
  61. package/src/fx/index.ts +6 -0
  62. package/src/fx/measure.ts +70 -0
  63. package/src/fx/sounds.ts +213 -0
  64. package/src/react/Desktop.tsx +97 -29
  65. package/src/react/Dock.tsx +16 -3
  66. package/src/react/context.tsx +25 -3
  67. package/src/react/index.ts +4 -2
  68. package/src/react/shell.tsx +5 -2
  69. package/src/react/soundsToggle.tsx +19 -0
package/src/css/dock.css CHANGED
@@ -19,12 +19,56 @@
19
19
 
20
20
  .desk-dock[data-placement="overlay"] {
21
21
  position: absolute;
22
- left: 50%;
23
- bottom: 12px;
24
22
  z-index: 1000;
23
+ }
24
+
25
+ .desk-dock[data-placement="overlay"][data-side="bottom"] {
26
+ left: 50%;
27
+ bottom: var(--desk-dock-offset);
25
28
  transform: translateX(-50%);
26
29
  }
27
30
 
31
+ .desk-dock[data-placement="overlay"]:not([data-side="bottom"]) {
32
+ top: 50%;
33
+ transform: translateY(-50%);
34
+ }
35
+
36
+ .desk-dock[data-placement="overlay"][data-side="left"] {
37
+ left: var(--desk-dock-offset);
38
+ }
39
+
40
+ .desk-dock[data-placement="overlay"][data-side="right"] {
41
+ right: var(--desk-dock-offset);
42
+ }
43
+
44
+ /* Standing in a column: items top to bottom, and never taller than the room it stands in. */
45
+ .desk-dock:not([data-side="bottom"]) {
46
+ flex-direction: column;
47
+ align-items: center;
48
+ max-width: none;
49
+ max-height: calc(100% - 2 * var(--desk-dock-offset));
50
+ /* The same thickness standing up as lying down, so --desk-dock-room holds on every side. */
51
+ padding: 9px 7px;
52
+ }
53
+
54
+ /*
55
+ * An overlay dock makes its own room. The stage beside it starts where the dock ends — a margin, not
56
+ * padding, because windows are placed from the stage's own corner: padding would keep filled windows
57
+ * clear and still open every free one underneath the dock.
58
+ */
59
+ :has(> .desk-dock[data-placement="overlay"][data-side="left"]) > .desk-stage {
60
+ margin-left: var(--desk-dock-room);
61
+ }
62
+
63
+ :has(> .desk-dock[data-placement="overlay"][data-side="right"]) > .desk-stage {
64
+ margin-right: var(--desk-dock-room);
65
+ }
66
+
67
+ :has(> .desk-dock[data-placement="overlay"][data-side="bottom"]) > .desk-stage {
68
+ /* The stage is `height: 100%`, so a margin would push it past its parent rather than shorten it. */
69
+ height: calc(100% - var(--desk-dock-room));
70
+ }
71
+
28
72
  .desk-dock-separator {
29
73
  flex: none;
30
74
  align-self: stretch;
@@ -33,6 +77,12 @@
33
77
  background: var(--desk-rule);
34
78
  }
35
79
 
80
+ .desk-dock:not([data-side="bottom"]) .desk-dock-separator {
81
+ width: auto;
82
+ height: 1px;
83
+ margin: 4px 6px;
84
+ }
85
+
36
86
  .desk-dock-slot {
37
87
  position: relative;
38
88
  display: flex;
@@ -66,11 +116,22 @@
66
116
  outline-offset: 2px;
67
117
  }
68
118
 
119
+ /* Magnified toward the desk, whichever side that is. */
69
120
  @media (hover: hover) and (pointer: fine) {
70
- .desk-dock-item:hover:not(:disabled) {
121
+ .desk-dock[data-side="bottom"] .desk-dock-item:hover:not(:disabled) {
71
122
  transform: translateY(-8px) scale(1.14);
72
123
  margin: 0 5px;
73
124
  }
125
+
126
+ .desk-dock[data-side="left"] .desk-dock-item:hover:not(:disabled) {
127
+ transform: translateX(8px) scale(1.14);
128
+ margin: 5px 0;
129
+ }
130
+
131
+ .desk-dock[data-side="right"] .desk-dock-item:hover:not(:disabled) {
132
+ transform: translateX(-8px) scale(1.14);
133
+ margin: 5px 0;
134
+ }
74
135
  }
75
136
 
76
137
  .desk-dock-icon {
@@ -126,6 +187,22 @@
126
187
  background: var(--desk-muted);
127
188
  }
128
189
 
190
+ /* On a dock at the side the dot sits between the item and the edge of the screen, as it does under a bottom one. */
191
+ .desk-dock:not([data-side="bottom"]) .desk-dock-item[data-running]::after {
192
+ top: 50%;
193
+ bottom: auto;
194
+ margin: -2px 0 0;
195
+ }
196
+
197
+ .desk-dock[data-side="left"] .desk-dock-item[data-running]::after {
198
+ left: -6px;
199
+ }
200
+
201
+ .desk-dock[data-side="right"] .desk-dock-item[data-running]::after {
202
+ left: auto;
203
+ right: -6px;
204
+ }
205
+
129
206
  .desk-dock-item[data-focused]::after {
130
207
  background: var(--desk-accent);
131
208
  }
@@ -173,6 +250,21 @@
173
250
  transition: opacity .12s;
174
251
  }
175
252
 
253
+ .desk-dock:not([data-side="bottom"]) .desk-dock-tip {
254
+ bottom: auto;
255
+ top: 50%;
256
+ transform: translateY(-50%);
257
+ }
258
+
259
+ .desk-dock[data-side="left"] .desk-dock-tip {
260
+ left: calc(100% + 14px);
261
+ }
262
+
263
+ .desk-dock[data-side="right"] .desk-dock-tip {
264
+ left: auto;
265
+ right: calc(100% + 14px);
266
+ }
267
+
176
268
  .desk-dock-item:hover .desk-dock-tip,
177
269
  .desk-dock-item:focus-visible .desk-dock-tip {
178
270
  opacity: 1;
@@ -210,6 +302,30 @@
210
302
  }
211
303
  }
212
304
 
305
+ /* Beside a dock at the side, level with the stack it came from, opening toward the desk. */
306
+ .desk-dock:not([data-side="bottom"]) .desk-stack {
307
+ bottom: auto;
308
+ top: 0;
309
+ transform: none;
310
+ animation-name: desk-pop-aside;
311
+ }
312
+
313
+ .desk-dock[data-side="left"] .desk-stack {
314
+ left: calc(100% + 18px);
315
+ }
316
+
317
+ .desk-dock[data-side="right"] .desk-stack {
318
+ left: auto;
319
+ right: calc(100% + 18px);
320
+ }
321
+
322
+ @keyframes desk-pop-aside {
323
+ from {
324
+ opacity: 0;
325
+ scale: .98;
326
+ }
327
+ }
328
+
213
329
  .desk-stack-title {
214
330
  margin: 0 0 8px 4px;
215
331
  font-size: 11px;
@@ -275,3 +391,7 @@
275
391
  .desk-dock-item[data-desk-drop][data-over] {
276
392
  box-shadow: -6px 0 0 -2px var(--desk-accent);
277
393
  }
394
+
395
+ .desk-dock:not([data-side="bottom"]) .desk-dock-item[data-desk-drop][data-over] {
396
+ box-shadow: 0 -6px 0 -2px var(--desk-accent);
397
+ }
@@ -0,0 +1,257 @@
1
+ /*
2
+ * Looks: the platform chrome, on `data-look` at the root. The rest of desk.css is the Mac look, so
3
+ * this part only says how GNOME and Windows differ from it.
4
+ *
5
+ * A look changes chrome, never brand: nothing here sets --desk-accent or --desk-ground. Selectors
6
+ * carry the look AND the base rule's own specificity, so they win by coming later, not by !important.
7
+ */
8
+
9
+ /* ── GNOME (Adwaita) ── */
10
+
11
+ [data-look="gnome"] {
12
+ --desk-font: Cantarell, "Adwaita Sans", system-ui, sans-serif;
13
+ }
14
+
15
+ /*
16
+ * Title and note stacked in the middle of the bar, centred on the bar and not on the space left over,
17
+ * as a GNOME header bar does. The end of the bar keeps room for the controls and a little more.
18
+ */
19
+ [data-look="gnome"] .desk-titlebar {
20
+ position: relative;
21
+ min-height: 46px;
22
+ padding: 0 11px;
23
+ justify-content: flex-end;
24
+ }
25
+
26
+ [data-look="gnome"] .desk-titlebar-heading {
27
+ position: absolute;
28
+ left: 50%;
29
+ transform: translateX(-50%);
30
+ max-width: calc(100% - 2 * 120px);
31
+ display: flex;
32
+ flex-direction: column;
33
+ align-items: center;
34
+ text-align: center;
35
+ pointer-events: none;
36
+ }
37
+
38
+ [data-look="gnome"] .desk-titlebar-heading > * {
39
+ max-width: 100%;
40
+ }
41
+
42
+ [data-look="gnome"] .desk-title {
43
+ font-size: 13.5px;
44
+ font-weight: 700;
45
+ }
46
+
47
+ [data-look="gnome"] .desk-window-note {
48
+ margin: 0;
49
+ font-size: 11.5px;
50
+ }
51
+
52
+ [data-look="gnome"] .desk-window .desk-controls {
53
+ gap: 12px;
54
+ }
55
+
56
+ [data-look="gnome"] .desk-window .desk-control[data-control] {
57
+ width: 24px;
58
+ height: 24px;
59
+ background: color-mix(in srgb, var(--desk-ink) 10%, transparent);
60
+ color: var(--desk-ink);
61
+ }
62
+
63
+ [data-look="gnome"] .desk-window .desk-controls .desk-control[data-control]:hover {
64
+ background: color-mix(in srgb, var(--desk-ink) 16%, transparent);
65
+ }
66
+
67
+ [data-look="gnome"] .desk-window .desk-controls .desk-control[data-control]:active {
68
+ background: color-mix(in srgb, var(--desk-ink) 26%, transparent);
69
+ }
70
+
71
+ /* GNOME's marks are always there: symbolic icons, not colours. */
72
+ [data-look="gnome"] .desk-window .desk-control-glyph {
73
+ opacity: 1;
74
+ color: currentColor;
75
+ stroke-width: 1.3;
76
+ }
77
+
78
+ /* The key window is told apart by its shadow and its title, not by an accent border. */
79
+ [data-look="gnome"] .desk-window[data-focused] {
80
+ border-color: var(--desk-rule);
81
+ }
82
+
83
+ /* ── Windows 11 ── */
84
+
85
+ [data-look="windows"] {
86
+ --desk-font: "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
87
+ --desk-radius: 8px;
88
+ }
89
+
90
+ /* The controls run the full height of the bar, flush to its end. */
91
+ [data-look="windows"] .desk-titlebar {
92
+ min-height: 32px;
93
+ padding: 0 0 0 12px;
94
+ align-items: stretch;
95
+ background: var(--desk-surface);
96
+ }
97
+
98
+ [data-look="windows"] .desk-titlebar > * {
99
+ align-self: center;
100
+ }
101
+
102
+ [data-look="windows"] .desk-title {
103
+ flex: 1 1 auto;
104
+ font-size: 12px;
105
+ font-weight: 400;
106
+ }
107
+
108
+ [data-look="windows"] .desk-titlebar > .desk-controls {
109
+ align-self: stretch;
110
+ margin-left: var(--desk-space-2);
111
+ gap: 0;
112
+ }
113
+
114
+ [data-look="windows"] .desk-window .desk-control[data-control] {
115
+ width: 46px;
116
+ height: auto;
117
+ border-radius: 0;
118
+ background: transparent;
119
+ color: var(--desk-ink);
120
+ }
121
+
122
+ [data-look="windows"] .desk-window .desk-controls .desk-control[data-control]:hover {
123
+ background: color-mix(in srgb, var(--desk-ink) 8%, transparent);
124
+ }
125
+
126
+ [data-look="windows"] .desk-window .desk-controls .desk-control[data-control="close"]:hover {
127
+ background: #c42b1c;
128
+ color: #ffffff;
129
+ }
130
+
131
+ [data-look="windows"] .desk-window .desk-control-glyph {
132
+ width: 10px;
133
+ height: 10px;
134
+ opacity: 1;
135
+ color: currentColor;
136
+ stroke-width: 1;
137
+ stroke-linecap: butt;
138
+ }
139
+
140
+ [data-look="windows"] .desk-window:not([data-focused]) .desk-control-glyph {
141
+ opacity: .45;
142
+ }
143
+
144
+ [data-look="windows"] .desk-control:focus-visible {
145
+ outline-offset: -2px;
146
+ }
147
+
148
+ /*
149
+ * ── GNOME shell: the top bar and the dash are dark in either theme, and so is what opens from them ──
150
+ * The tokens are redefined on the bar and the dock themselves, so their menus and stacks, which sit
151
+ * inside them, come out dark too — as the shell's own menus do.
152
+ */
153
+ [data-look="gnome"] .desk-menubar,
154
+ [data-look="gnome"] .desk-dock {
155
+ --desk-ink: #ffffff;
156
+ --desk-muted: rgb(255 255 255 / 68%);
157
+ --desk-surface: #2a2a2a;
158
+ --desk-rule: rgb(255 255 255 / 10%);
159
+ --desk-dock-icon: transparent;
160
+ }
161
+
162
+ [data-look="gnome"] .desk-menubar {
163
+ height: 32px;
164
+ --desk-dock-glass: rgb(0 0 0 / 94%);
165
+ border-bottom: 0;
166
+ font-weight: 600;
167
+ }
168
+
169
+ [data-look="gnome"] .desk-menubar-title {
170
+ border-radius: 999px;
171
+ }
172
+
173
+ [data-look="gnome"] .desk-menu {
174
+ border-radius: 12px;
175
+ padding: 6px;
176
+ }
177
+
178
+ [data-look="gnome"] .desk-menu-item {
179
+ border-radius: 6px;
180
+ font-weight: 400;
181
+ }
182
+
183
+ [data-look="gnome"] .desk-menu-item[data-active],
184
+ [data-look="windows"] .desk-menu-item[data-active] {
185
+ background: color-mix(in srgb, var(--desk-ink) 10%, transparent);
186
+ color: var(--desk-ink);
187
+ }
188
+
189
+ [data-look="gnome"] .desk-dock {
190
+ --desk-dock-glass: rgb(24 24 24 / 86%);
191
+ border-radius: 20px;
192
+ }
193
+
194
+ [data-look="gnome"] .desk-dock .desk-dock-item {
195
+ border: 0;
196
+ border-radius: 12px;
197
+ }
198
+
199
+ /* No magnification: GNOME's dash lights an item up rather than growing it. */
200
+ [data-look="gnome"] .desk-dock .desk-dock-item:hover:not(:disabled),
201
+ [data-look="windows"] .desk-dock .desk-dock-item:hover:not(:disabled) {
202
+ transform: none;
203
+ margin: 0;
204
+ background: color-mix(in srgb, var(--desk-ink) 12%, transparent);
205
+ }
206
+
207
+ /* ── Windows: a flat bar, taskbar tiles, and a pill that widens for the key window ── */
208
+
209
+ [data-look="windows"] .desk-menubar {
210
+ background: var(--desk-surface);
211
+ backdrop-filter: none;
212
+ -webkit-backdrop-filter: none;
213
+ }
214
+
215
+ [data-look="windows"] .desk-menubar-title,
216
+ [data-look="windows"] .desk-menu-item {
217
+ border-radius: 4px;
218
+ }
219
+
220
+ [data-look="windows"] .desk-menu {
221
+ border-radius: 8px;
222
+ padding: 4px;
223
+ }
224
+
225
+ [data-look="windows"] .desk-dock {
226
+ border-radius: 8px;
227
+ }
228
+
229
+ [data-look="windows"] .desk-dock .desk-dock-item {
230
+ border: 0;
231
+ border-radius: 4px;
232
+ background: transparent;
233
+ }
234
+
235
+ [data-look="windows"] .desk-dock .desk-dock-item[data-running]::after {
236
+ width: 6px;
237
+ height: 3px;
238
+ margin-left: -3px;
239
+ border-radius: 2px;
240
+ bottom: -4px;
241
+ }
242
+
243
+ [data-look="windows"] .desk-dock[data-side="bottom"] .desk-dock-item[data-focused]::after {
244
+ width: 16px;
245
+ margin-left: -8px;
246
+ }
247
+
248
+ [data-look="windows"] .desk-dock:not([data-side="bottom"]) .desk-dock-item[data-running]::after {
249
+ width: 3px;
250
+ height: 6px;
251
+ margin: -3px 0 0;
252
+ }
253
+
254
+ [data-look="windows"] .desk-dock:not([data-side="bottom"]) .desk-dock-item[data-focused]::after {
255
+ height: 16px;
256
+ margin-top: -8px;
257
+ }
@@ -46,11 +46,15 @@
46
46
  --desk-on-accent: #ffffff;
47
47
  --desk-ease: cubic-bezier(.32, .72, 0, 1);
48
48
  --desk-dock-size: 48px;
49
+ /* How far an overlay dock stands off the edge of the desk. */
50
+ --desk-dock-offset: 12px;
51
+ /* The room an overlay dock takes from the stage beside it: the offset, the dock, its border and padding. */
52
+ --desk-dock-room: calc(var(--desk-dock-offset) + var(--desk-dock-size) + 16px);
49
53
  --desk-dock-glass: rgb(255 255 255 / 72%);
50
54
  --desk-dock-icon: linear-gradient(160deg, #ffffff, #eceff5);
51
55
  --desk-badge: #ef4444;
52
56
  --desk-badge-ink: #ffffff;
53
- /* Room the stage leaves at the bottom for an overlay dock. */
57
+ /* Room the stage leaves at the bottom for something of the app's own. An overlay dock makes its own. */
54
58
  --desk-inset-bottom: 0px;
55
59
  }
56
60
 
package/src/fx/fx.ts ADDED
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Sounds for an app on the desk: `attention`, `ready` and `failed`, and any an app adds.
3
+ *
4
+ * WHEN to make one is the app's decision and the hard part; see HIG.md, "Sound". This is the
5
+ * instrument: one audio context, opened on the first press, a repeat of the same sound inside a
6
+ * short window heard once, and a mute this browser remembers.
7
+ */
8
+ import { SCORES, scored, type BuiltInSound, type Sound } from './sounds.js'
9
+
10
+ export interface FxOptions<Extra extends string> {
11
+ /** Sounds of the app's own, or replacements for the built-ins. */
12
+ readonly sounds?: Readonly<Record<Extra, Sound>>
13
+ /** Where the mute is kept. Two apps on one origin that should not share a switch need two keys. */
14
+ readonly storageKey?: string
15
+ /** A second request for the same sound inside this many milliseconds is the same event. */
16
+ readonly coalesceMs?: number
17
+ /** Master gain over every sound. */
18
+ readonly volume?: number
19
+ }
20
+
21
+ export interface Fx<Name extends string> {
22
+ /** Plays now if it can; whether it did. Silent when muted, repeated, or before any press. */
23
+ play(name: Name, nowMs?: number): boolean
24
+ /**
25
+ * Opens audio on the first press. A browser refuses sound on a page nobody has touched, and a
26
+ * sound arrives unasked, long after the last click: the press has to be taken while it happens.
27
+ * Returns a teardown.
28
+ */
29
+ unlock(): () => void
30
+ muted(): boolean
31
+ setMuted(muted: boolean): void
32
+ /** Told when the mute changes, here or in another tab. */
33
+ subscribe(listener: () => void): () => void
34
+ /** Schedules a sound into any context, an offline one included, for measuring. */
35
+ render(ctx: BaseAudioContext, name: Name, out: AudioNode): void
36
+ }
37
+
38
+ type Ctor = typeof AudioContext
39
+
40
+ function audioContextClass(): Ctor | null {
41
+ if (typeof window === 'undefined') return null
42
+ return window.AudioContext ?? (window as { webkitAudioContext?: Ctor }).webkitAudioContext ?? null
43
+ }
44
+
45
+ export function createFx<Extra extends string = never>(
46
+ options: FxOptions<Extra> = {},
47
+ ): Fx<BuiltInSound | Extra> {
48
+ type Name = BuiltInSound | Extra
49
+ const { storageKey = 'desk-fx-muted', coalesceMs = 1500, volume = 0.8 } = options
50
+ const builtIn = Object.fromEntries(Object.entries(SCORES).map(([name, score]) => [name, scored(score)]))
51
+ const sounds = { ...builtIn, ...options.sounds } as Record<Name, Sound>
52
+ const lastPlayed = new Map<string, number>()
53
+ const listeners = new Set<() => void>()
54
+ let audio: AudioContext | null = null
55
+
56
+ const context = (): AudioContext | null => {
57
+ if (audio) return audio
58
+ const Class = audioContextClass()
59
+ if (!Class) return null
60
+ try {
61
+ audio = new Class()
62
+ } catch {
63
+ return null
64
+ }
65
+ return audio
66
+ }
67
+
68
+ const muted = (): boolean => {
69
+ try {
70
+ return localStorage.getItem(storageKey) === 'yes'
71
+ } catch {
72
+ return false
73
+ }
74
+ }
75
+
76
+ const render = (ctx: BaseAudioContext, name: Name, out: AudioNode): void => {
77
+ const master = ctx.createGain()
78
+ const at = ctx.currentTime + 0.01
79
+ master.gain.setValueAtTime(volume, at)
80
+ master.connect(out)
81
+ sounds[name](ctx, master, at)
82
+ }
83
+
84
+ return {
85
+ play(name, nowMs = Date.now()) {
86
+ if (muted()) return false
87
+ const previous = lastPlayed.get(name)
88
+ if (previous !== undefined && nowMs - previous < coalesceMs) return false
89
+ const ctx = context()
90
+ // Never resumed from here: outside a press the promise may never settle, and a sound that
91
+ // lands a click late belongs to the wrong moment.
92
+ if (!ctx || ctx.state !== 'running') return false
93
+ lastPlayed.set(name, nowMs)
94
+ try {
95
+ render(ctx, name, ctx.destination)
96
+ return true
97
+ } catch {
98
+ return false
99
+ }
100
+ },
101
+
102
+ unlock() {
103
+ if (typeof window === 'undefined') return () => undefined
104
+ const stop = (): void => {
105
+ window.removeEventListener('pointerdown', open)
106
+ window.removeEventListener('keydown', open)
107
+ }
108
+ function open(): void {
109
+ stop()
110
+ const ctx = context()
111
+ // Inside the handler, where the browser is willing to listen.
112
+ if (ctx && ctx.state !== 'running') void ctx.resume().catch(() => undefined)
113
+ }
114
+ window.addEventListener('pointerdown', open)
115
+ window.addEventListener('keydown', open)
116
+ return stop
117
+ },
118
+
119
+ muted,
120
+
121
+ setMuted(next) {
122
+ try {
123
+ if (next) localStorage.setItem(storageKey, 'yes')
124
+ else localStorage.removeItem(storageKey)
125
+ } catch {
126
+ // Blocked site data: sounds stay on, which is the default.
127
+ }
128
+ listeners.forEach((listener) => listener())
129
+ },
130
+
131
+ subscribe(listener) {
132
+ listeners.add(listener)
133
+ const fromAnotherTab = (event: StorageEvent): void => {
134
+ if (event.key === storageKey) listener()
135
+ }
136
+ if (typeof window !== 'undefined') window.addEventListener('storage', fromAnotherTab)
137
+ return () => {
138
+ listeners.delete(listener)
139
+ if (typeof window !== 'undefined') window.removeEventListener('storage', fromAnotherTab)
140
+ }
141
+ },
142
+
143
+ render,
144
+ }
145
+ }
@@ -0,0 +1,6 @@
1
+ export { createFx } from './fx.js'
2
+ export type { Fx, FxOptions } from './fx.js'
3
+ export { BELL, GROWL, SCORES, scored } from './sounds.js'
4
+ export type { BuiltInSound, Note, Overtone, Score, Sound, Voice } from './sounds.js'
5
+ export { measureLoudness } from './measure.js'
6
+ export type { Loudness, MeasureOptions } from './measure.js'
@@ -0,0 +1,70 @@
1
+ /*
2
+ * How loud a sound is to a listener, measured rather than judged by ear: rendered offline,
3
+ * K-weighted (ITU-R BS.1770: a +4 dB shelf above ~1.7 kHz and a high-pass near 38 Hz, roughly the
4
+ * ear's sensitivity), and scored by its loudest 400 ms — momentary loudness, the right figure for
5
+ * a sound shorter than the gated programme measure. Browser only: it needs OfflineAudioContext.
6
+ */
7
+ import type { Fx } from './fx.js'
8
+
9
+ export interface Loudness {
10
+ /** Momentary loudness, LUFS. */
11
+ readonly lufs: number
12
+ /** Sample peak before weighting, dBFS: how close it comes to clipping. */
13
+ readonly peakDb: number
14
+ }
15
+
16
+ export interface MeasureOptions {
17
+ /**
18
+ * `laptop` adds a 4th-order high-pass at 200 Hz, about where small speakers give up. Level for
19
+ * that one: it is what most people have.
20
+ */
21
+ readonly speaker?: 'full' | 'laptop'
22
+ /** Seconds rendered; longer than the sound. */
23
+ readonly seconds?: number
24
+ }
25
+
26
+ const RATE = 48000
27
+
28
+ // Web Audio reads a pass filter's Q in dB, not linear.
29
+ const dB = (q: number): number => 20 * Math.log10(q)
30
+
31
+ export async function measureLoudness<Name extends string>(
32
+ fx: Fx<Name>,
33
+ name: Name,
34
+ { speaker = 'full', seconds = 3 }: MeasureOptions = {},
35
+ ): Promise<Loudness> {
36
+ const weighted = new OfflineAudioContext(1, RATE * seconds, RATE)
37
+ const shelf = new BiquadFilterNode(weighted, { type: 'highshelf', frequency: 1682, gain: 4 })
38
+ const rest = [
39
+ new BiquadFilterNode(weighted, { type: 'highpass', frequency: 38, Q: dB(0.5) }),
40
+ ...(speaker === 'laptop'
41
+ ? [
42
+ new BiquadFilterNode(weighted, { type: 'highpass', frequency: 200, Q: dB(0.541) }),
43
+ new BiquadFilterNode(weighted, { type: 'highpass', frequency: 200, Q: dB(1.307) }),
44
+ ]
45
+ : []),
46
+ ]
47
+ const last = rest.reduce<AudioNode>((from, to) => (from.connect(to), to), shelf)
48
+ last.connect(weighted.destination)
49
+ fx.render(weighted, name, shelf)
50
+
51
+ const plain = new OfflineAudioContext(1, RATE * seconds, RATE)
52
+ fx.render(plain, name, plain.destination)
53
+
54
+ const [w, p] = await Promise.all([weighted.startRendering(), plain.startRendering()])
55
+ return { lufs: momentary(w.getChannelData(0)), peakDb: 20 * Math.log10(peak(p.getChannelData(0))) }
56
+ }
57
+
58
+ function momentary(x: Float32Array): number {
59
+ const window = RATE * 0.4
60
+ const hop = RATE * 0.1
61
+ const starts = Array.from({ length: Math.max(0, Math.floor((x.length - window) / hop) + 1) }, (_, i) => i * hop)
62
+ const loudest = starts.reduce((best, start) => {
63
+ let sum = 0
64
+ for (let j = start; j < start + window; j++) sum += (x[j] ?? 0) ** 2
65
+ return Math.max(best, sum / window)
66
+ }, 0)
67
+ return -0.691 + 10 * Math.log10(loudest)
68
+ }
69
+
70
+ const peak = (x: Float32Array): number => x.reduce((m, v) => Math.max(m, Math.abs(v)), 0)