30 lines
895 B
TypeScript
30 lines
895 B
TypeScript
import { createWasmCodec } from "./wasm-base.js";
|
|
|
|
function mp3Magic(bytes: Uint8Array): boolean {
|
|
if (bytes.byteLength < 2) return false;
|
|
// MPEG audio sync word (11 bits) — 0xFFE mask on first two bytes
|
|
if (bytes[0] === 0xff && (bytes[1]! & 0xe0) === 0xe0) return true;
|
|
// ID3v2 prefix requires three bytes
|
|
if (
|
|
bytes.byteLength >= 3 &&
|
|
bytes[0] === 0x49 &&
|
|
bytes[1] === 0x44 &&
|
|
bytes[2] === 0x33
|
|
)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/** Lazy MP3 codec backed by `@audio/decode-mp3`. */
|
|
export const Mp3Codec = createWasmCodec({
|
|
name: "mp3",
|
|
extensions: ["mp3", "mpeg"],
|
|
mimeTypes: ["audio/mpeg", "audio/mp3"],
|
|
module: "@audio/decode-mp3",
|
|
magic: mp3Magic,
|
|
supportsEncode: true,
|
|
load: () =>
|
|
// @ts-expect-error optional dependency resolved at runtime
|
|
import("@audio/decode-mp3"),
|
|
});
|