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

react-use-wav-audio-recorder

v0.0.6

Published

This is a simple React hook that uses [`extendable-media-recorder`](https://www.npmjs.com/package/extendable-media-recorder) and [extendable-media-recorder-wav-encoder](https://www.npmjs.com/package/extendable-media-recorder-wav-encoder) under the hood to

Downloads

10

Readme

React useWavAudioRecorder

This is a simple React hook that uses extendable-media-recorder and extendable-media-recorder-wav-encoder under the hood to record raw WAV audio in a cross-browser compatible manner. It produces a Blob of the WAV audio.

Installation

Install as react-use-wav-audio-recorder from NPM:

pnpm i react-use-wav-audio-recorder

Usage

function MyComponent() {
  const [audioBlob, setAudioBlob] = useState<Blob>();
  
  const {
    // recording status flag
    recording,
    // a method to start the permission grant flow and start recording immediately afterwards
    startRecording,
    // a method to stop the recording
    stopRecording
    } = useWavAudioRecorder({
    onComplete: async (blob) => {
      // store the blob
      setAudioBlob(setAudioUrl(window.URL.createObjectURL(blob)));
      // ... or you can convert it to an object URL so it can be assigned to an <audio> element
      // setAudioUrl(window.URL.createObjectURL(blob));
      // ... or you can turn it to a File so you can upload it later etc.
      // const file = new File([blob], 'recording.wav');
    },
    onNoPermissionError: () => {
      // Called when recording permissions coudln't be obtained
    },
    onNotSupportedError: () => {
      // Called when browser doesn't support recording
    },
    onOtherError: (e) => {
      // Called on any other unexpected errors
    },
  });

  // ...wire up the props to buttons etc.
}

MP3 compression

There's also a utility hook called useWavToMp3Worker that uses @breezystack/lamejs to encode the WAV into an MP3 file using a Web Worker so that the main thread isn't blocked during the encoding process.

The bitrate of the MP3 is currently fixed to 256 Kbps but this will be made into a parametrized value in the future (feel free to open a PR!).

The worker is also set to terminate after 60 seconds (feel free to open a PR to parametrize this as well).

Web Worker issues with Vite

Vite has some issues with resolving the library-mode-bundled Web Workers. In order for the Web Worker to work properly during development you should add this to your vite.config.ts optimizeDeps.exclude field:

export default defineConfig({
  // ...
  optimizeDeps: {
    exclude: ['react-use-wav-audio-recorder']
  }
  // ...
})

In production builds everything seems to be working just fine without the optimizeDeps.exclude entry.

Usage

const {
  // a method to start the encoding process, accepts a Blob
  convert,
  // a flag telling you if the process is in progress
  inProgress
} = useWavToMp3Worker({
  onComplete: async (blob) => {
    // here you grab the processed blob just like with the WAV recording and do whatever you please with it
  },
  onError: (e) => {
    // this is called if any errors occur
  },
});

Credits

Here's a great article that was used for the initial implementation of the MP3 encoder and inspired the creation of these hooks.