Skip to content

danigb/music-pitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

music-pitch

Build Status Code Climate js-standard-style npm version

music-pitch is the bridge between midi, your app and your synthetizers. It's a small (2.5kb minified) and fast library to manipulate note names, midi notes and note frequencies.

var pitch = require('music-pitch')
// get a note from midi
var note = pitch.fromMidi( ... )
// write it to the console
console.log(note)
// give it to your synth
synth.play(pitch.toFreq(note))

Installation

For node

Install via npm: npm install --save music-pitch and require it.

#### Browsers

No distribution (yet). Use webpack, browserify or a similar tool.

Usage

Note names

The str function returns the scientific notation of a given note if valid. Can be used to check if its a valid note:

pitch.str('bbb') // => 'Bbb'
pitch.str('fx4') // => 'F##4'

if (pitch.str(str) !== null) { /* valid pitch str */ }

You can get also pitch classes (pitches without octaves), note letter, octave and accidentals:

pitch.pitchClass('c##5') // => 'C##'
pitch.letter('eb3') // => 'E'
pitch.octave('eb3') // => 3
pitch.accidentals('eb3') // => 'b'

Working with midi

You have two functions for converting from and to midi numbers:

pitch.toMidi('A4') // => '69'
pitch.fromMidi(69) // => 'A4'

#### Working with frequencies

The same way, you have two frequency related functions:

pitch.toFreq('A4') // => 440
pitch.fromFreq(440) // => 'A4'

#### Using different pitch notation

In the case scientific notation is not what you need, you can always use pitch array notation for every function that expects a string:

pitch.toFreq([5, 0, 3]) // => 220

Also you can covert from scientific notation to pitch array notation with the pitch function:

pitch('A3') // => [5, 0, 3]

More...

That's all for this library, but maybe you need:

## API

accidentals(pitch) → {String}

Get the accidentals from a pitch

Parameters:
Name Type Description
pitch String | Array

the pitch

Source:
Returns:

the pitch accidentals

Type
String
Example
pitch.accidentals('C##3') // => '##'
pitch.accidentals('Bb4') // => 'b'
pitch.accidentals('E') // => ''

cents(from, to, decimals) → {Integer}

Get the distance in cents between pitches or frequencies

Parameters:
Name Type Description
from String | Integer

first pitch or frequency

to String | Integer

other pitch or frequency

decimals Integer

the decimal precision (2 by default)

Source:
Returns:

the distance in cents

Type
Integer
Example
cents(440, 444) // => 15.66
cents('A4', 444) // => 15.66
cents('A4', 'A#4') // => 100

fromFreq(freq) → {String}

Get the pitch of a given frequency.

It will round the frequency to the nearest pitch frequency. Use cents function if you need to know the difference between the the frequency and the pitch.

Parameters:
Name Type Description
freq Float

the frequency

Source:
See:
Returns:

the pitch

Type
String
Example
pitch.fromFreq(440) // => 'A4'
pitch.fromFreq(443) // => 'A4'
pitch.cents(443, 'A4') // => ... to get the difference

fromMidi(midi) → {String}

Get the pitch of the given midi number

This method doesn't take into account diatonic spelling. Always the same pitch class is given to the same midi number.

Parameters:
Name Type Description
midi Integer

the midi number

Source:
Returns:

the pitch

Type
String
Example
pitch.fromMidi(69) // => 'A4'

letter(pitch) → {String}

Get letter of the pitch (in uppercase)

Parameters:
Name Type Description
pitch String | Array

the pitch as string or array

Source:
Returns:

the letter of the pitch in uppercase

Type
String
Example
pitch.letter('fx') // => 'F'

octave(pitch) → {Integer}

Get the octave of a pitch

Parameters:
Name Type Description
pitch String | Array

the pitch

Source:
Returns:

the octave number

Type
Integer
Example
pitch.octave('F#3') // => 3

pitchClass(pitch) → {String}

Get the pitch class (pitch name without octaves) from a pitch

Parameters:
Name Type Description
pitch String | Array

the pitch to get the pitchClass number from

Source:
Returns:

the pitch class

Type
String
Example
pitch.pitchClass('a4') // => 'A'
pitch.pitchClass('ab') // => 'Ab'
pitch.pitchClass('cx2') // => 'C##'

str(pitch) → {String}

Get pitch name in scientific notation

Parameters:
Name Type Description
pitch String | Array

the pitch string or array

Source:
Returns:

the name of the pitch

Type
String
Example
pitch.str('cb') // => 'Cb'
pitch.str('fx2') // => 'F##2'

toFreq(pitch, tuning) → {Float}

Get the pitch frequency in hertzs

Parameters:
Name Type Description
pitch String

the pitch

tuning Integer

optional tuning, 440 by default

Source:
Returns:
  • the pitch frequency
Type
Float
Example
pitch.toFreq('A4') // => 440
pitch.toFreq('A3', 444) // => 222

toMidi(pitch) → {Integer}

Get the midi number of a pitch

Parameters:
Name Type Description
pitch String | Array

the pitch string (or pitch array)

Source:
Returns:

the midi number

Type
Integer
Example
pitch.toMidi('A4') // => 69
pitch.toMidi('A3') // => 57

generated with docme

License

MIT License

About

Note names, midi, frequencies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published