Skip to content

A package that returns information about audio input (e.g. note and frequency)

License

Notifications You must be signed in to change notification settings

kyunwang/pitch-analyser

Repository files navigation

Pitch Analyser

A super simple package for reading audio input from a microphone. E.g. pitch frequency, music notes and the cents.

This package makes use of the Web Audio API

Here is an example project called note detector using this package here. (Repo / Demo)

CURRENT ISSUE: Known issue - Chrome does not resume the analyser after pausing.

WARNING: Breaking changes possible - This will be done as MINOR updates.

Not in active development, but at times I can make a breaking change to optimise usage. So keep that in mind, if you are one of the lovely people trying this package out.

Will remove the warning if this will not be the case anymore. Or help me out with this I guess? 🤷🏻‍♂️

Demo image

Table of Content

Installing

Npm: npm i pitch-analyser

Usage

This package returns the frequency, music note and cents from audio input and nothing more.

  1. Import the package
import pitchAnalyser from 'pitch-analyser';
  1. Initialize the analyser In the callback we can do whatever we want with the payload
const analyser = new pitchAnalyser({
	callback: function(payload) {
		console.log(payload); // E.g. { frequency: 220, note: "A3" }
	}
});
  1. Stopping the analyser In case you want to stop the analyser. In componentWillUnmount in React for example.
componentWillUnmount() {
	analyser.stopAnalyser();
}

Thats all you need to do to get started. Check out this example project to see it in action. (Repo / Demo)

Payload format

The payload is an object and can look like this with default options.

{
	note: "D7",
	frequency: 2349.32,
	cents: -21 // if returnCents is set to true
}

Options

These are the few options available.

  • callback
  • returnNote
  • returnCents
  • decimals

callback

Type Default isRequired
function null true

Through the callback you will receive the output and will need to handle what to do with it here.

new pitchAnalyser({
	callback: function(value) {
		// E.g. { frequency: 220, note: "A3" }
		console.log(value);
	}
});

returnNote

Type Default isRequired
boolean true false

If this option is set to true, the frequency and note will be passed to the callback function.

new pitchAnalyser({
	returnNote: true,
	...other options...
});

returnCents

Requires returnNote

Type Default isRequired
boolean false false

Will return the cents if set to true. Requires returnNote to be true.

new pitchAnalyser({
	returnNote: true, // Is required
	returnCents: true,
	...other options...
});

Methods/Properties

The methods and properties available for use. You will need to have access to the initializes analyser

Methods

  • resume
  • closeContext

Properties

  • audioContext

Methods

resume

An methods form the AudioContext. It is important to call this to start/resume the analyser, because on chrome it will not start automatically.

analyser.audioContext.resume();

closeContext

When you want fully stop the analyser (will remove the instance);

Arguments Type Output
callback function If a callback is passed, it will be called when the audioContext has closed
analyser.closeContext(callback);

Properties

audioContext

The native audioContext can be accessed here for if you are ambitious and need customization. Check the MDN docs or somewhere else for all the information about the usage of AudioContext

analyser.audioContext

License

This repo is under the MIT License