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

@tonaljs/midi

v4.10.0

Published

Functions to work with midi numbers

Downloads

9,051

Readme

@tonaljs/midi tonal npm version

A collection of functions to work with midi numbers.

Usage

ES6:

import { Midi } from "tonal";

nodejs:

const { Midi } = require("tonal");

API

toMidi(note: string | number) => number | null

Given a note name or number, return the midi number. Midi numbers are always in range 0..127

Examples:

Midi.toMidi("C4"); // => 60
Midi.toMidi("#"); // => null
Midi.toMidi(60); // => 60
Midi.toMidi("60"); // => 60
Midi.toMidi(-1); // => null

midiToFreq(midi: number, tuning = 440) => number

Given a midi number, return the frequency:

Examples:

Midi.midiToFreq(60); // => 261.6255653005986
Midi.midiToFreq(69); // => 440
Midi.midiToFreq(69, 443); // => 443

midiToNoteName(midi: number) => string

Given a midi number, returns a note name. The altered notes will have flats unless explicitly set with the optional useSharps parameter.

Examples:

Midi.midiToNoteName(61); // => "Db4"
Midi.midiToNoteName(61, { pitchClass: true }); // => "Db"
Midi.midiToNoteName(61, { sharps: true }); // => "C#4"
Midi.midiToNoteName(61, { pitchClass: true, sharps: true }); // => "C#"
// it rounds to nearest note
midiToNoteName(61.7); // => "D4"

freqToMidi(freq: number) => number

Given a frequency in hertz, returns the midi number. The midi number can have decimals (with two digits precision)

Example:

Midi.freqToMidi(220); //=> 57
Midi.freqToMidi(261.62); //=> 60
Midi.freqToMidi(261); //=> 59.96

pcset(set: number[] | string) => number[]

Return the pitch class set from a number of midi note numbers or pcset chroma.

A pitch class set in this Midi package refers to a unique sorted collection of notes between 0 and 11 (that represents the pitch class of the note.

Example:

Midi.pcset([62, 63, 60, 65, 70, 72]); // => [0, 2, 3, 5, 10]
Midi.pcset("100100100101"); // => [0, 3, 6, 9, 11]

You can read more about pitch classes on Note module.

The string is a pitch class chroma, a string with a binary representation of a set. Read more about pitch class chroma in Pcset module documentation.

pcsetNearest(set: number[] | string) => (midi: number) => number | undefined

Returns a function that finds the nearest midi note of a pitch class set. Can be used to constrain a note to a scale, for example:

const nearest = Midi.pcsetNearest(Scale.get("D dorian").chroma);
[60, 61, 62, 63, 64, 65, 66].map(nearest); // => [60, 62, 62, 63, 65, 65, 67]

pcsetSteps(set: number[] | string, tonic: number) => (index: number) => number

Returns a function to map a pitch class set over any note. Given a tonic a pitch class set, step 0 means the first note, step 1 the second, and so on:

const steps = Midi.pcsetSteps(Scale.get("D dorian").chroma, 60);
[-2, -1, 0, 1, 2, 3].map(steps); // => [ 57, 58, 60, 62, 63, 65 ]

A similar function called pcsetDegrees exists that just uses 1 as first note instead of 0 (more common in music theory books). See Scale.degrees and Chord.degrees for more information.