Skip to content

Commit 65c4425

Browse files
committed
Add self-bundle
1 parent 5cd4f2d commit 65c4425

11 files changed

Lines changed: 568 additions & 103 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"scripts": {
3131
"test": "node test.js",
3232
"test:all": "npm test --workspaces && npm test",
33-
"publish:all": "npm publish --workspaces && npm publish"
33+
"publish:all": "npm publish --workspaces && npm publish",
34+
"build": "npm run build --workspaces --if-present"
3435
},
3536
"repository": {
3637
"type": "git",
@@ -69,6 +70,7 @@
6970
"audio-buffer": "^7.3.0",
7071
"audio-decode": "^4.0.0",
7172
"audio-lena": "^3.0.0",
73+
"esbuild": "^0.28.0",
7274
"tst": "^9.2.2"
7375
}
7476
}

packages/encode-flac/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
"node": ">=16"
4040
},
4141
"bugs": "https://github.com/audiojs/audio-encode/issues",
42-
"scripts": {
43-
"test": "node test.js"
44-
},
4542
"dependencies": {
4643
"libflacjs": "^5.4.0"
44+
},
45+
"scripts": {
46+
"test": "node test.js"
4747
}
4848
}

packages/encode-mp3/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
cd "$(dirname "$0")"
4+
npx esbuild src/mp3-encode.src.js --bundle --format=esm --outfile=mp3-encode.js --platform=browser

packages/encode-mp3/mp3-encode.js

Lines changed: 230 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/encode-mp3/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
},
4141
"bugs": "https://github.com/audiojs/audio-encode/issues",
4242
"scripts": {
43-
"test": "node test.js"
43+
"test": "node test.js",
44+
"build": "bash build.sh",
45+
"prepack": "npm run build"
4446
},
4547
"dependencies": {
4648
"wasm-media-encoders": "^0.7.0"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// requires: wasm-media-encoders
2+
import { createMp3Encoder } from 'wasm-media-encoders'
3+
4+
/**
5+
* MP3 encoder — browser + Node, via wasm-media-encoders
6+
*
7+
* @param {Object} opts
8+
* @param {number} opts.sampleRate - required
9+
* @param {number} [opts.bitrate=128] - kbps (CBR)
10+
* @param {number} [opts.quality] - 0-9 VBR quality (0=best, 9=worst). If set, uses VBR mode.
11+
* @param {number} [opts.channels] - 1 or 2
12+
* @returns {{ encode, flush, free }}
13+
*
14+
* encode(channels: Float32Array[]) → Uint8Array
15+
* flush() → Uint8Array
16+
* free() → void
17+
*/
18+
export default async function mp3(opts) {
19+
let { sampleRate, bitrate = 128, quality, channels } = opts
20+
if (!channels || channels < 1 || channels > 2) channels = 2
21+
22+
let encoder = await createMp3Encoder()
23+
24+
let cfg = { sampleRate, channels }
25+
if (quality != null) cfg.vbrQuality = quality
26+
else cfg.bitrate = bitrate
27+
28+
encoder.configure(cfg)
29+
30+
// WASM encoder has ~320MB/channel buffer limit.
31+
// Chunk large inputs in 1152*1024 (~1.18M) sample blocks.
32+
const CHUNK = 1152 * 1024
33+
34+
return { encode, flush, free }
35+
36+
function encode(ch) {
37+
let n = ch[0].length
38+
if (n <= CHUNK) {
39+
let raw = encoder.encode(ch)
40+
return new Uint8Array(raw)
41+
}
42+
let parts = []
43+
for (let i = 0; i < n; i += CHUNK) {
44+
let end = Math.min(i + CHUNK, n)
45+
let slice = ch.map(c => c.subarray(i, end))
46+
let raw = encoder.encode(slice)
47+
if (raw.length) parts.push(new Uint8Array(raw))
48+
}
49+
let total = 0
50+
for (let p of parts) total += p.length
51+
let out = new Uint8Array(total)
52+
let off = 0
53+
for (let p of parts) { out.set(p, off); off += p.length }
54+
return out
55+
}
56+
57+
function flush() {
58+
let raw = encoder.finalize()
59+
return new Uint8Array(raw)
60+
}
61+
62+
function free() {
63+
encoder = null
64+
}
65+
}

packages/encode-ogg/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
cd "$(dirname "$0")"
4+
npx esbuild src/ogg-encode.src.js --bundle --format=esm --outfile=ogg-encode.js --platform=browser

packages/encode-ogg/ogg-encode.js

Lines changed: 211 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/encode-ogg/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
},
3939
"bugs": "https://github.com/audiojs/audio-encode/issues",
4040
"scripts": {
41-
"test": "node test.js"
41+
"test": "node test.js",
42+
"build": "bash build.sh",
43+
"prepack": "npm run build"
4244
},
4345
"dependencies": {
4446
"wasm-media-encoders": "^0.7.0"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// requires: wasm-media-encoders
2+
import { createOggEncoder } from 'wasm-media-encoders'
3+
4+
/**
5+
* OGG Vorbis encoder — browser + Node (WASM)
6+
*
7+
* @param {Object} opts
8+
* @param {number} opts.sampleRate - required
9+
* @param {number} [opts.quality=3] - quality level -1 to 10
10+
* @param {number} [opts.channels] - 1 or 2
11+
* @returns {Promise<{ encode, flush, free }>}
12+
*/
13+
export default async function ogg(opts) {
14+
let { sampleRate, quality = 3, channels } = opts
15+
let enc = await createOggEncoder()
16+
let nch = channels || 0
17+
18+
if (nch) enc.configure({ sampleRate, channels: nch, vbrQuality: quality })
19+
20+
return { encode, flush, free }
21+
22+
function encode(ch) {
23+
if (!nch) {
24+
nch = ch.length
25+
enc.configure({ sampleRate, channels: nch, vbrQuality: quality })
26+
}
27+
let raw = enc.encode(ch)
28+
return new Uint8Array(raw)
29+
}
30+
31+
function flush() {
32+
let raw = enc.finalize()
33+
return new Uint8Array(raw)
34+
}
35+
36+
function free() {
37+
enc = null
38+
}
39+
}

0 commit comments

Comments
 (0)