A native audio decoder for NAPI-compatible runtimes.
npm install @napi-audio/decoderaiff | Yes | No |
caf | No | No |
isomp4 | No | No |
mkv | No | Yes |
ogg | Yes | Yes |
wav | Yes | Yes |
With npm
npm install @napi-audio/decoder
With pnpm
pnpm install @napi-audio/decoder
With yarn
yarn add @napi-audio/decoder
`
#### Usage
`js
import { readFileSync } from "fs";
import { Decoder } from "@napi-audio/decoder";
// read audio data from a file
const audioData = readFileSync("./audio-file.mp3");
// create an instance of Decoder
const decoder = new Decoder({
// don't enable gapless decoding (optional, disabled by default)
enableGapless: false,
// a hint about the audio format (optional, recommended)
mimeType: "audio/mpeg3",
// a hint about the audio container (optional, recommended)
fileExtension: "mp3",
});
// Append the audio data to the decoder, getting back a possible sample
// note that this can be called repeatedly with any "chunk" of data
const maybe_sample = decoder.append(audioData);
// if the sample is not null, we can process the contents
if (maybe_sample) {
console.log(Audio sample obtained with ${maybe_sample.data.length} bytes);
console.log(
Audio sample has ${maybe_sample.channelCount} channels @ ${maybe_sample.sampleRate}hz
);
}
// repeat the previous "append" step as many times as needed
// inform the decoder that we've sent all the data
decoder.finalize();
// flush the decoder, ensuring that all data has been decoded and processed
const maybe_final_sample = decoder.flush();
if (maybe_final_sample) {
console.log(
Final sample obtained with ${maybe_final_sample.data.length} bytes
);
}
// then close the decoder, freeing native resources
decoder.close();
`
#### Debugging
The native library is written in Rust, and can output tracing information if configured to do so.
The setNativeTracing export can be used for this - pass true to enable logging, or false to disable.
You'll also need to ensure the RUST_LOG environment variable is set according to the EnvFilter docs; for example RUST_LOG=trace`. Note that tracing disabled by default.