tonal-array
Advanced tools
Comparing version 0.63.0 to 0.64.0
@@ -93,50 +93,2 @@ 'use strict'; | ||
/** | ||
* Given a list of notes, return the distance from the first note to the rest. | ||
* @param {Array|String} notes - the list of notes | ||
* @return {Array} the intervals | ||
* @example | ||
* tonal.harmonics('C E g') // => ['1P', '3M', '5P'] | ||
*/ | ||
function harmonics (list) { | ||
var a = asArr(list) | ||
return a.length ? a.map(tonalDistance.interval(a[0])).filter(hasVal) : a | ||
} | ||
/** | ||
* Given an array of intervals, create a function that harmonizes a | ||
* note with this intervals. Given a list of notes, return a function that | ||
* transpose the notes by an interval. | ||
* | ||
* @param {Array|String} ivls - the list of pitches | ||
* @return {Function} The harmonizer | ||
* @example | ||
* import { harmonizer } from 'tonal-arrays' | ||
* var maj7 = harmonizer('P1 M3 P5 M7') | ||
* maj7('C') // => ['C', 'E', 'G', 'B'] | ||
* var C = harmonizer('C D E') | ||
* C('M3') // => ['E', 'G#', 'B'] | ||
*/ | ||
function harmonizer (list) { | ||
return function (tonic) { | ||
return compact(map(tonalTranspose.transpose(tonic || 'P1'), list)) | ||
} | ||
} | ||
/** | ||
* Harmonizes a note with an array of intervals. It's a layer of sintatic | ||
* sugar over `harmonizer`. | ||
* | ||
* @function | ||
* @param {String|Array} ivl - the array of intervals | ||
* @param {String|Pitch} note - the note to be harmonized | ||
* @return {Array} the resulting notes | ||
* @example | ||
* var tonal = require('tonal') | ||
* tonal.harmonise('P1 M3 P5 M7', 'C') // => ['C', 'E', 'G', 'B'] | ||
*/ | ||
var harmonize = function (list, pitch) { | ||
return arguments.length > 1 ? harmonizer(list)(pitch) : harmonizer(list) | ||
} | ||
// a custom height function that | ||
@@ -282,3 +234,3 @@ // - returns -Infinity for non-pitch objects | ||
* | ||
* @function | ||
* @private | ||
* @param {Function} fn - the function to decorate | ||
@@ -303,5 +255,2 @@ * @return {Function} the decorated function | ||
exports.filter = filter; | ||
exports.harmonics = harmonics; | ||
exports.harmonizer = harmonizer; | ||
exports.harmonize = harmonize; | ||
exports.sort = sort; | ||
@@ -311,3 +260,2 @@ exports.shuffle = shuffle; | ||
exports.rotateAsc = rotateAsc; | ||
exports.select = select; | ||
exports.listFn = listFn; | ||
exports.select = select; |
57
index.js
@@ -9,5 +9,2 @@ /** | ||
* | ||
* There are also functions to transpose or calculate distances of a list | ||
* of notes or intervals: `harmonize`, `harmonizer` and `harmonics` | ||
* | ||
* One key feature of tonal is that you can represent lists with arrays or | ||
@@ -21,3 +18,3 @@ * with space separated string of elements. This module implements that | ||
import { transpose as tr } from 'tonal-transpose' | ||
import { interval, semitones } from 'tonal-distance' | ||
import { semitones } from 'tonal-distance' | ||
import * as toArr from 'as-arr' | ||
@@ -108,50 +105,2 @@ | ||
/** | ||
* Given a list of notes, return the distance from the first note to the rest. | ||
* @param {Array|String} notes - the list of notes | ||
* @return {Array} the intervals | ||
* @example | ||
* tonal.harmonics('C E g') // => ['1P', '3M', '5P'] | ||
*/ | ||
export function harmonics (list) { | ||
var a = asArr(list) | ||
return a.length ? a.map(interval(a[0])).filter(hasVal) : a | ||
} | ||
/** | ||
* Given an array of intervals, create a function that harmonizes a | ||
* note with this intervals. Given a list of notes, return a function that | ||
* transpose the notes by an interval. | ||
* | ||
* @param {Array|String} ivls - the list of pitches | ||
* @return {Function} The harmonizer | ||
* @example | ||
* import { harmonizer } from 'tonal-arrays' | ||
* var maj7 = harmonizer('P1 M3 P5 M7') | ||
* maj7('C') // => ['C', 'E', 'G', 'B'] | ||
* var C = harmonizer('C D E') | ||
* C('M3') // => ['E', 'G#', 'B'] | ||
*/ | ||
export function harmonizer (list) { | ||
return function (tonic) { | ||
return compact(map(tr(tonic || 'P1'), list)) | ||
} | ||
} | ||
/** | ||
* Harmonizes a note with an array of intervals. It's a layer of sintatic | ||
* sugar over `harmonizer`. | ||
* | ||
* @function | ||
* @param {String|Array} ivl - the array of intervals | ||
* @param {String|Pitch} note - the note to be harmonized | ||
* @return {Array} the resulting notes | ||
* @example | ||
* var tonal = require('tonal') | ||
* tonal.harmonise('P1 M3 P5 M7', 'C') // => ['C', 'E', 'G', 'B'] | ||
*/ | ||
export var harmonize = function (list, pitch) { | ||
return arguments.length > 1 ? harmonizer(list)(pitch) : harmonizer(list) | ||
} | ||
// a custom height function that | ||
@@ -297,3 +246,3 @@ // - returns -Infinity for non-pitch objects | ||
* | ||
* @function | ||
* @private | ||
* @param {Function} fn - the function to decorate | ||
@@ -306,3 +255,3 @@ * @return {Function} the decorated function | ||
*/ | ||
export function listFn (fn) { | ||
function listFn (fn) { | ||
return function (list) { | ||
@@ -309,0 +258,0 @@ var arr = asArr(list).map(asPitch) |
{ | ||
"name": "tonal-array", | ||
"version": "0.63.0", | ||
"version": "0.64.0", | ||
"description": "Create and manipulate arrays of notes and intervals", | ||
@@ -22,6 +22,6 @@ "keywords": [ | ||
"as-arr": "^1.0.0", | ||
"tonal-distance": "^0.61.0", | ||
"tonal-pitch": "^0.61.0", | ||
"tonal-transpose": "^0.61.0" | ||
"tonal-distance": "^0.64.0", | ||
"tonal-pitch": "^0.64.0", | ||
"tonal-transpose": "^0.64.0" | ||
} | ||
} |
347
README.md
@@ -1,4 +0,4 @@ | ||
# tonal-array [![npm version](https://img.shields.io/npm/v/tonal-array.svg)](https://www.npmjs.com/package/tonal-array) | ||
# tonal-array [![npm version](https://img.shields.io/npm/v/tonal-array.svg?style=flat-square)](https://www.npmjs.com/package/tonal-array) | ||
[![tonal](https://img.shields.io/badge/tonal-array-yellow.svg)](https://www.npmjs.com/browse/keyword/tonal) | ||
[![tonal](https://img.shields.io/badge/tonal-array-yellow.svg?style=flat-square)](https://www.npmjs.com/browse/keyword/tonal) | ||
@@ -11,343 +11,2 @@ `tonal-array` is a collection of functions to create and manipulate arrays of notes or intervals. | ||
## API Reference | ||
<dl> | ||
<dt><a href="#asArr">asArr(source)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Convert anything to array. Speifically, split string separated by spaces, | ||
commas or bars. The arrays are passed without modifications and the rest of | ||
the objects are wrapped.</p> | ||
<p>This function always returns an array (null or undefined values are converted | ||
to empty arrays)</p> | ||
<p>Thanks to this function, the rest of the functions of this module accepts | ||
any object (or more useful: strings) as an array parameter.</p> | ||
</dd> | ||
<dt><a href="#map">map(fn, arr)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Return a new array with the elements mapped by a function. | ||
Basically the same as the JavaScript standard <code>array.map</code> but with | ||
two enhacements:</p> | ||
<ul> | ||
<li>Arrays can be expressed as strings (see [asArr])</li> | ||
<li>This function can be partially applied. This is useful to create <em>mapped</em> | ||
versions of single element functions. For an excellent introduction of | ||
the adventages <a href="https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch4.html">read this</a></li> | ||
</ul> | ||
</dd> | ||
<dt><a href="#cMap">cMap(fn, list)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Compact map: map an array with a function and remove nulls. | ||
Can be partially applied.</p> | ||
</dd> | ||
<dt><a href="#compact">compact(list)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Return a copy of the array with the null values removed</p> | ||
</dd> | ||
<dt><a href="#filter">filter(fn, arr)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Filter an array with a function. Again, almost the same as JavaScript standard | ||
filter function but:</p> | ||
<ul> | ||
<li>It accepts strings as arrays</li> | ||
<li>Can be partially applied</li> | ||
</ul> | ||
</dd> | ||
<dt><a href="#harmonics">harmonics(notes)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Given a list of notes, return the distance from the first note to the rest.</p> | ||
</dd> | ||
<dt><a href="#harmonizer">harmonizer(ivls)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Given an array of intervals, create a function that harmonizes a | ||
note with this intervals. Given a list of notes, return a function that | ||
transpose the notes by an interval.</p> | ||
</dd> | ||
<dt><a href="#harmonize">harmonize(ivl, note)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Harmonizes a note with an array of intervals. It's a layer of sintatic | ||
sugar over <code>harmonizer</code>.</p> | ||
</dd> | ||
<dt><a href="#sort">sort(comp, arr)</a></dt> | ||
<dd><p>Sort an array or notes or intervals. It uses the JavaScript standard sort | ||
function.</p> | ||
</dd> | ||
<dt><a href="#shuffle">shuffle(arr)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Randomizes the order of the specified array using the Fisher–Yates shuffle.</p> | ||
</dd> | ||
<dt><a href="#rotate">rotate(times, list)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Rotates a list a number of times. It's completly agnostic about the | ||
contents of the list.</p> | ||
</dd> | ||
<dt><a href="#rotateAsc">rotateAsc(times, list)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Rotates an ascending list of pitches n times keeping the ascending property. | ||
This functions assumes the list is an ascending list of pitches, and | ||
transposes the them to ensure they are ascending after rotation. | ||
It can be used, for example, to invert chords.</p> | ||
</dd> | ||
<dt><a href="#select">select(numbers, list)</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Select elements from a list.</p> | ||
</dd> | ||
<dt><a href="#listFn">listFn(fn)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorates a function to so it's first parameter is an array of pitches in | ||
array notation. Also, if the return value is a pitch or an array of pitches | ||
in array notation, it convert backs to strings.</p> | ||
</dd> | ||
</dl> | ||
<a name="asArr"></a> | ||
## asArr(source) ⇒ <code>Array</code> | ||
Convert anything to array. Speifically, split string separated by spaces, | ||
commas or bars. The arrays are passed without modifications and the rest of | ||
the objects are wrapped. | ||
This function always returns an array (null or undefined values are converted | ||
to empty arrays) | ||
Thanks to this function, the rest of the functions of this module accepts | ||
any object (or more useful: strings) as an array parameter. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the object as an array | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| source | <code>\*</code> | the thing to get an array from | | ||
**Example** | ||
```js | ||
import { asArr } from 'tonal-arrays' | ||
asArr('C D E F G') // => ['C', 'D', 'E', 'F', 'G'] | ||
``` | ||
<a name="map"></a> | ||
## map(fn, arr) ⇒ <code>Array</code> | ||
Return a new array with the elements mapped by a function. | ||
Basically the same as the JavaScript standard `array.map` but with | ||
two enhacements: | ||
- Arrays can be expressed as strings (see [asArr]) | ||
- This function can be partially applied. This is useful to create _mapped_ | ||
versions of single element functions. For an excellent introduction of | ||
the adventages [read this](https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch4.html) | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fn | <code>function</code> | the function | | ||
| arr | <code>Array</code> | <code>String</code> | the array to be mapped | | ||
**Example** | ||
```js | ||
var arr = require('tonal-arr') | ||
var toUp = arr.map(function(e) { return e.toUpperCase() }) | ||
toUp('a b c') // => ['A', 'B', 'C'] | ||
``` | ||
**Example** | ||
```js | ||
var tonal = require('tonal') | ||
tonal.map(tonal.transpose('M3'), 'C D E') // => ['E', 'F#', 'G#'] | ||
``` | ||
<a name="cMap"></a> | ||
## cMap(fn, list) ⇒ <code>Array</code> | ||
Compact map: map an array with a function and remove nulls. | ||
Can be partially applied. | ||
**Kind**: global function | ||
**See**: map | ||
| Param | Type | | ||
| --- | --- | | ||
| fn | <code>function</code> | | ||
| list | <code>Array</code> | <code>String</code> | | ||
<a name="compact"></a> | ||
## compact(list) ⇒ <code>Array</code> | ||
Return a copy of the array with the null values removed | ||
**Kind**: global function | ||
| Param | Type | | ||
| --- | --- | | ||
| list | <code>String</code> | <code>Array</code> | | ||
<a name="filter"></a> | ||
## filter(fn, arr) ⇒ <code>Array</code> | ||
Filter an array with a function. Again, almost the same as JavaScript standard | ||
filter function but: | ||
- It accepts strings as arrays | ||
- Can be partially applied | ||
**Kind**: global function | ||
| Param | Type | | ||
| --- | --- | | ||
| fn | <code>function</code> | | ||
| arr | <code>String</code> | <code>Array</code> | | ||
<a name="harmonics"></a> | ||
## harmonics(notes) ⇒ <code>Array</code> | ||
Given a list of notes, return the distance from the first note to the rest. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the intervals | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| notes | <code>Array</code> | <code>String</code> | the list of notes | | ||
**Example** | ||
```js | ||
tonal.harmonics('C E g') // => ['1P', '3M', '5P'] | ||
``` | ||
<a name="harmonizer"></a> | ||
## harmonizer(ivls) ⇒ <code>function</code> | ||
Given an array of intervals, create a function that harmonizes a | ||
note with this intervals. Given a list of notes, return a function that | ||
transpose the notes by an interval. | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - The harmonizer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| ivls | <code>Array</code> | <code>String</code> | the list of pitches | | ||
**Example** | ||
```js | ||
import { harmonizer } from 'tonal-arrays' | ||
var maj7 = harmonizer('P1 M3 P5 M7') | ||
maj7('C') // => ['C', 'E', 'G', 'B'] | ||
var C = harmonizer('C D E') | ||
C('M3') // => ['E', 'G#', 'B'] | ||
``` | ||
<a name="harmonize"></a> | ||
## harmonize(ivl, note) ⇒ <code>Array</code> | ||
Harmonizes a note with an array of intervals. It's a layer of sintatic | ||
sugar over `harmonizer`. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the resulting notes | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| ivl | <code>String</code> | <code>Array</code> | the array of intervals | | ||
| note | <code>String</code> | <code>Pitch</code> | the note to be harmonized | | ||
**Example** | ||
```js | ||
var tonal = require('tonal') | ||
tonal.harmonise('P1 M3 P5 M7', 'C') // => ['C', 'E', 'G', 'B'] | ||
``` | ||
<a name="sort"></a> | ||
## sort(comp, arr) | ||
Sort an array or notes or intervals. It uses the JavaScript standard sort | ||
function. | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| comp | <code>Boolean</code> | <code>function</code> | the comparator. `true` means use an ascending comparator, `false` a descending comparator, or you can pass a custom comparator (that receives pitches in array notation) | | ||
| arr | <code>Array</code> | <code>String</code> | the array of notes or intervals | | ||
**Example** | ||
```js | ||
import { sort } from 'tonal-arrays' | ||
sort(true, 'D E C') // => ['C', 'D', 'E'] | ||
``` | ||
**Example** | ||
```js | ||
var tonal = require('tonal') | ||
tonal.sort(false, 'D E C') // => ['E', 'D', 'C'] | ||
``` | ||
<a name="shuffle"></a> | ||
## shuffle(arr) ⇒ <code>Array</code> | ||
Randomizes the order of the specified array using the Fisher–Yates shuffle. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the shuffled array | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| arr | <code>Array</code> | <code>String</code> | the array | | ||
**Example** | ||
```js | ||
import { shuffle } from 'tonal-arrays' | ||
``` | ||
**Example** | ||
```js | ||
var tonal = require('tonal') | ||
tonal.shuffle('C D E F') | ||
``` | ||
<a name="rotate"></a> | ||
## rotate(times, list) ⇒ <code>Array</code> | ||
Rotates a list a number of times. It's completly agnostic about the | ||
contents of the list. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the rotated array | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| times | <code>Integer</code> | the number of rotations | | ||
| list | <code>Array</code> | <code>String</code> | the list to be rotated | | ||
<a name="rotateAsc"></a> | ||
## rotateAsc(times, list) ⇒ <code>Array</code> | ||
Rotates an ascending list of pitches n times keeping the ascending property. | ||
This functions assumes the list is an ascending list of pitches, and | ||
transposes the them to ensure they are ascending after rotation. | ||
It can be used, for example, to invert chords. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the rotated array | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| times | <code>Integer</code> | the number of rotations | | ||
| list | <code>Array</code> | <code>String</code> | the list to be rotated | | ||
<a name="select"></a> | ||
## select(numbers, list) ⇒ <code>Array</code> | ||
Select elements from a list. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - the selected elements (with nulls if not valid index) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| numbers | <code>String</code> | <code>Array</code> | a __1-based__ index of the elements | | ||
| list | <code>String</code> | <code>Array</code> | the list of pitches | | ||
**Example** | ||
```js | ||
import { select } from 'tonal-array' | ||
select('1 3 5', 'C D E F G A B') // => ['C', 'E', 'G'] | ||
select('-1 0 1 2 3', 'C D') // => [ null, null, 'C', 'D', null ] | ||
``` | ||
<a name="listFn"></a> | ||
## listFn(fn) ⇒ <code>function</code> | ||
Decorates a function to so it's first parameter is an array of pitches in | ||
array notation. Also, if the return value is a pitch or an array of pitches | ||
in array notation, it convert backs to strings. | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - the decorated function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fn | <code>function</code> | the function to decorate | | ||
**Example** | ||
```js | ||
import { listFn } from 'tonal-arrays' | ||
var octUp = listFn((p) => { p[2] = p[2] + 1; return p[2] }) | ||
octUp('C2 D2 E2') // => ['C3', 'D3', 'E3'] | ||
``` | ||
[Read the API documentation](http://danigb.github.io/tonal/api/module-array.html). |
@@ -42,23 +42,3 @@ var test = require('tape') | ||
test('harmonics', function (t) { | ||
t.deepEqual(_.harmonics('C E G'), [ '1P', '3M', '5P' ]) | ||
t.deepEqual(_.harmonics('C2 E3 G4'), [ '1P', '10M', '19P' ]) | ||
t.deepEqual(_.harmonics('x y z'), []) | ||
t.end() | ||
}) | ||
test('harmonizer', function (t) { | ||
t.deepEqual(_.harmonize('1P 3M 5P', 'A4'), [ 'A4', 'C#5', 'E5' ]) | ||
t.deepEqual(_.harmonize('C E G', 'M3'), [ 'E', 'G#', 'B' ]) | ||
t.deepEqual(_.harmonize('C blah D', '7m'), [ 'Bb', 'C' ]) | ||
t.deepEqual(_.harmonize(null, '7m'), []) | ||
t.deepEqual(_.harmonize('c d e', null), [ 'C', 'D', 'E' ]) | ||
var maj7 = _.harmonizer('1P 3M 5P 7M') | ||
t.deepEqual(maj7('Bb'), [ 'Bb', 'D', 'F', 'A' ]) | ||
t.end() | ||
}) | ||
test('rotate', function (t) { | ||
test('array: rotate', function (t) { | ||
t.deepEqual(_.rotate(1, 'c d e'), ['d', 'e', 'c']) | ||
@@ -70,3 +50,3 @@ t.deepEqual(_.rotate(-1, 'c d e'), [ 'e', 'c', 'd' ]) | ||
test('rotateAsc', function (t) { | ||
test('array: rotateAsc', function (t) { | ||
t.deepEqual(_.rotateAsc(1, 'c d e'), ['D', 'E', 'C']) | ||
@@ -73,0 +53,0 @@ t.deepEqual(_.rotateAsc(-1, 'c d e'), [ 'E', 'C', 'D' ]) |
19166
541
12
+ Addedinterval-notation@1.0.1(transitive)
+ Addedtonal-distance@0.64.0(transitive)
+ Addedtonal-encoding@0.64.0(transitive)
+ Addedtonal-pitch@0.64.0(transitive)
+ Addedtonal-transpose@0.64.0(transitive)
- Removedinterval-notation@0.1.0(transitive)
- Removedtonal-distance@0.61.0(transitive)
- Removedtonal-encoding@0.60.0(transitive)
- Removedtonal-pitch@0.61.0(transitive)
- Removedtonal-transpose@0.61.0(transitive)
Updatedtonal-distance@^0.64.0
Updatedtonal-pitch@^0.64.0
Updatedtonal-transpose@^0.64.0