@swisper.ai/web-chat-ui 0.3.3 → 0.4.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.
- package/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts +10 -1
- package/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts +85 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts +20 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts +45 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box.d.ts +24 -1
- package/dist/components/chat-prompt-box/chat-prompt-box.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/chat-prompt-box.stories.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts +31 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts +17 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts +29 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts +12 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts +44 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts +17 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts +14 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts +11 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts +18 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts +18 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts +14 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts +11 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/index.d.ts +7 -0
- package/dist/components/chat-prompt-box/components/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/index.d.ts +5 -1
- package/dist/components/chat-prompt-box/index.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/use-voice-motion.d.ts +22 -0
- package/dist/components/chat-prompt-box/use-voice-motion.d.ts.map +1 -0
- package/dist/index.css +1 -1
- package/dist/index.es.js +2285 -1443
- package/dist/index.es.js.map +1 -1
- package/package.json +2 -2
- package/src/components/chat-prompt-box/chat-prompt-box-context.tsx +35 -1
- package/src/components/chat-prompt-box/chat-prompt-box-voice-motion.ts +436 -0
- package/src/components/chat-prompt-box/chat-prompt-box-voice.stories.tsx +243 -0
- package/src/components/chat-prompt-box/chat-prompt-box-voice.tsx +191 -0
- package/src/components/chat-prompt-box/chat-prompt-box.json +66 -7
- package/src/components/chat-prompt-box/chat-prompt-box.stories.tsx +16 -0
- package/src/components/chat-prompt-box/chat-prompt-box.tsx +131 -77
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.tsx +155 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.tsx +80 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.tsx +107 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.tsx +101 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.tsx +177 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.tsx +283 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.tsx +50 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.tsx +46 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.ts +2 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.tsx +149 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.tsx +55 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.tsx +50 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.tsx +46 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.ts +2 -0
- package/src/components/chat-prompt-box/components/index.ts +6 -0
- package/src/components/chat-prompt-box/index.ts +23 -0
- package/src/components/chat-prompt-box/use-voice-motion.ts +184 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swisper.ai/web-chat-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "React components for the Swisper chat: messages, composer and cards",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"@radix-ui/react-slot": "^1.1.0",
|
|
65
65
|
"@shadcn/react": "^0.3.1",
|
|
66
66
|
"@swisper.ai/design-system": "0.1.2",
|
|
67
|
-
"@swisper.ai/icons": "0.0
|
|
67
|
+
"@swisper.ai/icons": "0.1.0",
|
|
68
68
|
"@swisper.ai/tailwind-config": "0.0.8",
|
|
69
69
|
"@swisper.ai/ui": "0.5.0",
|
|
70
70
|
"@swisper.ai/web-core": "0.0.8",
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { createContext, useCallback, useContext, useMemo, useRef, type ReactNode } from 'react';
|
|
2
2
|
|
|
3
|
+
import type { VoiceMotionRegistry } from './use-voice-motion';
|
|
4
|
+
|
|
5
|
+
export type ChatPromptBoxMode =
|
|
6
|
+
| 'text'
|
|
7
|
+
| 'connecting'
|
|
8
|
+
| 'listening'
|
|
9
|
+
| 'user-speaking'
|
|
10
|
+
| 'thinking'
|
|
11
|
+
| 'answering'
|
|
12
|
+
| 'mic-muted'
|
|
13
|
+
| 'reconnecting';
|
|
14
|
+
|
|
3
15
|
interface ChatPromptBoxContextValue {
|
|
16
|
+
mode: ChatPromptBoxMode;
|
|
17
|
+
/** The box's voice clock; the voice parts register with it to move with the box. */
|
|
18
|
+
voiceMotion: VoiceMotionRegistry;
|
|
4
19
|
reportInputHeight: (height: number) => void;
|
|
5
20
|
resetCompact: () => void;
|
|
6
21
|
registerFocusInput: (focus: () => void) => void;
|
|
@@ -20,14 +35,23 @@ export function useChatPromptBoxContext() {
|
|
|
20
35
|
return context;
|
|
21
36
|
}
|
|
22
37
|
|
|
38
|
+
/** The box's context, or `null` outside a `ChatPromptBox`. For parts that also work on their own. */
|
|
39
|
+
export function useOptionalChatPromptBoxContext() {
|
|
40
|
+
return useContext(ChatPromptBoxContext);
|
|
41
|
+
}
|
|
42
|
+
|
|
23
43
|
interface ChatPromptBoxContextProviderProps {
|
|
24
44
|
children: ReactNode;
|
|
45
|
+
mode: ChatPromptBoxMode;
|
|
46
|
+
voiceMotion: VoiceMotionRegistry;
|
|
25
47
|
reportInputHeight: (height: number) => void;
|
|
26
48
|
resetCompact: () => void;
|
|
27
49
|
}
|
|
28
50
|
|
|
29
51
|
export function ChatPromptBoxContextProvider({
|
|
30
52
|
children,
|
|
53
|
+
mode,
|
|
54
|
+
voiceMotion,
|
|
31
55
|
reportInputHeight,
|
|
32
56
|
resetCompact,
|
|
33
57
|
}: ChatPromptBoxContextProviderProps) {
|
|
@@ -47,13 +71,23 @@ export function ChatPromptBoxContextProvider({
|
|
|
47
71
|
|
|
48
72
|
const contextValue = useMemo(
|
|
49
73
|
() => ({
|
|
74
|
+
mode,
|
|
75
|
+
voiceMotion,
|
|
50
76
|
reportInputHeight,
|
|
51
77
|
resetCompact,
|
|
52
78
|
registerFocusInput,
|
|
53
79
|
unregisterFocusInput,
|
|
54
80
|
focusInput,
|
|
55
81
|
}),
|
|
56
|
-
[
|
|
82
|
+
[
|
|
83
|
+
mode,
|
|
84
|
+
voiceMotion,
|
|
85
|
+
reportInputHeight,
|
|
86
|
+
resetCompact,
|
|
87
|
+
registerFocusInput,
|
|
88
|
+
unregisterFocusInput,
|
|
89
|
+
focusInput,
|
|
90
|
+
],
|
|
57
91
|
);
|
|
58
92
|
|
|
59
93
|
return (
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Voice-mode motion, from the Echo DS spec (Prompt Box page, section "Voice motion", and "Voice
|
|
3
|
+
* mode — behaviour spec"). No React and no DOM: the formulas, and an engine that steps them on one
|
|
4
|
+
* clock so the border, the light and the voice button's bars share it. `use-voice-motion.ts` runs
|
|
5
|
+
* it every frame and writes the results to CSS variables.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ChatPromptBoxMode } from './chat-prompt-box-context';
|
|
9
|
+
|
|
10
|
+
/** `pulse`: the grey breath. `travel`: two lights round the outline. `beat`: the Thinking surge. */
|
|
11
|
+
export type VoiceBorderKind = 'pulse' | 'travel' | 'beat';
|
|
12
|
+
|
|
13
|
+
/** The light behind the box, or `none`. */
|
|
14
|
+
export type VoiceLightKind = 'none' | 'listening' | 'user-speaking' | 'thinking' | 'speaking';
|
|
15
|
+
|
|
16
|
+
/** The voice button's bars. `voice-starts` is `listening` with the two kicks of the sound on entry. */
|
|
17
|
+
export type VoiceBarsKind = 'still' | 'listening' | 'voice-starts' | 'thinking' | 'voice' | 'muted';
|
|
18
|
+
|
|
19
|
+
export interface VoiceMotionKinds {
|
|
20
|
+
border: VoiceBorderKind;
|
|
21
|
+
light: VoiceLightKind;
|
|
22
|
+
bars: VoiceBarsKind;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type VoiceMode = Exclude<ChatPromptBoxMode, 'text'>;
|
|
26
|
+
|
|
27
|
+
/** What each part does in each mode. */
|
|
28
|
+
export const VOICE_MOTION_BY_MODE: Record<VoiceMode, VoiceMotionKinds> = {
|
|
29
|
+
connecting: { border: 'pulse', light: 'none', bars: 'still' },
|
|
30
|
+
reconnecting: { border: 'pulse', light: 'none', bars: 'still' },
|
|
31
|
+
listening: { border: 'travel', light: 'listening', bars: 'listening' },
|
|
32
|
+
'user-speaking': { border: 'travel', light: 'user-speaking', bars: 'voice' },
|
|
33
|
+
thinking: { border: 'beat', light: 'thinking', bars: 'thinking' },
|
|
34
|
+
answering: { border: 'travel', light: 'speaking', bars: 'voice' },
|
|
35
|
+
'mic-muted': { border: 'pulse', light: 'none', bars: 'muted' },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The parts for `mode`, or `null` with voice off. Reaching Listening from Connecting plays the
|
|
40
|
+
* "Voice starts" kicks on the bars.
|
|
41
|
+
*/
|
|
42
|
+
export function voiceMotionKindsFor(
|
|
43
|
+
mode: ChatPromptBoxMode,
|
|
44
|
+
previous: ChatPromptBoxMode,
|
|
45
|
+
): VoiceMotionKinds | null {
|
|
46
|
+
if (mode === 'text') return null;
|
|
47
|
+
const kinds = VOICE_MOTION_BY_MODE[mode];
|
|
48
|
+
if (mode === 'listening' && previous === 'connecting') {
|
|
49
|
+
return { ...kinds, bars: 'voice-starts' };
|
|
50
|
+
}
|
|
51
|
+
return kinds;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/* ---------------------------------------------------------------------------------------------
|
|
55
|
+
* Constants
|
|
56
|
+
* ------------------------------------------------------------------------------------------- */
|
|
57
|
+
|
|
58
|
+
/** The reasoning shield's beat, and the surge at its start. */
|
|
59
|
+
const BEAT = 1.1;
|
|
60
|
+
const SURGE = 0.6;
|
|
61
|
+
|
|
62
|
+
/** Handover between states: the border's pace and the pulse/lights crossfade. */
|
|
63
|
+
const BORDER_TAU = 0.45;
|
|
64
|
+
/** The light's level: up fast, down slowly, within a state and between states. */
|
|
65
|
+
const LIGHT_RISE_TAU = 0.08;
|
|
66
|
+
const LIGHT_FALL_TAU = 0.6;
|
|
67
|
+
|
|
68
|
+
/** Where the border lights rest, as a share of the outline (Echo DS `_Voice-border` frames). */
|
|
69
|
+
const SPOT_REST_TRAVEL = 0.083;
|
|
70
|
+
const SPOT_REST_BEAT = 0.208;
|
|
71
|
+
|
|
72
|
+
const TRAVEL_STRENGTH = 0.73;
|
|
73
|
+
|
|
74
|
+
/** The light's height at level 1. */
|
|
75
|
+
export const VOICE_LIGHT_MAX_RISE = 72;
|
|
76
|
+
/** Below this level the light fades out (every live state stays above it). */
|
|
77
|
+
const LIGHT_PRESENCE_LEVEL = 0.1;
|
|
78
|
+
|
|
79
|
+
/** The bars' shape, left to right, as a share of 16px. */
|
|
80
|
+
const BAR_SHAPE = [0.42, 0.72, 1, 0.72, 0.42] as const;
|
|
81
|
+
const BAR_UNIT = 16;
|
|
82
|
+
const BAR_MIN = 2;
|
|
83
|
+
const BAR_STEP = 0.5;
|
|
84
|
+
|
|
85
|
+
const TAU = Math.PI * 2;
|
|
86
|
+
|
|
87
|
+
/* ---------------------------------------------------------------------------------------------
|
|
88
|
+
* Formulas
|
|
89
|
+
* ------------------------------------------------------------------------------------------- */
|
|
90
|
+
|
|
91
|
+
function clamp01(value: number) {
|
|
92
|
+
if (Number.isNaN(value)) return 0;
|
|
93
|
+
return Math.min(1, Math.max(0, value));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function easeInOutCubic(x: number) {
|
|
97
|
+
return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(2 - 2 * x, 3) / 2;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The slope of `easeInOutCubic`. */
|
|
101
|
+
function easeInOutCubicSlope(x: number) {
|
|
102
|
+
return x < 0.5 ? 12 * x * x : 3 * Math.pow(2 - 2 * x, 2);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Time within the current beat. */
|
|
106
|
+
function inBeat(t: number) {
|
|
107
|
+
return t % BEAT;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The beat: sin² over the first 0.6s, then 0. */
|
|
111
|
+
function beatPulse(tau: number) {
|
|
112
|
+
return tau < SURGE ? Math.pow(Math.sin((Math.PI * tau) / SURGE), 2) : 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The border lights' pace, in turns per second. */
|
|
116
|
+
function borderPace(kind: VoiceBorderKind, t: number) {
|
|
117
|
+
if (kind === 'pulse') return 0;
|
|
118
|
+
if (kind === 'travel') return 1 / 30;
|
|
119
|
+
// A quarter turn surging with the beat, on top of a steady 40°/s.
|
|
120
|
+
const tau = inBeat(t);
|
|
121
|
+
const surge = tau < SURGE ? (0.25 * easeInOutCubicSlope(tau / SURGE)) / SURGE : 0;
|
|
122
|
+
return 40 / 360 + surge;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function spotStrength(kind: VoiceBorderKind, t: number) {
|
|
126
|
+
return kind === 'beat' ? 0.69 + 0.2 * beatPulse(inBeat(t)) : TRAVEL_STRENGTH;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The grey breath: 17% to 73% and back every 1.9s. */
|
|
130
|
+
function pulseOpacity(t: number) {
|
|
131
|
+
return 0.17 + 0.56 * (0.5 - 0.5 * Math.cos((TAU * t) / 1.9));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The light's level, 0 to 1 of 72px. `voice` is the speaker's loudness, 0 to 1. */
|
|
135
|
+
function lightLevel(kind: VoiceLightKind, t: number, voice: number) {
|
|
136
|
+
switch (kind) {
|
|
137
|
+
case 'none':
|
|
138
|
+
return 0;
|
|
139
|
+
case 'listening':
|
|
140
|
+
return 0.35 * (0.3 + 0.7 * (0.5 - 0.5 * Math.cos((TAU * t) / 5.5)));
|
|
141
|
+
case 'user-speaking':
|
|
142
|
+
return 0.35 * (0.35 + 0.65 * voice);
|
|
143
|
+
case 'thinking':
|
|
144
|
+
return 0.56 * (0.2 + 0.8 * beatPulse(inBeat(t)));
|
|
145
|
+
case 'speaking':
|
|
146
|
+
// A faint line stays in the pauses (Echo DS Speaking, Min).
|
|
147
|
+
return Math.max(0.11, voice);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A note of "Voice starts": rises in 15ms, then decays. */
|
|
152
|
+
function kick(time: number, at: number, amount: number, decay: number) {
|
|
153
|
+
const x = time - at;
|
|
154
|
+
if (x < 0) return 0;
|
|
155
|
+
if (x < 0.015) return (amount * x) / 0.015;
|
|
156
|
+
return amount * Math.exp(-(x - 0.015) / decay);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Each bar's height as a share of 16px. */
|
|
160
|
+
function barShare(kind: VoiceBarsKind, i: number, t: number, voice: number) {
|
|
161
|
+
const shape = BAR_SHAPE[i];
|
|
162
|
+
switch (kind) {
|
|
163
|
+
case 'still':
|
|
164
|
+
return 0;
|
|
165
|
+
case 'listening':
|
|
166
|
+
return shape * (0.32 + 0.04 * Math.sin(1.6 * t + 0.7 * i));
|
|
167
|
+
case 'voice-starts': {
|
|
168
|
+
// The sound's two notes, middle bar first, 30ms later per bar outwards.
|
|
169
|
+
const local = t - 0.03 * Math.abs(i - 2);
|
|
170
|
+
const kicks = kick(local, 0, 0.32, 0.12) + kick(local, 0.11, 0.55, 0.5);
|
|
171
|
+
return shape * (0.32 + 0.04 * Math.sin(1.6 * t + 0.7 * i) + kicks);
|
|
172
|
+
}
|
|
173
|
+
case 'muted':
|
|
174
|
+
return shape * (0.24 + 0.07 * Math.sin(1.1 * t + 0.9 * i));
|
|
175
|
+
case 'voice': {
|
|
176
|
+
const flicker = 0.55 + 0.45 * Math.abs(Math.sin(9.3 * t + 1.9 * i));
|
|
177
|
+
return Math.min(1, 0.22 + (Math.min(1, 1.25 * voice) * flicker * shape) / 0.9);
|
|
178
|
+
}
|
|
179
|
+
case 'thinking': {
|
|
180
|
+
// One wave per beat, entering on the left: two thirds with the border's surge, one third
|
|
181
|
+
// creeping with the beat's time.
|
|
182
|
+
const tau = inBeat(t);
|
|
183
|
+
const g = (2 / 3) * easeInOutCubic(Math.min(1, tau / SURGE)) + tau / BEAT / 3;
|
|
184
|
+
const crest = 0.48 + 0.2 * beatPulse(tau);
|
|
185
|
+
return 0.22 + crest * Math.max(0, Math.sin(TAU * g - 0.85 * i));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function barHeights(kind: VoiceBarsKind, t: number, voice: number) {
|
|
191
|
+
return BAR_SHAPE.map((_, i) => {
|
|
192
|
+
const px = barShare(kind, i, t, voice) * BAR_UNIT;
|
|
193
|
+
return Math.max(BAR_MIN, Math.round(px / BAR_STEP) * BAR_STEP);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* ---------------------------------------------------------------------------------------------
|
|
198
|
+
* The light's look per state (Echo DS `_Voice-light`, State × Intensity)
|
|
199
|
+
* ------------------------------------------------------------------------------------------- */
|
|
200
|
+
|
|
201
|
+
export type VoiceLightState = Exclude<VoiceLightKind, 'none'>;
|
|
202
|
+
|
|
203
|
+
type Triple = readonly [number, number, number];
|
|
204
|
+
|
|
205
|
+
interface VoiceLightStateSpec {
|
|
206
|
+
/** The haze's rise above the 22px base at Min, Base and Max, in px. */
|
|
207
|
+
rise: Triple;
|
|
208
|
+
/** Opacity of haze, body and rim at Min, Base and Max. */
|
|
209
|
+
opacity: { min: Triple; base: Triple; max: Triple };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const VOICE_LIGHT_STATES: Record<VoiceLightState, VoiceLightStateSpec> = {
|
|
213
|
+
listening: {
|
|
214
|
+
rise: [7.6, 15.6, 25.1],
|
|
215
|
+
opacity: { min: [0.25, 0.31, 0.26], base: [0.27, 0.34, 0.31], max: [0.31, 0.39, 0.37] },
|
|
216
|
+
},
|
|
217
|
+
'user-speaking': {
|
|
218
|
+
rise: [8.8, 18.2, 25.2],
|
|
219
|
+
opacity: { min: [0.29, 0.36, 0.28], base: [0.28, 0.36, 0.33], max: [0.31, 0.38, 0.37] },
|
|
220
|
+
},
|
|
221
|
+
thinking: {
|
|
222
|
+
rise: [8, 23.2, 39.8],
|
|
223
|
+
opacity: { min: [0.28, 0.35, 0.26], base: [0.28, 0.36, 0.36], max: [0.35, 0.43, 0.46] },
|
|
224
|
+
},
|
|
225
|
+
speaking: {
|
|
226
|
+
rise: [7.9, 43.3, VOICE_LIGHT_MAX_RISE],
|
|
227
|
+
opacity: { min: [0.32, 0.39, 0.23], base: [0.45, 0.57, 0.63], max: [0.47, 0.58, 0.63] },
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/** Haze, body and rim: how far each rises (share of the haze) and its cream peak. */
|
|
232
|
+
export const VOICE_LIGHT_LAYERS = [
|
|
233
|
+
{ name: 'haze', riseScale: 1, creamScale: 1, creamCap: 1 },
|
|
234
|
+
{ name: 'body', riseScale: 0.7, creamScale: 2, creamCap: 1 },
|
|
235
|
+
{ name: 'rim', riseScale: 0.3, creamScale: 2.9, creamCap: 0.98 },
|
|
236
|
+
] as const;
|
|
237
|
+
|
|
238
|
+
/** 0 = Min, 0.5 = Base, 1 = Max. */
|
|
239
|
+
function atIntensity(min: number, base: number, max: number, intensity: number) {
|
|
240
|
+
return intensity <= 0.5
|
|
241
|
+
? min + (base - min) * intensity * 2
|
|
242
|
+
: base + (max - base) * (intensity - 0.5) * 2;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The haze's rise at an intensity of the state (0 = Min frame, 0.5 = Base, 1 = Max). */
|
|
246
|
+
export function voiceLightRiseAt(state: VoiceLightState, intensity: number) {
|
|
247
|
+
const [min, base, max] = VOICE_LIGHT_STATES[state].rise;
|
|
248
|
+
return atIntensity(min, base, max, clamp01(intensity));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function intensityOfRise(state: VoiceLightState, rise: number) {
|
|
252
|
+
const [min, base, max] = VOICE_LIGHT_STATES[state].rise;
|
|
253
|
+
if (rise <= base) return clamp01(((rise - min) / (base - min)) * 0.5);
|
|
254
|
+
return clamp01(0.5 + ((rise - base) / (max - base)) * 0.5);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The light's CSS variables for a rise: `--voice-light-rise`, and per layer
|
|
259
|
+
* `--voice-light-opacity-{layer}` and, in Speaking, `--voice-light-cream-{layer}`. `presence`
|
|
260
|
+
* fades the whole light, 0 to 1.
|
|
261
|
+
*/
|
|
262
|
+
export function voiceLightVars(state: VoiceLightState, rise: number, presence = 1) {
|
|
263
|
+
const spec = VOICE_LIGHT_STATES[state];
|
|
264
|
+
const intensity = intensityOfRise(state, rise);
|
|
265
|
+
const cream = clamp01((rise - 6) / 120);
|
|
266
|
+
const vars: Record<string, string> = { '--voice-light-rise': `${rise.toFixed(2)}px` };
|
|
267
|
+
|
|
268
|
+
VOICE_LIGHT_LAYERS.forEach((layer, index) => {
|
|
269
|
+
const opacity = atIntensity(
|
|
270
|
+
spec.opacity.min[index],
|
|
271
|
+
spec.opacity.base[index],
|
|
272
|
+
spec.opacity.max[index],
|
|
273
|
+
intensity,
|
|
274
|
+
);
|
|
275
|
+
vars[`--voice-light-opacity-${layer.name}`] = (opacity * presence).toFixed(3);
|
|
276
|
+
vars[`--voice-light-cream-${layer.name}`] = Math.min(
|
|
277
|
+
layer.creamCap,
|
|
278
|
+
cream * layer.creamScale,
|
|
279
|
+
).toFixed(3);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
return vars;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/* ---------------------------------------------------------------------------------------------
|
|
286
|
+
* Engine
|
|
287
|
+
* ------------------------------------------------------------------------------------------- */
|
|
288
|
+
|
|
289
|
+
export interface VoiceMotionFrame {
|
|
290
|
+
/** The two border lights along the outline, 0 to 1. */
|
|
291
|
+
spotA: number;
|
|
292
|
+
spotB: number;
|
|
293
|
+
spotStrength: number;
|
|
294
|
+
/** The faint line, relative to its token (1 at the Listening strength). */
|
|
295
|
+
lineStrength: number;
|
|
296
|
+
/** How much the grey pulse covers the lights, 0 to 1. */
|
|
297
|
+
pulseMix: number;
|
|
298
|
+
/** The grey pulse's opacity. */
|
|
299
|
+
pulse: number;
|
|
300
|
+
/** The light's look (the last live state while it fades out). */
|
|
301
|
+
lightState: VoiceLightState;
|
|
302
|
+
lightRise: number;
|
|
303
|
+
lightPresence: number;
|
|
304
|
+
/** Bar heights in px, left to right. */
|
|
305
|
+
bars: number[];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface VoiceMotion {
|
|
309
|
+
setKinds: (kinds: VoiceMotionKinds) => void;
|
|
310
|
+
/** Advance by `dt` seconds; `voice` is the speaker's loudness, 0 to 1. */
|
|
311
|
+
step: (dt: number, voice: number) => void;
|
|
312
|
+
frame: () => VoiceMotionFrame;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function sameKinds(a: VoiceMotionKinds, b: VoiceMotionKinds) {
|
|
316
|
+
return a.border === b.border && a.light === b.light && a.bars === b.bars;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function lightStateOf(kind: VoiceLightKind, fallback: VoiceLightState): VoiceLightState {
|
|
320
|
+
return kind === 'none' ? fallback : kind;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function wrap(value: number) {
|
|
324
|
+
return value - Math.floor(value);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* One clock for the border, the light and the bars. Each state's motion is its formula of the time
|
|
329
|
+
* since the state began; on a change, the difference to the new formula decays (0.45s for the
|
|
330
|
+
* border's pace and the crossfade, 0.08s up / 0.6s down for the light), so nothing jumps.
|
|
331
|
+
*/
|
|
332
|
+
export function createVoiceMotion(initial: VoiceMotionKinds): VoiceMotion {
|
|
333
|
+
let kinds = initial;
|
|
334
|
+
let t = 0;
|
|
335
|
+
let position = kinds.border === 'beat' ? SPOT_REST_BEAT : SPOT_REST_TRAVEL;
|
|
336
|
+
let pace = borderPace(kinds.border, 0);
|
|
337
|
+
let paceOffset = 0;
|
|
338
|
+
let pulseMix = kinds.border === 'pulse' ? 1 : 0;
|
|
339
|
+
let voice = 0;
|
|
340
|
+
let level = lightLevel(kinds.light, 0, 0);
|
|
341
|
+
let levelOffset = 0;
|
|
342
|
+
let lightState = lightStateOf(kinds.light, 'listening');
|
|
343
|
+
|
|
344
|
+
return {
|
|
345
|
+
setKinds(next) {
|
|
346
|
+
if (sameKinds(kinds, next)) return;
|
|
347
|
+
kinds = next;
|
|
348
|
+
t = 0;
|
|
349
|
+
paceOffset = pace - borderPace(kinds.border, 0);
|
|
350
|
+
levelOffset = level - lightLevel(kinds.light, 0, voice);
|
|
351
|
+
lightState = lightStateOf(kinds.light, lightState);
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
step(dt, input) {
|
|
355
|
+
t += dt;
|
|
356
|
+
|
|
357
|
+
const target = clamp01(input);
|
|
358
|
+
voice +=
|
|
359
|
+
(target - voice) * (1 - Math.exp(-dt / (target > voice ? LIGHT_RISE_TAU : LIGHT_FALL_TAU)));
|
|
360
|
+
|
|
361
|
+
paceOffset *= Math.exp(-dt / BORDER_TAU);
|
|
362
|
+
pace = borderPace(kinds.border, t) + paceOffset;
|
|
363
|
+
position = wrap(position + pace * dt);
|
|
364
|
+
|
|
365
|
+
const mixTarget = kinds.border === 'pulse' ? 1 : 0;
|
|
366
|
+
pulseMix += (mixTarget - pulseMix) * (1 - Math.exp(-dt / BORDER_TAU));
|
|
367
|
+
|
|
368
|
+
levelOffset *= Math.exp(-dt / (levelOffset < 0 ? LIGHT_RISE_TAU : LIGHT_FALL_TAU));
|
|
369
|
+
level = Math.max(0, lightLevel(kinds.light, t, voice) + levelOffset);
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
frame() {
|
|
373
|
+
const strength = spotStrength(kinds.border, t);
|
|
374
|
+
return {
|
|
375
|
+
spotA: position,
|
|
376
|
+
spotB: wrap(position + 0.5),
|
|
377
|
+
spotStrength: strength,
|
|
378
|
+
lineStrength: strength / TRAVEL_STRENGTH,
|
|
379
|
+
pulseMix,
|
|
380
|
+
pulse: pulseOpacity(t),
|
|
381
|
+
lightState,
|
|
382
|
+
lightRise: level * VOICE_LIGHT_MAX_RISE,
|
|
383
|
+
lightPresence: clamp01(level / LIGHT_PRESENCE_LEVEL),
|
|
384
|
+
bars: barHeights(kinds.bars, t, voice),
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The resting look of each state, for reduced motion: the lights where the Echo DS frames draw
|
|
392
|
+
* them, the light and the bars at Base. The voice states still follow `voice`, without easing.
|
|
393
|
+
*/
|
|
394
|
+
export function restVoiceMotion(
|
|
395
|
+
kinds: VoiceMotionKinds,
|
|
396
|
+
voice: number,
|
|
397
|
+
lightState: VoiceLightState = lightStateOf(kinds.light, 'listening'),
|
|
398
|
+
): VoiceMotionFrame {
|
|
399
|
+
const spot = kinds.border === 'beat' ? SPOT_REST_BEAT : SPOT_REST_TRAVEL;
|
|
400
|
+
const strength = kinds.border === 'beat' ? 0.79 : TRAVEL_STRENGTH;
|
|
401
|
+
const loudness = clamp01(voice);
|
|
402
|
+
|
|
403
|
+
let rise = 0;
|
|
404
|
+
if (kinds.light === 'listening' || kinds.light === 'thinking') {
|
|
405
|
+
rise = voiceLightRiseAt(kinds.light, 0.5);
|
|
406
|
+
} else if (kinds.light !== 'none') {
|
|
407
|
+
rise = lightLevel(kinds.light, 0, loudness) * VOICE_LIGHT_MAX_RISE;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Thinking rests at the top of its beat; the others where their sway is at its middle.
|
|
411
|
+
const barsKind = kinds.bars === 'voice-starts' ? 'listening' : kinds.bars;
|
|
412
|
+
const barsTime = barsKind === 'thinking' ? 0.3 : 0;
|
|
413
|
+
|
|
414
|
+
return {
|
|
415
|
+
spotA: spot,
|
|
416
|
+
spotB: wrap(spot + 0.5),
|
|
417
|
+
spotStrength: strength,
|
|
418
|
+
lineStrength: strength / TRAVEL_STRENGTH,
|
|
419
|
+
pulseMix: kinds.border === 'pulse' ? 1 : 0,
|
|
420
|
+
pulse: 0.45,
|
|
421
|
+
lightState,
|
|
422
|
+
lightRise: rise,
|
|
423
|
+
lightPresence: kinds.light === 'none' ? 0 : 1,
|
|
424
|
+
bars: BAR_SHAPE.map((_, i) => {
|
|
425
|
+
const share =
|
|
426
|
+
barsKind === 'voice'
|
|
427
|
+
? Math.min(1, 0.22 + (Math.min(1, 1.25 * loudness) * 0.84 * BAR_SHAPE[i]) / 0.9)
|
|
428
|
+
: barsKind === 'listening'
|
|
429
|
+
? BAR_SHAPE[i] * 0.32
|
|
430
|
+
: barsKind === 'muted'
|
|
431
|
+
? BAR_SHAPE[i] * 0.24
|
|
432
|
+
: barShare(barsKind, i, barsTime, loudness);
|
|
433
|
+
return Math.max(BAR_MIN, Math.round((share * BAR_UNIT) / BAR_STEP) * BAR_STEP);
|
|
434
|
+
}),
|
|
435
|
+
};
|
|
436
|
+
}
|