• Stars
    star
    436
  • Rank 99,877 (Top 2 %)
  • Language
    TypeScript
  • Created about 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A compilation of pitch detection algorithms for Javascript.

Build Status

pitchfinder

A compilation of pitch detection algorithms for Javascript. Supports both the browser and node.

Provided pitch-finding algorithms

  • YIN - The best balance of accuracy and speed, in my experience. Occasionally provides values that are wildly incorrect.
  • AMDF - Slow and only accurate to around +/- 2%, but finds a frequency more consistenly than others.
  • Dynamic Wavelet - Very fast, but struggles to identify lower frequencies.
  • YIN w/ FFT (coming soon)
  • Goertzel (coming soon)
  • Mcleod (coming soon)

Installation

npm install --save pitchfinder

Usage

Finding the pitch of a wav file in node

All pitchfinding algorithms provided operate on Float32Arrays. To find the pitch of a wav file, we can use the wav-decoder library to extract the data into such an array.

const fs = require("fs");
const WavDecoder = require("wav-decoder");
const Pitchfinder = require("pitchfinder");

// see below for optional configuration parameters.
const detectPitch = Pitchfinder.YIN();

const buffer = fs.readFileSync(PATH_TO_FILE);
const decoded = WavDecoder.decode.sync(buffer); // get audio data from file using `wav-decoder`
const float32Array = decoded.channelData[0]; // get a single channel of sound
const pitch = detectPitch(float32Array); // null if pitch cannot be identified

Finding the pitch of a WebAudio AudioBuffer in the browser

This assumes you are using an npm-compatible build system, like Webpack or Browserify, and that your target browser supports WebAudio. Ample documentation on WebAudio is available online, especially on Mozilla's MDN.

import * as Pitchfinder from "pitchfinder";

const myAudioBuffer = getAudioBuffer(); // assume this returns a WebAudio AudioBuffer object
const float32Array = myAudioBuffer.getChannelData(0); // get a single channel of sound

const detectPitch = Pitchfinder.AMDF();
const pitch = detectPitch(float32Array); // null if pitch cannot be identified

Finding a series of pitches

Set a tempo and a quantization interval, and an array of pitches at each interval will be returned.

const Pitchfinder = require("pitchfinder");
const detectPitch = Pitchfinder.YIN();

const frequencies = Pitchfinder.frequencies(detectPitch, float32Array, {
  tempo: 130, // in BPM, defaults to 120
  quantization: 4, // samples per beat, defaults to 4 (i.e. 16th notes)
});

// or use multiple detectors for better accuracy at the cost of speed.
const detectors = [detectPitch, Pitchfinder.AMDF()];
const moreAccurateFrequencies = Pitchfinder.frequencies(
  detectors,
  float32Array,
  {
    tempo: 130, // in BPM, defaults to 120
    quantization: 4, // samples per beat, defaults to 4 (i.e. 16th notes)
  }
);

Configuration

All detectors

  • sampleRate - defaults to 44100

YIN

  • threshold - used by the algorithm
  • probabilityThreshold - don't return a pitch if probability estimate is below this number.

AMDF

  • minFrequency - Lowest frequency detectable
  • maxFrequency - Highest frequency detectable
  • sensitivity
  • ratio

Dynamic Wavelet

no special config

Note

If you'd like a version that uses compiled C++ code and runs much faster, check out this repo. However, it will not work in the browser.

Todo

  • Integrate with teoria or another music theory tool to add more intelligent parsing.
  • Note-onset algorithms.
  • Enable requiring of single detectors.

Thanks

Several of these algorithms were ported from Jonas Six's excellent TarsosDSP library (written in Java). If you're looking for a far deeper set of tools than this, check out his work on his website or on Github.

Thanks to Aubio for his YIN code

More Repositories

1

rolling-rate-limiter

Rate limiter for node.js that supports a rolling window, either in-memory or backed by redis
TypeScript
338
star
2

solitaireVictory

A Jquery plugin that emulates the victory animation from Windows Solitaire, using your choice of DOM element(s).
JavaScript
42
star
3

inlineAB

Easy AB-testing using HTML markup and Google Analytics.
JavaScript
36
star
4

HackReactorProject

Record songs with only your voice - no instruments required!
JavaScript
32
star
5

instrumental.js

Easy-to-use sample playback for use with music apps
JavaScript
24
star
6

EarTraining

A game to help you learn ear training and become a better musician!
JavaScript
19
star
7

extended-proptypes

Useful proptypes for React
JavaScript
15
star
8

multisort

Sort an array using any number of separate, ranked criteria
JavaScript
14
star
9

functional-matrix

A matrix library for javascript supporting standard functional methods.
JavaScript
10
star
10

mandelbrotcat

The mandelbrot set in cat GIFs.
JavaScript
7
star
11

Doorbell

A doorbell for Hack Reactor.
JavaScript
6
star
12

ih

simple immutability helpers for javascript
JavaScript
5
star
13

eslint-plugin-mutation

ESLint plugin to guard against unexpected mutations of objects in javascript
JavaScript
4
star
14

restroom

One-line RESTful CRUD server in Express
JavaScript
3
star
15

redis-intro-presentation

Materials for a short talk I'm giving at Hack Reactor on the fundamentals of Redis
CoffeeScript
3
star
16

HackReactorDinnerParty

Assigns Hack Reactor students to dinner groups.
JavaScript
2
star
17

scrabblewords

Algorithm to find all words in a given hand from a given dictionary.
JavaScript
2
star
18

simpledb

An attempt to implement a basic SQL database in Rust for my learning
Rust
2
star
19

itslikebutfor

It's like X, but for Y!
JavaScript
2
star
20

MusicSpaceShooter

Arcade-style game in 3.js with a musical theme
JavaScript
2
star
21

projecteuler

Solutions to Project Euler problems I've solved, in a variety of languages.
JavaScript
2
star
22

dance-party

a dance floor with a disco theme.
JavaScript
1
star
23

TheLibraryOfBabel

Based on "The Library Of Babel", by Jorge Luis Borges
1
star
24

paras-goodbye

TypeScript
1
star
25

music-cube-client

HTML frontend for music cube project
TypeScript
1
star
26

ojala

expectaciones
CoffeeScript
1
star
27

jsheckler

Randomly changes your code to make sure your test coverage is good enough.
JavaScript
1
star