#!/usr/bin/env bash # Rebuilds Cinejma's independently replaceable encoder-only LAME WebAssembly component. set -euo pipefail project_dir="$(cd "$(dirname "$0")/../../../.." && pwd)" lame_source="${LAME_SOURCE_DIR:-$project_dir/cpp/third-party/lame-4.0}" lame_build="${LAME_BUILD_DIR:-$project_dir/.wasm-local/lame}" output_dir="${LAME_OUTPUT_DIR:-$project_dir/web/public/wasm}" bridge="$project_dir/web/public/open-source/lame/lame_bridge.c" component_source="$project_dir/web/public/open-source/lame" # Stops with a useful error if the Emscripten environment is not active. require_tool() { command -v "$1" >/dev/null 2>&1 || { echo "Missing $1. Source emsdk_env.sh first." >&2; exit 1; }; } require_tool emcc require_tool emar mkdir -p "$lame_build" "$output_dir" # Compiles exactly the encoder library units; mpglib_interface.c and every frontend source are deliberately absent. lame_sources=(VbrTag bitstream encoder fft gain_analysis id3tag lame newmdct presets psymodel quantize quantize_pvt reservoir set_get tables takehiro util vbrquantize version) lame_objects=() for source in "${lame_sources[@]}"; do object="$lame_build/$source.o" emcc -O3 -DNDEBUG -DHAVE_CONFIG_H -I"$component_source" -I"$lame_source/include" -I"$lame_source/libmp3lame" \ -c "$lame_source/libmp3lame/$source.c" -o "$object" lame_objects+=("$object") done emar rcs "$lame_build/libmp3lame-encoder.a" "${lame_objects[@]}" emcc "$bridge" "$lame_build/libmp3lame-encoder.a" -I"$component_source" -I"$lame_source/include" -O3 -DNDEBUG \ -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sFILESYSTEM=0 -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=worker,node \ -sEXPORTED_RUNTIME_METHODS='["HEAPU8"]' \ -sEXPORTED_FUNCTIONS='["_malloc","_free","_init_lame_encoder","_encode_lame_pcm","_finish_lame_encoder","_get_lame_output_pointer","_get_lame_output_size","_close_lame_encoder"]' \ -sEXPORT_NAME=createLameModule -sASSERTIONS=0 --no-entry -o "$output_dir/lame.js"