reactor-effect-browser 0.0.0-reserved.0 → 0.3.0-rc.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 +201 -0
- package/NOTICE +15 -0
- package/README.md +49 -2
- package/dist/_internal/audio-packet.d.ts +13 -0
- package/dist/_internal/audio-packet.d.ts.map +1 -0
- package/dist/_internal/audio-packet.js +43 -0
- package/dist/_internal/audio-packet.js.map +1 -0
- package/dist/_internal/audio.d.ts +54 -0
- package/dist/_internal/audio.d.ts.map +1 -0
- package/dist/_internal/audio.js +242 -0
- package/dist/_internal/audio.js.map +1 -0
- package/dist/_internal/media.d.ts +80 -0
- package/dist/_internal/media.d.ts.map +1 -0
- package/dist/_internal/media.js +414 -0
- package/dist/_internal/media.js.map +1 -0
- package/dist/_internal/pcm-worklet.d.ts +2 -0
- package/dist/_internal/pcm-worklet.d.ts.map +1 -0
- package/dist/_internal/pcm-worklet.js +51 -0
- package/dist/_internal/pcm-worklet.js.map +1 -0
- package/dist/_internal/peer.d.ts +30 -0
- package/dist/_internal/peer.d.ts.map +1 -0
- package/dist/_internal/peer.js +294 -0
- package/dist/_internal/peer.js.map +1 -0
- package/dist/_internal/recording.d.ts +15 -0
- package/dist/_internal/recording.d.ts.map +1 -0
- package/dist/_internal/recording.js +9 -0
- package/dist/_internal/recording.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/notices/effect-LICENSE +21 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Reactor Technologies, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
reactor-effect-browser
|
|
2
|
+
|
|
3
|
+
This independent SDK is not an official Reactor SDK.
|
|
4
|
+
|
|
5
|
+
The browser-media implementation incorporates the user-supplied
|
|
6
|
+
reactor-effect-client reference source. Protocol material, the canonical
|
|
7
|
+
session implementation and their upstream notices are distributed with the
|
|
8
|
+
reactor-effect-client package this host depends on.
|
|
9
|
+
|
|
10
|
+
Transport uses the browser's built-in RTCPeerConnection and media tracks; no
|
|
11
|
+
WebRTC engine is shipped by this package.
|
|
12
|
+
|
|
13
|
+
Effect is MIT-licensed. The package declares Effect as a peer dependency; its
|
|
14
|
+
license is retained in notices/effect-LICENSE. Development tooling and
|
|
15
|
+
node_modules are excluded from the npm package.
|
package/README.md
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
# reactor-effect-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser WebRTC host for [`reactor-effect-client`](https://www.npmjs.com/package/reactor-effect-client). It selects the built-in `RTCPeerConnection` and browser media tracks as the transport for the canonical `Session`, and exposes generation-scoped tracks, track-to-frame/audio conversion, and recording helpers.
|
|
4
4
|
|
|
5
|
-
This
|
|
5
|
+
This is not an official Reactor SDK.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install reactor-effect-client reactor-effect-browser effect@4.0.0-rc.115
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`reactor-effect-client` and Effect `4.0.0-rc.115` are exact peer dependencies. The package needs a secure context with WebRTC and Web Crypto. It has no Node dependency: its declarations compile with DOM types and without `@types/node`, and the workspace's installed-package check bundles it for the browser and runs that bundle without the Node `Buffer` global.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
`Browser.layer` supplies the `PeerFactory` for `Reactor.layer()`. Constructing a factory makes no allocation. Effect HTTP and crypto services remain explicit.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Effect, Layer } from "effect";
|
|
21
|
+
import * as Reactor from "reactor-effect-client";
|
|
22
|
+
import * as Browser from "reactor-effect-browser";
|
|
23
|
+
|
|
24
|
+
const clientLayer = Reactor.layer({ apiUrl: "https://api.reactor.inc" }).pipe(
|
|
25
|
+
Layer.provide(Layer.mergeAll(Reactor.FetchHttp.layer, Browser.layer)),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const useTrack = Effect.scoped(
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const client = yield* Reactor.Client;
|
|
31
|
+
const session = yield* client.createConnected({ model: "your-model" });
|
|
32
|
+
const media = yield* Browser.media(session);
|
|
33
|
+
return yield* media.track("main_video");
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The application also provides Effect's `Crypto` service, backed by Web Crypto.
|
|
39
|
+
|
|
40
|
+
`Browser.media(session)` obtains the negotiated generation; `media.track(name)` acquires a scoped `MediaStreamTrack`, and `media.publish(name, track)` publishes through that generation. Media values stay bound to the generation that negotiated them. A reconnect creates a new generation; existing readers end or fail with their source, and applications obtain the new generation explicitly.
|
|
41
|
+
|
|
42
|
+
The entry point also exports `videoFrames`, `audioSamples`, `webAudioSamples`, `audioContext`, `play`, `nextPresentation` and the `Recording` namespace, whose `downloadClip(coordinator, clip, options)` transfers a prepared recording within a caller wall deadline through the portable `Coordinator.Client` from `Coordinator.make()`; it is the same operation as `coordinator.downloadClip(clip, options)`.
|
|
43
|
+
|
|
44
|
+
If the host lacks `RTCPeerConnection` or `MediaStream`, building `Browser.layer` fails with `UnsupportedHost`, before any coordinator request can be made.
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
This package is built and tested from the workspace root; see the repository [CONTRIBUTING](https://github.com/mannyc2/reactor-effect-client/blob/main/CONTRIBUTING.md). Its tests are simulated-host policy tests: they do not establish browser WebRTC or codec support. Real Chrome interoperability with the native host is exercised by the workspace's `integration` project.
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
Apache-2.0. See [NOTICE](./NOTICE) and [`notices/`](./notices/).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** A Web Audio rendering-clock block, not an RTP or capture timestamp. */
|
|
2
|
+
export interface WebAudioSample {
|
|
3
|
+
readonly format: "f32-planar";
|
|
4
|
+
readonly planes: readonly Float32Array<ArrayBuffer>[];
|
|
5
|
+
readonly sampleRate: number;
|
|
6
|
+
readonly frames: number;
|
|
7
|
+
readonly timestampUs: number;
|
|
8
|
+
readonly contextFrame: number;
|
|
9
|
+
readonly skippedFrames: number;
|
|
10
|
+
readonly clock: "audio-context-render";
|
|
11
|
+
}
|
|
12
|
+
export declare const decodeAudioPacket: (input: unknown, maxBytes: number) => WebAudioSample;
|
|
13
|
+
//# sourceMappingURL=audio-packet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-packet.d.ts","sourceRoot":"","sources":["../../src/_internal/audio-packet.ts"],"names":[],"mappings":"AAEA,0EAA0E;AAC1E,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,SAAS,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;IACtD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,sBAAsB,CAAC;CACxC;AACD,eAAO,MAAM,iBAAiB,UAAW,OAAO,YAAY,MAAM,KAAG,cAyCpE,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ReactorError } from "reactor-effect-client";
|
|
2
|
+
import { record, finite } from "reactor-effect-client/host";
|
|
3
|
+
export const decodeAudioPacket = (input, maxBytes) => {
|
|
4
|
+
const p = record(input, "AudioWorklet packet");
|
|
5
|
+
if (p.type !== "pcm")
|
|
6
|
+
throw ReactorError.fromCode("Protocol", "unexpected AudioWorklet packet");
|
|
7
|
+
const frames = finite(p.frames, "PCM frames");
|
|
8
|
+
const sampleRate = finite(p.sampleRate, "sample rate");
|
|
9
|
+
if (!Number.isSafeInteger(frames) ||
|
|
10
|
+
frames < 1 ||
|
|
11
|
+
frames > 16_384 ||
|
|
12
|
+
!Number.isSafeInteger(sampleRate) ||
|
|
13
|
+
sampleRate < 1 ||
|
|
14
|
+
sampleRate > 384_000) {
|
|
15
|
+
throw ReactorError.fromCode("Protocol", "invalid PCM frame count or sample rate");
|
|
16
|
+
}
|
|
17
|
+
const frame = finite(p.frame, "context frame"), skipped = finite(p.skipped, "skipped frames");
|
|
18
|
+
if (!Number.isSafeInteger(frame) || frame < 0 || !Number.isSafeInteger(skipped) || skipped < 0)
|
|
19
|
+
throw ReactorError.fromCode("Protocol", "invalid render-clock frame counter");
|
|
20
|
+
if (!Array.isArray(p.planes) || p.planes.length < 1 || p.planes.length > 32)
|
|
21
|
+
throw ReactorError.fromCode("Protocol", "invalid PCM channel count");
|
|
22
|
+
if (p.planes.length * frames * 4 > maxBytes)
|
|
23
|
+
throw ReactorError.fromCode("Overflow", "PCM sample byte bound exceeded");
|
|
24
|
+
const planes = p.planes.map((plane) => {
|
|
25
|
+
if (!(plane instanceof ArrayBuffer) || plane.byteLength !== frames * 4)
|
|
26
|
+
throw ReactorError.fromCode("Protocol", "PCM plane length/type mismatch");
|
|
27
|
+
const data = new Float32Array(plane);
|
|
28
|
+
if (!data.every(Number.isFinite))
|
|
29
|
+
throw ReactorError.fromCode("Protocol", "nonfinite PCM sample");
|
|
30
|
+
return data;
|
|
31
|
+
});
|
|
32
|
+
return Object.freeze({
|
|
33
|
+
format: "f32-planar",
|
|
34
|
+
planes: Object.freeze(planes),
|
|
35
|
+
frames,
|
|
36
|
+
sampleRate,
|
|
37
|
+
timestampUs: (frame * 1_000_000) / sampleRate,
|
|
38
|
+
contextFrame: frame,
|
|
39
|
+
skippedFrames: skipped,
|
|
40
|
+
clock: "audio-context-render",
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=audio-packet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-packet.js","sourceRoot":"","sources":["../../src/_internal/audio-packet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAY5D,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAAE,QAAgB,EAAkB,EAAE;IACpF,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,qBAAqB,CAAC,CAAC;IAC/C,IAAI,CAAC,CAAC,IAAI,KAAK,KAAK;QAAE,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,gCAAgC,CAAC,CAAC;IAChG,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAC9C,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IACvD,IACE,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC;QAC7B,MAAM,GAAG,CAAC;QACV,MAAM,GAAG,MAAM;QACf,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC;QACjC,UAAU,GAAG,CAAC;QACd,UAAU,GAAG,OAAO,EACpB,CAAC;QACD,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,wCAAwC,CAAC,CAAC;IACpF,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,eAAe,CAAC,EAC5C,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;IAChD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,OAAO,GAAG,CAAC;QAC5F,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,oCAAoC,CAAC,CAAC;IAChF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,EAAE;QACzE,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,2BAA2B,CAAC,CAAC;IACvE,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,QAAQ;QACzC,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,gCAAgC,CAAC,CAAC;IAC5E,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAc,EAAE,EAAE;QAC7C,IAAI,CAAC,CAAC,KAAK,YAAY,WAAW,CAAC,IAAI,KAAK,CAAC,UAAU,KAAK,MAAM,GAAG,CAAC;YACpE,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,gCAAgC,CAAC,CAAC;QAC5E,MAAM,IAAI,GAAG,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;YAC9B,MAAM,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,sBAAsB,CAAC,CAAC;QAClE,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,MAAM,EAAE,YAAY;QACpB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;QAC7B,MAAM;QACN,UAAU;QACV,WAAW,EAAE,CAAC,KAAK,GAAG,SAAS,CAAC,GAAG,UAAU;QAC7C,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,OAAO;QACtB,KAAK,EAAE,sBAAsB;KAC9B,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as Duration from "effect/Duration";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Scope from "effect/Scope";
|
|
4
|
+
import type * as Stream from "effect/Stream";
|
|
5
|
+
import { ReactorError } from "reactor-effect-client";
|
|
6
|
+
import type { WebAudioSample } from "./audio-packet.js";
|
|
7
|
+
export type { WebAudioSample } from "./audio-packet.js";
|
|
8
|
+
/** Call from an ordinary start click. No autoplay-policy workaround is attempted.
|
|
9
|
+
* The scope owns this context; audio streams borrow it and own their nodes/track clones. */
|
|
10
|
+
export interface AudioContextOptions {
|
|
11
|
+
readonly sampleRate?: number;
|
|
12
|
+
/**
|
|
13
|
+
* How long resuming the context, and closing it on release, may each take; 5
|
|
14
|
+
* seconds by default and at most 1 minute. A bare number is milliseconds.
|
|
15
|
+
*/
|
|
16
|
+
readonly transitionTimeout?: Duration.Input | undefined;
|
|
17
|
+
/** @deprecated Removed in 0.3.0: use `transitionTimeout` (a bare number is milliseconds). */
|
|
18
|
+
readonly timeoutMs?: never;
|
|
19
|
+
}
|
|
20
|
+
export declare const audioContext: (options?: AudioContextOptions) => Effect.Effect<AudioContext, ReactorError, Scope.Scope>;
|
|
21
|
+
export interface WebAudioOptions {
|
|
22
|
+
/** A running built-in context; caller or audioContext() scope retains ownership. */
|
|
23
|
+
readonly context: AudioContext;
|
|
24
|
+
/** Serve dist/browser/_internal/pcm-worklet.js locally. A bundler must preserve/copy this asset. */
|
|
25
|
+
readonly workletUrl?: string | URL;
|
|
26
|
+
readonly maxSampleBytes?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Each consumption owns a muted HTMLAudioElement and waits for its ordinary
|
|
29
|
+
* play(). This deadline is separate from worklet registration and individual
|
|
30
|
+
* PCM reads; 5 seconds by default and at most 1 minute. A bare number is
|
|
31
|
+
* milliseconds.
|
|
32
|
+
*/
|
|
33
|
+
readonly activationTimeout?: Duration.Input | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* How long registering the worklet, and then each PCM read, may take; 10
|
|
36
|
+
* seconds by default and at most 10 minutes. A bare number is milliseconds.
|
|
37
|
+
*/
|
|
38
|
+
readonly readTimeout?: Duration.Input | undefined;
|
|
39
|
+
/** @deprecated Removed in 0.3.0: use `activationTimeout` (a bare number is milliseconds). */
|
|
40
|
+
readonly activationTimeoutMs?: never;
|
|
41
|
+
/** @deprecated Removed in 0.3.0: use `readTimeout` (a bare number is milliseconds). */
|
|
42
|
+
readonly readTimeoutMs?: never;
|
|
43
|
+
}
|
|
44
|
+
/** Window-realm PCM with OWNED muted playback activation, not a bare Web Audio tap.
|
|
45
|
+
* Each consumption clones the borrowed source once, attaches that clone to an owned muted
|
|
46
|
+
* HTMLAudioElement, awaits ordinary play(), and uses the same MediaStream for Web Audio.
|
|
47
|
+
* Activation rejection/timeout is an error, not silently successful PCM. No extra play()
|
|
48
|
+
* call is required; no autoplay policy is overridden. The caller owns the running context.
|
|
49
|
+
* One JS output slot and one credited worklet block; skipped render frames are reported.
|
|
50
|
+
* Closing/interrupting the stream releases the sink, clone, nodes, ports and timers.
|
|
51
|
+
* A stopped source lease fails Disconnected; reacquire a track/stream after reconnect.
|
|
52
|
+
* No ScriptProcessorNode, getUserMedia, device permission or native engine is used. */
|
|
53
|
+
export declare const webAudioSamples: (source: MediaStreamTrack, options: WebAudioOptions) => Stream.Stream<WebAudioSample, ReactorError>;
|
|
54
|
+
//# sourceMappingURL=audio.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio.d.ts","sourceRoot":"","sources":["../../src/_internal/audio.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AASrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAyBxD;4FAC4F;AAC5F,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC;IACxD,6FAA6F;IAC7F,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC;CAC5B;AACD,eAAO,MAAM,YAAY,aACd,mBAAmB,KAC3B,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,CAAC,KAAK,CAsDpD,CAAC;AACL,MAAM,WAAW,eAAe;IAC9B,oFAAoF;IACpF,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,oGAAoG;IACpG,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IACnC,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC;IACxD;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC;IAClD,6FAA6F;IAC7F,QAAQ,CAAC,mBAAmB,CAAC,EAAE,KAAK,CAAC;IACrC,uFAAuF;IACvF,QAAQ,CAAC,aAAa,CAAC,EAAE,KAAK,CAAC;CAChC;AAkBD;;;;;;;;uFAQuF;AACvF,eAAO,MAAM,eAAe,WAClB,gBAAgB,WACf,eAAe,KACvB,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,YAAY,CAyMzC,CAAC"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import * as Duration from "effect/Duration";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Scope from "effect/Scope";
|
|
4
|
+
import { ReactorError } from "reactor-effect-client";
|
|
5
|
+
import { duration, errorOf, fromOwnedReadableStream, parsed, positiveLimit, } from "reactor-effect-client/host";
|
|
6
|
+
import { decodeAudioPacket } from "./audio-packet.js";
|
|
7
|
+
const bounded = async (work, timeout, label, signal) => {
|
|
8
|
+
let timer;
|
|
9
|
+
let rejectAbort;
|
|
10
|
+
try {
|
|
11
|
+
return await Promise.race([
|
|
12
|
+
work,
|
|
13
|
+
new Promise((_, reject) => {
|
|
14
|
+
timer = setTimeout(() => reject(ReactorError.fromCode("Timeout", label)), timeout);
|
|
15
|
+
rejectAbort = () => reject(ReactorError.fromCode("Aborted", label));
|
|
16
|
+
if (signal?.aborted)
|
|
17
|
+
rejectAbort();
|
|
18
|
+
else
|
|
19
|
+
signal?.addEventListener("abort", rejectAbort, { once: true });
|
|
20
|
+
}),
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
if (timer !== undefined)
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
if (rejectAbort !== undefined)
|
|
27
|
+
signal?.removeEventListener("abort", rejectAbort);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const audioContext = (options = {}) => Effect.gen(function* () {
|
|
31
|
+
const timeout = yield* parsed(() => duration(options.transitionTimeout ?? "5 seconds", "audio context transition timeout", {
|
|
32
|
+
maximum: "1 minute",
|
|
33
|
+
}));
|
|
34
|
+
const context = yield* Effect.acquireRelease(Effect.try({
|
|
35
|
+
try: () => {
|
|
36
|
+
if (typeof AudioContext !== "function" || typeof AudioWorkletNode !== "function")
|
|
37
|
+
throw ReactorError.fromCode("UnsupportedCapability", "built-in AudioContext and AudioWorkletNode are required");
|
|
38
|
+
return new AudioContext(options.sampleRate === undefined ? {} : { sampleRate: options.sampleRate });
|
|
39
|
+
},
|
|
40
|
+
catch: (e) => errorOf(e, "UnsupportedCapability"),
|
|
41
|
+
}),
|
|
42
|
+
// Neither browser promise can be cancelled; each deadline only stops waiting for it.
|
|
43
|
+
(context) => Effect.tryPromise({
|
|
44
|
+
try: () => context.close(),
|
|
45
|
+
catch: (cause) => errorOf(cause, "Shutdown", "AudioContext.close"),
|
|
46
|
+
}).pipe(Effect.timeout(timeout), Effect.ignore));
|
|
47
|
+
yield* Effect.tryPromise({
|
|
48
|
+
try: () => context.resume(),
|
|
49
|
+
catch: (cause) => ReactorError.fromCode("UnsupportedCapability", "AudioContext resume failed; ordinary user activation is required", { detail: cause }),
|
|
50
|
+
}).pipe(Effect.timeoutOrElse({
|
|
51
|
+
duration: timeout,
|
|
52
|
+
orElse: () => Effect.fail(ReactorError.fromCode("Timeout", "AudioContext resume deadline; supply user activation")),
|
|
53
|
+
}));
|
|
54
|
+
if (context.state !== "running")
|
|
55
|
+
return yield* ReactorError.fromCode("UnsupportedCapability", `AudioContext is ${context.state}, not running`);
|
|
56
|
+
return context;
|
|
57
|
+
});
|
|
58
|
+
// A worklet is registered once per context and URL. Failed registration is retryable, not cached success.
|
|
59
|
+
const modules = new WeakMap();
|
|
60
|
+
const load = (context, url) => {
|
|
61
|
+
let cache = modules.get(context);
|
|
62
|
+
if (cache === undefined) {
|
|
63
|
+
cache = new Map();
|
|
64
|
+
modules.set(context, cache);
|
|
65
|
+
}
|
|
66
|
+
const previous = cache.get(url);
|
|
67
|
+
if (previous !== undefined)
|
|
68
|
+
return previous;
|
|
69
|
+
const task = context.audioWorklet.addModule(url, { credentials: "omit" });
|
|
70
|
+
cache.set(url, task);
|
|
71
|
+
task.catch(() => {
|
|
72
|
+
cache?.delete(url);
|
|
73
|
+
});
|
|
74
|
+
return task;
|
|
75
|
+
};
|
|
76
|
+
/** Window-realm PCM with OWNED muted playback activation, not a bare Web Audio tap.
|
|
77
|
+
* Each consumption clones the borrowed source once, attaches that clone to an owned muted
|
|
78
|
+
* HTMLAudioElement, awaits ordinary play(), and uses the same MediaStream for Web Audio.
|
|
79
|
+
* Activation rejection/timeout is an error, not silently successful PCM. No extra play()
|
|
80
|
+
* call is required; no autoplay policy is overridden. The caller owns the running context.
|
|
81
|
+
* One JS output slot and one credited worklet block; skipped render frames are reported.
|
|
82
|
+
* Closing/interrupting the stream releases the sink, clone, nodes, ports and timers.
|
|
83
|
+
* A stopped source lease fails Disconnected; reacquire a track/stream after reconnect.
|
|
84
|
+
* No ScriptProcessorNode, getUserMedia, device permission or native engine is used. */
|
|
85
|
+
export const webAudioSamples = (source, options) => fromOwnedReadableStream({
|
|
86
|
+
evaluate: () => {
|
|
87
|
+
// The deadlines race host promises inside the stream's callbacks, in milliseconds.
|
|
88
|
+
const timeout = Duration.toMillis(duration(options.readTimeout ?? "10 seconds", "PCM read timeout", {
|
|
89
|
+
maximum: "10 minutes",
|
|
90
|
+
}));
|
|
91
|
+
const activationTimeout = Duration.toMillis(duration(options.activationTimeout ?? "5 seconds", "PCM activation timeout", {
|
|
92
|
+
maximum: "1 minute",
|
|
93
|
+
}));
|
|
94
|
+
const maxBytes = positiveLimit(options.maxSampleBytes ?? 1048576, "PCM sample bound", 64 * 1024 * 1024);
|
|
95
|
+
const context = options.context;
|
|
96
|
+
if (source.kind !== "audio" || source.readyState !== "live")
|
|
97
|
+
throw ReactorError.fromCode("InvalidState", "expected a live audio track");
|
|
98
|
+
if (typeof AudioWorkletNode !== "function" || context.audioWorklet === undefined)
|
|
99
|
+
throw ReactorError.fromCode("UnsupportedCapability", "built-in AudioWorklet is unavailable in this realm");
|
|
100
|
+
if (typeof document === "undefined" ||
|
|
101
|
+
// Feature detection reads the method itself, not its deprecated overload.
|
|
102
|
+
// oxlint-disable-next-line typescript/no-deprecated
|
|
103
|
+
typeof document.createElement !== "function" ||
|
|
104
|
+
typeof MediaStream !== "function")
|
|
105
|
+
throw ReactorError.fromCode("UnsupportedCapability", "PCM activation requires a Window document and built-in HTML audio playback");
|
|
106
|
+
if (context.state !== "running")
|
|
107
|
+
throw ReactorError.fromCode("UnsupportedCapability", "PCM requires an explicitly resumed, running AudioContext");
|
|
108
|
+
const url = new URL(options.workletUrl ?? new URL("./pcm-worklet.js", import.meta.url), import.meta.url);
|
|
109
|
+
if (!/^(https?:|file:)$/.test(url.protocol))
|
|
110
|
+
throw ReactorError.fromCode("Protocol", "serve the PCM worklet as a normal local module, not a data/blob URL");
|
|
111
|
+
const track = source.clone(), lifetime = new AbortController();
|
|
112
|
+
let input, node;
|
|
113
|
+
let sink, watch;
|
|
114
|
+
let closed = false, pending;
|
|
115
|
+
let controller;
|
|
116
|
+
const close = () => {
|
|
117
|
+
if (closed)
|
|
118
|
+
return;
|
|
119
|
+
closed = true;
|
|
120
|
+
lifetime.abort();
|
|
121
|
+
if (watch !== undefined) {
|
|
122
|
+
clearInterval(watch);
|
|
123
|
+
watch = undefined;
|
|
124
|
+
}
|
|
125
|
+
source.removeEventListener("ended", ended);
|
|
126
|
+
track.removeEventListener("ended", ended);
|
|
127
|
+
context.removeEventListener("statechange", state);
|
|
128
|
+
// The element was created here and is never offered to another owner.
|
|
129
|
+
if (sink !== undefined) {
|
|
130
|
+
sink.pause();
|
|
131
|
+
sink.srcObject = null;
|
|
132
|
+
}
|
|
133
|
+
pending?.reject(ReactorError.fromCode("Aborted", "PCM reader closed"));
|
|
134
|
+
pending = undefined;
|
|
135
|
+
input?.disconnect();
|
|
136
|
+
if (node !== undefined) {
|
|
137
|
+
node.port.onmessage = null;
|
|
138
|
+
node.port.onmessageerror = null;
|
|
139
|
+
node.onprocessorerror = null;
|
|
140
|
+
node.port.postMessage("stop");
|
|
141
|
+
node.port.close();
|
|
142
|
+
node.disconnect();
|
|
143
|
+
}
|
|
144
|
+
track.stop();
|
|
145
|
+
};
|
|
146
|
+
const fail = (error) => {
|
|
147
|
+
if (!closed) {
|
|
148
|
+
controller?.error(error);
|
|
149
|
+
close();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const ended = () => fail(ReactorError.fromCode("Disconnected", "PCM track ended"));
|
|
153
|
+
const state = () => {
|
|
154
|
+
if (context.state !== "running")
|
|
155
|
+
fail(ReactorError.fromCode("Disconnected", `PCM context ${context.state}`));
|
|
156
|
+
};
|
|
157
|
+
const live = () => {
|
|
158
|
+
if (source.readyState !== "live" || track.readyState !== "live")
|
|
159
|
+
ended();
|
|
160
|
+
state();
|
|
161
|
+
return !closed;
|
|
162
|
+
};
|
|
163
|
+
const initialize = async () => {
|
|
164
|
+
source.addEventListener("ended", ended);
|
|
165
|
+
track.addEventListener("ended", ended);
|
|
166
|
+
context.addEventListener("statechange", state);
|
|
167
|
+
// MediaStreamTrack.stop() sets readyState without dispatching ended, and does not
|
|
168
|
+
// stop its clones. Session reconnect stops borrowed leases this way. Inspect the
|
|
169
|
+
// lease on each read/message and every 100ms even while a consumer is stalled.
|
|
170
|
+
// Timer throttling can delay this check; it is not a wall-clock termination proof.
|
|
171
|
+
watch = setInterval(live, 100);
|
|
172
|
+
if (!live())
|
|
173
|
+
return;
|
|
174
|
+
const stream = new MediaStream([track]);
|
|
175
|
+
sink = document.createElement("audio");
|
|
176
|
+
sink.muted = true;
|
|
177
|
+
sink.srcObject = stream;
|
|
178
|
+
try {
|
|
179
|
+
await bounded(sink.play(), activationTimeout, "PCM playback activation deadline; supply normal user activation", lifetime.signal);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
throw errorOf(error, "UnsupportedCapability", "PCM HTMLAudioElement.play activation");
|
|
183
|
+
}
|
|
184
|
+
if (!live())
|
|
185
|
+
return;
|
|
186
|
+
await bounded(load(context, url.href), timeout, "AudioWorklet module deadline", lifetime.signal);
|
|
187
|
+
// Cancellation while addModule is pending must not create a late graph.
|
|
188
|
+
if (!live())
|
|
189
|
+
return;
|
|
190
|
+
input = context.createMediaStreamSource(stream);
|
|
191
|
+
node = new AudioWorkletNode(context, "reactor-pcm-tap-v1", {
|
|
192
|
+
numberOfInputs: 1,
|
|
193
|
+
numberOfOutputs: 1,
|
|
194
|
+
outputChannelCount: [1],
|
|
195
|
+
});
|
|
196
|
+
node.port.onmessage = (event) => {
|
|
197
|
+
if (!live())
|
|
198
|
+
return;
|
|
199
|
+
try {
|
|
200
|
+
if (pending === undefined)
|
|
201
|
+
throw ReactorError.fromCode("Protocol", "unsolicited PCM block without a read credit");
|
|
202
|
+
const sample = decodeAudioPacket(event.data, maxBytes), waiter = pending;
|
|
203
|
+
pending = undefined;
|
|
204
|
+
waiter.resolve(sample);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
fail(errorOf(error, "Protocol", "PCM block"));
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
node.port.onmessageerror = () => fail(ReactorError.fromCode("Protocol", "PCM transfer could not be deserialized"));
|
|
211
|
+
node.onprocessorerror = () => fail(ReactorError.fromCode("UnsupportedCapability", "PCM AudioWorklet processor failed"));
|
|
212
|
+
input.connect(node);
|
|
213
|
+
node.connect(context.destination);
|
|
214
|
+
};
|
|
215
|
+
let ready;
|
|
216
|
+
return new ReadableStream({
|
|
217
|
+
start(c) {
|
|
218
|
+
controller = c;
|
|
219
|
+
ready = initialize().catch((error) => fail(errorOf(error, "UnsupportedCapability", "open PCM")));
|
|
220
|
+
},
|
|
221
|
+
async pull(c) {
|
|
222
|
+
try {
|
|
223
|
+
await ready;
|
|
224
|
+
if (!live())
|
|
225
|
+
return;
|
|
226
|
+
const sample = await bounded(new Promise((resolve, reject) => {
|
|
227
|
+
pending = { resolve, reject };
|
|
228
|
+
node?.port.postMessage("pull");
|
|
229
|
+
}), timeout, "PCM read deadline", lifetime.signal);
|
|
230
|
+
if (live())
|
|
231
|
+
c.enqueue(sample);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
fail(errorOf(error, "Protocol", "PCM read"));
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
cancel: close,
|
|
238
|
+
}, { highWaterMark: 1 });
|
|
239
|
+
},
|
|
240
|
+
onError: (error) => errorOf(error, "UnsupportedCapability", "webAudioSamples"),
|
|
241
|
+
});
|
|
242
|
+
//# sourceMappingURL=audio.js.map
|