New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@tonaljs/chord

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tonaljs/chord - npm Package Compare versions

Comparing version

to
6.0.0

32

dist/index.d.ts

@@ -6,5 +6,4 @@ import { detect } from '@tonaljs/chord-detect';

declare function deprecate<ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>>(original: string, alternative: string, fn: ResultFn): (this: unknown, ...args: unknown[]) => ReturnType<ResultFn>;
type ChordName = string;
type ChordNameTokens = [string, string];
type ChordNameOrTokens = string | [string] | [string, string] | [string, string, string];
type ChordNameTokens = [string, string, string];
interface Chord extends ChordType {

@@ -14,2 +13,3 @@ tonic: string | null;

root: string;
bass: string;
rootDegree: number;

@@ -20,3 +20,3 @@ symbol: string;

/**
* Tokenize a chord name. It returns an array with the tonic and chord type
* Tokenize a chord name. It returns an array with the tonic, chord type and bass
* If not tonic is found, all the name is considered the chord name.

@@ -27,5 +27,8 @@ *

*
* This function does NOT check if the bass is part of the chord or not but it
* only accepts a pitch class as bass
*
* @function
* @param {string} name - the chord name
* @return {Array} an array with [tonic, type]
* @return {Array} an array with [tonic, type, bass]
* @example

@@ -41,3 +44,3 @@ * tokenize("Cmaj7") // => [ "C", "maj7" ]

*/
declare function get(src: ChordName | ChordNameTokens): Chord;
declare function get(src: ChordNameOrTokens): Chord;
/**

@@ -50,4 +53,4 @@ * Get chord properties

*/
declare function getChord(typeName: string, optionalTonic?: string, optionalRoot?: string): Chord;
declare const chord: (this: unknown, ...args: unknown[]) => Chord;
declare function getChord(typeName: string, optionalTonic?: string, optionalBass?: string): Chord;
declare const chord: typeof get;
/**

@@ -89,2 +92,6 @@ * Transpose a chord name

/**
* Return the chord notes
*/
declare function notes(chordName: ChordNameOrTokens, tonic?: string): string[];
/**
* Returns a function to get a note name from the scale degree.

@@ -96,7 +103,7 @@ *

*/
declare function degrees(chordName: string | ChordNameTokens): (degree: number) => string;
declare function degrees(chordName: ChordNameOrTokens, tonic?: string): (degree: number) => string;
/**
* Sames as `degree` but with 0-based index
*/
declare function steps(chordName: string | ChordNameTokens): (normalized: number) => string;
declare function steps(chordName: ChordNameOrTokens, tonic?: string): (normalized: number) => string;
declare const _default: {

@@ -113,5 +120,6 @@ getChord: typeof getChord;

steps: typeof steps;
chord: (this: unknown, ...args: unknown[]) => Chord;
notes: typeof notes;
chord: typeof get;
};
export { type Chord, chord, chordScales, _default as default, degrees, deprecate, extended, get, getChord, reduced, steps, tokenize, transpose };
export { type Chord, chord, chordScales, _default as default, degrees, extended, get, getChord, notes, reduced, steps, tokenize, transpose };

@@ -27,3 +27,2 @@ "use strict";

degrees: () => degrees,
deprecate: () => deprecate,
detect: () => import_chord_detect2.detect,

@@ -33,2 +32,3 @@ extended: () => extended,

getChord: () => getChord,
notes: () => notes,
reduced: () => reduced,

@@ -42,2 +42,3 @@ steps: () => steps,

var import_chord_type = require("@tonaljs/chord-type");
var import_interval = require("@tonaljs/interval");
var import_pcset = require("@tonaljs/pcset");

@@ -48,8 +49,2 @@ var import_pitch_distance = require("@tonaljs/pitch-distance");

var import_chord_detect2 = require("@tonaljs/chord-detect");
function deprecate(original, alternative, fn) {
return function(...args) {
console.warn(`${original} is deprecated. Use ${alternative}.`);
return fn.apply(this, args);
};
}
var NoChord = {

@@ -60,2 +55,3 @@ empty: true,

root: "",
bass: "",
rootDegree: 0,

@@ -75,45 +71,63 @@ type: "",

if (letter === "") {
return ["", name];
return tokenizeBass("", name);
} else if (letter === "A" && type === "ug") {
return tokenizeBass("", "aug");
} else {
return tokenizeBass(letter + acc, oct + type);
}
if (letter === "A" && type === "ug") {
return ["", "aug"];
}
function tokenizeBass(note2, chord2) {
const split = chord2.split("/");
if (split.length === 1) {
return [note2, split[0], ""];
}
return [letter + acc, oct + type];
const [letter, acc, oct, type] = (0, import_pitch_note.tokenizeNote)(split[1]);
if (letter !== "" && oct === "" && type === "") {
return [note2, split[0], letter + acc];
} else {
return [note2, chord2, ""];
}
}
function get(src) {
if (src === "") {
if (Array.isArray(src)) {
return getChord(src[1] || "", src[0], src[2]);
} else if (src === "") {
return NoChord;
}
if (Array.isArray(src) && src.length === 2) {
return getChord(src[1], src[0]);
} else {
const [tonic, type] = tokenize(src);
const chord2 = getChord(type, tonic);
const [tonic, type, bass] = tokenize(src);
const chord2 = getChord(type, tonic, bass);
return chord2.empty ? getChord(src) : chord2;
}
}
function getChord(typeName, optionalTonic, optionalRoot) {
function getChord(typeName, optionalTonic, optionalBass) {
const type = (0, import_chord_type.get)(typeName);
const tonic = (0, import_pitch_note.note)(optionalTonic || "");
const root = (0, import_pitch_note.note)(optionalRoot || "");
if (type.empty || optionalTonic && tonic.empty || optionalRoot && root.empty) {
const bass = (0, import_pitch_note.note)(optionalBass || "");
if (type.empty || optionalTonic && tonic.empty || optionalBass && bass.empty) {
return NoChord;
}
const rootInterval = (0, import_pitch_distance.distance)(tonic.pc, root.pc);
const rootDegree = type.intervals.indexOf(rootInterval) + 1;
if (!root.empty && !rootDegree) {
return NoChord;
}
const bassInterval = (0, import_pitch_distance.distance)(tonic.pc, bass.pc);
const bassIndex = type.intervals.indexOf(bassInterval);
const hasRoot = bassIndex >= 0;
const root = hasRoot ? bass : (0, import_pitch_note.note)("");
const rootDegree = bassIndex === -1 ? NaN : bassIndex + 1;
const hasBass = bass.pc && bass.pc !== tonic.pc;
const intervals = Array.from(type.intervals);
for (let i = 1; i < rootDegree; i++) {
const num = intervals[0][0];
const quality = intervals[0][1];
const newNum = parseInt(num, 10) + 7;
intervals.push(`${newNum}${quality}`);
intervals.shift();
if (hasRoot) {
for (let i = 1; i < rootDegree; i++) {
const num = intervals[0][0];
const quality = intervals[0][1];
const newNum = parseInt(num, 10) + 7;
intervals.push(`${newNum}${quality}`);
intervals.shift();
}
} else if (hasBass) {
const ivl = (0, import_interval.subtract)((0, import_pitch_distance.distance)(tonic.pc, bass.pc), "8P");
if (ivl)
intervals.unshift(ivl);
}
const notes = tonic.empty ? [] : intervals.map((i) => (0, import_pitch_distance.transpose)(tonic, i));
const notes2 = tonic.empty ? [] : intervals.map((i) => (0, import_pitch_distance.transpose)(tonic.pc, i));
typeName = type.aliases.indexOf(typeName) !== -1 ? typeName : type.aliases[0];
const symbol = `${tonic.empty ? "" : tonic.pc}${typeName}${root.empty || rootDegree <= 1 ? "" : "/" + root.pc}`;
const name = `${optionalTonic ? tonic.pc + " " : ""}${type.name}${rootDegree > 1 && optionalRoot ? " over " + root.pc : ""}`;
const symbol = `${tonic.empty ? "" : tonic.pc}${typeName}${hasRoot && rootDegree > 1 ? "/" + root.pc : hasBass ? "/" + bass.pc : ""}`;
const name = `${optionalTonic ? tonic.pc + " " : ""}${type.name}${hasRoot && rootDegree > 1 ? " over " + root.pc : hasBass ? " over " + bass.pc : ""}`;
return {

@@ -123,17 +137,20 @@ ...type,

symbol,
tonic: tonic.pc,
type: type.name,
root: root.name,
root: root.pc,
bass: hasBass ? bass.pc : "",
intervals,
rootDegree,
tonic: tonic.name,
notes
notes: notes2
};
}
var chord = deprecate("Chord.chord", "Chord.get", get);
var chord = get;
function transpose(chordName, interval) {
const [tonic, type] = tokenize(chordName);
const [tonic, type, bass] = tokenize(chordName);
if (!tonic) {
return chordName;
}
return (0, import_pitch_distance.transpose)(tonic, interval) + type;
const tr = (0, import_pitch_distance.transpose)(bass, interval);
const slash = tr ? "/" + tr : "";
return (0, import_pitch_distance.transpose)(tonic, interval) + type + slash;
}

@@ -155,10 +172,19 @@ function chordScales(name) {

}
function degrees(chordName) {
const { intervals, tonic } = get(chordName);
const transpose2 = (0, import_pitch_distance.tonicIntervalsTransposer)(intervals, tonic);
function notes(chordName, tonic) {
const chord2 = get(chordName);
const note2 = tonic || chord2.tonic;
if (!note2 || chord2.empty)
return [];
return chord2.intervals.map((ivl) => (0, import_pitch_distance.transpose)(note2, ivl));
}
function degrees(chordName, tonic) {
const chord2 = get(chordName);
const note2 = tonic || chord2.tonic;
const transpose2 = (0, import_pitch_distance.tonicIntervalsTransposer)(chord2.intervals, note2);
return (degree) => degree ? transpose2(degree > 0 ? degree - 1 : degree) : "";
}
function steps(chordName) {
const { intervals, tonic } = get(chordName);
return (0, import_pitch_distance.tonicIntervalsTransposer)(intervals, tonic);
function steps(chordName, tonic) {
const chord2 = get(chordName);
const note2 = tonic || chord2.tonic;
return (0, import_pitch_distance.tonicIntervalsTransposer)(chord2.intervals, note2);
}

@@ -176,3 +202,3 @@ var chord_default = {

steps,
// deprecate
notes,
chord

@@ -185,3 +211,2 @@ };

degrees,
deprecate,
detect,

@@ -191,2 +216,3 @@ extended,

getChord,
notes,
reduced,

@@ -193,0 +219,0 @@ steps,

{
"name": "@tonaljs/chord",
"version": "5.0.3",
"version": "6.0.0",
"description": "Musical chords and its relations",

@@ -19,9 +19,10 @@ "keywords": [

"dependencies": {
"@tonaljs/chord-detect": "4.8.4",
"@tonaljs/chord-type": "5.0.4",
"@tonaljs/chord-detect": "4.8.5",
"@tonaljs/chord-type": "5.0.5",
"@tonaljs/collection": "4.8.1",
"@tonaljs/pitch-note": "5.0.3",
"@tonaljs/pitch-distance": "5.0.2",
"@tonaljs/pcset": "4.9.1",
"@tonaljs/scale-type": "4.8.4"
"@tonaljs/interval": "^5.0.0",
"@tonaljs/pcset": "4.9.2",
"@tonaljs/pitch-distance": "5.0.3",
"@tonaljs/pitch-note": "6.0.0",
"@tonaljs/scale-type": "4.8.5"
},

@@ -28,0 +29,0 @@ "author": "danigb@gmail.com",

@@ -21,11 +21,14 @@ # @tonaljs/chord ![tonal](https://img.shields.io/badge/@tonaljs-chord-yellow.svg?style=flat-square) [![npm version](https://img.shields.io/npm/v/@tonaljs/chord.svg?style=flat-square)](https://www.npmjs.com/package/@tonaljs/chord)

Get the chord properties from a chord type and an (optional) tonic and (optional) root. Notice that the tonic must be present if the root is present. Also the root must be part of the chord notes.
#### `Chord.get(name: string | [string, string] | [string, string, string]) => Chord`
It returns the same object that `ChordType.get` but with additional properties:
Given a chord symbol or tokens, it returns the chord properties.
- symbol: the chord symbol (a combiation of the tonic, chord type shortname and root, if present). For example: `Cmaj7`, `Db7b5/F`. The symbol always uses pitch classes (note names without octaves) for both the tonic and root.
Chord properties is the the same object that `ChordType.get` but with additional fields:
- symbol: the chord symbol (a combination of the tonic, chord type shortname and root, if present). For example: `Cmaj7`, `Db7b5/F`. The symbol always uses pitch classes (note names without octaves) for both the tonic and root.
- tonic: the tonic of the chord (or an empty string if not present)
- root: the root of the chord (or an empty string if not present)
- rootDegree: the degree of the root. 0 if root not present. A number greater than 0 if present, where 1 indicates the tonic, 2 the second note (normally the 3th), 2 the third note (normally the 5th), etc.
- notes: an array of notes, or empty array if tonic is not present. If the root is pres
- bass: the bass of the chord (or an empty string if not present). The bass can be any pitch class.
- root: the root of the chord (or an empty string if not present). The root is present if the bass not belongs to the chord. It could be chords with bass without root field, but not the opposite.
- rootDegree: the degree of the root. NaN if root not present. A number greater than 0 if present, where 1 indicates the tonic, 2 the second note (normally the 3th), 2 the third note (normally the 5th), etc.
- notes: an array of notes, or empty array if tonic is not present. The notes will be always pitch classes.

@@ -35,40 +38,45 @@ Example:

```js
Chord.getChord("maj7", "G4", "B4"); // =>
Chord.get("Cmaj7/B"); // =>
// {
// empty: false,
// name: "G major seventh over B",
// symbol: "Gmaj7/B",
// tonic: "G4",
// root: "B4",
// rootDegree: 2,
// name: 'C major seventh over B',
// setNum: 2193,
// type: "major seventh",
// aliases: ["maj7", "Δ", "ma7", "M7", "Maj7"],
// chroma: "100010010001",
// intervals: ["3M", "5P", "7M", "8P"],
// normalized: "100010010001",
// notes: ["B4", "D5", "F#5", "G5"],
// quality: "Major",
// chroma: '100010010001',
// normalized: '100010010001',
// intervals: [ '7M', '8P', '10M', '12P' ],
// quality: 'Major',
// aliases: [ 'maj7', 'Δ', 'ma7', 'M7', 'Maj7', '^7' ],
// symbol: 'Cmaj7/B',
// tonic: 'C',
// type: 'major seventh',
// root: 'B',
// bass: 'B',
// rootDegree: 4,
// notes: [ 'B', 'C', 'E', 'G' ]
// }
```
#### `Chord.get(name: string | [string, string]) => Chord`
`Chord.chord` is an alias that is handy if using the `@tonaljs/chord` directly:
An alias of `Chord.getChord` but accepts a chord symbol as parameter.
```js
import { chord } from "@tonaljs/chord";
chord("C6add2");
```
`Chord.getChord(chordType, tonic, bass)` is very similar but with arguments for each chord part:
```js
Chord.get("Cmaj7");
// same as
Chord.get(["C", "maj7"]);
// same as
Chord.getChord("maj7", "C");
Chord.getChord("maj7", "C", "B") === Chord.get("Cmaj7/B");
```
Important: currently chord with bass are NOT allowed (will be implemented in next version):
### `Chord.notes(chordType: string, tonic?: string) => string[]`
Print the notes of the given chord at the given tonic:
```js
Chord.get("Cmaj7/E"); // => { empty: true }
Chord.notes("maj4", "C4"); // => ["C4", "E4", "G4", "B4"]
```
### `Chord.degrees(chordName: string | [string, string]) => (degree: number) => string`
### `Chord.degrees(chordType: string, tonic?: string) => (degree: number) => string`

@@ -78,3 +86,3 @@ `Scale.degrees` returns a function to get a note name from a scale degree:

```js
const c4m7 = Chord.degrees(["C4", "m7");
const c4m7 = Chord.degrees("m7", "C4");
c4m7(1); // => "C4"

@@ -87,9 +95,9 @@ c4m7(2); // => "Eb4"

It can be used to find chord inversions:
It can be used, for example, to get the notes of chord inversions:
```js
[1, 2, 3, 4].map(chord); // => ["C4", "Eb4", "G4", "Bb4"]
[2, 3, 4, 5].map(chord); // => ["Eb4", "G4", "Bb4", "C5"]
[3, 4, 5, 6].map(chord); // => ["G4", "Bb4", "C5", "Eb5"]
[4, 5, 6, 7].map(chord); // => ["Bb4", "C5", "Eb5", "G5"]
[1, 2, 3, 4].map(c4m7); // => ["C4", "Eb4", "G4", "Bb4"]
[2, 3, 4, 5].map(c4m7); // => ["Eb4", "G4", "Bb4", "C5"]
[3, 4, 5, 6].map(c4m7); // => ["G4", "Bb4", "C5", "Eb5"]
[4, 5, 6, 7].map(c4m7); // => ["Bb4", "C5", "Eb5", "G5"]
```

@@ -96,0 +104,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet