@semi-app/miplay 0.0.0-stage → 1.0.1
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/README.md +4 -2
- package/dist/MAGPIE-LICENSE.txt +21 -0
- package/dist/assets/airplay-0f650dbf.png +0 -0
- package/dist/assets/index-c3d52e7b.js +1 -0
- package/dist/assets/index-cf41e0aa.css +1 -0
- package/dist/assets/mina-f2e7977b.png +0 -0
- package/dist/assets/miplay-6539f915.js +124 -0
- package/dist/assets/miplay-70349e65.png +0 -0
- package/dist/assets/miplay-c5d217a1.css +1 -0
- package/dist/assets/semi-595e39af.ico +0 -0
- package/dist/assets/semi-core-38c824f6.png +0 -0
- package/dist/assets/tray-59cb5e08.js +1 -0
- package/dist/assets/tray-d30ea4dd.css +1 -0
- package/dist/background/main.cjs +19658 -0
- package/dist/background/playfair.wasm +0 -0
- package/dist/background/vendor/LICENSE +201 -0
- package/dist/background/vendor/NOTICE +22 -0
- package/dist/background/vendor/README.md +20 -0
- package/dist/background/vendor/alac.js +87 -0
- package/dist/background/vendor/alacDecoder.js +317 -0
- package/dist/background/vendor/appleKey.js +61 -0
- package/dist/background/vendor/ntpTiming.js +99 -0
- package/dist/background/vendor/package.json +1 -0
- package/dist/background/vendor/playfair/BUILD.md +17 -0
- package/dist/background/vendor/playfair/LICENSE.md +637 -0
- package/dist/background/vendor/playfair/MUSL-COPYRIGHT +193 -0
- package/dist/background/vendor/playfair/README.md +24 -0
- package/dist/background/vendor/playfair/hand_garble.c +443 -0
- package/dist/background/vendor/playfair/modified_md5.c +119 -0
- package/dist/background/vendor/playfair/omg_hax.c +540 -0
- package/dist/background/vendor/playfair/omg_hax.h +32 -0
- package/dist/background/vendor/playfair/playfair.c +31 -0
- package/dist/background/vendor/playfair/playfair.h +6 -0
- package/dist/background/vendor/playfair/sap_hash.c +96 -0
- package/dist/background/vendor/playfair/wasm-wrapper.c +22 -0
- package/dist/background/vendor/receiver.js +677 -0
- package/dist/icon-dark.svg +1 -0
- package/dist/icon.svg +1 -0
- package/dist/index.html +6 -0
- package/dist/licenses/@leichtgewicht/ip-codec/LICENSE +21 -0
- package/dist/licenses/@mediabunny/aac-encoder/LICENSE +373 -0
- package/dist/licenses/array-flatten/LICENSE +21 -0
- package/dist/licenses/bonjour/LICENSE +21 -0
- package/dist/licenses/buffer-indexof/LICENSE +20 -0
- package/dist/licenses/call-bind/LICENSE +21 -0
- package/dist/licenses/call-bind-apply-helpers/LICENSE +21 -0
- package/dist/licenses/call-bound/LICENSE +21 -0
- package/dist/licenses/deep-equal/LICENSE +21 -0
- package/dist/licenses/define-data-property/LICENSE +21 -0
- package/dist/licenses/define-properties/LICENSE +21 -0
- package/dist/licenses/dns-equal/LICENSE +21 -0
- package/dist/licenses/dns-packet/LICENSE +21 -0
- package/dist/licenses/dns-txt/LICENSE +21 -0
- package/dist/licenses/dunder-proto/LICENSE +21 -0
- package/dist/licenses/es-define-property/LICENSE +21 -0
- package/dist/licenses/es-errors/LICENSE +21 -0
- package/dist/licenses/es-object-atoms/LICENSE +21 -0
- package/dist/licenses/ffmpeg/COPYING.LGPLv2.1 +502 -0
- package/dist/licenses/ffmpeg/LICENSE.md +127 -0
- package/dist/licenses/function-bind/LICENSE +20 -0
- package/dist/licenses/functions-have-names/LICENSE +21 -0
- package/dist/licenses/get-intrinsic/LICENSE +21 -0
- package/dist/licenses/get-proto/LICENSE +21 -0
- package/dist/licenses/gopd/LICENSE +21 -0
- package/dist/licenses/has-property-descriptors/LICENSE +21 -0
- package/dist/licenses/has-symbols/LICENSE +21 -0
- package/dist/licenses/has-tostringtag/LICENSE +21 -0
- package/dist/licenses/hasown/LICENSE +21 -0
- package/dist/licenses/is-arguments/LICENSE +20 -0
- package/dist/licenses/is-date-object/LICENSE +22 -0
- package/dist/licenses/is-regex/LICENSE +20 -0
- package/dist/licenses/magpie-MIT.txt +21 -0
- package/dist/licenses/math-intrinsics/LICENSE +21 -0
- package/dist/licenses/mediabunny/LICENSE +373 -0
- package/dist/licenses/mediabunny-MPL-2.0.txt +373 -0
- package/dist/licenses/multicast-dns/LICENSE +21 -0
- package/dist/licenses/multicast-dns-service-types/LICENSE +21 -0
- package/dist/licenses/object-is/LICENSE +20 -0
- package/dist/licenses/object-keys/LICENSE +21 -0
- package/dist/licenses/regexp.prototype.flags/LICENSE +22 -0
- package/dist/licenses/set-function-length/LICENSE +21 -0
- package/dist/licenses/set-function-name/LICENSE +21 -0
- package/dist/licenses/thunky/LICENSE +21 -0
- package/dist/tray.html +5 -0
- package/package.json +93 -5
|
Binary file
|
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
@sonn-audio/node-airplay
|
|
2
|
+
Copyright 2026 Rudy Berends
|
|
3
|
+
|
|
4
|
+
This product includes software ported from cliairplay, part of Music Assistant
|
|
5
|
+
(https://github.com/music-assistant), Copyright 2024-2026 Music Assistant
|
|
6
|
+
Contributors, licensed under the Apache License, Version 2.0.
|
|
7
|
+
|
|
8
|
+
The AirPlay 2 protocol logic in this package — HAP pair-setup and its SRP-6a
|
|
9
|
+
parameters, the binary-plist message shapes, the PTP grandmaster's message
|
|
10
|
+
layouts and announce dataset, the realtime RTP framing and its encryption, and
|
|
11
|
+
the anchor packet — is a TypeScript port of the Apache-2.0 licensed sources in
|
|
12
|
+
that project's `src/` directory (ap2_hap.c, ap2_ptp.c, ap2_client.c, ap2_plist.c
|
|
13
|
+
and ap2_bplist.cpp).
|
|
14
|
+
|
|
15
|
+
Those files carry per-file `SPDX-License-Identifier: Apache-2.0` headers and are
|
|
16
|
+
explicitly reusable under that licence on their own. The GPL parts of cliairplay
|
|
17
|
+
— its `cliairplay.c` entry point and the libraop RAOP core it links — are NOT
|
|
18
|
+
used here: this package contains no AirPlay 1 / RAOP code and links no native
|
|
19
|
+
libraries.
|
|
20
|
+
|
|
21
|
+
The ALAC frame layout was read from libraop's decoder (alac.c) to establish what
|
|
22
|
+
receivers accept; no decoder code is included.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Receiver subset from @sonn-audio/node-airplay 0.1.2 (published npm archive),
|
|
2
|
+
https://github.com/sonn-audio/node-airplay . Original LICENSE and NOTICE are retained.
|
|
3
|
+
The receiver, ALAC decoder, Apple key and NTP helpers are used; the ALAC encoder
|
|
4
|
+
is retained solely for protocol-test/reference use. No AirPlay 2 implementation is used.
|
|
5
|
+
|
|
6
|
+
Semi changes: explicit IPv4 listener binding; bounded RTSP input; separate bounded probe connections and a single active audio session; malformed request/codec error handling; L16 conversion; bounded ALAC configuration/frame sizes and truncated-input checks; FLUSHBUFFERED
|
|
7
|
+
compatibility. PCM is forwarded into the plugin-owned authenticated WAV endpoint.
|
|
8
|
+
|
|
9
|
+
Discovery declarations follow the previously working MiAir RAOP record (vn=65537,
|
|
10
|
+
vs=105.1, da/sf/pw/fn/ft). AAC is excluded from codec/feature claims because this
|
|
11
|
+
receiver implements PCM and ALAC only.
|
|
12
|
+
|
|
13
|
+
MiAir-compatible setup responses are in ../fairplay-setup.mjs, with attribution to
|
|
14
|
+
the original PlayFair reference. The setup state is retained per connection and fpaeskey is unwrapped by the
|
|
15
|
+
separate playfair WASM module; its sources, build instructions and GPLv3 license
|
|
16
|
+
are in playfair/. No encrypted key material is logged.
|
|
17
|
+
|
|
18
|
+
OPTIONS probes no longer reserve the audio session or stop it on disconnect.
|
|
19
|
+
Responses carry explicit lengths and per-connection diagnostics. Playback state
|
|
20
|
+
requires ANNOUNCE/SETUP/RECORD; OPTIONS alone is not evidence of audio transfer.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ALAC encoding for the AirPlay wire — uncompressed frames.
|
|
3
|
+
*
|
|
4
|
+
* ALAC's bitstream has an escape mode: a frame may carry its samples verbatim
|
|
5
|
+
* instead of predictor + Rice output, and every decoder must handle it because
|
|
6
|
+
* Apple's own encoder falls back to it whenever compression would not pay. That
|
|
7
|
+
* is what this writes. The layout below is taken from libraop's decoder
|
|
8
|
+
* (`alac.c`), which is what the receiver runs, so it is what the frame is
|
|
9
|
+
* checked against.
|
|
10
|
+
*
|
|
11
|
+
* The cost is bandwidth: a 352-frame stereo packet is 1412 bytes instead of
|
|
12
|
+
* roughly half that. On a LAN carrying one AirPlay stream that is not a
|
|
13
|
+
* constraint, and it keeps the predictor and the adaptive Rice coder — the part
|
|
14
|
+
* of ALAC that is genuinely intricate — out of the first working version. Real
|
|
15
|
+
* compression can be added behind this same interface later; the receiver
|
|
16
|
+
* cannot tell the difference beyond packet size.
|
|
17
|
+
*
|
|
18
|
+
* Uncompressed stereo frame:
|
|
19
|
+
* 3 bits element/channel count (1 = stereo)
|
|
20
|
+
* 4 bits unused
|
|
21
|
+
* 12 bits unused
|
|
22
|
+
* 1 bit hasSize
|
|
23
|
+
* 2 bits uncompressedBytes (0)
|
|
24
|
+
* 1 bit isNotCompressed (1)
|
|
25
|
+
* 32 bits sample count, only when hasSize
|
|
26
|
+
* then, per frame: left sample, right sample, each `bitDepth` bits
|
|
27
|
+
* 3 bits end-of-elements marker (7), then byte alignment
|
|
28
|
+
*/
|
|
29
|
+
/** Writes a big-endian bit stream, MSB first. */
|
|
30
|
+
class BitWriter {
|
|
31
|
+
bytes = [];
|
|
32
|
+
current = 0;
|
|
33
|
+
used = 0;
|
|
34
|
+
write(value, bits) {
|
|
35
|
+
for (let i = bits - 1; i >= 0; i--) {
|
|
36
|
+
this.current = (this.current << 1) | ((value >>> i) & 1);
|
|
37
|
+
this.used++;
|
|
38
|
+
if (this.used === 8) {
|
|
39
|
+
this.bytes.push(this.current & 0xff);
|
|
40
|
+
this.current = 0;
|
|
41
|
+
this.used = 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
finish() {
|
|
46
|
+
if (this.used > 0) {
|
|
47
|
+
this.bytes.push((this.current << (8 - this.used)) & 0xff);
|
|
48
|
+
this.current = 0;
|
|
49
|
+
this.used = 0;
|
|
50
|
+
}
|
|
51
|
+
return Buffer.from(this.bytes);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Element type written after the payload to close the frame. */
|
|
55
|
+
const ID_END = 7;
|
|
56
|
+
/**
|
|
57
|
+
* Encode one packet of interleaved PCM as an uncompressed ALAC frame.
|
|
58
|
+
*
|
|
59
|
+
* :param pcm: interleaved signed 16-bit little-endian samples, exactly
|
|
60
|
+
* `framesPerPacket * channels` of them. A short final packet is allowed and
|
|
61
|
+
* is written with an explicit sample count.
|
|
62
|
+
*/
|
|
63
|
+
export function encodeAlacPacket(pcm, options) {
|
|
64
|
+
const frameCount = pcm.length / (options.channels * 2);
|
|
65
|
+
if (!Number.isInteger(frameCount)) {
|
|
66
|
+
throw new Error('ALAC: PCM length is not a whole number of stereo frames');
|
|
67
|
+
}
|
|
68
|
+
const writer = new BitWriter();
|
|
69
|
+
const partial = frameCount !== options.framesPerPacket;
|
|
70
|
+
writer.write(1, 3); // stereo element
|
|
71
|
+
writer.write(0, 4);
|
|
72
|
+
writer.write(0, 12);
|
|
73
|
+
writer.write(partial ? 1 : 0, 1); // hasSize
|
|
74
|
+
writer.write(0, 2); // uncompressedBytes
|
|
75
|
+
writer.write(1, 1); // isNotCompressed — the escape mode
|
|
76
|
+
if (partial) {
|
|
77
|
+
// 32 bits, written in two halves because the bit writer takes 32-bit ints.
|
|
78
|
+
writer.write((frameCount >>> 16) & 0xffff, 16);
|
|
79
|
+
writer.write(frameCount & 0xffff, 16);
|
|
80
|
+
}
|
|
81
|
+
for (let i = 0; i < frameCount; i++) {
|
|
82
|
+
writer.write(pcm.readUInt16LE(i * 4), 16);
|
|
83
|
+
writer.write(pcm.readUInt16LE(i * 4 + 2), 16);
|
|
84
|
+
}
|
|
85
|
+
writer.write(ID_END, 3);
|
|
86
|
+
return writer.finish();
|
|
87
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ALAC decoding — what a receiver needs, and the mirror of `alac.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Ported from libraop's `alac.c` (David Hammerton's decoder). A sender only
|
|
5
|
+
* emits the uncompressed escape frames this project's encoder writes when
|
|
6
|
+
* compression would not pay; a real sender — an iPhone, iTunes — sends the
|
|
7
|
+
* compressed form nearly always, so a receiver has to implement the whole
|
|
8
|
+
* thing: adaptive Rice entropy coding and the adaptive FIR predictor whose
|
|
9
|
+
* coefficients are rewritten as it goes.
|
|
10
|
+
*
|
|
11
|
+
* The predictor is the part worth reading twice: its coefficient table is not
|
|
12
|
+
* constant, it is nudged after every sample by the sign of the error, so
|
|
13
|
+
* decoding is only correct if the table is updated in exactly the same order
|
|
14
|
+
* the encoder did it.
|
|
15
|
+
*/
|
|
16
|
+
const RICE_THRESHOLD = 8;
|
|
17
|
+
/** Sign-extend the low `bits` of a value to a full 32-bit signed integer. */
|
|
18
|
+
function signExtend(value, bits) {
|
|
19
|
+
const shift = 32 - bits;
|
|
20
|
+
return (value << shift) >> shift;
|
|
21
|
+
}
|
|
22
|
+
function countLeadingZeros(value) {
|
|
23
|
+
return value === 0 ? 32 : Math.clz32(value >>> 0);
|
|
24
|
+
}
|
|
25
|
+
/** MSB-first bit reader with the one-bit rewind the Rice decoder needs. */
|
|
26
|
+
class BitReader {
|
|
27
|
+
data;
|
|
28
|
+
bit = 0;
|
|
29
|
+
constructor(data) {
|
|
30
|
+
this.data = data;
|
|
31
|
+
}
|
|
32
|
+
readBit() {
|
|
33
|
+
if (this.bit >= this.data.length * 8) throw new RangeError('Truncated ALAC frame');
|
|
34
|
+
const byte = this.data[this.bit >> 3] ?? 0;
|
|
35
|
+
const value = (byte >> (7 - (this.bit & 7))) & 1;
|
|
36
|
+
this.bit++;
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
read(count) {
|
|
40
|
+
let value = 0;
|
|
41
|
+
for (let i = 0; i < count; i++) {
|
|
42
|
+
value = ((value << 1) | this.readBit()) >>> 0;
|
|
43
|
+
}
|
|
44
|
+
return value >>> 0;
|
|
45
|
+
}
|
|
46
|
+
rewind(count) {
|
|
47
|
+
this.bit -= count;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read the ALAC parameters out of an SDP `a=fmtp` line.
|
|
52
|
+
*
|
|
53
|
+
* The line is the ALAC magic cookie in decimal:
|
|
54
|
+
* `frameLength 0 bitDepth historyMult initialHistory kModifier channels 255 0 0 sampleRate`.
|
|
55
|
+
*/
|
|
56
|
+
export function parseAlacFmtp(fmtp) {
|
|
57
|
+
const numbers = fmtp
|
|
58
|
+
.trim()
|
|
59
|
+
.split(/\s+/)
|
|
60
|
+
.map((part) => Number(part))
|
|
61
|
+
.filter((value) => Number.isFinite(value));
|
|
62
|
+
if (numbers.length < 11) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
framesPerPacket: numbers[0],
|
|
67
|
+
bitDepth: numbers[2],
|
|
68
|
+
riceHistoryMult: numbers[3],
|
|
69
|
+
riceInitialHistory: numbers[4],
|
|
70
|
+
riceKModifier: numbers[5],
|
|
71
|
+
channels: numbers[6],
|
|
72
|
+
sampleRate: numbers[10],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function decodeValue(reader, readSampleSize, k, mask) {
|
|
76
|
+
// A run of ones gives the quotient; more than the threshold means the value
|
|
77
|
+
// was too large for that and follows raw.
|
|
78
|
+
let x = 0;
|
|
79
|
+
while (x <= RICE_THRESHOLD && reader.readBit()) {
|
|
80
|
+
x++;
|
|
81
|
+
}
|
|
82
|
+
if (x > RICE_THRESHOLD) {
|
|
83
|
+
return reader.read(readSampleSize) & (0xffffffff >>> (32 - readSampleSize));
|
|
84
|
+
}
|
|
85
|
+
if (k !== 1) {
|
|
86
|
+
const extra = reader.read(k);
|
|
87
|
+
x = x * (((1 << k) - 1) & mask);
|
|
88
|
+
if (extra > 1) {
|
|
89
|
+
x += extra - 1;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
reader.rewind(1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return x;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Adaptive Rice decoding: `k` is derived from a running history of how large
|
|
99
|
+
* recent values were, so the code length tracks the signal instead of being
|
|
100
|
+
* fixed.
|
|
101
|
+
*/
|
|
102
|
+
function riceDecode(reader, out, count, readSampleSize, config, historyMult) {
|
|
103
|
+
let history = config.riceInitialHistory;
|
|
104
|
+
const kModifierMask = (1 << config.riceKModifier) - 1;
|
|
105
|
+
let signModifier = 0;
|
|
106
|
+
for (let i = 0; i < count; i++) {
|
|
107
|
+
let k = 31 - config.riceKModifier - countLeadingZeros((history >> 9) + 3);
|
|
108
|
+
k = k < 0 ? k + config.riceKModifier : config.riceKModifier;
|
|
109
|
+
let decoded = decodeValue(reader, readSampleSize, k, 0xffffffff);
|
|
110
|
+
decoded += signModifier;
|
|
111
|
+
let final = (decoded + 1) >> 1;
|
|
112
|
+
if (decoded & 1) {
|
|
113
|
+
final = -final;
|
|
114
|
+
}
|
|
115
|
+
out[i] = final;
|
|
116
|
+
signModifier = 0;
|
|
117
|
+
history += decoded * historyMult - ((history * historyMult) >> 9);
|
|
118
|
+
if (decoded > 0xffff) {
|
|
119
|
+
history = 0xffff;
|
|
120
|
+
}
|
|
121
|
+
// A long run of zeros is coded once rather than sample by sample.
|
|
122
|
+
if (history < 128 && i + 1 < count) {
|
|
123
|
+
signModifier = 1;
|
|
124
|
+
const runK = countLeadingZeros(history) + (((history + 16) / 64) | 0) - 24;
|
|
125
|
+
const blockSize = decodeValue(reader, 16, runK, kModifierMask);
|
|
126
|
+
if (blockSize > 0) {
|
|
127
|
+
out.fill(0, i + 1, Math.min(count, i + 1 + blockSize));
|
|
128
|
+
i += blockSize;
|
|
129
|
+
}
|
|
130
|
+
if (blockSize > 0xffff) {
|
|
131
|
+
history = 0xffff;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
history = 0;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Undo the adaptive FIR prediction.
|
|
141
|
+
*
|
|
142
|
+
* The coefficient table is rewritten as decoding proceeds — after each sample
|
|
143
|
+
* every coefficient is nudged by the sign of the residual — so this has to walk
|
|
144
|
+
* the samples in order and cannot be vectorised or reordered.
|
|
145
|
+
*/
|
|
146
|
+
function predictorDecompress(errors, out, count, readSampleSize, coefficients, coefficientCount, quantisation) {
|
|
147
|
+
if (count === 0) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
out[0] = errors[0];
|
|
151
|
+
if (coefficientCount === 0) {
|
|
152
|
+
for (let i = 1; i < count; i++) {
|
|
153
|
+
out[i] = errors[i];
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// 0x1f means "the residual is a difference from the previous sample" — a
|
|
158
|
+
// common shortcut the encoder takes.
|
|
159
|
+
if (coefficientCount === 0x1f) {
|
|
160
|
+
for (let i = 0; i < count - 1; i++) {
|
|
161
|
+
out[i + 1] = signExtend(out[i] + errors[i + 1], readSampleSize);
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
for (let i = 0; i < coefficientCount && i + 1 < count; i++) {
|
|
166
|
+
out[i + 1] = signExtend(out[i] + errors[i + 1], readSampleSize);
|
|
167
|
+
}
|
|
168
|
+
let base = 0; // stands in for the C code's advancing buffer_out pointer
|
|
169
|
+
for (let i = coefficientCount + 1; i < count; i++) {
|
|
170
|
+
let sum = 0;
|
|
171
|
+
for (let j = 0; j < coefficientCount; j++) {
|
|
172
|
+
sum += (out[base + coefficientCount - j] - out[base]) * coefficients[j];
|
|
173
|
+
}
|
|
174
|
+
let value = (1 << (quantisation - 1)) + sum;
|
|
175
|
+
value >>= quantisation;
|
|
176
|
+
value += out[base] + errors[i];
|
|
177
|
+
value = signExtend(value, readSampleSize);
|
|
178
|
+
out[base + coefficientCount + 1] = value;
|
|
179
|
+
let error = errors[i];
|
|
180
|
+
if (error > 0) {
|
|
181
|
+
let index = coefficientCount - 1;
|
|
182
|
+
while (index >= 0 && error > 0) {
|
|
183
|
+
const difference = out[base] - out[base + coefficientCount - index];
|
|
184
|
+
const sign = Math.sign(difference);
|
|
185
|
+
coefficients[index] = coefficients[index] - sign;
|
|
186
|
+
error -= ((difference * sign) >> quantisation) * (coefficientCount - index);
|
|
187
|
+
index--;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else if (error < 0) {
|
|
191
|
+
let index = coefficientCount - 1;
|
|
192
|
+
while (index >= 0 && error < 0) {
|
|
193
|
+
const difference = out[base] - out[base + coefficientCount - index];
|
|
194
|
+
const sign = -Math.sign(difference);
|
|
195
|
+
coefficients[index] = coefficients[index] - sign;
|
|
196
|
+
error -= ((difference * sign) >> quantisation) * (coefficientCount - index);
|
|
197
|
+
index--;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
base++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Decodes ALAC frames to interleaved s16le PCM.
|
|
205
|
+
*
|
|
206
|
+
* One instance per stream: the scratch buffers are sized from the configuration
|
|
207
|
+
* and reused, which matters at 125 frames a second.
|
|
208
|
+
*/
|
|
209
|
+
export class AlacDecoder {
|
|
210
|
+
config;
|
|
211
|
+
errorsA;
|
|
212
|
+
errorsB;
|
|
213
|
+
samplesA;
|
|
214
|
+
samplesB;
|
|
215
|
+
constructor(config) {
|
|
216
|
+
if (!Number.isInteger(config.framesPerPacket) || config.framesPerPacket < 1 || config.framesPerPacket > 4096
|
|
217
|
+
|| config.bitDepth !== 16 || config.channels !== 2 || config.sampleRate !== 44100) throw new RangeError('Unsupported ALAC configuration');
|
|
218
|
+
this.config = config;
|
|
219
|
+
const size = config.framesPerPacket;
|
|
220
|
+
this.errorsA = new Int32Array(size);
|
|
221
|
+
this.errorsB = new Int32Array(size);
|
|
222
|
+
this.samplesA = new Int32Array(size);
|
|
223
|
+
this.samplesB = new Int32Array(size);
|
|
224
|
+
}
|
|
225
|
+
/** Decode one frame. Returns interleaved s16le, or null when unsupported. */
|
|
226
|
+
decode(frame) {
|
|
227
|
+
const reader = new BitReader(frame);
|
|
228
|
+
const channels = reader.read(3);
|
|
229
|
+
return channels === 0
|
|
230
|
+
? this.decodeMono(reader)
|
|
231
|
+
: channels === 1
|
|
232
|
+
? this.decodeStereo(reader)
|
|
233
|
+
: null;
|
|
234
|
+
}
|
|
235
|
+
readFrameHeader(reader) {
|
|
236
|
+
reader.read(4);
|
|
237
|
+
reader.read(12);
|
|
238
|
+
const hasSize = reader.readBit();
|
|
239
|
+
const uncompressedBytes = reader.read(2);
|
|
240
|
+
const notCompressed = reader.readBit() === 1;
|
|
241
|
+
const count = hasSize ? reader.read(32) : this.config.framesPerPacket;
|
|
242
|
+
if (count < 1 || count > this.config.framesPerPacket) throw new RangeError('Invalid ALAC frame size');
|
|
243
|
+
return { count, uncompressedBytes, notCompressed };
|
|
244
|
+
}
|
|
245
|
+
decodeMono(reader) {
|
|
246
|
+
const { count, uncompressedBytes, notCompressed } = this.readFrameHeader(reader);
|
|
247
|
+
const readSampleSize = this.config.bitDepth - uncompressedBytes * 8;
|
|
248
|
+
const out = Buffer.alloc(count * 2);
|
|
249
|
+
if (notCompressed) {
|
|
250
|
+
for (let i = 0; i < count; i++) {
|
|
251
|
+
out.writeInt16LE(signExtend(reader.read(readSampleSize), readSampleSize), i * 2);
|
|
252
|
+
}
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
reader.read(8); // interlacing fields, unused on a single channel
|
|
256
|
+
reader.read(8);
|
|
257
|
+
const predictor = this.readPredictorHeader(reader);
|
|
258
|
+
riceDecode(reader, this.errorsA, count, readSampleSize, this.config, predictor.historyMult);
|
|
259
|
+
predictorDecompress(this.errorsA, this.samplesA, count, readSampleSize, predictor.coefficients, predictor.coefficientCount, predictor.quantisation);
|
|
260
|
+
for (let i = 0; i < count; i++) {
|
|
261
|
+
out.writeInt16LE((this.samplesA[i] << 16) >> 16, i * 2);
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
/** Read one channel's predictor description: quantisation and coefficients. */
|
|
266
|
+
readPredictorHeader(reader) {
|
|
267
|
+
reader.read(4); // prediction type; only 0 (adaptive FIR) occurs in practice
|
|
268
|
+
const quantisation = reader.read(4);
|
|
269
|
+
const riceModifier = reader.read(3);
|
|
270
|
+
const coefficientCount = reader.read(5);
|
|
271
|
+
const coefficients = new Int16Array(32);
|
|
272
|
+
for (let i = 0; i < coefficientCount; i++) {
|
|
273
|
+
coefficients[i] = signExtend(reader.read(16), 16);
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
quantisation,
|
|
277
|
+
coefficients,
|
|
278
|
+
coefficientCount,
|
|
279
|
+
historyMult: (riceModifier * this.config.riceHistoryMult) / 4,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
decodeStereo(reader) {
|
|
283
|
+
const { count, uncompressedBytes, notCompressed } = this.readFrameHeader(reader);
|
|
284
|
+
// Stereo carries one extra bit of headroom: the mid/side transform can
|
|
285
|
+
// produce a difference that needs it.
|
|
286
|
+
const readSampleSize = this.config.bitDepth - uncompressedBytes * 8 + 1;
|
|
287
|
+
const out = Buffer.alloc(count * 4);
|
|
288
|
+
if (notCompressed) {
|
|
289
|
+
const size = this.config.bitDepth;
|
|
290
|
+
for (let i = 0; i < count; i++) {
|
|
291
|
+
out.writeInt16LE(signExtend(reader.read(size), size), i * 4);
|
|
292
|
+
out.writeInt16LE(signExtend(reader.read(size), size), i * 4 + 2);
|
|
293
|
+
}
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
const interlacingShift = reader.read(8);
|
|
297
|
+
const interlacingLeftWeight = reader.read(8);
|
|
298
|
+
const a = this.readPredictorHeader(reader);
|
|
299
|
+
const b = this.readPredictorHeader(reader);
|
|
300
|
+
riceDecode(reader, this.errorsA, count, readSampleSize, this.config, a.historyMult);
|
|
301
|
+
predictorDecompress(this.errorsA, this.samplesA, count, readSampleSize, a.coefficients, a.coefficientCount, a.quantisation);
|
|
302
|
+
riceDecode(reader, this.errorsB, count, readSampleSize, this.config, b.historyMult);
|
|
303
|
+
predictorDecompress(this.errorsB, this.samplesB, count, readSampleSize, b.coefficients, b.coefficientCount, b.quantisation);
|
|
304
|
+
// Undo the mid/side transform the encoder applied across the pair.
|
|
305
|
+
for (let i = 0; i < count; i++) {
|
|
306
|
+
const midRight = this.samplesA[i];
|
|
307
|
+
const difference = this.samplesB[i];
|
|
308
|
+
const right = interlacingLeftWeight
|
|
309
|
+
? midRight - ((difference * interlacingLeftWeight) >> interlacingShift)
|
|
310
|
+
: difference;
|
|
311
|
+
const left = interlacingLeftWeight ? right + difference : midRight;
|
|
312
|
+
out.writeInt16LE(left << 16 >> 16, i * 4);
|
|
313
|
+
out.writeInt16LE(right << 16 >> 16, i * 4 + 2);
|
|
314
|
+
}
|
|
315
|
+
return out;
|
|
316
|
+
}
|
|
317
|
+
}
|