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

microphone-stream

v6.0.1

Published

A stream of raw audio data from the microphone for use in browsers with getUserMedia

Downloads

31,862

Readme

Node-style stream for getUserMedia

npm-version Build Status

If you just want to get some audio data from your microphone, this is what you're looking for!

Converts a MediaStream (from getUserMedia) into a standard Node.js-style stream for easy pipe()ing.

Note: This only works in a limited set of browsers (typically with webpack or browserify), and then only for https or localhost in Chrome. It does not work in Node.js.

Example

const getUserMedia = require('get-user-media-promise');
const MicrophoneStream = require('microphone-stream').default;

document.getElementById('my-start-button').onclick = function() {

  // Note: in most browsers, this constructor must be called in response to a click/tap, 
  // or else the AudioContext will remain suspended and will not provide any audio data.
  const micStream = new MicrophoneStream();

  getUserMedia({ video: false, audio: true })
    .then(function(stream) {
      micStream.setStream(stream);
    }).catch(function(error) {
      console.log(error);
    });

  // get Buffers (Essentially a Uint8Array DataView of the same Float32 values)
  micStream.on('data', function(chunk) {
    // Optionally convert the Buffer back into a Float32Array
    // (This actually just creates a new DataView - the underlying audio data is not copied or modified.)
    const raw = MicrophoneStream.toRaw(chunk)
    //...

    // note: if you set options.objectMode=true, the `data` event will output AudioBuffers instead of Buffers
   });

  // or pipe it to another stream
  micStream.pipe(/*...*/);

  // Access the internal audioInput for connecting to another nodes
  micStream.audioInput.connect(/*...*/));

  // It also emits a format event with various details (frequency, channels, etc)
  micStream.on('format', function(format) {
    console.log(format);
  });

  // Stop when ready
  document.getElementById('my-stop-button').onclick = function() {
    micStream.stop();
  };
 }

API

new MicrophoneStream(opts) -> Readable Stream

Where opts is an option object, with defaults:

{
  stream: null,
  objectMode: false,
  bufferSize: null,
  context: null
}
  • stream: MediaStream instance. For iOS compatibility, it is recommended that you create the MicrophoneStream instance in response to the tap - before you have a MediaStream, and then later call setStream() with the MediaStream.

  • bufferSize: Possible values: null, 256, 512, 1024, 2048, 4096, 8192, 16384. From Mozilla's Docs:

    It is recommended for authors to not specify this buffer size and allow the implementation to pick a good buffer size to balance between latency and audio quality.

  • objectMode: if true, stream enters objectMode and emits AudioBuffers instead of Buffers. This has implications for pipe()'ing to other streams.

  • context is the AudioContext instance. If omitted, one will be created automatically.

.setStream(mediaStream)

Set the mediaStream, necessary for iOS 11 support where the underlying AudioContext must be resumed in response to a user tap, but the mediaStream won't be available until later. Note: Some versions of Firefox leave the recording icon in place after recording has stopped.

.stop()

Stops the recording. Note: Some versions of Firefox leave the recording icon in place after recording has stopped.

.pauseRecording()

Temporarily stop emitting new data. Audio data recieved from the microphone after this will be dropped.

Note: the underlying Stream interface has a .pause() API that causes new data to be buffered rather than dropped.

.playRecording()

Resume emitting new audio data after pauseRecording() was called.

Event: data

Emits either a Buffer with raw 32-bit Floating point audio data, or if objectMode is set, an AudioBuffer containing the data + some metadata.

Event: format

One-time event with details of the audio format. Example:

{
  channels: 1,
  bitDepth: 32,
  sampleRate: 48000,
  signed: true,
  float: true
}

MicrophoneStream.toRaw(Buffer) -> Float32Array

Converts a Buffer (from a data event or from calling .read()) back to the original Float32Array DataView format. (The underlying audio data is not copied or modified.)