Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voicing Package #224

Merged
merged 14 commits into from
Nov 15, 2020
43 changes: 23 additions & 20 deletions packages/tonal/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import AbcNotation from "@tonaljs/abc-notation";
import * as Array from "@tonaljs/array";
import Chord from "@tonaljs/chord";
import ChordType from "@tonaljs/chord-type";
import Collection from "@tonaljs/collection";
import * as Core from "@tonaljs/core";
import DurationValue from "@tonaljs/duration-value";
import Interval from "@tonaljs/interval";
import Key from "@tonaljs/key";
import Midi from "@tonaljs/midi";
import Mode from "@tonaljs/mode";
import Note from "@tonaljs/note";
import Pcset from "@tonaljs/pcset";
import Progression from "@tonaljs/progression";
import Range from "@tonaljs/range";
import RomanNumeral from "@tonaljs/roman-numeral";
import Scale from "@tonaljs/scale";
import ScaleType from "@tonaljs/scale-type";
import TimeSignature from "@tonaljs/time-signature";
import AbcNotation from '@tonaljs/abc-notation';
import * as Array from '@tonaljs/array';
import Chord from '@tonaljs/chord';
import ChordType from '@tonaljs/chord-type';
import Collection from '@tonaljs/collection';
import * as Core from '@tonaljs/core';
import DurationValue from '@tonaljs/duration-value';
import Interval from '@tonaljs/interval';
import Key from '@tonaljs/key';
import Midi from '@tonaljs/midi';
import Mode from '@tonaljs/mode';
import Note from '@tonaljs/note';
import Pcset from '@tonaljs/pcset';
import Progression from '@tonaljs/progression';
import Range from '@tonaljs/range';
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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm the pre commit hook messed something up here. I actually wanted to revert formatting everything with single quotes

Copy link
Collaborator

@danigb danigb Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tonal enforces prettier to be used, but it seems that is not working with you? 🤔 You can npm run format to format the code (that's what the pre commit hook does). And it should result with double quotes. I don't know why is not the case...
EDIT: are you using a custom prettier config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it uses my local prettier config because the project does not contain a prettier config

Copy link
Contributor Author

@felixroos felixroos Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm run format also formats everything wrong (things that I did not even touch)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might help adding a .prettierrc with your local setup

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! I thought it was one... let me add it 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a prettier configuration to package.json file: 638b5ef

Can you try to add the line "prettier": {}, to your package.json to see if it solves the problem?


export * from "@tonaljs/core";
export * from '@tonaljs/core';

// deprecated (backwards compatibility)
const Tonal = 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.

57 changes: 57 additions & 0 deletions packages/voice-leading/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# @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'],
['C4', 'E4', 'F4', 'A4'],
],
['C4', 'E4', 'G4', 'B4']
);
```

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

See [@tonaljs/voicings](../voicings) for usage examples.
17 changes: 17 additions & 0 deletions packages/voice-leading/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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,
};
27 changes: 27 additions & 0 deletions packages/voice-leading/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@tonaljs/voice-leading",
"version": "4.5.1",
"description": "Voice leading logic for transitions between voicings",
"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"
}
}
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'],
};
27 changes: 27 additions & 0 deletions packages/voicing-dictionary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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,
};
29 changes: 29 additions & 0 deletions packages/voicing-dictionary/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@tonaljs/voicing-dictionary",
"version": "4.5.1",
"description": "Collections of chord voicings",
"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"
}
}
9 changes: 9 additions & 0 deletions packages/voicing-dictionary/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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