@cngx/common 0.1.0-rc.4 → 0.1.0-rc.5
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/audio/README.md +180 -0
- package/fesm2022/cngx-common-audio.mjs +1205 -0
- package/fesm2022/cngx-common-audio.mjs.map +1 -0
- package/fesm2022/cngx-common-chart.mjs +2 -2
- package/fesm2022/cngx-common-chart.mjs.map +1 -1
- package/fesm2022/cngx-common-data.mjs +307 -5
- package/fesm2022/cngx-common-data.mjs.map +1 -1
- package/fesm2022/cngx-common-interactive.mjs +31 -6
- package/fesm2022/cngx-common-interactive.mjs.map +1 -1
- package/fesm2022/cngx-common.mjs +1 -1
- package/package.json +5 -1
- package/types/cngx-common-audio.d.ts +736 -0
- package/types/cngx-common-data.d.ts +191 -3
- package/types/cngx-common-interactive.d.ts +14 -0
package/audio/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# @cngx/common/audio
|
|
2
|
+
|
|
3
|
+
Signal-first audio-feedback system. A thin directive layer over a small Web Audio adapter: earcons synthesised from `OscillatorNode` (zero audio assets), a single shared `AudioContext` behind the engine, and the `provideCngxAudio(...)` aggregator. It is the fourth sense - audition - alongside the sibling feedback primitives `CngxRipple` (visual), `CngxPressable` (tactile), and `CngxSpeak` (spoken).
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
- **`[cngxAudio]`** - an attribute directive that maps DOM events to earcons with the `event:earcon` grammar (`'click:tap'`). No state, no config; drop it on any element.
|
|
8
|
+
- **`injectCngxAudio()`** - a typed handle to the shared engine for programmatic control (`play`, `register`, `setMuted`, `setVolume`) plus reactive `muted` / `volume` / `status` / `lastPlayed` signals.
|
|
9
|
+
- **`provideCngxAudio(...)`** - the app-level configuration aggregator with `with*` features.
|
|
10
|
+
- **Six built-in earcons**, all oscillator-generated: `tap`, `success`, `error`, `warning`, `notification`, `complete`.
|
|
11
|
+
|
|
12
|
+
## When you reach for it
|
|
13
|
+
|
|
14
|
+
You want a state change to be *heard*, not just seen. Sound is interaction design first (analogous to a ripple) and accessibility reinforcement second - a non-visual channel that confirms an action landed. Typical reasons:
|
|
15
|
+
|
|
16
|
+
- A tactile-feeling click on buttons, toggles, and chips.
|
|
17
|
+
- Confirming an async outcome audibly (a rising chime on save, a low buzz on failure) so the user does not have to watch a spinner.
|
|
18
|
+
- Reinforcing status transitions for users who benefit from a second sensory channel.
|
|
19
|
+
- Branded UI sounds (a "sent" swoosh, a "received" ping) without shipping any `.wav`/`.mp3` weight.
|
|
20
|
+
|
|
21
|
+
You do **not** reach for it for background music, long samples, or spatial audio - it synthesises short earcons, nothing more. For text-to-speech, use `CngxSpeak`; the two compose freely on one element.
|
|
22
|
+
|
|
23
|
+
## Why it is built this way
|
|
24
|
+
|
|
25
|
+
- **Web Audio, not the Vibration API.** Vibration is effectively dead (Apple never shipped it, Firefox removed it, Chrome restricts it). Web Audio is universal, needs no permission prompt, and both plays and synthesises.
|
|
26
|
+
- **Zero asset weight.** Earcons are oscillator sequences, so the published bundle carries 0 KB of audio. Need real samples? Swap the tone generator (see below) - the engine does not change.
|
|
27
|
+
- **One shared `AudioContext`.** Browsers cap the number of contexts. The engine owns exactly one, lazily created on the first allowed play, shared by every directive on the page through the injected handle.
|
|
28
|
+
- **Autoplay-policy-safe.** Browsers block audio until a user gesture. The engine arms itself on the first `pointerdown` / `keydown` / `touchstart` and resumes the context only then - no console warnings, no failed playback.
|
|
29
|
+
- **Reduced-motion aware.** Sound and motion are both sensory stimulation, so `prefers-reduced-motion: reduce` mutes audio by default. Opt out with `withRespectReducedMotion(false)`.
|
|
30
|
+
|
|
31
|
+
## Mental model
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
[cngxAudio] directive ──▶ injectCngxAudio() handle ──▶ shared engine (root)
|
|
35
|
+
│
|
|
36
|
+
┌───────────────────────────────┼───────────────────────────┐
|
|
37
|
+
▼ ▼ ▼
|
|
38
|
+
autoplay gate tone generator debouncer
|
|
39
|
+
(arms on first gesture) (OscillatorNode synthesis) (per-name window)
|
|
40
|
+
│
|
|
41
|
+
one AudioContext
|
|
42
|
+
+ master gain (volume)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Every `play(name)` runs through one central gate in the engine, so the four mute conditions live in a single place rather than scattered across directives:
|
|
46
|
+
|
|
47
|
+
1. **`prefers-reduced-motion: reduce`** is active (and `respectReducedMotion` is on).
|
|
48
|
+
2. The **autoplay gate is not armed** yet (no user gesture has happened).
|
|
49
|
+
3. The engine is **globally muted** (`engine.muted()`).
|
|
50
|
+
4. Only for `[cngxAudio]`: the element carries **`[audioDisabled]="true"`** (enforced in the directive, since it is per-element).
|
|
51
|
+
|
|
52
|
+
If none block, the same-name **debouncer** suppresses repeats inside a 100 ms window, then the earcon plays and `lastPlayed` advances.
|
|
53
|
+
|
|
54
|
+
## Quick start
|
|
55
|
+
|
|
56
|
+
Bind an event to an earcon - the first click both arms audio and plays:
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<button type="button" [cngxAudio]="'click:tap'">Save</button>
|
|
60
|
+
<a [cngxAudio]="'pointerenter:notification, click:complete'">Open</a>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { CngxAudio } from '@cngx/common/audio';
|
|
65
|
+
|
|
66
|
+
@Component({ imports: [CngxAudio], /* ... */ })
|
|
67
|
+
export class Toolbar {}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The grammar is `event:earcon`, comma-separated. Bindable DOM events: `click`, `focus`, `blur`, `pointerenter`, `pointerleave`, `submit`, `change`, `input`.
|
|
71
|
+
|
|
72
|
+
### Programmatic control
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { injectCngxAudio } from '@cngx/common/audio';
|
|
76
|
+
|
|
77
|
+
@Component({ /* ... */ })
|
|
78
|
+
export class MuteToggle {
|
|
79
|
+
private readonly audio = injectCngxAudio();
|
|
80
|
+
protected readonly muted = this.audio.muted; // Signal<boolean>
|
|
81
|
+
|
|
82
|
+
protected toggle(): void {
|
|
83
|
+
this.audio.setMuted(!this.muted());
|
|
84
|
+
}
|
|
85
|
+
protected save(): void {
|
|
86
|
+
// ... your save logic
|
|
87
|
+
this.audio.play('success');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### App-level configuration
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import {
|
|
96
|
+
provideCngxAudio,
|
|
97
|
+
withVolume,
|
|
98
|
+
withMuted,
|
|
99
|
+
withEarcons,
|
|
100
|
+
withRespectReducedMotion,
|
|
101
|
+
withDebounceMs,
|
|
102
|
+
} from '@cngx/common/audio';
|
|
103
|
+
|
|
104
|
+
bootstrapApplication(App, {
|
|
105
|
+
providers: [
|
|
106
|
+
provideCngxAudio(
|
|
107
|
+
withVolume(0.6),
|
|
108
|
+
withDebounceMs(120),
|
|
109
|
+
withEarcons({
|
|
110
|
+
send: { sequence: [{ freq: 880, duration: 60 }, { freq: 1180, duration: 90 }] },
|
|
111
|
+
}),
|
|
112
|
+
),
|
|
113
|
+
],
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Now `[cngxAudio]="'click:send'"` plays your branded earcon everywhere.
|
|
118
|
+
|
|
119
|
+
### Runtime registration
|
|
120
|
+
|
|
121
|
+
When earcons are scope-specific (a widget that brings its own sounds), register them on the shared engine at runtime instead of at bootstrap:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
private readonly audio = injectCngxAudio();
|
|
125
|
+
constructor() {
|
|
126
|
+
this.audio.register('receive', { sequence: [{ freq: 520, duration: 90 }] });
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Earcon shape
|
|
131
|
+
|
|
132
|
+
An earcon is a sequence of tone steps played back-to-back:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
interface ToneStep {
|
|
136
|
+
freq: number; // Hz
|
|
137
|
+
duration: number; // ms
|
|
138
|
+
type?: OscillatorType; // 'sine' (default) | 'square' | 'sawtooth' | 'triangle'
|
|
139
|
+
gain?: number; // [0, 1] peak, default 0.2
|
|
140
|
+
delay?: number; // ms of silence before this step
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each tone carries a 5 ms attack and 30 ms release envelope so `start()`/`stop()` never produce an audible click.
|
|
145
|
+
|
|
146
|
+
## Swapping the synthesis (samples instead of oscillators)
|
|
147
|
+
|
|
148
|
+
The engine and the tone generator are boundary-layer DI factories. To play real audio files instead of oscillators, override the tone-generator factory - the engine, directives, and earcon names stay untouched:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import { CNGX_AUDIO_TONE_GENERATOR_FACTORY } from '@cngx/common/audio';
|
|
152
|
+
|
|
153
|
+
providers: [
|
|
154
|
+
{ provide: CNGX_AUDIO_TONE_GENERATOR_FACTORY, useValue: mySampleBackedFactory },
|
|
155
|
+
];
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`CNGX_AUDIO_ENGINE_FACTORY` is the matching seam for wrapping the engine with telemetry or audit logging.
|
|
159
|
+
|
|
160
|
+
## Best practices
|
|
161
|
+
|
|
162
|
+
- **Bind sound to the gesture, not to a lifecycle effect.** `[cngxAudio]="'click:tap'"` is correct; playing inside an `effect()` that reacts to route or data changes fights the autoplay policy and surprises users. For status-driven sound, prefer the Phase 2 `[cngxAudioStatus]` bridge.
|
|
163
|
+
- **Keep earcons short (under ~300 ms total).** Earcons confirm, they do not entertain. Long sequences feel laggy and collide with the debounce window.
|
|
164
|
+
- **Do not mute by removing the directive.** Use `setMuted(true)` or `[audioDisabled]`; the engine keeps one context and one mute state for the whole page.
|
|
165
|
+
- **Respect the user.** Leave `respectReducedMotion` on unless you have a specific reason; expose a mute control somewhere reachable. Audio is opt-out for the user, never a surprise.
|
|
166
|
+
- **Volume is a `[0, 1]` scalar.** `setVolume` clamps; `[audioVolume]` on `[cngxAudio]` scales one element without touching the shared master volume.
|
|
167
|
+
- **Earcon names are English by default.** Register branded/localised names via `withEarcons` or `register`; the built-ins stay English.
|
|
168
|
+
- **One engine per app.** `injectCngxAudio()` returns the shared root instance. Only re-provide it in `viewProviders` if you deliberately want an isolated context for a subtree.
|
|
169
|
+
|
|
170
|
+
## Testing
|
|
171
|
+
|
|
172
|
+
Audio is unsniffable in headless/jsdom environments, so assert through the engine, never by listening:
|
|
173
|
+
|
|
174
|
+
- Unit tests use `createAudioContextMock()` from `@cngx/testing` - a pure stub with assertable `connect` / `start` / `stop` records; no real `AudioContext` is required.
|
|
175
|
+
- Read `engine.lastPlayed()` to confirm a play was accepted and `engine.status()` for the context lifecycle.
|
|
176
|
+
- End-to-end tests route through the same `lastPlayed()` signal (exposed on `window` under a demo guard) rather than sniffing output.
|
|
177
|
+
|
|
178
|
+
## Status
|
|
179
|
+
|
|
180
|
+
Phase 1 ships the engine, the `[cngxAudio]` event-mode directive, the six earcons, `injectCngxAudio`, and `provideCngxAudio`. Phase 2 adds the status-bridge (`[cngxAudioStatus]`), zone (`[cngxAudioZone]`), and pitch (`[cngxAudioPitch]`) directives. Chart sonification is a later release. The public API is stable for adoption; the internal factory composition may evolve.
|