Skip to content

Commit

Permalink
Merge pull request #21 from eshaz/common-module
Browse files Browse the repository at this point in the history
Common Module
eshaz authored Feb 24, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents dcef116 + 85ee179 commit a1103f3
Showing 47 changed files with 4,479 additions and 9,982 deletions.
40 changes: 6 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ OGG_OPUS_DECODER_MODULE_MIN=src/ogg-opus-decoder/dist/ogg-opus-decoder.min.js

ogg-opus-decoder: opus-wasmlib ogg-opus-decoder-minify $(OGG_OPUS_DECODER_EMSCRIPTEN_BUILD)
ogg-opus-decoder-minify: $(OGG_OPUS_DECODER_EMSCRIPTEN_BUILD)
node build/compress.js ${OGG_OPUS_DECODER_EMSCRIPTEN_BUILD}
node build.js ${OGG_OPUS_DECODER_EMSCRIPTEN_BUILD}
node_modules/.bin/rollup src/ogg-opus-decoder/index.js --file $(OGG_OPUS_DECODER_MODULE) --config src/ogg-opus-decoder/rollup.config.js
node_modules/.bin/terser --config-file src/ogg-opus-decoder/terser.json ${OGG_OPUS_DECODER_MODULE} -o ${OGG_OPUS_DECODER_MODULE_MIN}
cp $(OGG_OPUS_DECODER_MODULE) $(DEMO_PATH)
@@ -34,7 +34,7 @@ OPUS_DECODER_MODULE_MIN=src/opus-decoder/dist/opus-decoder.min.js

opus-decoder: opus-wasmlib opus-decoder-minify $(OPUS_DECODER_EMSCRIPTEN_BUILD)
opus-decoder-minify: $(OPUS_DECODER_EMSCRIPTEN_BUILD)
node build/compress.js $(OPUS_DECODER_EMSCRIPTEN_BUILD)
node build.js $(OPUS_DECODER_EMSCRIPTEN_BUILD)
node_modules/.bin/rollup src/opus-decoder/index.js --file $(OPUS_DECODER_MODULE) --config src/opus-decoder/rollup.config.js
node_modules/.bin/terser --config-file src/opus-decoder/terser.json $(OPUS_DECODER_MODULE) -o $(OPUS_DECODER_MODULE_MIN)
cp $(OPUS_DECODER_MODULE) $(DEMO_PATH)
@@ -54,7 +54,7 @@ MPG123_MODULE_MIN=src/mpg123-decoder/dist/mpg123-decoder.min.js

mpg123-decoder: mpg123-wasmlib mpg123-decoder-minify ${MPG123_EMSCRIPTEN_BUILD}
mpg123-decoder-minify: $(MPG123_EMSCRIPTEN_BUILD)
node build/compress.js $(MPG123_EMSCRIPTEN_BUILD)
node build.js $(MPG123_EMSCRIPTEN_BUILD)
node_modules/.bin/rollup src/mpg123-decoder/index.js --file $(MPG123_MODULE) --config src/mpg123-decoder/rollup.config.js
node_modules/.bin/terser --config-file src/mpg123-decoder/terser.json $(MPG123_MODULE) -o $(MPG123_MODULE_MIN)
cp $(MPG123_MODULE) $(DEMO_PATH)
@@ -81,6 +81,8 @@ define EMCC_OPTS
-flto \
-s BINARYEN_EXTRA_PASSES="-O4" \
-s MINIMAL_RUNTIME=2 \
-s TEXTDECODER=2 \
-s SUPPORT_ERRNO=0 \
-s SINGLE_FILE=1 \
-s SUPPORT_LONGJMP=0 \
-s MALLOC="emmalloc" \
@@ -121,21 +123,6 @@ $(OPUS_DECODER_EMSCRIPTEN_BUILD): $(OPUS_WASM_LIB)
@ echo "|"
@ echo "+-------------------------------------------------------------------------------"

$(OPUS_DECODER_MODULE_ESM): $(OPUS_DECODER_EMSCRIPTEN_BUILD)
@ echo "Building Emscripten WebAssembly ES Module $(OPUS_DECODER_MODULE_ESM)..."
@ emcc \
-o "$(OPUS_DECODER_MODULE_ESM)" \
-s EXPORT_ES6=1 \
-s MODULARIZE=1 \
${EMCC_OPTS} \
$(OPUS_DECODER_EMCC_OPTS) \
$(OPUS_WASM_LIB)
@ echo "+-------------------------------------------------------------------------------"
@ echo "|"
@ echo "| Successfully built ES Module: $(OPUS_DECODER_MODULE_ESM)"
@ echo "|"
@ echo "+-------------------------------------------------------------------------------"

# ------------
# ogg-opus-decoder
# ------------
@@ -170,21 +157,6 @@ $(OGG_OPUS_DECODER_EMSCRIPTEN_BUILD): $(OPUS_WASM_LIB)
@ echo "|"
@ echo "+-------------------------------------------------------------------------------"

$(OGG_OPUS_DECODER_MODULE_ESM): $(OGG_OPUS_DECODER_EMSCRIPTEN_BUILD)
@ echo "Building Emscripten WebAssembly ES Module $(OGG_OPUS_DECODER_MODULE_ESM)..."
@ emcc \
-o "$(OGG_OPUS_DECODER_MODULE_ESM)" \
-s EXPORT_ES6=1 \
-s MODULARIZE=1 \
${EMCC_OPTS} \
$(OGG_OPUS_DECODER_EMCC_OPTS) \
$(OPUS_WASM_LIB)
@ echo "+-------------------------------------------------------------------------------"
@ echo "|"
@ echo "| Successfully built ES Module: $(OGG_OPUS_DECODER_MODULE_ESM)"
@ echo "|"
@ echo "+-------------------------------------------------------------------------------"

# -------------------
# Shared Opus library
# -------------------
@@ -307,7 +279,7 @@ configure-mpg123:
--enable-layer2 \
--enable-layer3 \
--disable-largefile \
--disable-feature_report \
--disable-feature-report \
--enable-runtime-tables
cd $(MPG123_SRC); rm a.wasm

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -14,23 +14,23 @@ Pre-built minified JS files are available from NPM and in each decoder's `dist`

### [`mpg123-decoder`](https://github.com/eshaz/wasm-audio-decoders/tree/master/src/mpg123-decoder)
Decodes MPEG Layer I/II/III into PCM
* 85.5 KiB minified bundle size
* 85.4 KiB minified bundle size
* Browser and NodeJS support
* Built in Web Worker support
* Based on [`mpg123`](https://www.mpg123.de/)
* Install using [NPM](https://www.npmjs.com/package/mpg123-decoder)

### [`ogg-opus-decoder`](https://github.com/eshaz/wasm-audio-decoders/tree/master/src/ogg-opus-decoder)
Decodes Ogg Opus data into PCM
* 115.1 KiB minified bundle size
* 115.5 KiB minified bundle size
* Browser and NodeJS support
* Built in Web Worker support
* Based on [`libopusfile`](https://github.com/xiph/opusfile)
* Install using [NPM](https://www.npmjs.com/package/ogg-opus-decoder)

### [`opus-decoder`](https://github.com/eshaz/wasm-audio-decoders/tree/master/src/opus-decoder)
Decodes raw Opus audio frames into PCM
* 87.2 KiB minified bundle size
* 87.7 KiB minified bundle size
* Browser and NodeJS support
* Built in Web Worker support
* Based on [`libopus`](https://github.com/xiph/opus)
@@ -41,7 +41,7 @@ Decodes raw Opus audio frames into PCM

### Prerequisites
1. Install Emscripten by following these [instructions](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html#installation-instructions).
* This repository has been tested with Emscripten 2.0.32.
* This repository has been tested with Emscripten 2.0.34.

### Building
1. Make sure to `source` the Emscripten path in the terminal you want build in.
25 changes: 9 additions & 16 deletions build/compress.js → build.js
Original file line number Diff line number Diff line change
@@ -3,21 +3,12 @@ import yenc from "simple-yenc";
import { deflateSync } from "fflate";

const distPath = process.argv[2];
const tinyInflatePath = new URL("tiny-inflate.js", import.meta.url).pathname;
const decoder = fs.readFileSync(distPath, { encoding: "ascii" });
const tinyInflate = fs.readFileSync(tinyInflatePath, { encoding: "ascii" });

const wasmBase64ContentMatcher =
/Module\["wasm"\] = base64Decode\("(?<wasm>(.+))"\)/;
const wasmBase64DeclarationMatcher = 'Module["wasm"] = base64Decode("';

// code before the wasm
const startIdx = decoder.indexOf(wasmBase64DeclarationMatcher);
let start = decoder.substring(0, startIdx);

// add the yenc decode function and inline decoding
start += 'Module["wasm"] = tinf_uncompress((' + yenc.decode.toString() + ")(`";

// original wasm
const wasmContent = decoder.match(wasmBase64ContentMatcher).groups.wasm;
// compressed buffer
@@ -30,11 +21,12 @@ const wasmBufferCompressed = deflateSync(wasmBuffer, {
const yencEncodedWasm = yenc.encode(wasmBufferCompressed);
const yencStringifiedWasm = yenc.stringify(yencEncodedWasm);

// code before the wasm
const startIdx = decoder.indexOf(wasmBase64DeclarationMatcher);

// code after the wasm
const endIdx =
startIdx + wasmBase64DeclarationMatcher.length + wasmContent.length + 2;
let end = `\`), new Uint8Array(${wasmBuffer.length}))`;
end += decoder.substring(endIdx);

const banner =
"/* **************************************************\n" +
@@ -44,15 +36,16 @@ const banner =
"\n\n";

// Concatenate the strings as buffers to preserve extended ascii
let finalString = Buffer.concat(
const finalString = Buffer.concat(
[
banner,
"export default class EmscriptenWASM {\n",
"constructor() {\n",
tinyInflate,
start,
"constructor(WASMAudioDecoderCommon) {\n",
decoder.substring(0, startIdx),
'Module["wasm"] = WASMAudioDecoderCommon.inflateYencString(`',
yencStringifiedWasm,
end,
`\`, new Uint8Array(${wasmBuffer.length}))`,
decoder.substring(endIdx),
"}",
"}",
].map(Buffer.from)
Loading

0 comments on commit a1103f3

Please sign in to comment.