note-parser

Parse music notes in scientific notation


Keywords
note, parse, parser, midi, scientific, notation, frequency
License
MIT
Install
npm install note-parser@2.0.1

Documentation

note-parser

Code Climate js-standard-style

Parse notes with javascript. Fast and simple: give it a string, obtain a hash with note's pitchClass, accidentals, octave, midi number and frequency.

Usage

Add the module to your project: npm i --save note-parser and require it:

var parse = require('note-parser');

 parse(noteString [, defaultOctave, defaultValue ])

Use the function to parse notes:

parse('Db4');   // => { name: 'db4',   pc: 'd', acc: 'b',  oct: 4,  midi: 61, freq: 277.18 }
parse('f##-2'); // => { name: 'f##-2', pc: 'f', acc: '##', oct: -2, midi: -5, freq: 6.12 }
parse('Eb++');  // => { name: 'eb6',   pc: 'e', acc: 'b',  oct: 6,  midi: 87, freq: 1244.50 }
parse('b#-');   // => { name: 'b#3',   pc: 'b', acc: '#',  oct: 3,  midi: 60, freq: 261.62 }
parse('g');     // => { name: 'g4',    pc: 'g', acc: '',   oct: 4,  midi: 67, freq: 391.99 }

The parse method receives a string and return an object with the following attributes:

  • name: the scientific name of the note. Always in lowercase
  • pc: pitchClass, the letter of the note. From "a" to "g". Always in lowercase.
  • acc: a string with the accidentals. An empty string if no accidentals present.
  • oct: the octave as integer.
  • midi: the midi number
  • freq: the note frequency

You can change the default octave with the second parameter. Otherwise is 4:

parse('C');      // => { name: 'c4', pc: 'c', acc: '', oct: 4, midi: 60, freq: 261.63 }
parse('C', 2);   // => { name: 'c2', pc: 'c', acc: '', oct: 2, midi: 36, freq: 65.41 }
parse('C+', 2);  // => { name: 'c3', pc: 'c', acc: '', oct: 3, midi: 48, freq: 130.81 }
parse('C--', 2); // => { name: 'c0', pc: 'c', acc: '', oct: 0, midi: 12, freq: 16.35 }

If defaultValue is not defined, the parse throws an exception if the note format is invalid. Otherwise returns the defaultValue:

parse('blah'); // => throws Error
parse('blah', 2, parse('C4')); // => { name: 'c4', pc: 'c', acc: '', oct: 4 ... }
parse('blah', 2, null); // => null

Note: calling parse on a parsed object return itself:

var n = parse('C');
n === parse(n) // => true

License

MIT License