Skip to content

Commit

Permalink
Voicing packages (wip). Thanks @felixroos (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos authored Nov 15, 2020
1 parent 638b5ef commit 1deafd8
Show file tree
Hide file tree
Showing 18 changed files with 740 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/tonal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import RomanNumeral from "@tonaljs/roman-numeral";
import Scale from "@tonaljs/scale";
import ScaleType from "@tonaljs/scale-type";
import TimeSignature from "@tonaljs/time-signature";
import Voicing from "@tonaljs/voicing";
import VoiceLeading from "@tonaljs/voice-leading";
import VoicingDictionary from "@tonaljs/voicing-dictionary";

export * from "@tonaljs/core";

Expand Down
5 changes: 4 additions & 1 deletion packages/tonal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"@tonaljs/roman-numeral": "^4.5.1",
"@tonaljs/scale": "^4.5.1",
"@tonaljs/scale-type": "^4.5.1",
"@tonaljs/time-signature": "^4.5.1"
"@tonaljs/time-signature": "^4.5.1",
"@tonaljs/voice-leading": "^4.5.1",
"@tonaljs/voicing-dictionary": "^4.5.1",
"@tonaljs/voicing": "^4.5.1"
},
"author": "[email protected]",
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions packages/voice-leading/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2020 felixroos

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

58 changes: 58 additions & 0 deletions packages/voice-leading/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# @tonaljs/voice-leading

Contains a collection functions to find optimal transitions between chord voicings. Used by [@tonaljs/voicings](../voicings).

## Usage

ES6:

```js
import { VoiceLeading } from '@tonaljs/tonal';
```

Nodejs:

```js
const { VoiceLeading } = require('@tonaljs/tonal');
```

## API

### VoiceLeading

```ts
declare type VoiceLeadingFunction = (voicings: string[][], lastVoicing: string[]) => string[];
```

A function that decides which of a set of voicings is picked as a follow up to lastVoicing.

Example:

```ts
const topNoteDiff: VoiceLeadingFunction = (voicings, lastVoicing) => {
if (!lastVoicing || !lastVoicing.length) {
// if no lastVoicing is given
return voicings[0];
}
const topNoteMidi = (voicing: string[]) => Note.midi(voicing[voicing.length - 1]) || 0;
const diff = (voicing: string[]) => Math.abs(topNoteMidi(lastVoicing) - topNoteMidi(voicing));
return voicings.sort((a, b) => diff(a) - diff(b))[0]; // return voicing with least diff
};
```

Usage

```ts
topNoteDiff(
[
['F3', 'A3', 'C4', 'E4'], // top note = E4
['C4', 'E4', 'F4', 'A4'], // top note = A4
],
['C4', 'E4', 'G4', 'B4'] // top note = B4
);
// ['C4', 'E4', 'F4', 'A4'] // => A4 is closer to B4 than E4
```

[show available voice leading functions](./index.ts).

See [@tonaljs/voicings](../voicings) for usage examples.
22 changes: 22 additions & 0 deletions packages/voice-leading/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Note from "@tonaljs/note";

// A function that decides which of a set of voicings is picked as a follow up to lastVoicing.
export declare type VoiceLeadingFunction = (
voicings: string[][],
lastVoicing: string[]
) => string[];

export const topNoteDiff: VoiceLeadingFunction = (voicings, lastVoicing) => {
if (!lastVoicing || !lastVoicing.length) {
return voicings[0];
}
const topNoteMidi = (voicing: string[]) =>
Note.midi(voicing[voicing.length - 1]) || 0;
const diff = (voicing: string[]) =>
Math.abs(topNoteMidi(lastVoicing) - topNoteMidi(voicing));
return voicings.sort((a, b) => diff(a) - diff(b))[0];
};

export default {
topNoteDiff,
};
28 changes: 28 additions & 0 deletions packages/voice-leading/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@tonaljs/voice-leading",
"version": "4.5.1",
"description": "Voice leading logic for transitions between voicings",
"private": true,
"keywords": [
"chord",
"voicing",
"voicings",
"music",
"theory",
"@tonaljs"
],
"main": "dist/index.js",
"module": "dist/index.es.js",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"dependencies": {
"@tonaljs/note": "^4.5.1"
},
"author": "[email protected]",
"license": "MIT",
"publishConfig": {
"access": "public"
}
}
15 changes: 15 additions & 0 deletions packages/voice-leading/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { topNoteDiff } from ".";

describe("VoiceLeading", () => {
test("topNoteDiff", () => {
expect(
topNoteDiff(
[
["F3", "A3", "C4", "E4"],
["C4", "E4", "F4", "A4"],
],
["C4", "E4", "G4", "B4"]
)
).toEqual(["C4", "E4", "F4", "A4"]);
});
});
22 changes: 22 additions & 0 deletions packages/voicing-dictionary/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2020 felixroos

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

44 changes: 44 additions & 0 deletions packages/voicing-dictionary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# @tonaljs/voicing-dictionary

Contains dictionaries for many chord voicings. Used by [@tonaljs/voicings](../voicings).

## Usage

ES6:

```js
import { VoicingDictionary } from '@tonaljs/tonal';
```

Nodejs:

```js
const { VoicingDictionary } = require('@tonaljs/tonal');
```

## API

### VoicingDictionary

Maps a chord symbol to a set of voicings:

```ts
const lefthand = {
m7: ['3m 5P 7m 9M', '7m 9M 10m 12P'],
'7': ['3M 6M 7m 9M', '7m 9M 10M 13M'],
'^7': ['3M 5P 7M 9M', '7M 9M 10M 12P'],
'69': ['3M 5P 6A 9M'],
m7b5: ['3m 5d 7m 8P', '7m 8P 10m 12d'],
'7b9': ['3M 6m 7m 9m', '7m 9m 10M 13m'],
'7b13': ['3M 6m 7m 9m', '7m 9m 10M 13m'],
o7: ['1P 3m 5d 6M', '5d 6M 8P 10m'],
'7#11': ['7m 9M 11A 13A'],
'7#9': ['3M 7m 9A'],
mM7: ['3m 5P 7M 9M', '7M 9M 10m 12P'],
m6: ['3m 5P 6M 9M', '6M 9M 10m 12P'],
};
```

[show available dictionaries](./data.ts).

See [@tonaljs/voicings](../voicings) for usage examples.
40 changes: 40 additions & 0 deletions packages/voicing-dictionary/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { VoicingDictionary } from ".";

export const triads: VoicingDictionary = {
M: ["1P 3M 5P", "3M 5P 8P", "5P 8P 10M"],
m: ["1P 3m 5P", "3m 5P 8P", "5P 8P 10m"],
o: ["1P 3m 5d", "3m 5d 8P", "5d 8P 10m"],
aug: ["1P 3m 5A", "3m 5A 8P", "5A 8P 10m"],
};
export const lefthand: VoicingDictionary = {
m7: ["3m 5P 7m 9M", "7m 9M 10m 12P"],
"7": ["3M 6M 7m 9M", "7m 9M 10M 13M"],
"^7": ["3M 5P 7M 9M", "7M 9M 10M 12P"],
"69": ["3M 5P 6A 9M"],
m7b5: ["3m 5d 7m 8P", "7m 8P 10m 12d"],
"7b9": ["3M 6m 7m 9m", "7m 9m 10M 13m"], // b9 / b13
"7b13": ["3M 6m 7m 9m", "7m 9m 10M 13m"], // b9 / b13
o7: ["1P 3m 5d 6M", "5d 6M 8P 10m"],
"7#11": ["7m 9M 11A 13A"],
"7#9": ["3M 7m 9A"],
mM7: ["3m 5P 7M 9M", "7M 9M 10m 12P"],
m6: ["3m 5P 6M 9M", "6M 9M 10m 12P"],
};
export const all: VoicingDictionary = {
M: ["1P 3M 5P", "3M 5P 8P", "5P 8P 10M"],
m: ["1P 3m 5P", "3m 5P 8P", "5P 8P 10m"],
o: ["1P 3m 5d", "3m 5d 8P", "5d 8P 10m"],
aug: ["1P 3m 5A", "3m 5A 8P", "5A 8P 10m"],
m7: ["3m 5P 7m 9M", "7m 9M 10m 12P"],
"7": ["3M 6M 7m 9M", "7m 9M 10M 13M"],
"^7": ["3M 5P 7M 9M", "7M 9M 10M 12P"],
"69": ["3M 5P 6A 9M"],
m7b5: ["3m 5d 7m 8P", "7m 8P 10m 12d"],
"7b9": ["3M 6m 7m 9m", "7m 9m 10M 13m"], // b9 / b13
"7b13": ["3M 6m 7m 9m", "7m 9m 10M 13m"], // b9 / b13
o7: ["1P 3m 5d 6M", "5d 6M 8P 10m"],
"7#11": ["7m 9M 11A 13A"],
"7#9": ["3M 7m 9A"],
mM7: ["3m 5P 7M 9M", "7M 9M 10m 12P"],
m6: ["3m 5P 6M 9M", "6M 9M 10m 12P"],
};
31 changes: 31 additions & 0 deletions packages/voicing-dictionary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Chord from "@tonaljs/chord";
import { lefthand, all, triads } from "./data";

export declare type VoicingDictionary = { [symbol: string]: string[] };

const defaultDictionary: VoicingDictionary = lefthand;

function lookup(
symbol: string,
dictionary = defaultDictionary
): string[] | undefined {
if (dictionary[symbol]) {
return dictionary[symbol];
}
const { aliases } = Chord.get("C" + symbol);
// TODO: find other way to get aliases of symbol
const match =
Object.keys(dictionary).find((_symbol) => aliases.includes(_symbol)) || "";
if (match !== undefined) {
return dictionary[match];
}
return undefined;
}

export default {
lookup,
lefthand,
triads,
all,
defaultDictionary,
};
30 changes: 30 additions & 0 deletions packages/voicing-dictionary/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@tonaljs/voicing-dictionary",
"version": "4.5.1",
"description": "Collections of chord voicings",
"private": true,
"keywords": [
"chord",
"voicing",
"voicings",
"music",
"theory",
"@tonaljs"
],
"main": "dist/index.js",
"module": "dist/index.es.js",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"dependencies": {
"@tonaljs/note": "^4.5.1",
"@tonaljs/chord": "^4.5.1",
"@tonaljs/voice-leading": "^4.5.1"
},
"author": "[email protected]",
"license": "MIT",
"publishConfig": {
"access": "public"
}
}
19 changes: 19 additions & 0 deletions packages/voicing-dictionary/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import VoicingDictionary from "./index";

describe("lookup", () => {
test("lookup", () => {
expect(VoicingDictionary.lookup("M", VoicingDictionary.triads)).toEqual([
"1P 3M 5P",
"3M 5P 8P",
"5P 8P 10M",
]);
expect(VoicingDictionary.lookup("", VoicingDictionary.triads)).toEqual([
"1P 3M 5P",
"3M 5P 8P",
"5P 8P 10M",
]);
expect(VoicingDictionary.lookup("minor", { minor: ["1P 3m 5P"] })).toEqual([
"1P 3m 5P",
]);
});
});
22 changes: 22 additions & 0 deletions packages/voicing/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2020 felixroos

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Loading

0 comments on commit 1deafd8

Please sign in to comment.