npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

opus-to-pcm

v1.0.2

Published

Decode raw opus packet to pcm without using any external library

Downloads

90

Readme

Opus to PCM

If we want to decode raw opus packet to PCM in our browsers, there are two ways to do that:

  1. Using libopus decoder of javascript version that can be ported using Emscripten.
  2. Using the Web Audio API method decodeAudioData

First approach is very straight forward but it needs to carry a big size decoder js file (approx. 900KB) and it is very resource expensive that become difficult to handle for the browsers that are running on low resource like mobile devices.

Second approach is the best way but unfortunately decodeAudioData fails to decode raw opus packet. Actually it expects Ogg bitstream instead of raw opus packet. This project basically does that thing. It encapsulate raw opus packet into ogg packet on the fly so that decodeAudioData can decode to PCM data. One problem with this approach is that opus is supported by all browsers till today e.g. Safari so libopus is included as a fallback.

How to use?

var decoder = new OpusToPCM(option);

Available options are:

channels - no of channels in opus data

fallback - true/false. Whether it will use libopus as fallback or not. Default is true. libopusPath - If fallback is true, you must provide libopus.js file path. A pre-built libopus file is available in dist directory

Decoder fire an event decode whenever it completes decoding. Usually it decodes several opus packet at a time for better performance although it need to be provided single opus packet into decode method.

Complete example:

var decoder = new OpusToPCM({
  channels: 1,
  fallback: true,
  libopusPath: 'libopus/opus.min.js' /* a pre-built libopus file is available in dist directory */
});
decoder.on('decode', function(pcmData) {
     //do whatever you want to do with PCM data
});

// single opus packet and it is a typedArray
decoder.decode(opus_packet); 

** ES6 example:** npn install opus-to-pcm --save;

import OpusToPCM from 'opus-to-pcm';

const decoder = new OpusToPCM({
    channels: 1
});

decoder.on('decode', (pcmData)=> {
    console.log(pcmData); /* PCM data */
}); 

Available Methods

| Name | Parameter | Remark | | ------------- |:-------------:| -----:| | getSampleRate | - | It return output sample rate of the PCM data | | decode | data TypedArray | Decode provided opus packet to PCM | | destroy | - | Destroy the decoder instance and release the resources |

Compatibility

it is supported on:

  • Chrome for Android 34+
  • Chrome for Desktop 34+
  • Firefox for Android 41+
  • Firefox for Desktop 42+
  • IE11+ for Windows 8.1+ (fallback)
  • Edge for Windows 10+
  • Opera for Desktop
  • Safari for Mac 8+ (fallback)

How to run example?

An example with simple node server script is available that include some raw opus packets that will be served by websocket and at the client end, it will be played through simple PCM player after decoding from opus. For running the example, first run the node server by following command:

node server.js

then, visit example/index.html page through any webserver.

How to build?

A distribution version is available inside dist directory. However, if you need to build, you can do as follows:

  1. git clone https://github.com/samirkumardas/opus-to-pcm.git
  2. cd pcm-player
  3. npm install
  4. npm run build