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

decibel-meter

v2.0.0

Published

Decibel meter for detecting noise levels from microphone via Web Audio API

Downloads

1,412

Readme

DecibelMeter

Monitor dB Levels Using HTML5 Audio API

DecibelMeter allows you to monitor the noise level using your device's microphone(s) from within a web page or node-webkit application.

// connect to first audio input device found
new DecibelMeter.listenTo(0, (dB, percent, value) => console.log(dB))

Browser Support

I have tested this in Chrome and Firefox for Windows and Android. iOS support is not available yet.

Live Example

You can try the DecibelMeter now: DecibelMeter example

Installation

npm install --save decibel-meter

Usage Examples

Import DecibelMeter as ES6 module

import DecibelMeter from 'decibel-meter'

Or, using a script tag

<script src="decibel-meter.js"></script>

Create a decibel meter

const meter = new DecibelMeter('unique-id');

Get a list of audio sources on the device

// resolves with array of MediaDeviceInfo objects, filtered by type: audioinput
meter.sources.then(sources => console.log(sources))

Connect to an audio source


// first method

meter.sources.then(sources => {
	meter.connect(sources[0])
})

// second method

meter.connectTo('default') // find audioinput by deviceId, and connect if possible
meter.connectTo(0) // find the first audioinput, and connect if possible

// handling errors

meter.connectTo('not-real').catch(err => alert('Connection Error'))

Do something with the decibel data

const level = document.getElementById('db-level')

meter.on('sample', (dB, percent, value) => level.textContent = `${dB} dB`) // display current dB level

Start listening to audio source

meter.listen() // "sample" callback set above will now receive data

Stop listening to audio source

meter.stopListening()

Disconnect from the audio source entirely

meter.disconnect() // returns Promise

Connect to a source, add a 'sample' event listener, and start listening to the connected source, in one terse command

meter.listenTo(0, (dB, percent, value) => level.textContent = `${dB} dB`)

Multiple meters on one page

var meter1 = new DecibelMeter('meter-1')
var meter2 = new DecibelMeter('meter-2')

DecimelMeter.meters // all meters created on this page

meter1 === DecimelMeter.getMeterById('meter-1'); // true

Events

DecibelMeter has its own events dispatcher, and supports the following Events

  • change
  • connect
  • disconnect
  • sample

change - the meter's listening state has changed

meter.on('change', listening => {
	if (listening)
		level.classList.add('active')
	else
		level.classList.remove('active')
})

connect - the meter successfully connected to an audioinput

meter.on('connect', (source, previous) => {
	console.log(`
		Connected to ${source.label}
		Disconnected from ${previous.label}`
	)
})

disconnect - the meter was disconnected from a source

sample - the meter received decibel data from the source