@tonaljs/pcset
Version:
Functions to work with midi numbers
178 lines (175 loc) • 6.43 kB
text/typescript
import { IntervalName } from '@tonaljs/pitch-interval';
import { NoteName } from '@tonaljs/pitch-note';
/**
* The properties of a pitch class set
* @param {number} num - a number between 1 and 4095 (both included) that
* uniquely identifies the set. It's the decimal number of the chroma.
* @param {string} chroma - a string representation of the set: a 12-char string
* with either "1" or "0" as characters, representing a pitch class or not
* for the given position in the octave. For example, a "1" at index 0 means 'C',
* a "1" at index 2 means 'D', and so on...
* @param {string} normalized - the chroma but shifted to the first 1
* @param {number} length - the number of notes of the pitch class set
* @param {IntervalName[]} intervals - the intervals of the pitch class set
* *starting from C*
*/
interface Pcset {
readonly name: string;
readonly empty: boolean;
readonly setNum: number;
readonly chroma: PcsetChroma;
readonly normalized: PcsetChroma;
readonly intervals: IntervalName[];
}
declare const EmptyPcset: Pcset;
type PcsetChroma = string;
type PcsetNum = number;
declare function isChroma(set: any): set is PcsetChroma;
/**
* A definition of a pitch class set. It could be:
* - The pitch class set chroma (a 12-length string with only 1s or 0s)
* - The pitch class set number (an integer between 1 and 4095)
* - An array of note names
* - An array of interval names
*/
type Set = Partial<Pcset> | PcsetChroma | PcsetNum | NoteName[] | IntervalName[];
/**
* Get the pitch class set of a collection of notes or set number or chroma
*/
declare function get(src: Set): Pcset;
/**
* @use Pcset.get
* @deprecated
*/
declare const pcset: typeof get;
/**
* Get pitch class set chroma
* @function
* @example
* Pcset.chroma(["c", "d", "e"]); //=> "101010000000"
*/
declare const chroma: (set: Set) => string;
/**
* Get intervals (from C) of a set
* @function
* @example
* Pcset.intervals(["c", "d", "e"]); //=>
*/
declare const intervals: (set: Set) => string[];
/**
* Get pitch class set number
* @function
* @example
* Pcset.num(["c", "d", "e"]); //=> 2192
*/
declare const num: (set: Set) => number;
declare function notes(set: Set): NoteName[];
/**
* Get a list of all possible pitch class sets (all possible chromas) *having
* C as root*. There are 2048 different chromas. If you want them with another
* note you have to transpose it
*
* @see http://allthescales.org/
* @return {Array<PcsetChroma>} an array of possible chromas from '10000000000' to '11111111111'
*/
declare function chromas(): PcsetChroma[];
/**
* Given a a list of notes or a pcset chroma, produce the rotations
* of the chroma discarding the ones that starts with "0"
*
* This is used, for example, to get all the modes of a scale.
*
* @param {Array|string} set - the list of notes or pitchChr of the set
* @param {boolean} normalize - (Optional, true by default) remove all
* the rotations that starts with "0"
* @return {Array<string>} an array with all the modes of the chroma
*
* @example
* Pcset.modes(["C", "D", "E"]).map(Pcset.intervals)
*/
declare function modes(set: Set, normalize?: boolean): PcsetChroma[];
/**
* Test if two pitch class sets are equal
*
* @param {Array|string} set1 - one of the pitch class sets
* @param {Array|string} set2 - the other pitch class set
* @return {boolean} true if they are equal
* @example
* Pcset.isEqual(["c2", "d3"], ["c5", "d2"]) // => true
*/
declare function isEqual(s1: Set, s2: Set): boolean;
/**
* Create a function that test if a collection of notes is a
* subset of a given set
*
* The function is curried.
*
* @param {PcsetChroma|NoteName[]} set - the superset to test against (chroma or
* list of notes)
* @return{function(PcsetChroma|NoteNames[]): boolean} a function accepting a set
* to test against (chroma or list of notes)
* @example
* const inCMajor = Pcset.isSubsetOf(["C", "E", "G"])
* inCMajor(["e6", "c4"]) // => true
* inCMajor(["e6", "c4", "d3"]) // => false
*/
declare function isSubsetOf(set: Set): (notes: Set | Pcset) => boolean | 0;
/**
* Create a function that test if a collection of notes is a
* superset of a given set (it contains all notes and at least one more)
*
* @param {Set} set - an array of notes or a chroma set string to test against
* @return {(subset: Set): boolean} a function that given a set
* returns true if is a subset of the first one
* @example
* const extendsCMajor = Pcset.isSupersetOf(["C", "E", "G"])
* extendsCMajor(["e6", "a", "c4", "g2"]) // => true
* extendsCMajor(["c6", "e4", "g3"]) // => false
*/
declare function isSupersetOf(set: Set): (notes: Set) => boolean | 0;
/**
* Test if a given pitch class set includes a note
*
* @param {Array<string>} set - the base set to test against
* @param {string} note - the note to test
* @return {boolean} true if the note is included in the pcset
*
* Can be partially applied
*
* @example
* const isNoteInCMajor = isNoteIncludedIn(['C', 'E', 'G'])
* isNoteInCMajor('C4') // => true
* isNoteInCMajor('C#4') // => false
*/
declare function isNoteIncludedIn(set: Set): (noteName: NoteName) => boolean;
/** @deprecated use: isNoteIncludedIn */
declare const includes: typeof isNoteIncludedIn;
/**
* Filter a list with a pitch class set
*
* @param {Array|string} set - the pitch class set notes
* @param {Array|string} notes - the note list to be filtered
* @return {Array} the filtered notes
*
* @example
* Pcset.filter(["C", "D", "E"], ["c2", "c#2", "d2", "c3", "c#3", "d3"]) // => [ "c2", "d2", "c3", "d3" ])
* Pcset.filter(["C2"], ["c2", "c#2", "d2", "c3", "c#3", "d3"]) // => [ "c2", "c3" ])
*/
declare function filter(set: Set): (notes: NoteName[]) => string[];
/** @deprecated */
declare const _default: {
get: typeof get;
chroma: (set: Set) => string;
num: (set: Set) => number;
intervals: (set: Set) => string[];
chromas: typeof chromas;
isSupersetOf: typeof isSupersetOf;
isSubsetOf: typeof isSubsetOf;
isNoteIncludedIn: typeof isNoteIncludedIn;
isEqual: typeof isEqual;
filter: typeof filter;
modes: typeof modes;
notes: typeof notes;
pcset: typeof get;
};
export { EmptyPcset, type Pcset, type PcsetChroma, type PcsetNum, type Set, chroma, chromas, _default as default, filter, get, includes, intervals, isChroma, isEqual, isNoteIncludedIn, isSubsetOf, isSupersetOf, modes, notes, num, pcset };