@ac-kit/browser 0.1.1 → 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/CHANGELOG.md +35 -0
- package/README.md +258 -4
- package/dist/_race-abort.d.ts +2 -0
- package/dist/_race-abort.d.ts.map +1 -0
- package/dist/_race-abort.js +20 -0
- package/dist/_race-abort.js.map +1 -0
- package/dist/canvas/image-data-pixels.d.ts +61 -0
- package/dist/canvas/image-data-pixels.d.ts.map +1 -0
- package/dist/canvas/image-data-pixels.js +74 -0
- package/dist/canvas/image-data-pixels.js.map +1 -0
- package/dist/canvas/read-image-data.d.ts +120 -0
- package/dist/canvas/read-image-data.d.ts.map +1 -0
- package/dist/canvas/read-image-data.js +147 -0
- package/dist/canvas/read-image-data.js.map +1 -0
- package/dist/dom/index.d.ts +2 -0
- package/dist/dom/index.d.ts.map +1 -0
- package/dist/dom/index.js +2 -0
- package/dist/dom/index.js.map +1 -0
- package/dist/dom/web-animations/wait-for-transitions.d.ts +59 -0
- package/dist/dom/web-animations/wait-for-transitions.d.ts.map +1 -0
- package/dist/dom/web-animations/wait-for-transitions.js +44 -0
- package/dist/dom/web-animations/wait-for-transitions.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/media-capture/microphone-access.d.ts +116 -0
- package/dist/media-capture/microphone-access.d.ts.map +1 -0
- package/dist/media-capture/microphone-access.js +224 -0
- package/dist/media-capture/microphone-access.js.map +1 -0
- package/dist/testing/fake-audio-context.d.ts +174 -0
- package/dist/testing/fake-audio-context.d.ts.map +1 -0
- package/dist/testing/fake-audio-context.js +314 -0
- package/dist/testing/fake-audio-context.js.map +1 -0
- package/dist/testing/fake-media-devices.d.ts +90 -0
- package/dist/testing/fake-media-devices.d.ts.map +1 -0
- package/dist/testing/fake-media-devices.js +147 -0
- package/dist/testing/fake-media-devices.js.map +1 -0
- package/dist/testing/fake-web-midi.d.ts +25 -0
- package/dist/testing/fake-web-midi.d.ts.map +1 -0
- package/dist/testing/fake-web-midi.js +52 -0
- package/dist/testing/fake-web-midi.js.map +1 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/web-audio/audio-clock-mapping.d.ts +124 -0
- package/dist/web-audio/audio-clock-mapping.d.ts.map +1 -0
- package/dist/web-audio/audio-clock-mapping.js +245 -0
- package/dist/web-audio/audio-clock-mapping.js.map +1 -0
- package/package.json +20 -6
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A microphone's audio track: live until stopped or until its device is lost
|
|
3
|
+
* ({@link end}), which a real track reports by an `ended` event, and not by
|
|
4
|
+
* `stop()`.
|
|
5
|
+
*/
|
|
6
|
+
export class FakeMediaStreamTrack extends EventTarget {
|
|
7
|
+
label;
|
|
8
|
+
settings_;
|
|
9
|
+
readyState = "live";
|
|
10
|
+
muted = false;
|
|
11
|
+
/** How many times the page called {@link stop}. */
|
|
12
|
+
stopCount = 0;
|
|
13
|
+
listeners_ = new Map();
|
|
14
|
+
constructor(label = "Fake microphone", settings_ = {}) {
|
|
15
|
+
super();
|
|
16
|
+
this.label = label;
|
|
17
|
+
this.settings_ = settings_;
|
|
18
|
+
}
|
|
19
|
+
/** How many listeners of `type` are registered, those removed by a signal not. */
|
|
20
|
+
listenerCount(type) {
|
|
21
|
+
return this.listeners_.get(type)?.size ?? 0;
|
|
22
|
+
}
|
|
23
|
+
addEventListener(type, listener, options) {
|
|
24
|
+
super.addEventListener(type, listener, options);
|
|
25
|
+
const registered = this.listeners_.get(type) ?? new Set();
|
|
26
|
+
this.listeners_.set(type, registered);
|
|
27
|
+
registered.add(listener);
|
|
28
|
+
if (typeof options === "object") {
|
|
29
|
+
options.signal?.addEventListener("abort", () => registered.delete(listener));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
removeEventListener(type, listener, options) {
|
|
33
|
+
super.removeEventListener(type, listener, options);
|
|
34
|
+
this.listeners_.get(type)?.delete(listener);
|
|
35
|
+
}
|
|
36
|
+
getSettings() {
|
|
37
|
+
return this.settings_;
|
|
38
|
+
}
|
|
39
|
+
stop() {
|
|
40
|
+
this.stopCount += 1;
|
|
41
|
+
this.readyState = "ended";
|
|
42
|
+
}
|
|
43
|
+
/** The device went away. */
|
|
44
|
+
end() {
|
|
45
|
+
this.readyState = "ended";
|
|
46
|
+
this.dispatchEvent(new Event("ended"));
|
|
47
|
+
}
|
|
48
|
+
/** Another application took the microphone. */
|
|
49
|
+
mute() {
|
|
50
|
+
this.muted = true;
|
|
51
|
+
this.dispatchEvent(new Event("mute"));
|
|
52
|
+
}
|
|
53
|
+
unmute() {
|
|
54
|
+
this.muted = false;
|
|
55
|
+
this.dispatchEvent(new Event("unmute"));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** A media stream that stands for the microphone's: one live track by default. */
|
|
59
|
+
export class FakeMediaStream {
|
|
60
|
+
tracks_;
|
|
61
|
+
constructor(tracks_ = [
|
|
62
|
+
new FakeMediaStreamTrack(),
|
|
63
|
+
]) {
|
|
64
|
+
this.tracks_ = tracks_;
|
|
65
|
+
}
|
|
66
|
+
getTracks() {
|
|
67
|
+
return this.tracks_;
|
|
68
|
+
}
|
|
69
|
+
getAudioTracks() {
|
|
70
|
+
return this.getTracks();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A `MediaDevices` whose `getUserMedia` answers with the given outcomes in
|
|
75
|
+
* turn, then opens streams, and which lists the given devices. It opens a
|
|
76
|
+
* stream of one track with {@link trackSettings}, keeps every request and every
|
|
77
|
+
* track it opened, and tells when the devices change.
|
|
78
|
+
*
|
|
79
|
+
* Its methods are plain: spy on them with the test runner's own spies
|
|
80
|
+
* (`vi.spyOn(mediaDevices, "getUserMedia")`) to assert on the calls.
|
|
81
|
+
*/
|
|
82
|
+
export class FakeMediaDevices extends EventTarget {
|
|
83
|
+
outcomes_;
|
|
84
|
+
devices;
|
|
85
|
+
/** What the tracks opened from now on report of themselves. */
|
|
86
|
+
trackSettings = {
|
|
87
|
+
sampleRate: 48000,
|
|
88
|
+
echoCancellation: false,
|
|
89
|
+
noiseSuppression: false,
|
|
90
|
+
autoGainControl: false,
|
|
91
|
+
};
|
|
92
|
+
/** The label of the tracks opened from now on. */
|
|
93
|
+
trackLabel = "Fake microphone";
|
|
94
|
+
/** The constraints of every `getUserMedia` call, in order. */
|
|
95
|
+
requests = [];
|
|
96
|
+
/** Every track opened, in order. */
|
|
97
|
+
tracks = [];
|
|
98
|
+
gates_ = [];
|
|
99
|
+
constructor(outcomes_ = [], devices = []) {
|
|
100
|
+
super();
|
|
101
|
+
this.outcomes_ = outcomes_;
|
|
102
|
+
this.devices = devices;
|
|
103
|
+
}
|
|
104
|
+
/** The tracks opened and neither stopped nor ended. */
|
|
105
|
+
get liveTracks() {
|
|
106
|
+
return this.tracks.filter(({ readyState }) => readyState === "live");
|
|
107
|
+
}
|
|
108
|
+
async getUserMedia(constraints) {
|
|
109
|
+
this.requests.push(constraints);
|
|
110
|
+
const outcome = this.outcomes_.shift() ?? "ok";
|
|
111
|
+
await this.gates_.shift();
|
|
112
|
+
if (typeof outcome !== "string") {
|
|
113
|
+
throw outcome.thrown;
|
|
114
|
+
}
|
|
115
|
+
if (outcome !== "ok") {
|
|
116
|
+
throw new DOMException(`fake ${outcome}`, outcome);
|
|
117
|
+
}
|
|
118
|
+
const track = new FakeMediaStreamTrack(this.trackLabel, {
|
|
119
|
+
...this.trackSettings,
|
|
120
|
+
});
|
|
121
|
+
this.tracks.push(track);
|
|
122
|
+
return new FakeMediaStream([track]);
|
|
123
|
+
}
|
|
124
|
+
async enumerateDevices() {
|
|
125
|
+
return this.devices.map((device) => ({
|
|
126
|
+
...device,
|
|
127
|
+
groupId: "",
|
|
128
|
+
toJSON: () => ({}),
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Makes the next `getUserMedia` wait, as for a permission prompt.
|
|
133
|
+
*
|
|
134
|
+
* @returns What lets it go on.
|
|
135
|
+
*/
|
|
136
|
+
holdNext() {
|
|
137
|
+
const gate = Promise.withResolvers();
|
|
138
|
+
this.gates_.push(gate.promise);
|
|
139
|
+
return gate.resolve;
|
|
140
|
+
}
|
|
141
|
+
/** Plugs `devices` in and out: the browser tells so. */
|
|
142
|
+
changeDevices(devices) {
|
|
143
|
+
this.devices = devices;
|
|
144
|
+
this.dispatchEvent(new Event("devicechange"));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=fake-media-devices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-media-devices.js","sourceRoot":"","sources":["../../src/testing/fake-media-devices.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,MAAM,OAAO,oBAAqB,SAAQ,WAAW;IAS1C,KAAK;IACG,SAAS;IAT3B,UAAU,GAA0B,MAAM,CAAC;IAC3C,KAAK,GAAG,KAAK,CAAC;IACd,mDAAmD;IACnD,SAAS,GAAG,CAAC,CAAC;IAEG,UAAU,GAAG,IAAI,GAAG,EAAwB,CAAC;IAE9D,YACU,KAAK,GAAG,iBAAiB,EACjB,SAAS,GAAsB,EAAE;QAElD,KAAK,EAAE,CAAC;qBAHC,KAAK;yBACG,SAAS;IAG3B,CAAC;IAED,kFAAkF;IAClF,aAAa,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,CAAC;IAC7C,CAAC;IAEQ,gBAAgB,CACxB,IAAY,EACZ,QAAmD,EACnD,OAA2C;QAE3C,KAAK,CAAC,gBAAgB,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;QAChD,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG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MAAM,OAAO,CAAC,MAAM,CAAC;QACtB,CAAC;QACD,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACtB,MAAM,IAAI,YAAY,CAAC,QAAQ,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC;QACpD,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,oBAAoB,CAAC,IAAI,CAAC,UAAU,EAAE;YACvD,GAAG,IAAI,CAAC,aAAa;SACrB,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAExB,OAAO,IAAI,eAAe,CAAC,CAAC,KAAK,CAAC,CAA2B,CAAC;IAC/D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;YACpC,GAAG,MAAM;YACT,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;SAClB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACP,MAAM,IAAI,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;QAC3C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE/B,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED,wDAAwD;IACxD,aAAa,CAAC,OAA8B;QAC3C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;IAC/C,CAAC;CACD"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** A Web MIDI input port that sends the messages it is told to. */
|
|
2
|
+
export declare class FakeMidiInput extends EventTarget {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly name: string | null;
|
|
5
|
+
readonly manufacturer: string | null;
|
|
6
|
+
state: MIDIPortDeviceState;
|
|
7
|
+
/** How many times the page called {@link close}. */
|
|
8
|
+
closeCount: number;
|
|
9
|
+
constructor(id: string, name: string | null, manufacturer: string | null);
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
/** Sends `bytes` as one event that arrived at `timeStamp`. */
|
|
12
|
+
send(bytes: readonly number[], timeStamp: number): void;
|
|
13
|
+
}
|
|
14
|
+
/** A Web MIDI access whose ports are plugged in and out by the test. */
|
|
15
|
+
export declare class FakeMidiAccess extends EventTarget {
|
|
16
|
+
readonly inputs: Map<string, FakeMidiInput>;
|
|
17
|
+
constructor(...inputs: FakeMidiInput[]);
|
|
18
|
+
plug(input: FakeMidiInput): void;
|
|
19
|
+
/**
|
|
20
|
+
* Unplugs `input`: kept in the list as disconnected, as Chrome does, or
|
|
21
|
+
* dropped from it.
|
|
22
|
+
*/
|
|
23
|
+
unplug(input: FakeMidiInput, isKept?: boolean): void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=fake-web-midi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-web-midi.d.ts","sourceRoot":"","sources":["../../src/testing/fake-web-midi.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,qBAAa,aAAc,SAAQ,WAAW;IAM5C,QAAQ,CAAC,EAAE,EAAE,MAAM;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAPrC,KAAK,EAAE,mBAAmB,CAAe;IACzC,oDAAoD;IACpD,UAAU,SAAK;IAEf,YACU,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,GAAG,IAAI,EACnB,YAAY,EAAE,MAAM,GAAG,IAAI,EAGpC;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;IAED,8DAA8D;IAC9D,IAAI,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,IAAI,CAKtD;CACD;AAED,wEAAwE;AACxE,qBAAa,cAAe,SAAQ,WAAW;IAC9C,QAAQ,CAAC,MAAM,6BAAoC;IAEnD,YAAY,GAAG,MAAM,EAAE,aAAa,EAAE,EAKrC;IAED,IAAI,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAI/B;IAED;;;OAGG;IACH,MAAM,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,UAAQ,GAAG,IAAI,CAMjD;CACD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** A Web MIDI input port that sends the messages it is told to. */
|
|
2
|
+
export class FakeMidiInput extends EventTarget {
|
|
3
|
+
id;
|
|
4
|
+
name;
|
|
5
|
+
manufacturer;
|
|
6
|
+
state = "connected";
|
|
7
|
+
/** How many times the page called {@link close}. */
|
|
8
|
+
closeCount = 0;
|
|
9
|
+
constructor(id, name, manufacturer) {
|
|
10
|
+
super();
|
|
11
|
+
this.id = id;
|
|
12
|
+
this.name = name;
|
|
13
|
+
this.manufacturer = manufacturer;
|
|
14
|
+
}
|
|
15
|
+
async close() {
|
|
16
|
+
this.closeCount += 1;
|
|
17
|
+
}
|
|
18
|
+
/** Sends `bytes` as one event that arrived at `timeStamp`. */
|
|
19
|
+
send(bytes, timeStamp) {
|
|
20
|
+
const event = new Event("midimessage");
|
|
21
|
+
Object.defineProperty(event, "data", { value: new Uint8Array(bytes) });
|
|
22
|
+
Object.defineProperty(event, "timeStamp", { value: timeStamp });
|
|
23
|
+
this.dispatchEvent(event);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** A Web MIDI access whose ports are plugged in and out by the test. */
|
|
27
|
+
export class FakeMidiAccess extends EventTarget {
|
|
28
|
+
inputs = new Map();
|
|
29
|
+
constructor(...inputs) {
|
|
30
|
+
super();
|
|
31
|
+
for (const input of inputs) {
|
|
32
|
+
this.inputs.set(input.id, input);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
plug(input) {
|
|
36
|
+
input.state = "connected";
|
|
37
|
+
this.inputs.set(input.id, input);
|
|
38
|
+
this.dispatchEvent(new Event("statechange"));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Unplugs `input`: kept in the list as disconnected, as Chrome does, or
|
|
42
|
+
* dropped from it.
|
|
43
|
+
*/
|
|
44
|
+
unplug(input, isKept = false) {
|
|
45
|
+
input.state = "disconnected";
|
|
46
|
+
if (!isKept) {
|
|
47
|
+
this.inputs.delete(input.id);
|
|
48
|
+
}
|
|
49
|
+
this.dispatchEvent(new Event("statechange"));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=fake-web-midi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-web-midi.js","sourceRoot":"","sources":["../../src/testing/fake-web-midi.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,OAAO,aAAc,SAAQ,WAAW;IAMnC,EAAE;IACF,IAAI;IACJ,YAAY;IAPtB,KAAK,GAAwB,WAAW,CAAC;IACzC,oDAAoD;IACpD,UAAU,GAAG,CAAC,CAAC;IAEf,YACU,EAAU,EACV,IAAmB,EACnB,YAA2B;QAEpC,KAAK,EAAE,CAAC;kBAJC,EAAE;oBACF,IAAI;4BACJ,YAAY;IAGtB,CAAC;IAED,KAAK,CAAC,KAAK;QACV,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,8DAA8D;IAC9D,IAAI,CAAC,KAAwB,EAAE,SAAiB;QAC/C,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;QACvC,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACvE,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;CACD;AAED,wEAAwE;AACxE,MAAM,OAAO,cAAe,SAAQ,WAAW;IACrC,MAAM,GAAG,IAAI,GAAG,EAAyB,CAAC;IAEnD,YAAY,GAAG,MAAuB;QACrC,KAAK,EAAE,CAAC;QACR,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAClC,CAAC;IACF,CAAC;IAED,IAAI,CAAC,KAAoB;QACxB,KAAK,CAAC,KAAK,GAAG,WAAW,CAAC;QAC1B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACjC,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC;IAC9C,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,KAAoB,EAAE,MAAM,GAAG,KAAK;QAC1C,KAAK,CAAC,KAAK,GAAG,cAAc,CAAC;QAC7B,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC;IAC9C,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** How an {@link AudioClockMapping} reads its ticks. */
|
|
2
|
+
export type AudioClockMappingOptions = {
|
|
3
|
+
/** How far back the offset is the minimum over, in milliseconds of page time. */
|
|
4
|
+
readonly windowMs: number;
|
|
5
|
+
/**
|
|
6
|
+
* The step, in milliseconds, that tells the audio clock was re-anchored: a
|
|
7
|
+
* tick that far below the offset, or `readyTicks` steady ticks in a row, all
|
|
8
|
+
* within the window, that far above it.
|
|
9
|
+
*/
|
|
10
|
+
readonly jumpMs: number;
|
|
11
|
+
/**
|
|
12
|
+
* How much, in milliseconds, the offsets of `readyTicks` ticks may differ and
|
|
13
|
+
* still be one step in the audio clock, not jank or a backlog draining (whose
|
|
14
|
+
* offsets keep falling).
|
|
15
|
+
*/
|
|
16
|
+
readonly steadyMs: number;
|
|
17
|
+
/** How many ticks since a reset make the mapping ready; a positive integer. */
|
|
18
|
+
readonly readyTicks: number;
|
|
19
|
+
/**
|
|
20
|
+
* How far the minimum lands above the true offset, taken off the offset, in
|
|
21
|
+
* milliseconds. Default 0.
|
|
22
|
+
*/
|
|
23
|
+
readonly biasMs?: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The mapping between an audio clock (the time of the samples an `AudioContext`
|
|
27
|
+
* or an `AudioWorklet` has processed) and `performance.now()`, from ticks the
|
|
28
|
+
* audio thread posts: a tick carries the audio time it was posted at, and the
|
|
29
|
+
* page notes the time it received it.
|
|
30
|
+
*
|
|
31
|
+
* A tick can only arrive late, never early, so the offset is the minimum of
|
|
32
|
+
* `received − audio time` over a window: jank on the page's thread falls out,
|
|
33
|
+
* and what is left is the posting delay, within one render quantum of the true
|
|
34
|
+
* one (`biasMs` takes off a steady remainder). The mapping starts again when
|
|
35
|
+
* the clock is re-anchored: a context suspended and resumed, a worklet
|
|
36
|
+
* restarted ({@link AudioClockMapping.reset}), or a step the ticks show. A step
|
|
37
|
+
* up is taken from `readyTicks` ticks in a row within the window; when that
|
|
38
|
+
* many do not fit in it, the window adopts the new offset alone once the older
|
|
39
|
+
* ticks leave it.
|
|
40
|
+
*
|
|
41
|
+
* It reads no clock and no global: the caller passes both times, so it runs in
|
|
42
|
+
* a worker or under Node as well.
|
|
43
|
+
*
|
|
44
|
+
* Time complexity: O(n) per {@link AudioClockMapping.observe}, n the ticks in
|
|
45
|
+
* the window (a scan for their minimum, and O(readyTicks) more when a step up
|
|
46
|
+
* is checked); O(n) for {@link AudioClockMapping.offsetMs},
|
|
47
|
+
* {@link AudioClockMapping.toPageMs} and {@link AudioClockMapping.toAudioMs}.
|
|
48
|
+
* Memory: O(n), the most ticks the window has held, in a ring buffer that grows
|
|
49
|
+
* by doubling and never shrinks: a tick allocates nothing otherwise.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const mapping = new AudioClockMapping({
|
|
53
|
+
* windowMs: 2000,
|
|
54
|
+
* jumpMs: 50,
|
|
55
|
+
* steadyMs: 5,
|
|
56
|
+
* readyTicks: 5,
|
|
57
|
+
* });
|
|
58
|
+
* port.onmessage = ({ data }) =>
|
|
59
|
+
* mapping.observe((data.frame / sampleRate) * 1000, performance.now());
|
|
60
|
+
* if (mapping.isReady) {
|
|
61
|
+
* const pageMs = mapping.toPageMs(dueAudioMs);
|
|
62
|
+
* }
|
|
63
|
+
*/
|
|
64
|
+
export declare class AudioClockMapping {
|
|
65
|
+
private readonly windowMs_;
|
|
66
|
+
private readonly jumpMs_;
|
|
67
|
+
private readonly steadyMs_;
|
|
68
|
+
private readonly readyTicks_;
|
|
69
|
+
private readonly biasMs_;
|
|
70
|
+
private pages_;
|
|
71
|
+
private offsets_;
|
|
72
|
+
private head_;
|
|
73
|
+
private size_;
|
|
74
|
+
private runLength_;
|
|
75
|
+
private tickCount_;
|
|
76
|
+
private resetCount_;
|
|
77
|
+
private lastAudioMs_;
|
|
78
|
+
private lastPageMs_;
|
|
79
|
+
/** @throws {RangeError} On an option that is not valid. */
|
|
80
|
+
constructor(options: AudioClockMappingOptions);
|
|
81
|
+
/** Whether `readyTicks` ticks have come since the last reset. */
|
|
82
|
+
get isReady(): boolean;
|
|
83
|
+
/** How many times the mapping started again, by {@link reset} or by a step. */
|
|
84
|
+
get resetCount(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Page time minus audio time, in milliseconds, or `undefined` until the
|
|
87
|
+
* mapping is ready.
|
|
88
|
+
*/
|
|
89
|
+
get offsetMs(): number | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Notes a tick: the audio time it was posted at and the page time it arrived.
|
|
92
|
+
*
|
|
93
|
+
* @throws {RangeError} On a time that is not finite, or a page time earlier
|
|
94
|
+
* than the previous tick's.
|
|
95
|
+
*/
|
|
96
|
+
observe(audioMs: number, pageMs: number): void;
|
|
97
|
+
/**
|
|
98
|
+
* Starts again: for the audio clock re-anchored, as when the context's
|
|
99
|
+
* `statechange` reports it suspended, or the worklet restarted.
|
|
100
|
+
*/
|
|
101
|
+
reset(): void;
|
|
102
|
+
/**
|
|
103
|
+
* The page time of `audioMs`.
|
|
104
|
+
*
|
|
105
|
+
* @throws {Error} Before the mapping is ready.
|
|
106
|
+
*/
|
|
107
|
+
toPageMs(audioMs: number): number;
|
|
108
|
+
/**
|
|
109
|
+
* The audio time of `pageMs`.
|
|
110
|
+
*
|
|
111
|
+
* @throws {Error} Before the mapping is ready.
|
|
112
|
+
*/
|
|
113
|
+
toAudioMs(pageMs: number): number;
|
|
114
|
+
private restart_;
|
|
115
|
+
private readyOffset_;
|
|
116
|
+
private slot_;
|
|
117
|
+
private minimumOffset_;
|
|
118
|
+
private windowMinimum_;
|
|
119
|
+
private isStepDown_;
|
|
120
|
+
private add_;
|
|
121
|
+
private grow_;
|
|
122
|
+
private trackStepUp_;
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=audio-clock-mapping.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file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AED;;;;;OAKG;IACH,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAe7C;IAED;;;OAGG;IACH,KAAK,IAAI,IAAI,CAGZ;IAED;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEhC;IAED;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEhC;IAED,OAAO,CAAC,QAAQ;IAQhB,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,KAAK;IAKb,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,IAAI;IAgBZ,OAAO,CAAC,KAAK;IAcb,OAAO,CAAC,YAAY;CAwCpB"}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
const INITIAL_CAPACITY_ = 16;
|
|
2
|
+
/**
|
|
3
|
+
* The mapping between an audio clock (the time of the samples an `AudioContext`
|
|
4
|
+
* or an `AudioWorklet` has processed) and `performance.now()`, from ticks the
|
|
5
|
+
* audio thread posts: a tick carries the audio time it was posted at, and the
|
|
6
|
+
* page notes the time it received it.
|
|
7
|
+
*
|
|
8
|
+
* A tick can only arrive late, never early, so the offset is the minimum of
|
|
9
|
+
* `received − audio time` over a window: jank on the page's thread falls out,
|
|
10
|
+
* and what is left is the posting delay, within one render quantum of the true
|
|
11
|
+
* one (`biasMs` takes off a steady remainder). The mapping starts again when
|
|
12
|
+
* the clock is re-anchored: a context suspended and resumed, a worklet
|
|
13
|
+
* restarted ({@link AudioClockMapping.reset}), or a step the ticks show. A step
|
|
14
|
+
* up is taken from `readyTicks` ticks in a row within the window; when that
|
|
15
|
+
* many do not fit in it, the window adopts the new offset alone once the older
|
|
16
|
+
* ticks leave it.
|
|
17
|
+
*
|
|
18
|
+
* It reads no clock and no global: the caller passes both times, so it runs in
|
|
19
|
+
* a worker or under Node as well.
|
|
20
|
+
*
|
|
21
|
+
* Time complexity: O(n) per {@link AudioClockMapping.observe}, n the ticks in
|
|
22
|
+
* the window (a scan for their minimum, and O(readyTicks) more when a step up
|
|
23
|
+
* is checked); O(n) for {@link AudioClockMapping.offsetMs},
|
|
24
|
+
* {@link AudioClockMapping.toPageMs} and {@link AudioClockMapping.toAudioMs}.
|
|
25
|
+
* Memory: O(n), the most ticks the window has held, in a ring buffer that grows
|
|
26
|
+
* by doubling and never shrinks: a tick allocates nothing otherwise.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const mapping = new AudioClockMapping({
|
|
30
|
+
* windowMs: 2000,
|
|
31
|
+
* jumpMs: 50,
|
|
32
|
+
* steadyMs: 5,
|
|
33
|
+
* readyTicks: 5,
|
|
34
|
+
* });
|
|
35
|
+
* port.onmessage = ({ data }) =>
|
|
36
|
+
* mapping.observe((data.frame / sampleRate) * 1000, performance.now());
|
|
37
|
+
* if (mapping.isReady) {
|
|
38
|
+
* const pageMs = mapping.toPageMs(dueAudioMs);
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
export class AudioClockMapping {
|
|
42
|
+
windowMs_;
|
|
43
|
+
jumpMs_;
|
|
44
|
+
steadyMs_;
|
|
45
|
+
readyTicks_;
|
|
46
|
+
biasMs_;
|
|
47
|
+
// The ticks of the window, oldest first, in a ring buffer: the page time each
|
|
48
|
+
// arrived at and its offset.
|
|
49
|
+
pages_ = new Float64Array(INITIAL_CAPACITY_);
|
|
50
|
+
offsets_ = new Float64Array(INITIAL_CAPACITY_);
|
|
51
|
+
head_ = 0;
|
|
52
|
+
size_ = 0;
|
|
53
|
+
// How many of the latest ticks of the window sit each more than `jumpMs`
|
|
54
|
+
// above the minimum of the ticks before it, up to `readyTicks`.
|
|
55
|
+
runLength_ = 0;
|
|
56
|
+
tickCount_ = 0;
|
|
57
|
+
resetCount_ = 0;
|
|
58
|
+
lastAudioMs_ = -Infinity;
|
|
59
|
+
lastPageMs_ = -Infinity;
|
|
60
|
+
/** @throws {RangeError} On an option that is not valid. */
|
|
61
|
+
constructor(options) {
|
|
62
|
+
const { windowMs, jumpMs, steadyMs, readyTicks, biasMs = 0 } = options;
|
|
63
|
+
if (!(Number.isFinite(windowMs) && windowMs > 0)) {
|
|
64
|
+
throw new RangeError("windowMs must be a positive number");
|
|
65
|
+
}
|
|
66
|
+
if (!(Number.isFinite(jumpMs) && jumpMs >= 0)) {
|
|
67
|
+
throw new RangeError("jumpMs must be a non-negative number");
|
|
68
|
+
}
|
|
69
|
+
if (!(Number.isFinite(steadyMs) && steadyMs >= 0)) {
|
|
70
|
+
throw new RangeError("steadyMs must be a non-negative number");
|
|
71
|
+
}
|
|
72
|
+
if (!Number.isInteger(readyTicks) || readyTicks < 1) {
|
|
73
|
+
throw new RangeError("readyTicks must be a positive integer");
|
|
74
|
+
}
|
|
75
|
+
if (!Number.isFinite(biasMs)) {
|
|
76
|
+
throw new RangeError("biasMs must be a finite number");
|
|
77
|
+
}
|
|
78
|
+
this.windowMs_ = windowMs;
|
|
79
|
+
this.jumpMs_ = jumpMs;
|
|
80
|
+
this.steadyMs_ = steadyMs;
|
|
81
|
+
this.readyTicks_ = readyTicks;
|
|
82
|
+
this.biasMs_ = biasMs;
|
|
83
|
+
}
|
|
84
|
+
/** Whether `readyTicks` ticks have come since the last reset. */
|
|
85
|
+
get isReady() {
|
|
86
|
+
return this.tickCount_ >= this.readyTicks_;
|
|
87
|
+
}
|
|
88
|
+
/** How many times the mapping started again, by {@link reset} or by a step. */
|
|
89
|
+
get resetCount() {
|
|
90
|
+
return this.resetCount_;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Page time minus audio time, in milliseconds, or `undefined` until the
|
|
94
|
+
* mapping is ready.
|
|
95
|
+
*/
|
|
96
|
+
get offsetMs() {
|
|
97
|
+
if (!this.isReady) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
return this.windowMinimum_() - this.biasMs_;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Notes a tick: the audio time it was posted at and the page time it arrived.
|
|
104
|
+
*
|
|
105
|
+
* @throws {RangeError} On a time that is not finite, or a page time earlier
|
|
106
|
+
* than the previous tick's.
|
|
107
|
+
*/
|
|
108
|
+
observe(audioMs, pageMs) {
|
|
109
|
+
if (!(Number.isFinite(audioMs) && Number.isFinite(pageMs))) {
|
|
110
|
+
throw new RangeError("a tick's times must be finite");
|
|
111
|
+
}
|
|
112
|
+
if (pageMs < this.lastPageMs_) {
|
|
113
|
+
throw new RangeError("a tick cannot arrive before the previous one");
|
|
114
|
+
}
|
|
115
|
+
const offsetMs = pageMs - audioMs;
|
|
116
|
+
if (audioMs < this.lastAudioMs_ || this.isStepDown_(offsetMs)) {
|
|
117
|
+
this.restart_();
|
|
118
|
+
}
|
|
119
|
+
this.lastAudioMs_ = audioMs;
|
|
120
|
+
this.lastPageMs_ = pageMs;
|
|
121
|
+
this.add_(pageMs, offsetMs);
|
|
122
|
+
this.trackStepUp_(offsetMs);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Starts again: for the audio clock re-anchored, as when the context's
|
|
126
|
+
* `statechange` reports it suspended, or the worklet restarted.
|
|
127
|
+
*/
|
|
128
|
+
reset() {
|
|
129
|
+
this.restart_();
|
|
130
|
+
this.lastAudioMs_ = -Infinity;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The page time of `audioMs`.
|
|
134
|
+
*
|
|
135
|
+
* @throws {Error} Before the mapping is ready.
|
|
136
|
+
*/
|
|
137
|
+
toPageMs(audioMs) {
|
|
138
|
+
return audioMs + this.readyOffset_();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The audio time of `pageMs`.
|
|
142
|
+
*
|
|
143
|
+
* @throws {Error} Before the mapping is ready.
|
|
144
|
+
*/
|
|
145
|
+
toAudioMs(pageMs) {
|
|
146
|
+
return pageMs - this.readyOffset_();
|
|
147
|
+
}
|
|
148
|
+
restart_() {
|
|
149
|
+
this.head_ = 0;
|
|
150
|
+
this.size_ = 0;
|
|
151
|
+
this.runLength_ = 0;
|
|
152
|
+
this.tickCount_ = 0;
|
|
153
|
+
this.resetCount_ += 1;
|
|
154
|
+
}
|
|
155
|
+
readyOffset_() {
|
|
156
|
+
const offset = this.offsetMs;
|
|
157
|
+
if (offset === undefined) {
|
|
158
|
+
throw new Error("the audio clock mapping is not ready");
|
|
159
|
+
}
|
|
160
|
+
return offset;
|
|
161
|
+
}
|
|
162
|
+
slot_(position) {
|
|
163
|
+
return (this.head_ + position) % this.pages_.length;
|
|
164
|
+
}
|
|
165
|
+
// The least offset of the `count` oldest ticks of the window.
|
|
166
|
+
minimumOffset_(count) {
|
|
167
|
+
let minimum = Infinity;
|
|
168
|
+
for (let position = 0; position < count; position += 1) {
|
|
169
|
+
minimum = Math.min(minimum, this.offsets_[this.slot_(position)]);
|
|
170
|
+
}
|
|
171
|
+
return minimum;
|
|
172
|
+
}
|
|
173
|
+
windowMinimum_() {
|
|
174
|
+
return this.minimumOffset_(this.size_);
|
|
175
|
+
}
|
|
176
|
+
isStepDown_(offsetMs) {
|
|
177
|
+
return this.size_ > 0 && offsetMs < this.windowMinimum_() - this.jumpMs_;
|
|
178
|
+
}
|
|
179
|
+
add_(pageMs, offsetMs) {
|
|
180
|
+
if (this.size_ === this.pages_.length) {
|
|
181
|
+
this.grow_();
|
|
182
|
+
}
|
|
183
|
+
const slot = this.slot_(this.size_);
|
|
184
|
+
this.pages_[slot] = pageMs;
|
|
185
|
+
this.offsets_[slot] = offsetMs;
|
|
186
|
+
this.size_ += 1;
|
|
187
|
+
this.tickCount_ += 1;
|
|
188
|
+
const oldest = pageMs - this.windowMs_;
|
|
189
|
+
while (this.pages_[this.head_] < oldest) {
|
|
190
|
+
this.head_ = (this.head_ + 1) % this.pages_.length;
|
|
191
|
+
this.size_ -= 1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
grow_() {
|
|
195
|
+
const capacity = this.pages_.length * 2;
|
|
196
|
+
const pages = new Float64Array(capacity);
|
|
197
|
+
const offsets = new Float64Array(capacity);
|
|
198
|
+
for (let position = 0; position < this.size_; position += 1) {
|
|
199
|
+
const slot = this.slot_(position);
|
|
200
|
+
pages[position] = this.pages_[slot];
|
|
201
|
+
offsets[position] = this.offsets_[slot];
|
|
202
|
+
}
|
|
203
|
+
this.pages_ = pages;
|
|
204
|
+
this.offsets_ = offsets;
|
|
205
|
+
this.head_ = 0;
|
|
206
|
+
}
|
|
207
|
+
trackStepUp_(offsetMs) {
|
|
208
|
+
const earlierCount = this.size_ - 1;
|
|
209
|
+
if (earlierCount === 0 ||
|
|
210
|
+
offsetMs <= this.minimumOffset_(earlierCount) + this.jumpMs_) {
|
|
211
|
+
this.runLength_ = 0;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
// The run is the latest ticks of the window: those that left it are no
|
|
215
|
+
// longer part of it.
|
|
216
|
+
this.runLength_ =
|
|
217
|
+
Math.min(this.runLength_, earlierCount, this.readyTicks_ - 1) + 1;
|
|
218
|
+
if (this.runLength_ < this.readyTicks_) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// A step in the audio clock leaves the offset flat from then on, within
|
|
222
|
+
// jitter. Jank spreads the offsets, and a backlog draining after a freeze
|
|
223
|
+
// is ticks each handled in a task shorter than their interval, so their
|
|
224
|
+
// offsets keep falling: a fall of more than half the tolerance is no step.
|
|
225
|
+
const first = this.size_ - this.runLength_;
|
|
226
|
+
let highest = -Infinity;
|
|
227
|
+
let lowest = Infinity;
|
|
228
|
+
for (let position = first; position < this.size_; position += 1) {
|
|
229
|
+
const offset = this.offsets_[this.slot_(position)];
|
|
230
|
+
highest = Math.max(highest, offset);
|
|
231
|
+
lowest = Math.min(lowest, offset);
|
|
232
|
+
}
|
|
233
|
+
const fall = this.offsets_[this.slot_(first)] - offsetMs;
|
|
234
|
+
if (highest - lowest > this.steadyMs_ || fall > this.steadyMs_ / 2) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
// Start again from the run alone.
|
|
238
|
+
this.head_ = this.slot_(first);
|
|
239
|
+
this.size_ = this.runLength_;
|
|
240
|
+
this.tickCount_ = this.runLength_;
|
|
241
|
+
this.runLength_ = 0;
|
|
242
|
+
this.resetCount_ += 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=audio-clock-mapping.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-clock-mapping.js","sourceRoot":"","sources":["../../src/web-audio/audio-clock-mapping.ts"],"names":[],"mappings":"AAyBA,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,OAAO,iBAAiB;IACZ,SAAS,CAAS;IAClB,OAAO,CAAS;IAChB,SAAS,CAAS;IAClB,WAAW,CAAS;IACpB,OAAO,CAAS;IACjC,8EAA8E;IAC9E,6BAA6B;IACrB,MAAM,GAAG,IAAI,YAAY,CAAC,iBAAiB,CAAC,CAAC;IAC7C,QAAQ,GAAG,IAAI,YAAY,CAAC,iBAAiB,CAAC,CAAC;IAC/C,KAAK,GAAG,CAAC,CAAC;IACV,KAAK,GAAG,CAAC,CAAC;IAClB,yEAAyE;IACzE,gEAAgE;IACxD,UAAU,GAAG,CAAC,CAAC;IACf,UAAU,GAAG,CAAC,CAAC;IACf,WAAW,GAAG,CAAC,CAAC;IAChB,YAAY,GAAG,CAAC,QAAQ,CAAC;IACzB,WAAW,GAAG,CAAC,QAAQ,CAAC;IAEhC,2DAA2D;IAC3D,YAAY,OAAiC;QAC5C,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;QACvE,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC,EAAE,CA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