tonal-freq
Version:
Conversion between frequencies and note names
141 lines (130 loc) • 4.09 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tonalMidi = require('tonal-midi');
/**
* [](https://www.npmjs.com/package/tonal-freq)
* [](https://www.npmjs.com/browse/keyword/tonal)
*
* `tonal-freq` is a collection of functions to perform calculations related to frequencies.
*
* This is part of [tonal](https://www.npmjs.com/package/tonal) music theory library.
*
* ## Usage
*
* ```js
* var freq = require('tonal-freq')
* freq.toFreq('A4') // => 440
* freq.note(440) // => 'A4'
* freq.noteAndDetune(320) // => ['C4', 200]
* ```
*
* ## Install
*
* [](https://npmjs.org/package/tonal-freq/)
*
* ## API Documentation
*
* @module freq
*/
// decorate a function to round the numeric result to a max
function round (m, fn) {
m = m || m === 0 ? Math.pow(10, m) : false;
return function (v) {
v = fn(v);
return v === null ? null : m ? Math.round(v * m) / m : v
}
}
/**
* Return the equal tempered frequency of a note.
*
* This function can be partially applied if note parameter is not present.
* @function
* @param {Float} ref - the tuning reference
* @param {Integer} maxDecimals - (Optional) the maximum number of decimals (all by default)
* @param {String|Pitch} note - the note to get the frequency from
* @return {Number} the frequency
* @example
* eqTempFreq(444, 4, 'C3')
* const toFreq = eqTempFreq(444, 2)
* toFreq('A3') // => 222
*/
function eqTempFreq (ref, max, note$$1) {
if (arguments.length > 2) return eqTempFreq(ref, max)(note$$1)
return round(max, function (p) {
var m = tonalMidi.toMidi(p);
return m ? Math.pow(2, (m - 69) / 12) * ref : null
})
}
/**
* Get the frequency of note with 2 decimals precission using A4 440Hz tuning
*
* This is an alias for: `eqTempFreq(440, 2, <note>)`
*
* @function
* @param {Number|String} note - the note name or midi number
* @return {Float} the frequency in herzs
* @example
* freq.toFreq('A4') // => 440
* freq.toFreq('C4') // => 261.63
*/
var toFreq = eqTempFreq(440, 2);
/**
* Get the midi note from a frequency in equal temperament scale. You can
* specify the number of decimals of the midi number.
*
* @param {Float} tuning - (Optional) the reference A4 tuning (440Hz by default)
* @param {Number} freq - the frequency
* @return {Number} the midi number
*/
function eqTempFreqToMidi (ref, max, freq) {
if (arguments.length > 2) return eqTempFreqToMidi(ref, max)(freq)
return round(max, function (freq) {
return 12 * (Math.log(freq) - Math.log(ref)) / Math.log(2) + 69
})
}
/**
* Get midi number from frequency with two decimals of precission.
*
* This is an alisas for: `eqTempFreqToMidi(440, 2, <freq>)`
*
* @function
* @param {Float} freq
* @return {Number} midi number
* @example
* freq.toMidi(361) // => 59.96
*/
var toMidi$1 = eqTempFreqToMidi(440, 2);
/**
* Get note name from frequency using an equal temperament scale with 440Hz
* as reference
*
* @param {Float} freq
* @param {Boolean} useSharps - (Optional) set to true to use sharps instead of flats
* @return {String} note name
* @example
* freq.note(440) // => 'A4'
*/
function note$1 (freq, useSharps) {
return tonalMidi.note(toMidi$1(freq), useSharps)
}
/**
* Get difference in cents between two frequencies. The frequencies can be
* expressed with hertzs or midi numbers or note names
* @param {Float|Integer|String} base
* @param {Float|Integer|String} freq
* @return {Integer} The difference in cents
* @example
* import { cents } from 'tonal-freq'
* cents('C4', 261) // => -4
*/
function cents (base, freq) {
var b = toFreq(base) || base;
var f = toFreq(freq) || freq;
return Math.round(1200 * (Math.log(f / b) / Math.log(2)))
}
exports.eqTempFreq = eqTempFreq;
exports.toFreq = toFreq;
exports.eqTempFreqToMidi = eqTempFreqToMidi;
exports.toMidi = toMidi$1;
exports.note = note$1;
exports.cents = cents;