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 🙏

© 2025 – Pkg Stats / Ryan Hefner

amrplayer

v2.0.3

Published

AMR Codec in Javascript (Modern Build Support)

Readme

AMRPlayer

AMRPlayer is a modernized version of amr.js, supporting the loading and playback of AMR audio format.

amr.js has ceased updates, utilizing numerous global variables, lacking support for strict mode, not supporting modularity, and missing documentation.

This project has modernized it, supporting ES Module, CommonJS, and IIFE formats, compatible with modern build tools (such as Webpack, Rollup, Vite, etc.). It also adds playback control support and convenient data writing methods.

I used AMR audio interpretation in my project and found that there are not many libraries to choose from. Although amr.js lacks maintenance, it is still usable. Thanks to the original authors for their contributions, and thus I also share my achievements with everyone.

中文

Features

  • Easy loading of AMR audio files.
  • Provides audio playback control.
  • Supports converting AMR audio to WAV format.
  • Compatible with modern JavaScript build tools.
  • No longer uses multiple global variables; modular environments do not require global variables, and only one global variable is exposed in the browser environment.

Installation

You can install AMRPlayer via npm:

npm install amrplayer

Import Methods

Depending on your project environment, you can choose any of the following methods to import AMRPlayer.

1. ES Module (Recommended)

If your project uses ES Module (e.g., Vite or modern browser environments), you can import it as follows:

import AMRPlayer from 'amrplayer';

// Using AMRPlayer
const amrplayer = new AMRPlayer();

2. CommonJS

If your project uses CommonJS, you can import it as follows:

const AMRPlayer = require('amrplayer');

// Using AMRPlayer
const amrplayer = new AMRPlayer();

3. Browser

If you are directly using the <script> tag in the browser, you can import it as follows:

<script src="https://unpkg.com/amrplayer/dist/amrplayer.js"></script>
<script>
    // AMRPlayer will be exposed as a global variable
    const amrplayer = new AMRPlayer();
</script>

Usage Examples

Initializing AMRPlayer

import AMRPlayer from 'amrplayer';

const amrplayer = new AMRPlayer();

Loading AMR Audio Files

Loading from ArrayBuffer

fetch('audio.amr')
   .then(response => response.arrayBuffer())
   .then(arrayBuffer => amrplayer.setBuffer(arrayBuffer));

Loading from Blob

<input type="file" name="file" id="file" accept=".amr">
const file = document.getElementById('file').files[0];
// Note that setBlob is an asynchronous interface
await amrplayer.setBlob(file);

Converting to WAV Format

Convenient for playback using <audio>.

<audio id="audio" controls></audio>
const url = amrplayer.getWAV();
document.getElementById('audio').src = url;

Audio Playback

const amraudio = amrplayer.getAudio(() => console.log('Audio playback ended'));

// Synchronous interface
amraudio.start(); // Start playback
amraudio.stop(); // Stop playback

// Asynchronous interface
amraudio.suspend(); // Pause playback
amraudio.resume(); // Resume playback
amraudio.toggle(); // Toggle playback state
amraudio.close(); // Close the audio context when amraudio is no longer needed

Audio State

console.log('Current audio state:', amraudio.state);

API Documentation

new AMRPlayer()

AMRPlayer.setBuffer(arrayBuffer)

Decodes the passed ArrayBuffer into Float32Array and sets it as the audio data.

  • arrayBuffer: The ArrayBuffer to decode.
  • Returns the decoded Float32Array.

AMRPlayer.setBlob(blob)

Converts the passed Blob into ArrayBuffer and sets it as the audio data.

  • blob: The Blob to convert.
  • Returns a Promise that resolves to the decoded Float32Array.

AMRPlayer.getAudio(onEnd)

Creates an AMRAudio instance and sets the callback function for when audio playback ends.

  • onEnd: The callback function triggered when audio playback ends.
  • Returns an AMRAudio instance.

AMRPlayer.getWAV()

Converts the current audio data to WAV format.

  • Returns a base64 string: data:audio/wav;base64,...

AMRAudio Class

new AMRAudio(float32Array)

Creates an AMRAudio instance.

  • float32Array: The Float32Array used to create the audio buffer.

AMRAudio.start()

Starts audio playback.

AMRAudio.stop()

Stops audio playback.

AMRAudio.close()

Closes the audio context. Asynchronous interface.

AMRAudio.suspend()

Suspends the audio context. Asynchronous interface.

AMRAudio.resume()

Resumes the audio context. Asynchronous interface.

AMRAudio.toggle()

Toggles the audio playback state, cycling through: play -> pause -> resume. Asynchronous interface.

AMRAudio.state

Gets the current audio playback state: 'stopped', 'running', 'suspended'.

AMRAudio.onEnd()

Automatically called when playback ends. Triggered by automatic end or calling AMRAudio.stop().


License

This project is open-sourced under the MIT license. For more details, please refer to the LICENSE file.