@audio/amr-decode 1.0.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 ADDED
@@ -0,0 +1,191 @@
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, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the
13
+ copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other
16
+ entities that control, are controlled by, or are under common control with
17
+ that entity. For the purposes of this definition, "control" means (i) the
18
+ power, direct or indirect, to cause the direction or management of such
19
+ entity, whether by contract or otherwise, or (ii) ownership of fifty
20
+ percent (50%) or more of the outstanding shares, or (iii) beneficial
21
+ ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
24
+ 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 source,
28
+ and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical transformation
31
+ or translation of a Source form, including but not limited to compiled
32
+ object code, generated documentation, and conversions to other media types.
33
+
34
+ "Work" shall mean the work of authorship, whether in Source or Object form,
35
+ made available under the License, as indicated by a copyright notice that
36
+ is included in or attached to the work (an example is provided in the
37
+ Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object form,
40
+ that is based on (or derived from) the Work and for which the editorial
41
+ revisions, annotations, elaborations, or other modifications represent, as
42
+ a whole, an original work of authorship. For the purposes of this License,
43
+ Derivative Works shall not include works that remain separable from, or
44
+ merely link (or bind by name) to the interfaces of, the Work and Derivative
45
+ Works thereof.
46
+
47
+ "Contribution" shall mean any work of authorship, including the original
48
+ version of the Work and any modifications or additions to that Work or
49
+ Derivative Works thereof, that is intentionally submitted to Licensor for
50
+ inclusion in the Work by the copyright owner or by an individual or Legal
51
+ Entity authorized to submit on behalf of the copyright owner. For the
52
+ purposes of this definition, "submitted" means any form of electronic,
53
+ verbal, or written communication sent to the Licensor or its
54
+ representatives, including but not limited to communication on electronic
55
+ mailing lists, source code control systems, and issue tracking systems that
56
+ are managed by, or on behalf of, the Licensor for the purpose of discussing
57
+ and improving the Work, but excluding communication that is conspicuously
58
+ marked or otherwise designated in writing by the copyright owner as "Not a
59
+ Contribution."
60
+
61
+ "Contributor" shall mean Licensor and any individual or Legal Entity on
62
+ behalf of whom a Contribution has been received by Licensor and
63
+ subsequently incorporated within the Work.
64
+
65
+ 2. Grant of Copyright License. Subject to the terms and conditions of this
66
+ License, each Contributor hereby grants to You a perpetual, worldwide,
67
+ non-exclusive, no-charge, royalty-free, irrevocable copyright license to
68
+ reproduce, prepare Derivative Works of, publicly display, publicly perform,
69
+ sublicense, and distribute the Work and such Derivative Works in Source or
70
+ Object form.
71
+
72
+ 3. Grant of Patent License. Subject to the terms and conditions of this
73
+ License, each Contributor hereby grants to You a perpetual, worldwide,
74
+ non-exclusive, no-charge, royalty-free, irrevocable (except as stated in
75
+ this section) patent license to make, have made, use, offer to sell, sell,
76
+ import, and otherwise transfer the Work, where such license applies only to
77
+ those patent claims licensable by such Contributor that are necessarily
78
+ infringed by their Contribution(s) alone or by combination of their
79
+ Contribution(s) with the Work to which such Contribution(s) was submitted.
80
+ If You institute patent litigation against any entity (including a
81
+ cross-claim or counterclaim in a lawsuit) alleging that the Work or a
82
+ Contribution incorporated within the Work constitutes direct or
83
+ contributory patent infringement, then any patent licenses granted to You
84
+ under this License for that Work shall terminate as of the date such
85
+ litigation is filed.
86
+
87
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
88
+ Derivative Works thereof in any medium, with or without modifications, and
89
+ in Source or Object form, provided that You meet the following conditions:
90
+
91
+ 1. You must give any other recipients of the Work or Derivative Works a
92
+ copy of this License; and
93
+
94
+ 2. You must cause any modified files to carry prominent notices stating
95
+ that You changed the files; and
96
+
97
+ 3. You must retain, in the Source form of any Derivative Works that You
98
+ distribute, all copyright, patent, trademark, and attribution notices from
99
+ the Source form of the Work, excluding those notices that do not pertain to
100
+ any part of the Derivative Works; and
101
+
102
+ 4. If the Work includes a "NOTICE" text file as part of its
103
+ distribution, then any Derivative Works that You distribute must include a
104
+ readable copy of the attribution notices contained within such NOTICE file,
105
+ excluding those notices that do not pertain to any part of the Derivative
106
+ Works, in at least one of the following places: within a NOTICE text file
107
+ distributed as part of the Derivative Works; within the Source form or
108
+ documentation, if provided along with the Derivative Works; or, within a
109
+ display generated by the Derivative Works, if and wherever such third-party
110
+ notices normally appear. The contents of the NOTICE file are for
111
+ informational purposes only and do not modify the License. You may add Your
112
+ own attribution notices within Derivative Works that You distribute,
113
+ alongside or as an addendum to the NOTICE text from the Work, provided that
114
+ such additional attribution notices cannot be construed as modifying the
115
+ License.
116
+
117
+ You may add Your own copyright statement to Your modifications and may
118
+ provide additional or different license terms and conditions for use,
119
+ reproduction, or distribution of Your modifications, or for any such
120
+ Derivative Works as a whole, provided Your use, reproduction, and
121
+ distribution of the Work otherwise complies with the conditions stated in
122
+ this License.
123
+
124
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any
125
+ Contribution intentionally submitted for inclusion in the Work by You to
126
+ the Licensor shall be under the terms and conditions of this License,
127
+ without any additional terms or conditions. Notwithstanding the above,
128
+ nothing herein shall supersede or modify the terms of any separate license
129
+ agreement you may have executed with Licensor regarding such Contributions.
130
+
131
+ 6. Trademarks. This License does not grant permission to use the trade
132
+ names, trademarks, service marks, or product names of the Licensor, except
133
+ as required for reasonable and customary use in describing the origin of
134
+ the Work and reproducing the content of the NOTICE file.
135
+
136
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to
137
+ in writing, Licensor provides the Work (and each Contributor provides its
138
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
139
+ KIND, either express or implied, including, without limitation, any
140
+ warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
141
+ FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
142
+ determining the appropriateness of using or redistributing the Work and
143
+ assume any risks associated with Your exercise of permissions under this
144
+ License.
145
+
146
+ 8. Limitation of Liability. In no event and under no legal theory, whether
147
+ in tort (including negligence), contract, or otherwise, unless required by
148
+ applicable law (such as deliberate and grossly negligent acts) or agreed to
149
+ in writing, shall any Contributor be liable to You for damages, including
150
+ any direct, indirect, special, incidental, or consequential damages of any
151
+ character arising as a result of this License or out of the use or
152
+ inability to use the Work (including but not limited to damages for loss of
153
+ goodwill, work stoppage, computer failure or malfunction, or any and all
154
+ other commercial damages or losses), even if such Contributor has been
155
+ advised of the possibility of such damages.
156
+
157
+ 9. Accepting Warranty or Additional Liability. While redistributing the
158
+ Work or Derivative Works thereof, You may choose to offer, and charge a fee
159
+ for, acceptance of support, warranty, indemnity, or other liability
160
+ obligations and/or rights consistent with this License. However, in
161
+ accepting such obligations, You may act only on Your own behalf and on Your
162
+ sole responsibility, not on behalf of any other Contributor, and only if
163
+ You agree to indemnify, defend, and hold each Contributor harmless for any
164
+ liability incurred by, or claims asserted against, such Contributor by
165
+ reason of your accepting any such warranty or additional liability.
166
+
167
+ END OF TERMS AND CONDITIONS
168
+
169
+ APPENDIX: How to apply the Apache License to your work
170
+
171
+ To apply the Apache License to your work, attach the following boilerplate
172
+ notice, with the fields enclosed by brackets "[]" replaced with your own
173
+ identifying information. (Don't include the brackets!) The text should be
174
+ enclosed in the appropriate comment syntax for the file format. We also
175
+ recommend that a file or class name and description of purpose be included
176
+ on the same "printed page" as the copyright notice for easier
177
+ identification within third-party archives.
178
+
179
+ Copyright [yyyy] [name of copyright owner]
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License"); you may
182
+ not use this file except in compliance with the License. You may obtain a
183
+ copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable
188
+ law or agreed to in writing, software distributed under the License is
189
+ distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
190
+ KIND, either express or implied. See the License for the specific language
191
+ governing permissions and limitations under the License.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # amr-decode
2
+
3
+ Decode AMR-NB and AMR-WB audio to PCM float samples. [opencore-amr](https://sourceforge.net/projects/opencore-amr/) compiled to WASM — works in Node.js and browsers.
4
+
5
+ Part of [audio-decode](https://github.com/audiojs/audio-decode).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ npm i @audio/amr-decode
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```js
16
+ import decode from '@audio/amr-decode'
17
+
18
+ // AMR-NB or AMR-WB — auto-detected from file header
19
+ let { channelData, sampleRate } = await decode(amrBuffer)
20
+ // AMR-NB: 8000 Hz mono
21
+ // AMR-WB: 16000 Hz mono
22
+ ```
23
+
24
+ ### Streaming
25
+
26
+ ```js
27
+ import { decoder } from '@audio/amr-decode'
28
+
29
+ let dec = await decoder()
30
+ let result = dec.decode(chunk)
31
+ dec.free()
32
+ ```
33
+
34
+ ## API
35
+
36
+ ### `decode(src): Promise<AudioData>`
37
+
38
+ Whole-file decode. Accepts `Uint8Array` or `ArrayBuffer`. Auto-detects AMR-NB (`#!AMR\n`) vs AMR-WB (`#!AMR-WB\n`).
39
+
40
+ ### `decoder(): Promise<AMRDecoder>`
41
+
42
+ Creates a decoder instance.
43
+
44
+ - **`dec.decode(data)`** — decode chunk, returns `{ channelData, sampleRate }`
45
+ - **`dec.flush()`** — flush (returns empty)
46
+ - **`dec.free()`** — release WASM memory
47
+
48
+ ## Formats
49
+
50
+ - AMR-NB — 8 kHz, modes 0–7 (4.75–12.2 kbps)
51
+ - AMR-WB — 16 kHz, modes 0–8 (6.6–23.85 kbps)
52
+
53
+ ## Building WASM
54
+
55
+ The WASM binary is prebuilt in `src/amr.wasm.cjs`. To rebuild:
56
+
57
+ ```
58
+ npm run build
59
+ ```
60
+
61
+ This auto-fetches opencore-amr 0.1.6 source and compiles with Emscripten.
62
+
63
+ ## License
64
+
65
+ Apache-2.0 (opencore-amr) — [krishnized](https://github.com/krishnized/license)
@@ -0,0 +1,16 @@
1
+ interface AudioData {
2
+ channelData: Float32Array[];
3
+ sampleRate: number;
4
+ }
5
+
6
+ interface AMRDecoder {
7
+ decode(data: Uint8Array): AudioData;
8
+ flush(): AudioData;
9
+ free(): void;
10
+ }
11
+
12
+ /** Whole-file decode — auto-detects AMR-NB vs AMR-WB */
13
+ export default function decode(src: ArrayBuffer | Uint8Array): Promise<AudioData>;
14
+
15
+ /** Create decoder instance */
16
+ export function decoder(): Promise<AMRDecoder>;
package/amr-decode.js ADDED
@@ -0,0 +1,148 @@
1
+ /**
2
+ * AMR decoder — opencore-amr compiled to WASM
3
+ * Decodes AMR-NB (8kHz) and AMR-WB (16kHz) audio files
4
+ *
5
+ * let { channelData, sampleRate } = await decode(amrbuf)
6
+ */
7
+
8
+ let _modP
9
+
10
+ async function getMod() {
11
+ if (_modP) return _modP
12
+ let p = (async () => {
13
+ let createAMR
14
+ if (typeof process !== 'undefined' && process.versions?.node) {
15
+ let m = 'module'
16
+ let { createRequire } = await import(m)
17
+ createAMR = createRequire(import.meta.url)('./src/amr.wasm.cjs')
18
+ } else {
19
+ let mod = await import('./src/amr.wasm.cjs')
20
+ createAMR = mod.default || mod
21
+ }
22
+ return createAMR()
23
+ })()
24
+ _modP = p
25
+ try { return await p }
26
+ catch (e) { _modP = null; throw e }
27
+ }
28
+
29
+ /**
30
+ * Whole-file decode
31
+ * @param {Uint8Array|ArrayBuffer} src
32
+ * @returns {Promise<{channelData: Float32Array[], sampleRate: number}>}
33
+ */
34
+ export default async function decode(src) {
35
+ let buf = src instanceof Uint8Array ? src : new Uint8Array(src)
36
+ let dec = await decoder()
37
+ try {
38
+ return dec.decode(buf)
39
+ } finally {
40
+ dec.free()
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Create decoder instance
46
+ * @returns {Promise<{decode(chunk: Uint8Array): {channelData, sampleRate}, flush(), free()}>}
47
+ */
48
+ export async function decoder() {
49
+ return new AMRDecoder(await getMod())
50
+ }
51
+
52
+ const EMPTY = Object.freeze({ channelData: [], sampleRate: 0 })
53
+
54
+ // AMR-NB frame sizes (bytes, incl. header) indexed by mode 0-15
55
+ const NB_SIZES = [13, 14, 16, 18, 20, 21, 27, 32, 6, 6, 6, 6, 1, 1, 1, 1]
56
+ // AMR-WB frame sizes (bytes, incl. header) indexed by mode 0-15
57
+ const WB_SIZES = [18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1]
58
+
59
+ class AMRDecoder {
60
+ constructor(mod) {
61
+ this.m = mod
62
+ this.done = false
63
+ this._ptr = 0
64
+ this._cap = 0
65
+ }
66
+
67
+ decode(data) {
68
+ if (this.done) throw Error('Decoder already freed')
69
+ if (!data?.length) return EMPTY
70
+
71
+ let buf = data instanceof Uint8Array ? data : new Uint8Array(data)
72
+
73
+ // detect AMR-NB "#!AMR\n" or AMR-WB "#!AMR-WB\n"
74
+ let isWB = false, off = 0
75
+ if (buf.length > 6 &&
76
+ buf[0] === 0x23 && buf[1] === 0x21 &&
77
+ buf[2] === 0x41 && buf[3] === 0x4D && buf[4] === 0x52) {
78
+ if (buf.length > 9 &&
79
+ buf[5] === 0x2D && buf[6] === 0x57 && buf[7] === 0x42 && buf[8] === 0x0A) {
80
+ isWB = true
81
+ off = 9
82
+ } else if (buf[5] === 0x0A) {
83
+ off = 6
84
+ }
85
+ }
86
+
87
+ if (!off) return EMPTY // no valid AMR header
88
+
89
+ let m = this.m
90
+ let sampleRate = isWB ? 16000 : 8000
91
+ let sizes = isWB ? WB_SIZES : NB_SIZES
92
+
93
+ let h = isWB ? m._amr_wb_create() : m._amr_nb_create()
94
+ let chunks = []
95
+
96
+ while (off < buf.length) {
97
+ let mode = (buf[off] >> 3) & 0x0F
98
+ let frameSize = sizes[mode]
99
+ if (!frameSize || off + frameSize > buf.length) break
100
+
101
+ let ptr = this._alloc(frameSize)
102
+ m.HEAPU8.set(buf.subarray(off, off + frameSize), ptr)
103
+
104
+ let n = isWB
105
+ ? m._amr_wb_decode(h, ptr, frameSize)
106
+ : m._amr_nb_decode(h, ptr, frameSize)
107
+
108
+ let outPtr = isWB ? m._amr_wb_output() : m._amr_nb_output()
109
+ chunks.push(new Float32Array(m.HEAPF32.buffer, outPtr, n).slice())
110
+
111
+ off += frameSize
112
+ }
113
+
114
+ if (isWB) m._amr_wb_close(h)
115
+ else m._amr_nb_close(h)
116
+
117
+ if (!chunks.length) return EMPTY
118
+
119
+ let total = 0
120
+ for (let c of chunks) total += c.length
121
+ let out = new Float32Array(total)
122
+ let pos = 0
123
+ for (let c of chunks) { out.set(c, pos); pos += c.length }
124
+
125
+ return { channelData: [out], sampleRate }
126
+ }
127
+
128
+ flush() { return EMPTY }
129
+
130
+ free() {
131
+ if (this.done) return
132
+ this.done = true
133
+ if (this._ptr) {
134
+ this.m._free(this._ptr)
135
+ this._ptr = 0
136
+ this._cap = 0
137
+ }
138
+ }
139
+
140
+ _alloc(len) {
141
+ if (len > this._cap) {
142
+ if (this._ptr) this.m._free(this._ptr)
143
+ this._cap = len
144
+ this._ptr = this.m._malloc(len)
145
+ }
146
+ return this._ptr
147
+ }
148
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@audio/amr-decode",
3
+ "version": "1.0.0",
4
+ "description": "Decode AMR-NB/WB audio via opencore-amr WASM",
5
+ "type": "module",
6
+ "main": "amr-decode.js",
7
+ "types": "amr-decode.d.ts",
8
+ "exports": {
9
+ ".": "./amr-decode.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "scripts": {
13
+ "build": "bash build.sh",
14
+ "prepack": "npm run build",
15
+ "test": "node test.js"
16
+ },
17
+ "files": [
18
+ "amr-decode.js",
19
+ "amr-decode.d.ts",
20
+ "src/amr.wasm.cjs",
21
+ "LICENSE"
22
+ ],
23
+ "keywords": [
24
+ "amr",
25
+ "amr-nb",
26
+ "amr-wb",
27
+ "audio",
28
+ "decode",
29
+ "decoder",
30
+ "wasm",
31
+ "opencore-amr",
32
+ "speech"
33
+ ],
34
+ "publishConfig": { "access": "public" },
35
+ "license": "Apache-2.0",
36
+ "author": "audiojs",
37
+ "homepage": "https://github.com/audiojs/amr-decode#readme",
38
+ "bugs": "https://github.com/audiojs/amr-decode/issues",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/audiojs/amr-decode.git"
42
+ },
43
+ "engines": {
44
+ "node": ">=16"
45
+ }
46
+ }
Binary file