Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
opus-decoder
Advanced tools
opus-decoder
opus-decoder
is a Web Assembly Opus audio decoder.
libopus
See the homepage of this repository for more Web Assembly audio decoders like this one.
Install from NPM.
Run npm i opus-decoder
import { OpusDecoder } from 'opus-decoder';
const decoder = new OpusDecoder();
Or download the build and include it as a script.
<script src="opus-decoder.min.js"></script>
<script>
const decoder = new window["opus-decoder"].OpusDecoder();
</script>
Create a new instance and wait for the WASM to finish compiling. Decoding can be done on the main thread synchronously, or in a webworker asynchronously.
Main thread synchronous decoding
import { OpusDecoder } from 'opus-decoder';
const decoder = new OpusDecoder();
// wait for the WASM to be compiled
await decoder.ready;
Web Worker asynchronous decoding
import { OpusDecoderWebWorker } from 'opus-decoder';
const decoder = new OpusDecoderWebWorker();
// wait for the WASM to be compiled
await decoder.ready;
Begin decoding Opus frames.
// Decode an individual Opus frame
const {channelData, samplesDecoded, sampleRate} = decoder.decodeFrame(opusFrame);
// Decode an array of individual Opus frames
const {channelData, samplesDecoded, sampleRate} = decoder.decodeFrames(opusFrameArray);
When done decoding, reset the decoder to decode a new stream, or free up the memory being used by the WASM module if you have no more audio to decode.
// `reset()` clears the decoder state and allows you do decode a new stream of Opus frames.
await decoder.reset();
// `free()` de-allocates the memory used by the decoder. You will need to create a new instance after calling `free()` to start decoding again.
decoder.free();
Decoded audio is always returned in the below structure.
{
channelData: [
leftAudio, // Float32Array of PCM samples for the left channel
rightAudio, // Float32Array of PCM samples for the right channel
... // additional channels
],
samplesDecoded: 1234, // number of PCM samples that were decoded per channel
sampleRate: 48000 // sample rate of the decoded PCM
}
Each Float32Array within channelData
can be used directly in the WebAudio API for playback.
Each channel is assigned to a speaker location in a conventional surround arrangement. Specific locations depend on the number of channels, and are given below in order of the corresponding channel indices. This set of surround options and speaker location orderings is the same as those used by the Vorbis codec.
See: https://datatracker.ietf.org/doc/html/rfc7845.html#section-5.1.1.2
Each Float32Array within channelData
can be used directly in the WebAudio API for playback.
OpusDecoder
Class that decodes Opus frames synchronously on the main thread.
const decoder = new OpusDecoder({
preSkip: 0,
channels: 2,
streamCount: 1,
coupledStreamCount: 1,
channelMappingTable: [0, 1]
});
See this documentation on the Opus header for more information. If you don't have access to the Opus header, the default values will successfully decode most stereo Opus streams.
preSkip
optional, defaults to 0
channels
optional, defaults to 2
streamCount
optional, defaults to 1
coupledStreamCount
optional, defaults to: 1
when 2 channels, 0
when 1 channel
channelMappingTable
optional, defaults to [0, 1]
when 2 channels, [0]
when 1 channel
decoder.ready
async
decoder.decodeFrame(opusFrame)
opusFrame
Uint8Array containing a single Opus frame.decoder.decodeFrames(opusFrames)
opusFrames
Array of Uint8Arrays containing Opus frames.decoder.reset()
async
decoder.free()
free()
, the current instance is made unusable, and a new instance will need to be created to decode additional Opus frames.OpusDecoderWebWorker
Class that decodes Opus frames asynchronously within a web worker. Decoding is performed in a separate, non-blocking thread. Each new instance spawns a new worker allowing you to run multiple workers for concurrent decoding of multiple streams.
const decoder = new OpusDecoderWebWorker({
preSkip: 0,
channels: 2,
streamCount: 1,
coupledStreamCount: 1,
channelMappingTable: [0, 1]
});
See this documentation on the Opus header for more information. If you don't have access to the Opus header, the default values will successfully decode most stereo Opus streams.
preSkip
optional, defaults to 0
channels
optional, defaults to 2
streamCount
optional, defaults to 1
coupledStreamCount
optional, defaults to: 1
when 2 channels, 0
when 1 channel
channelMappingTable
optional, defaults to [0, 1]
when 2 channels, [0]
when 1 channel
decoder.ready
async
decoder.decodeFrame(opusFrame)
async
opusFrame
Uint8Array containing a single Opus frame.decoder.decodeFrames(opusFrames)
async
opusFrames
Array of Uint8Arrays containing Opus frames.decoder.reset()
async
decoder.free()
async
free()
, the current instance is made unusable, and a new instance will need to be created to decode additional Opus frames.OpusDecoderWebWorker
uses async functions to send operations to the web worker without blocking the main thread. To fully take advantage of the concurrency provided by web workers, your code should avoid using await
on decode operations where it will block the main thread.
Each method call on a OpusDecoderWebWorker
instance will queue up an operation to the web worker. Operations will complete within the web worker thread one at a time and in the same order in which the methods were called.
Good Main thread is not blocked during each decode operation. The example playAudio
function is called when each decode operation completes. Also, the next decode operation can begin while playAudio
is doing work on the main thread.
const playAudio = ({ channelData, samplesDecoded, sampleRate }) => {
// does something to play the audio data.
}
decoder.decodeFrame(frameData1).then(playAudio);
decoder.decodeFrame(frameData2).then(playAudio);
decoder.decodeFrame(frameData3).then(playAudio);
// do some other operations while the audio is decoded
Bad Main thread is being blocked by await
during each decode operation. Synchronous code is halted while decoding completes, negating the benefits of using a webworker.
const decoded1 = await decoder.decodeFrame(frameData1); // blocks the main thread
playAudio(decoded1);
const decoded2 = await decoder.decodeFrame(frameData2); // blocks the main thread
playAudio(decoded2);
const decoded3 = await decoder.decodeFrame(frameData3); // blocks the main thread
playAudio(decoded3);
FAQs
Web Assembly streaming Opus decoder
The npm package opus-decoder receives a total of 240,312 weekly downloads. As such, opus-decoder popularity was classified as popular.
We found that opus-decoder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.