@glissade/interact 0.2.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/LICENSE +202 -0
- package/dist/audio.d.ts +29 -0
- package/dist/audio.js +82 -0
- package/dist/index.d.ts +357 -0
- package/dist/index.js +0 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/audio.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Track } from "@glissade/core";
|
|
2
|
+
|
|
3
|
+
//#region src/audio.d.ts
|
|
4
|
+
|
|
5
|
+
/** Decoded PCM — structurally an AudioBuffer's data, without requiring one. */
|
|
6
|
+
interface DecodedAudio {
|
|
7
|
+
sampleRate: number;
|
|
8
|
+
/** One Float32Array per channel; channels are mixed down equally. */
|
|
9
|
+
channelData: readonly Float32Array[];
|
|
10
|
+
}
|
|
11
|
+
interface AudioAmplitudeOptions {
|
|
12
|
+
/** Key grid for the emitted track; default 60. */
|
|
13
|
+
fps?: number;
|
|
14
|
+
/** Track target path; default 'audio/amplitude'. */
|
|
15
|
+
target?: string;
|
|
16
|
+
/** Hz range: band amplitude via Hann-windowed Goertzel probes instead of full-band RMS. */
|
|
17
|
+
band?: [number, number];
|
|
18
|
+
/** Scale the peak to 1; default true. */
|
|
19
|
+
normalize?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** Adapt a WebAudio AudioBuffer to DecodedAudio. */
|
|
22
|
+
declare function fromAudioBuffer(buffer: {
|
|
23
|
+
sampleRate: number;
|
|
24
|
+
numberOfChannels: number;
|
|
25
|
+
getChannelData(i: number): Float32Array;
|
|
26
|
+
}): DecodedAudio;
|
|
27
|
+
declare function audioAmplitudeTrack(audio: DecodedAudio, opts?: AudioAmplitudeOptions): Track<number>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AudioAmplitudeOptions, DecodedAudio, audioAmplitudeTrack, fromAudioBuffer };
|
package/dist/audio.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { key, track } from "@glissade/core";
|
|
2
|
+
//#region src/audio.ts
|
|
3
|
+
/**
|
|
4
|
+
* Offline audio amplitude (§C.1, separate entry '@glissade/interact/audio'):
|
|
5
|
+
* DSP over decoded samples compiles to an ordinary Track — pure-of-frame, so
|
|
6
|
+
* audio-reactive scenes with file-backed audio are PARAMETERIZED and export
|
|
7
|
+
* with no trace at all (§A.6 route 1). Decoding stays at the edge (WebAudio /
|
|
8
|
+
* ffmpeg); this module is pure float math, deterministic per the §2.5 engine
|
|
9
|
+
* pin. The realtime audioDriver (live mic/stream) is reserved for v2.x.
|
|
10
|
+
*/
|
|
11
|
+
/** Adapt a WebAudio AudioBuffer to DecodedAudio. */
|
|
12
|
+
function fromAudioBuffer(buffer) {
|
|
13
|
+
const channelData = [];
|
|
14
|
+
for (let i = 0; i < buffer.numberOfChannels; i++) channelData.push(buffer.getChannelData(i));
|
|
15
|
+
return {
|
|
16
|
+
sampleRate: buffer.sampleRate,
|
|
17
|
+
channelData
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function audioAmplitudeTrack(audio, opts = {}) {
|
|
21
|
+
const fps = opts.fps ?? 60;
|
|
22
|
+
const channels = audio.channelData;
|
|
23
|
+
const sr = audio.sampleRate;
|
|
24
|
+
const length = channels[0]?.length ?? 0;
|
|
25
|
+
const frames = Math.ceil(length / sr * fps);
|
|
26
|
+
const mono = (i) => {
|
|
27
|
+
let s = 0;
|
|
28
|
+
for (const ch of channels) s += ch[i] ?? 0;
|
|
29
|
+
return s / channels.length;
|
|
30
|
+
};
|
|
31
|
+
const values = [];
|
|
32
|
+
for (let f = 0; f < frames; f++) {
|
|
33
|
+
const start = Math.floor(f / fps * sr);
|
|
34
|
+
const end = Math.min(Math.floor((f + 1) / fps * sr), length);
|
|
35
|
+
const n = end - start;
|
|
36
|
+
if (n <= 0) {
|
|
37
|
+
values.push(0);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (!opts.band) {
|
|
41
|
+
let sum = 0;
|
|
42
|
+
for (let i = start; i < end; i++) {
|
|
43
|
+
const v = mono(i);
|
|
44
|
+
sum += v * v;
|
|
45
|
+
}
|
|
46
|
+
values.push(Math.sqrt(sum / n));
|
|
47
|
+
} else {
|
|
48
|
+
const [lo, hi] = opts.band;
|
|
49
|
+
const probes = 8;
|
|
50
|
+
let power = 0;
|
|
51
|
+
for (let k = 0; k < probes; k++) {
|
|
52
|
+
const frac = k / (probes - 1);
|
|
53
|
+
const freq = lo > 0 ? lo * Math.pow(hi / lo, frac) : lo + (hi - lo) * frac;
|
|
54
|
+
power += goertzelPower(mono, start, n, freq / sr);
|
|
55
|
+
}
|
|
56
|
+
values.push(Math.sqrt(power / probes));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (opts.normalize !== false) {
|
|
60
|
+
let peak = 0;
|
|
61
|
+
for (const v of values) if (v > peak) peak = v;
|
|
62
|
+
if (peak > 0) for (let i = 0; i < values.length; i++) values[i] = values[i] / peak;
|
|
63
|
+
}
|
|
64
|
+
const keys = values.length > 0 ? values.map((v, f) => key(f / fps, v)) : [key(0, 0)];
|
|
65
|
+
return track(opts.target ?? "audio/amplitude", "number", keys);
|
|
66
|
+
}
|
|
67
|
+
/** Hann-windowed Goertzel magnitude² at a normalized frequency, scaled to ~[0,1] for full-scale input. */
|
|
68
|
+
function goertzelPower(sample, start, n, normFreq) {
|
|
69
|
+
const coeff = 2 * Math.cos(2 * Math.PI * normFreq);
|
|
70
|
+
let s1 = 0;
|
|
71
|
+
let s2 = 0;
|
|
72
|
+
const denom = n > 1 ? n - 1 : 1;
|
|
73
|
+
for (let i = 0; i < n; i++) {
|
|
74
|
+
const hann = .5 - .5 * Math.cos(2 * Math.PI * i / denom);
|
|
75
|
+
const s0 = sample(start + i) * hann + coeff * s1 - s2;
|
|
76
|
+
s2 = s1;
|
|
77
|
+
s1 = s0;
|
|
78
|
+
}
|
|
79
|
+
return (s1 * s1 + s2 * s2 - coeff * s1 * s2) / (n * n / 4);
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { audioAmplitudeTrack, fromAudioBuffer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { AssetRef, BindTarget, EaseSpec, ReadonlySignal, Signal, SpringConfig, Timeline, ValueType, Vec2 } from "@glissade/core";
|
|
2
|
+
import { Node, Scene, TextMeasurer } from "@glissade/scene";
|
|
3
|
+
import { InputDriver } from "@glissade/player";
|
|
4
|
+
|
|
5
|
+
//#region src/doc.d.ts
|
|
6
|
+
|
|
7
|
+
type StateId = string;
|
|
8
|
+
type InputDecl = {
|
|
9
|
+
type: 'boolean';
|
|
10
|
+
default?: boolean;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'number';
|
|
13
|
+
default?: number;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'trigger';
|
|
16
|
+
};
|
|
17
|
+
/** Flat AND per transition; multiple transitions = OR (§A.3). No number ==/!= — use hysteresis. */
|
|
18
|
+
type Condition = {
|
|
19
|
+
input: string;
|
|
20
|
+
is: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
input: string;
|
|
23
|
+
op: '<' | '<=' | '>' | '>=';
|
|
24
|
+
value: number;
|
|
25
|
+
} | {
|
|
26
|
+
trigger: string;
|
|
27
|
+
};
|
|
28
|
+
/** Defined identically here and in §B.1; 'crossfade' is reserved-not-valid in version 1. */
|
|
29
|
+
type HandoffName = 'cut' | 'decay' | 'spring';
|
|
30
|
+
interface StateDoc {
|
|
31
|
+
/** Referenced as an asset ({ ref }) or inlined (§A.1). */
|
|
32
|
+
timeline: {
|
|
33
|
+
ref: string;
|
|
34
|
+
} | Timeline;
|
|
35
|
+
loop?: boolean;
|
|
36
|
+
/** Local playback rate; must be > 0. */
|
|
37
|
+
rate?: number;
|
|
38
|
+
/** Re-entry playhead policy (§A.1); default 'restart'. */
|
|
39
|
+
onEnter?: 'restart' | 'resume';
|
|
40
|
+
}
|
|
41
|
+
interface TransitionDoc {
|
|
42
|
+
id: string;
|
|
43
|
+
from: StateId | '*';
|
|
44
|
+
to: StateId;
|
|
45
|
+
/** A '*' edge never matches the current state unless allowSelf (§A.1). */
|
|
46
|
+
allowSelf?: boolean;
|
|
47
|
+
conditions: Condition[];
|
|
48
|
+
/** Window-guard, not a trigger: fraction of the SOURCE timeline's duration [0..1] (§A.3). */
|
|
49
|
+
exitTime?: number;
|
|
50
|
+
/** The transition's own clock, seconds; default 0 = hard cut (§A.3). */
|
|
51
|
+
duration?: number;
|
|
52
|
+
/** Shapes the 'decay' ramp / 'blend-from-frozen' blend (§B.1). */
|
|
53
|
+
ease?: EaseSpec;
|
|
54
|
+
handoff?: HandoffName;
|
|
55
|
+
/** Offset-oscillator config when handoff resolves to 'spring'; default §B.3's {170, 26, 1}. */
|
|
56
|
+
spring?: SpringConfig;
|
|
57
|
+
/** Default true; false skips evaluation and holds the trigger queue while in flight (§B.4). */
|
|
58
|
+
interruptible?: boolean;
|
|
59
|
+
}
|
|
60
|
+
interface StateMachineDoc {
|
|
61
|
+
version: 1;
|
|
62
|
+
id: string;
|
|
63
|
+
inputs?: Record<string, InputDecl>;
|
|
64
|
+
initial: StateId;
|
|
65
|
+
states: Record<StateId, StateDoc>;
|
|
66
|
+
transitions: TransitionDoc[];
|
|
67
|
+
assets?: Record<string, AssetRef>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Trace identity (§C.5): covers the machine doc plus each referenced timeline
|
|
71
|
+
* document, so a re-edited timeline invalidates old takes unless --force.
|
|
72
|
+
* FNV-1a 64 over the canonical serialization — engine-pinned like §2.5.
|
|
73
|
+
*/
|
|
74
|
+
declare function hashMachine(doc: StateMachineDoc, refTimelines?: Record<string, Timeline>): string;
|
|
75
|
+
declare class MachineValidationError extends Error {
|
|
76
|
+
constructor(message: string);
|
|
77
|
+
}
|
|
78
|
+
declare function validateMachineDoc(doc: StateMachineDoc): void;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/machine.d.ts
|
|
81
|
+
declare class UnknownInputError extends Error {
|
|
82
|
+
constructor(message: string);
|
|
83
|
+
}
|
|
84
|
+
interface MachineOptions {
|
|
85
|
+
/** Target resolution — the bindTimeline seam (§2.2: unbound targets are hard errors). */
|
|
86
|
+
resolve: (target: string) => BindTarget | undefined;
|
|
87
|
+
/** Resolved timeline documents for `{ ref }` states, keyed by asset id. */
|
|
88
|
+
timelines?: Record<string, Timeline>;
|
|
89
|
+
/** Applied before the first step — conditional entry settles via normal transitions (§A.1). */
|
|
90
|
+
initialInputs?: Record<string, boolean | number>;
|
|
91
|
+
}
|
|
92
|
+
interface Machine {
|
|
93
|
+
readonly id: string;
|
|
94
|
+
/** The validated source document — recordTrace/bakeTrace read inputs and identity from it. */
|
|
95
|
+
readonly doc: StateMachineDoc;
|
|
96
|
+
/** Trace identity (§C.5): doc + referenced timeline documents. */
|
|
97
|
+
readonly hash: string;
|
|
98
|
+
/** Observable, NOT writable (§A.2). */
|
|
99
|
+
readonly current: ReadonlySignal<StateId>;
|
|
100
|
+
/** Machine-clock signal; every composite transition binding depends on it. */
|
|
101
|
+
readonly clock: ReadonlySignal<number>;
|
|
102
|
+
/** Union of every state's track targets — the §A.1 disjointness set. */
|
|
103
|
+
readonly targets: ReadonlySet<string>;
|
|
104
|
+
/** True once step() has run — bakeTrace requires a fresh machine (§A.6). */
|
|
105
|
+
readonly hasStepped: boolean;
|
|
106
|
+
input<T extends boolean | number = boolean | number>(name: string): Signal<T>;
|
|
107
|
+
/** Enqueue a trigger; consumed during the next step (§A.2: triggers are not signals). */
|
|
108
|
+
fire(name: string): void;
|
|
109
|
+
/** Host tick (§A.5): drain triggers, take at most one transition, advance clocks. */
|
|
110
|
+
step(now: number): void;
|
|
111
|
+
/**
|
|
112
|
+
* Analytic sample of every machine-bound target at machine time t — the
|
|
113
|
+
* per-frame sampling surface bakeTrace consumes (§A.6). Valid at the
|
|
114
|
+
* current step's time; reads the live binding samplers, never the signals.
|
|
115
|
+
*/
|
|
116
|
+
sampleTargets(t: number): Map<string, {
|
|
117
|
+
value: unknown;
|
|
118
|
+
type: string;
|
|
119
|
+
}>;
|
|
120
|
+
dispose(): void;
|
|
121
|
+
}
|
|
122
|
+
declare function createMachine(doc: StateMachineDoc, opts: MachineOptions): Machine;
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/builder.d.ts
|
|
125
|
+
/**
|
|
126
|
+
* A pose state (§C.7): just values, keyed by full target path — compiled to a
|
|
127
|
+
* one-key timeline, no separate file. Covers "two looks, one toggle".
|
|
128
|
+
*/
|
|
129
|
+
declare function pose(values: Record<string, unknown>): Timeline;
|
|
130
|
+
type Cond<B extends string, N extends string, G extends string> = {
|
|
131
|
+
input: B;
|
|
132
|
+
is: boolean;
|
|
133
|
+
} | {
|
|
134
|
+
input: N;
|
|
135
|
+
op: '<' | '<=' | '>' | '>=';
|
|
136
|
+
value: number;
|
|
137
|
+
} | {
|
|
138
|
+
trigger: G;
|
|
139
|
+
};
|
|
140
|
+
interface TransitionOpts<B extends string, N extends string, G extends string> {
|
|
141
|
+
/** Flat AND (§A.3); a single condition or a list. Omit for always-eligible (pair with exitTime). */
|
|
142
|
+
when?: Cond<B, N, G> | Cond<B, N, G>[];
|
|
143
|
+
exitTime?: number;
|
|
144
|
+
duration?: number;
|
|
145
|
+
ease?: EaseSpec;
|
|
146
|
+
handoff?: HandoffName;
|
|
147
|
+
spring?: SpringConfig;
|
|
148
|
+
interruptible?: boolean;
|
|
149
|
+
/** Stable id; default '<from>-><to>#<n>'. */
|
|
150
|
+
id?: string;
|
|
151
|
+
}
|
|
152
|
+
interface StateInput {
|
|
153
|
+
timeline: StateDoc['timeline'];
|
|
154
|
+
loop?: boolean;
|
|
155
|
+
rate?: number;
|
|
156
|
+
onEnter?: 'restart' | 'resume';
|
|
157
|
+
}
|
|
158
|
+
declare class MachineBuilder<B extends string = never,
|
|
159
|
+
// boolean inputs
|
|
160
|
+
N extends string = never,
|
|
161
|
+
// number inputs
|
|
162
|
+
G extends string = never,
|
|
163
|
+
// triggers
|
|
164
|
+
S extends string = never> {
|
|
165
|
+
private readonly id;
|
|
166
|
+
private readonly inputs;
|
|
167
|
+
private readonly states;
|
|
168
|
+
private readonly transitions;
|
|
169
|
+
private first;
|
|
170
|
+
private initialState;
|
|
171
|
+
constructor(id: string);
|
|
172
|
+
input<K extends string>(name: K, type: 'boolean', dflt?: boolean): MachineBuilder<B | K, N, G, S>;
|
|
173
|
+
input<K extends string>(name: K, type: 'number', dflt?: number): MachineBuilder<B, N | K, G, S>;
|
|
174
|
+
trigger<K extends string>(name: K): MachineBuilder<B, N, G | K, S>;
|
|
175
|
+
state<K extends string>(name: K, state: Timeline | StateInput): MachineBuilder<B, N, G, S | K>;
|
|
176
|
+
initial(name: S): this;
|
|
177
|
+
transition(from: S | '*', to: S, opts?: TransitionOpts<B, N, G>): this;
|
|
178
|
+
/** Assemble and validate; the result is the same document JSON authoring produces. */
|
|
179
|
+
build(): StateMachineDoc;
|
|
180
|
+
}
|
|
181
|
+
declare function machineBuilder(id: string): MachineBuilder;
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/trace.d.ts
|
|
184
|
+
type TraceEvent = {
|
|
185
|
+
t: number;
|
|
186
|
+
input: string;
|
|
187
|
+
value: boolean | number;
|
|
188
|
+
} | {
|
|
189
|
+
t: number;
|
|
190
|
+
fire: string;
|
|
191
|
+
};
|
|
192
|
+
/** The single trace schema (§C.5): an event list — never dense samples. */
|
|
193
|
+
interface InputTrace {
|
|
194
|
+
version: 1;
|
|
195
|
+
/** hash(machine doc) + per-referenced-timeline hashes; mismatch at bake is an error. */
|
|
196
|
+
machineHash: string;
|
|
197
|
+
/** Replay quantization grid (§5.5 sample-position arithmetic). */
|
|
198
|
+
fps: number;
|
|
199
|
+
initialInputs: Record<string, boolean | number>;
|
|
200
|
+
/** Raw wall-clock seconds relative to record start; pre-filter values. */
|
|
201
|
+
events: TraceEvent[];
|
|
202
|
+
}
|
|
203
|
+
interface RecordOptions {
|
|
204
|
+
/** Replay grid stored in the trace; default 60. */
|
|
205
|
+
fps?: number;
|
|
206
|
+
/** Time source (seconds); defaults to performance.now()/1000. Events store t relative to start. */
|
|
207
|
+
now?: () => number;
|
|
208
|
+
}
|
|
209
|
+
interface TraceRecorder {
|
|
210
|
+
/** Restore the tapped writers and return the trace. */
|
|
211
|
+
stop(): InputTrace;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Tap a live machine's raw input writes (§C.5). Transparent: set()/fire()
|
|
215
|
+
* keep working; stop() restores them. Recording raw keeps every take
|
|
216
|
+
* re-bakeable after tuning a smoothing spring (§C.2).
|
|
217
|
+
*/
|
|
218
|
+
declare function recordTrace(machine: Machine, opts?: RecordOptions): TraceRecorder;
|
|
219
|
+
declare class TraceHashMismatchError extends Error {
|
|
220
|
+
constructor(expected: string, got: string);
|
|
221
|
+
}
|
|
222
|
+
interface BakeTraceOptions {
|
|
223
|
+
/** Output length in seconds; default: last event time + 1 s of settle. */
|
|
224
|
+
duration?: number;
|
|
225
|
+
/** Sampling grid; default: the trace's fps. */
|
|
226
|
+
fps?: number;
|
|
227
|
+
/** Downgrade a hash mismatch to a warning — re-baking an old take against tweaked timelines is legitimate. */
|
|
228
|
+
force?: boolean;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Replay a trace through a FRESH machine and emit a plain linear Timeline —
|
|
232
|
+
* bake() (§2.8) with the machine as the stepper: one track per bound target,
|
|
233
|
+
* frame-indexed keys. Bit-deterministic for a given trace (§B.5).
|
|
234
|
+
*/
|
|
235
|
+
declare function bakeTrace(machine: Machine, trace: InputTrace, opts?: BakeTraceOptions): Timeline;
|
|
236
|
+
/**
|
|
237
|
+
* Scene-module machine declaration (§A.6/§C.6) — the convention `gs render`
|
|
238
|
+
* and `gs dev` consume: `export default { createScene, timeline, machines }`.
|
|
239
|
+
*/
|
|
240
|
+
interface MachineSpec {
|
|
241
|
+
doc: StateMachineDoc;
|
|
242
|
+
/** Resolved documents for `{ ref }` states. */
|
|
243
|
+
timelines?: Record<string, Timeline>;
|
|
244
|
+
/** Live wiring (listeners, drivers) for dev and embeds; replay never calls it. */
|
|
245
|
+
wire?: (ctx: {
|
|
246
|
+
scene: Scene;
|
|
247
|
+
machine: Machine;
|
|
248
|
+
element: Element;
|
|
249
|
+
}) => (() => void) | void;
|
|
250
|
+
}
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/presets.d.ts
|
|
253
|
+
interface PoseMachineOptions {
|
|
254
|
+
/** Node-relative props for the resting pose, e.g. { scale: [1, 1] }. */
|
|
255
|
+
from: Record<string, unknown>;
|
|
256
|
+
/** The active pose. */
|
|
257
|
+
to: Record<string, unknown>;
|
|
258
|
+
/** Transition clock, seconds; default 0.15. */
|
|
259
|
+
duration?: number;
|
|
260
|
+
/** Offset-spring override for kinetic props (§B.3 default otherwise). */
|
|
261
|
+
spring?: SpringConfig;
|
|
262
|
+
/** Machine id; defaults to '<kind>-<nodeId>'. */
|
|
263
|
+
id?: string;
|
|
264
|
+
}
|
|
265
|
+
/** Hover in/out between two poses; listeners wire themselves on mount. */
|
|
266
|
+
declare function hoverMachine(node: Node | string, opts: PoseMachineOptions): MachineSpec;
|
|
267
|
+
/** Press/release between two poses (primary pointer, down-over-target). */
|
|
268
|
+
declare function pressMachine(node: Node | string, opts: PoseMachineOptions): MachineSpec;
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/handoff.d.ts
|
|
271
|
+
/** Analytic value + derivative of a live source w.r.t. the machine clock (§B.2). */
|
|
272
|
+
interface MachineSampler<T = unknown> {
|
|
273
|
+
value(t: number): T;
|
|
274
|
+
/** null when the source's type lacks operators (lerp-only). */
|
|
275
|
+
velocity(t: number): T | null;
|
|
276
|
+
}
|
|
277
|
+
type HandoffPolicy = {
|
|
278
|
+
kind: 'decay';
|
|
279
|
+
ease: EaseSpec | undefined;
|
|
280
|
+
duration: number;
|
|
281
|
+
} | {
|
|
282
|
+
kind: 'spring';
|
|
283
|
+
cfg: SpringConfig;
|
|
284
|
+
};
|
|
285
|
+
/** §B.3's decided default: critically-damped-ish, never derived from the declared duration. */
|
|
286
|
+
declare const DEFAULT_HANDOFF_SPRING: SpringConfig;
|
|
287
|
+
interface OffsetCurve<T> {
|
|
288
|
+
at(tau: number): T;
|
|
289
|
+
vel(tau: number): T;
|
|
290
|
+
/** τ after which the offset is dropped; dest alone is exact within the settle tolerance. */
|
|
291
|
+
settle: number;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Closed-form offset y(τ) → 0 with y(0)=x₀ (§B.2's solveOffset). Requires the
|
|
295
|
+
* type's add/sub/scale operators (callers degrade lerp-only types upstream).
|
|
296
|
+
*/
|
|
297
|
+
declare function solveOffset<T>(policy: HandoffPolicy, vt: ValueType<T>, x0: T, v0: T): OffsetCurve<T>;
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/pointer.d.ts
|
|
300
|
+
/** vec2 is not a machine input type in v2.0 (§A.2): fan out to two number writes. */
|
|
301
|
+
declare const splitVec2: (x: (v: number) => void, y: (v: number) => void) => (v: Vec2) => void;
|
|
302
|
+
/** One scalar smoothing filter: closed-form spring segments, retargeted per write. */
|
|
303
|
+
interface SpringFilter {
|
|
304
|
+
/** Aim at a new target at time t (seconds); velocity-matched (§B.3). First call snaps. */
|
|
305
|
+
retarget(t: number, target: number): void;
|
|
306
|
+
sample(t: number): number;
|
|
307
|
+
velocity(t: number): number;
|
|
308
|
+
/** True once the current segment's offset has decayed within tolerance. */
|
|
309
|
+
settled(t: number): boolean;
|
|
310
|
+
}
|
|
311
|
+
declare function springFilter(cfg: SpringConfig): SpringFilter;
|
|
312
|
+
interface PointerDriverOptions {
|
|
313
|
+
/** Event source — usually the canvas element. */
|
|
314
|
+
target: Element;
|
|
315
|
+
/** Scale element-local CSS px into scene units via scene.size; default 1:1. */
|
|
316
|
+
scene?: Scene;
|
|
317
|
+
/** Optional driver-resident smoothing (§C.2). */
|
|
318
|
+
smooth?: SpringConfig;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Position only — buttons are listeners (§C.3). Intermediate pointermove
|
|
322
|
+
* events coalesce; one write lands per animation frame (§C.1).
|
|
323
|
+
*/
|
|
324
|
+
declare function pointerDriver(opts: PointerDriverOptions): InputDriver<Vec2>;
|
|
325
|
+
//#endregion
|
|
326
|
+
//#region src/listeners.d.ts
|
|
327
|
+
/** Geometric shape test in node-local coordinates; hitArea overrides the geometry. */
|
|
328
|
+
declare function containsPoint(node: Node, p: Vec2, measurer: TextMeasurer): boolean;
|
|
329
|
+
/**
|
|
330
|
+
* Topmost-first over nodes with `interactive: true`: O(interactive nodes) per
|
|
331
|
+
* call, one 2×3 inverse each — worldMatrix is a cached computed (§3.1), so
|
|
332
|
+
* unmoved subtrees cost a cache read.
|
|
333
|
+
*/
|
|
334
|
+
declare function hitTest(scene: Scene, x: number, y: number): Node | null;
|
|
335
|
+
/** A machine input's set, or any boolean sink. */
|
|
336
|
+
type BoolSink = {
|
|
337
|
+
set(value: boolean): void;
|
|
338
|
+
} | ((value: boolean) => void);
|
|
339
|
+
interface Listeners {
|
|
340
|
+
/** Boolean input follows pointer-over; touch-emulated hover is filtered (Motion precedent). */
|
|
341
|
+
hover(node: Node, input: BoolSink): () => void;
|
|
342
|
+
/** Primary pointer only: true on down-over-target, false on release. */
|
|
343
|
+
press(node: Node, input: BoolSink): () => void;
|
|
344
|
+
/** Fires only if release lands over the same node; anything else cancels. */
|
|
345
|
+
click(node: Node, fn: () => void): () => void;
|
|
346
|
+
dispose(): void;
|
|
347
|
+
}
|
|
348
|
+
interface ListenersOptions {
|
|
349
|
+
scene: Scene;
|
|
350
|
+
/** Event source — usually the canvas element. */
|
|
351
|
+
element: Element;
|
|
352
|
+
/** Client coords → scene coords; default scales the element box to scene.size. */
|
|
353
|
+
toScene?: (clientX: number, clientY: number) => Vec2;
|
|
354
|
+
}
|
|
355
|
+
declare function createListeners(opts: ListenersOptions): Listeners;
|
|
356
|
+
//#endregion
|
|
357
|
+
export { type BakeTraceOptions, type BoolSink, type Condition, DEFAULT_HANDOFF_SPRING, type HandoffName, type HandoffPolicy, type InputDecl, type InputTrace, type Listeners, type ListenersOptions, type Machine, MachineBuilder, type MachineOptions, type MachineSampler, type MachineSpec, MachineValidationError, type OffsetCurve, type PointerDriverOptions, type PoseMachineOptions, type RecordOptions, type SpringFilter, type StateDoc, type StateId, type StateMachineDoc, type TraceEvent, TraceHashMismatchError, type TraceRecorder, type TransitionDoc, type TransitionOpts, UnknownInputError, bakeTrace, containsPoint, createListeners, createMachine, hashMachine, hitTest, hoverMachine, machineBuilder, pointerDriver, pose, pressMachine, recordTrace, solveOffset, splitVec2, springFilter, validateMachineDoc };
|
package/dist/index.js
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glissade/interact",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "glissade interactivity: state machines over timelines, velocity-matched handoffs, typed inputs. Opt-in layer; never imported by the linear pipeline.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./audio": {
|
|
14
|
+
"types": "./dist/audio.d.ts",
|
|
15
|
+
"default": "./dist/audio.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@glissade/core": "0.2.0",
|
|
23
|
+
"@glissade/player": "0.2.0",
|
|
24
|
+
"@glissade/scene": "0.2.0"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/tyevco/glissade.git",
|
|
29
|
+
"directory": "packages/interact"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"typecheck": "tsc --noEmit"
|
|
34
|
+
}
|
|
35
|
+
}
|