@tonaljs/pattern 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +217 -2
- package/dist/index.d.mts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +154 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +149 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +29 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 danigb
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,218 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tonaljs/pattern  [](https://www.npmjs.com/package/@tonaljs/pattern)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Note patterns: arpeggio orders and melodic sequences as lists of indexes
|
|
4
|
+
|
|
5
|
+
A note pattern says **which notes, in which order**: arpeggio orders (up, up-down, converge, thumb…) and melodic sequences (groups of four through a scale, thirds, 1-2-3-5). It doesn't say when they sound: that's a rhythm, and [`RhythmPattern`](/packages/rhythm-pattern) builds those.
|
|
6
|
+
|
|
7
|
+
A pattern is a list of indexes into a list of notes (a chord, a voicing, a scale), so the same pattern plays over any chord or scale:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { Chord, Pattern, Scale } from "tonal";
|
|
11
|
+
|
|
12
|
+
Pattern.arpeggio("up-down", 4); // => [0, 1, 2, 3, 2, 1]
|
|
13
|
+
Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"]));
|
|
14
|
+
// => ["C4", "E4", "G4", "B4", "G4", "E4"]
|
|
15
|
+
|
|
16
|
+
Pattern.sequence([0, 1, 2, 4], 1, 3).flat().map(Scale.steps("C4 major"));
|
|
17
|
+
// => ["C4", "D4", "E4", "G4", "D4", "E4", "F4", "A4", "E4", "F4", "G4", "B4"]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
ES6:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { Pattern } from "tonal";
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
node:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const { Pattern } = require("tonal");
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
single module:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { arpeggio, sequence } from "@tonaljs/pattern";
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Indexes and notes
|
|
41
|
+
|
|
42
|
+
The functions here only make and change lists of numbers. Turning an index into a note is the job of the notes it's played over, read as a loop that repeats every octave: over n notes, index `i` is note `i mod n`, `floor(i / n)` octaves up, and negative indexes go down.
|
|
43
|
+
|
|
44
|
+
- Over a scale: `Scale.steps("C4 major")`. Index 7 is `C5`, index -1 is `B3`.
|
|
45
|
+
- Over a chord: `Chord.steps(["C4", "maj7"])`. Index 4 is `C5`.
|
|
46
|
+
- Over a voicing: `Voicing.steps(["C3", "G3", "E4"])`. Index 3 is `C5`: past the top voice, the voicing repeats above itself.
|
|
47
|
+
|
|
48
|
+
To keep the notes in a register, fold them with [`Note.fold`](/packages/note#notefold).
|
|
49
|
+
|
|
50
|
+
Indexes count from 0, like every index in tonal: the 1-2-3-5 of the books is `[0, 1, 2, 4]`. A list counted from 1 plays over a scale with `Scale.degrees` instead:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
[0, 1, 2, 4].map(Scale.steps("D4 major")); // => ["D4", "E4", "F#4", "A4"]
|
|
54
|
+
[1, 2, 3, 5].map(Scale.degrees("D4 major")); // => ["D4", "E4", "F#4", "A4"]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
A pattern only means something together with the notes it's played over: the same four notes are `[0, 1, 2, 3]` over D major pentatonic.
|
|
58
|
+
|
|
59
|
+
## Arpeggios
|
|
60
|
+
|
|
61
|
+
### `Pattern.arpeggio`
|
|
62
|
+
|
|
63
|
+
`arpeggio(name: string, n: number) => number[]`
|
|
64
|
+
|
|
65
|
+
An arpeggio order over n notes, once round the loop. Index 0 is the lowest note and n - 1 the highest; every index is in that range. The order adapts to the number of notes (`"up"` is 3 notes over a triad, 4 over a seventh chord), unlike a sequence's cell, which has the same length over any notes:
|
|
66
|
+
|
|
67
|
+
| Name | What it plays | n = 3 | n = 4 |
|
|
68
|
+
| ------------------ | ---------------------------------------------------------------------- | ----------- | ----------------------- |
|
|
69
|
+
| `up` | lowest to highest | 0 1 2 | 0 1 2 3 |
|
|
70
|
+
| `down` | highest to lowest | 2 1 0 | 3 2 1 0 |
|
|
71
|
+
| `up-down` | up and back, each end once (2n - 2 notes) | 0 1 2 1 | 0 1 2 3 2 1 |
|
|
72
|
+
| `down-up` | down and back, each end once (2n - 2) | 2 1 0 1 | 3 2 1 0 1 2 |
|
|
73
|
+
| `up-down-repeat` | up and back, each end twice (2n) | 0 1 2 2 1 0 | 0 1 2 3 3 2 1 0 |
|
|
74
|
+
| `down-up-repeat` | down and back, each end twice (2n) | 2 1 0 0 1 2 | 3 2 1 0 0 1 2 3 |
|
|
75
|
+
| `converge` | lowest, highest, second lowest, second highest… | 0 2 1 | 0 3 1 2 |
|
|
76
|
+
| `diverge` | converge backwards: ends on the lowest | 1 2 0 | 2 1 3 0 |
|
|
77
|
+
| `converge-diverge` | converge and back, like up-down (2n - 2) | 0 2 1 2 | 0 3 1 2 1 3 |
|
|
78
|
+
| `thumb-up` | the lowest note before each note above it, upwards (2n - 2) | 0 1 0 2 | 0 1 0 2 0 3 |
|
|
79
|
+
| `thumb-up-down` | the lowest note before each note above it, up and down (4n - 8) | 0 1 0 2 | 0 1 0 2 0 3 0 2 |
|
|
80
|
+
| `pinky-up` | the highest note after each note below it, upwards (2n - 2) | 0 2 1 2 | 0 3 1 3 2 3 |
|
|
81
|
+
| `pinky-up-down` | the highest note after each note below it, up and down (4n - 8) | 0 2 1 2 | 0 3 1 3 2 3 1 3 |
|
|
82
|
+
|
|
83
|
+
The orders that go "and back" play each end once, so the loop doesn't repeat a note when it starts again (`0 1 2 1 | 0 1 2 1`); the `-repeat` orders play each end twice. Over one note every order is `[0]`, over two notes they're `[0, 1]` or `[1, 0]` (or `[0, 1, 1, 0]` and `[1, 0, 0, 1]` with repeated ends). It returns `[]` for 0 notes, an unknown name or a count that isn't a non-negative integer.
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
Pattern.arpeggio("converge", 4); // => [0, 3, 1, 2]
|
|
87
|
+
Pattern.arpeggio("thumb-up", 4); // => [0, 1, 0, 2, 0, 3]
|
|
88
|
+
|
|
89
|
+
Pattern.arpeggio("pinky-up", 4).map(Voicing.steps(["E3", "G3", "B3", "D4"]));
|
|
90
|
+
// => ["E3", "D4", "G3", "D4", "B3", "D4"]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
To play the notes as they were played, not from low to high, give them in that order and use `"up"`.
|
|
94
|
+
|
|
95
|
+
### `Pattern.arpeggios`
|
|
96
|
+
|
|
97
|
+
`arpeggios() => string[]`
|
|
98
|
+
|
|
99
|
+
The names of the arpeggio orders:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
Pattern.arpeggios();
|
|
103
|
+
// => ["up", "down", "up-down", "down-up", "up-down-repeat", "down-up-repeat", "converge", "diverge", "converge-diverge", "thumb-up", "thumb-up-down", "pinky-up", "pinky-up-down"]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Arpeggios over more octaves
|
|
107
|
+
|
|
108
|
+
There are two ways to spread an arpeggio over more octaves, and they give different orders for anything but `"up"`. Both come from the functions here. With a triad over two octaves, indexes 3 to 5 are the triad an octave up:
|
|
109
|
+
|
|
110
|
+
- **Expand first**: the order over all the notes of every octave (3 × 2):
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
Pattern.arpeggio("up-down", 3 * 2); // => [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]
|
|
114
|
+
Pattern.arpeggio("up-down", 3 * 2).map(Chord.steps(["C4", "M"]));
|
|
115
|
+
// => ["C4", "E4", "G4", "C5", "E5", "G5", "E5", "C5", "G4", "E4"]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
- **Order per octave**: the triad's order, played in each octave in turn (a sequence with a step of 3 notes):
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
Pattern.sequence(Pattern.arpeggio("up-down", 3), 3, 2).flat();
|
|
122
|
+
// => [0, 1, 2, 1, 3, 4, 5, 4]
|
|
123
|
+
Pattern.sequence(Pattern.arpeggio("up-down", 3), 3, 2)
|
|
124
|
+
.flat()
|
|
125
|
+
.map(Chord.steps(["C4", "M"]));
|
|
126
|
+
// => ["C4", "E4", "G4", "E4", "C5", "E5", "G5", "E5"]
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Sequences
|
|
130
|
+
|
|
131
|
+
### `Pattern.sequence`
|
|
132
|
+
|
|
133
|
+
`sequence(cell: number[], step: number, count: number) => number[][]`
|
|
134
|
+
|
|
135
|
+
A melodic sequence: a cell of indexes, moved by a step, again and again. Group i is the cell with i × step added to each index, so the cell keeps its shape and moves through the notes it's played over (through a scale, it moves diatonically). It returns the groups, as the books print them; `.flat()` gives one list. A negative step moves the cell down, and a step of 0 repeats it:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
Pattern.sequence([0, 1, 2, 3], 1, 3);
|
|
139
|
+
// => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
|
|
140
|
+
|
|
141
|
+
// groups of four through a pentatonic
|
|
142
|
+
Pattern.sequence([0, 1, 2, 3], 1, 3).flat().map(Scale.steps("C4 major pentatonic"));
|
|
143
|
+
// => ["C4", "D4", "E4", "G4", "D4", "E4", "G4", "A4", "E4", "G4", "A4", "C5"]
|
|
144
|
+
|
|
145
|
+
// triads on each degree
|
|
146
|
+
Pattern.sequence([0, 2, 4], 1, 3).map((group) => group.map(Scale.steps("C4 major")));
|
|
147
|
+
// => [["C4", "E4", "G4"], ["D4", "F4", "A4"], ["E4", "G4", "B4"]]
|
|
148
|
+
|
|
149
|
+
// groups of four, down from the top
|
|
150
|
+
Pattern.sequence([7, 6, 5, 4], -1, 3).flat().map(Scale.steps("C4 major"));
|
|
151
|
+
// => ["C5", "B4", "A4", "G4", "B4", "A4", "G4", "F4", "A4", "G4", "F4", "E4"]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
It returns `[]` if the cell is empty or has a number that isn't an integer, or if the step isn't an integer or the count isn't a non-negative integer.
|
|
155
|
+
|
|
156
|
+
Sources: Tim Richards, _Exploring Jazz Piano_, vol. 1, Figs 3.12-3.13 (groups of three and four notes, and thirds, through a scale) and Figs 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, _The Teaching of Jazz_, "digital patterns" (small groups such as 1-2-3-1, 1-2-3-5, 1-3-5-3, moved to each passing chord).
|
|
157
|
+
|
|
158
|
+
### `Pattern.alternate`
|
|
159
|
+
|
|
160
|
+
`alternate(groups: number[][]) => number[][]`
|
|
161
|
+
|
|
162
|
+
Reverse every other group, starting with the second. A sequence of thirds becomes thirds alternately up and down:
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]);
|
|
166
|
+
// => [[0, 2], [3, 1], [2, 4], [5, 3]]
|
|
167
|
+
Pattern.alternate(Pattern.sequence([0, 2], 1, 4)).flat().map(Scale.steps("C4 major"));
|
|
168
|
+
// => ["C4", "E4", "F4", "D4", "E4", "G4", "A4", "F4"]
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Sources: Tim Richards, _Exploring Jazz Piano_, vol. 1, Fig 5.11 (pentatonic "thirds" alternately up and down, "obtained by reversing the direction of every other pair of notes").
|
|
172
|
+
|
|
173
|
+
### `Pattern.repeat`
|
|
174
|
+
|
|
175
|
+
`repeat(indexes: number[], times: number) => number[]`
|
|
176
|
+
|
|
177
|
+
Repeat each index a number of times, in place. Over an arpeggio, it gives the doubled orders:
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
Pattern.repeat([0, 1, 2], 2); // => [0, 0, 1, 1, 2, 2]
|
|
181
|
+
Pattern.repeat(Pattern.arpeggio("up", 3), 2); // => [0, 0, 1, 1, 2, 2]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
To repeat the whole list, use `sequence` with a step of 0: `Pattern.sequence([0, 2, 1], 0, 2).flat()` is `[0, 2, 1, 0, 2, 1]`.
|
|
185
|
+
|
|
186
|
+
## More with plain arrays
|
|
187
|
+
|
|
188
|
+
Other changes are one line of plain JavaScript or another tonal function:
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
// retrograde: the cell backwards
|
|
192
|
+
[0, 1, 2, 4].reverse(); // => [4, 2, 1, 0]
|
|
193
|
+
|
|
194
|
+
// inversion: the cell upside down (realise it from a higher note)
|
|
195
|
+
[0, 1, 2, 4].map((i) => -i).map(Scale.steps("C5 major")); // => ["C5", "B4", "A4", "F4"]
|
|
196
|
+
|
|
197
|
+
// start later in the order: RhythmPattern.rotate takes any list of numbers,
|
|
198
|
+
// so note patterns and rhythms rotate the same way
|
|
199
|
+
RhythmPattern.rotate(Pattern.arpeggio("up", 4), 1); // => [3, 0, 1, 2]
|
|
200
|
+
|
|
201
|
+
// the 24 orders of a cell
|
|
202
|
+
Array.permutations([0, 1, 2, 4]).length; // => 24
|
|
203
|
+
|
|
204
|
+
// a real sequence (moved by an interval, not through the scale): transpose the notes
|
|
205
|
+
[0, 1, 2, 4].map(Scale.steps("C4 major")).map(Note.transposeBy("m3"));
|
|
206
|
+
// => ["Eb4", "F4", "G4", "Bb4"]
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## With rhythm patterns
|
|
210
|
+
|
|
211
|
+
A note pattern and a rhythm pattern are independent: one says which notes, the other when. Put the notes on the onsets of a rhythm:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
const notes = Pattern.arpeggio("up-down", 3).map(Chord.steps(["C4", "M"]));
|
|
215
|
+
const onsets = RhythmPattern.toOnsets(RhythmPattern.euclid(8, 3)); // => [0, 3, 6]
|
|
216
|
+
onsets.map((step, i) => [step, notes[i % notes.length]]);
|
|
217
|
+
// => [[0, "C4"], [3, "E4"], [6, "G4"]]
|
|
218
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name of an arpeggio order. Other strings are accepted (and give `[]`),
|
|
3
|
+
* so a name read at runtime still type-checks.
|
|
4
|
+
*/
|
|
5
|
+
type ArpeggioName = "up" | "down" | "up-down" | "down-up" | "up-down-repeat" | "down-up-repeat" | "converge" | "diverge" | "converge-diverge" | "thumb-up" | "thumb-up-down" | "pinky-up" | "pinky-up-down";
|
|
6
|
+
/**
|
|
7
|
+
* Get the names of the arpeggio orders that `Pattern.arpeggio` knows.
|
|
8
|
+
*
|
|
9
|
+
* @returns the arpeggio names
|
|
10
|
+
* @example
|
|
11
|
+
* Pattern.arpeggios() // => ["up", "down", "up-down", "down-up", "up-down-repeat", "down-up-repeat", "converge", "diverge", "converge-diverge", "thumb-up", "thumb-up-down", "pinky-up", "pinky-up-down"]
|
|
12
|
+
*/
|
|
13
|
+
declare function arpeggios(): ArpeggioName[];
|
|
14
|
+
/**
|
|
15
|
+
* Get an arpeggio order over n notes: the indexes of the notes (0 is the
|
|
16
|
+
* lowest, n - 1 the highest) in the order they're played, once round the
|
|
17
|
+
* loop. The order adapts to the number of notes: `"up"` is 3 notes over a
|
|
18
|
+
* triad, 4 over a seventh chord. Every index is in 0..n - 1.
|
|
19
|
+
*
|
|
20
|
+
* - `"up"`, `"down"`: lowest to highest, highest to lowest.
|
|
21
|
+
* - `"up-down"`, `"down-up"`: there and back, playing each end once, so the
|
|
22
|
+
* loop doesn't repeat a note (2n - 2 notes). `"up-down-repeat"` and
|
|
23
|
+
* `"down-up-repeat"` play each end twice (2n notes).
|
|
24
|
+
* - `"converge"`: lowest, highest, second lowest, second highest… towards
|
|
25
|
+
* the middle. `"diverge"` is converge backwards, so it ends on the lowest
|
|
26
|
+
* note. `"converge-diverge"` bounces like up-down (2n - 2 notes).
|
|
27
|
+
* - `"thumb-up"`: the lowest note before each of the notes above it, upwards
|
|
28
|
+
* (2n - 2 notes). `"thumb-up-down"` does the same with the notes above going
|
|
29
|
+
* up and down (4n - 8 notes).
|
|
30
|
+
* - `"pinky-up"`: the highest note after each of the notes below it, upwards
|
|
31
|
+
* (2n - 2 notes). `"pinky-up-down"` does the same up and down (4n - 8
|
|
32
|
+
* notes).
|
|
33
|
+
*
|
|
34
|
+
* Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown
|
|
35
|
+
* name, or a count that isn't a non-negative integer. For more than n notes
|
|
36
|
+
* (octaves), ask for an order over more notes, or move one with
|
|
37
|
+
* `Pattern.sequence`: see the README.
|
|
38
|
+
*
|
|
39
|
+
* Sources: the common vocabulary of arpeggiators (up, down, up-down,
|
|
40
|
+
* converge, diverge, thumb, pinky). Products define the ends of up-down and
|
|
41
|
+
* the direction of diverge differently; the definitions here are tonal's.
|
|
42
|
+
* @param name the name of the order (see `Pattern.arpeggios`)
|
|
43
|
+
* @param n the number of notes
|
|
44
|
+
* @returns the indexes of the notes, in order
|
|
45
|
+
* @example
|
|
46
|
+
* Pattern.arpeggio("up-down", 4) // => [0, 1, 2, 3, 2, 1]
|
|
47
|
+
* Pattern.arpeggio("converge", 4) // => [0, 3, 1, 2]
|
|
48
|
+
* Pattern.arpeggio("thumb-up", 4) // => [0, 1, 0, 2, 0, 3]
|
|
49
|
+
* Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"])) // => ["C4", "E4", "G4", "B4", "G4", "E4"]
|
|
50
|
+
*/
|
|
51
|
+
declare function arpeggio(name: ArpeggioName | (string & {}), n: number): number[];
|
|
52
|
+
/**
|
|
53
|
+
* Move a cell of indexes by a step, again and again: a melodic sequence.
|
|
54
|
+
* Group i is the cell with i × step added to each index, so the cell keeps
|
|
55
|
+
* its shape and moves through the notes it's realised over (a scale moves
|
|
56
|
+
* diatonically). It returns the groups; `.flat()` gives one list.
|
|
57
|
+
*
|
|
58
|
+
* Indexes can be negative or past the last note: `Scale.steps` realises them
|
|
59
|
+
* in the octaves below and above. A negative step moves the cell down. It
|
|
60
|
+
* returns `[]` if the cell is empty or has a non-integer, or if the step
|
|
61
|
+
* isn't an integer or the count isn't a non-negative integer.
|
|
62
|
+
*
|
|
63
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13
|
|
64
|
+
* (groups of three and four notes, and thirds, through a scale) and Figs
|
|
65
|
+
* 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching
|
|
66
|
+
* of Jazz*, "digital patterns" (1-2-3-5, 1-3-5-3… moved to each chord).
|
|
67
|
+
* @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)
|
|
68
|
+
* @param step how far each group moves from the one before
|
|
69
|
+
* @param count the number of groups
|
|
70
|
+
* @returns the groups of indexes
|
|
71
|
+
* @example
|
|
72
|
+
* Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
|
|
73
|
+
* Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]
|
|
74
|
+
* Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps("C4 major pentatonic")) // => ["C4", "D4", "E4", "D4", "E4", "G4", "E4", "G4", "A4"]
|
|
75
|
+
*/
|
|
76
|
+
declare function sequence(cell: number[], step: number, count: number): number[][];
|
|
77
|
+
/**
|
|
78
|
+
* Reverse every other group, starting with the second: the first group stays
|
|
79
|
+
* as it is, the second is played backwards, and so on. A sequence of thirds
|
|
80
|
+
* becomes thirds alternately up and down.
|
|
81
|
+
*
|
|
82
|
+
* It returns new groups, or `[]` if the groups aren't arrays of integers.
|
|
83
|
+
*
|
|
84
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11
|
|
85
|
+
* (pentatonic "thirds" alternately up and down, "obtained by reversing the
|
|
86
|
+
* direction of every other pair of notes").
|
|
87
|
+
* @param groups the groups of indexes (from `Pattern.sequence`)
|
|
88
|
+
* @returns the groups, every other one reversed
|
|
89
|
+
* @example
|
|
90
|
+
* Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]
|
|
91
|
+
*/
|
|
92
|
+
declare function alternate(groups: number[][]): number[][];
|
|
93
|
+
/**
|
|
94
|
+
* Repeat each index a number of times, in place: `[0, 1]` twice is
|
|
95
|
+
* `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat
|
|
96
|
+
* the whole list, use `Pattern.sequence` with a step of 0.)
|
|
97
|
+
*
|
|
98
|
+
* It returns `[]` if the list isn't an array of integers or the times isn't
|
|
99
|
+
* a non-negative integer.
|
|
100
|
+
* @param indexes the indexes
|
|
101
|
+
* @param times how many times each index is played
|
|
102
|
+
* @returns the indexes, each repeated
|
|
103
|
+
* @example
|
|
104
|
+
* Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]
|
|
105
|
+
* Pattern.repeat(Pattern.arpeggio("down", 3), 2) // => [2, 2, 1, 1, 0, 0]
|
|
106
|
+
*/
|
|
107
|
+
declare function repeat(indexes: number[], times: number): number[];
|
|
108
|
+
export { ArpeggioName, alternate, arpeggio, arpeggios, repeat, sequence };
|
|
109
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name of an arpeggio order. Other strings are accepted (and give `[]`),
|
|
3
|
+
* so a name read at runtime still type-checks.
|
|
4
|
+
*/
|
|
5
|
+
type ArpeggioName = "up" | "down" | "up-down" | "down-up" | "up-down-repeat" | "down-up-repeat" | "converge" | "diverge" | "converge-diverge" | "thumb-up" | "thumb-up-down" | "pinky-up" | "pinky-up-down";
|
|
6
|
+
/**
|
|
7
|
+
* Get the names of the arpeggio orders that `Pattern.arpeggio` knows.
|
|
8
|
+
*
|
|
9
|
+
* @returns the arpeggio names
|
|
10
|
+
* @example
|
|
11
|
+
* Pattern.arpeggios() // => ["up", "down", "up-down", "down-up", "up-down-repeat", "down-up-repeat", "converge", "diverge", "converge-diverge", "thumb-up", "thumb-up-down", "pinky-up", "pinky-up-down"]
|
|
12
|
+
*/
|
|
13
|
+
declare function arpeggios(): ArpeggioName[];
|
|
14
|
+
/**
|
|
15
|
+
* Get an arpeggio order over n notes: the indexes of the notes (0 is the
|
|
16
|
+
* lowest, n - 1 the highest) in the order they're played, once round the
|
|
17
|
+
* loop. The order adapts to the number of notes: `"up"` is 3 notes over a
|
|
18
|
+
* triad, 4 over a seventh chord. Every index is in 0..n - 1.
|
|
19
|
+
*
|
|
20
|
+
* - `"up"`, `"down"`: lowest to highest, highest to lowest.
|
|
21
|
+
* - `"up-down"`, `"down-up"`: there and back, playing each end once, so the
|
|
22
|
+
* loop doesn't repeat a note (2n - 2 notes). `"up-down-repeat"` and
|
|
23
|
+
* `"down-up-repeat"` play each end twice (2n notes).
|
|
24
|
+
* - `"converge"`: lowest, highest, second lowest, second highest… towards
|
|
25
|
+
* the middle. `"diverge"` is converge backwards, so it ends on the lowest
|
|
26
|
+
* note. `"converge-diverge"` bounces like up-down (2n - 2 notes).
|
|
27
|
+
* - `"thumb-up"`: the lowest note before each of the notes above it, upwards
|
|
28
|
+
* (2n - 2 notes). `"thumb-up-down"` does the same with the notes above going
|
|
29
|
+
* up and down (4n - 8 notes).
|
|
30
|
+
* - `"pinky-up"`: the highest note after each of the notes below it, upwards
|
|
31
|
+
* (2n - 2 notes). `"pinky-up-down"` does the same up and down (4n - 8
|
|
32
|
+
* notes).
|
|
33
|
+
*
|
|
34
|
+
* Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown
|
|
35
|
+
* name, or a count that isn't a non-negative integer. For more than n notes
|
|
36
|
+
* (octaves), ask for an order over more notes, or move one with
|
|
37
|
+
* `Pattern.sequence`: see the README.
|
|
38
|
+
*
|
|
39
|
+
* Sources: the common vocabulary of arpeggiators (up, down, up-down,
|
|
40
|
+
* converge, diverge, thumb, pinky). Products define the ends of up-down and
|
|
41
|
+
* the direction of diverge differently; the definitions here are tonal's.
|
|
42
|
+
* @param name the name of the order (see `Pattern.arpeggios`)
|
|
43
|
+
* @param n the number of notes
|
|
44
|
+
* @returns the indexes of the notes, in order
|
|
45
|
+
* @example
|
|
46
|
+
* Pattern.arpeggio("up-down", 4) // => [0, 1, 2, 3, 2, 1]
|
|
47
|
+
* Pattern.arpeggio("converge", 4) // => [0, 3, 1, 2]
|
|
48
|
+
* Pattern.arpeggio("thumb-up", 4) // => [0, 1, 0, 2, 0, 3]
|
|
49
|
+
* Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"])) // => ["C4", "E4", "G4", "B4", "G4", "E4"]
|
|
50
|
+
*/
|
|
51
|
+
declare function arpeggio(name: ArpeggioName | (string & {}), n: number): number[];
|
|
52
|
+
/**
|
|
53
|
+
* Move a cell of indexes by a step, again and again: a melodic sequence.
|
|
54
|
+
* Group i is the cell with i × step added to each index, so the cell keeps
|
|
55
|
+
* its shape and moves through the notes it's realised over (a scale moves
|
|
56
|
+
* diatonically). It returns the groups; `.flat()` gives one list.
|
|
57
|
+
*
|
|
58
|
+
* Indexes can be negative or past the last note: `Scale.steps` realises them
|
|
59
|
+
* in the octaves below and above. A negative step moves the cell down. It
|
|
60
|
+
* returns `[]` if the cell is empty or has a non-integer, or if the step
|
|
61
|
+
* isn't an integer or the count isn't a non-negative integer.
|
|
62
|
+
*
|
|
63
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13
|
|
64
|
+
* (groups of three and four notes, and thirds, through a scale) and Figs
|
|
65
|
+
* 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching
|
|
66
|
+
* of Jazz*, "digital patterns" (1-2-3-5, 1-3-5-3… moved to each chord).
|
|
67
|
+
* @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)
|
|
68
|
+
* @param step how far each group moves from the one before
|
|
69
|
+
* @param count the number of groups
|
|
70
|
+
* @returns the groups of indexes
|
|
71
|
+
* @example
|
|
72
|
+
* Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
|
|
73
|
+
* Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]
|
|
74
|
+
* Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps("C4 major pentatonic")) // => ["C4", "D4", "E4", "D4", "E4", "G4", "E4", "G4", "A4"]
|
|
75
|
+
*/
|
|
76
|
+
declare function sequence(cell: number[], step: number, count: number): number[][];
|
|
77
|
+
/**
|
|
78
|
+
* Reverse every other group, starting with the second: the first group stays
|
|
79
|
+
* as it is, the second is played backwards, and so on. A sequence of thirds
|
|
80
|
+
* becomes thirds alternately up and down.
|
|
81
|
+
*
|
|
82
|
+
* It returns new groups, or `[]` if the groups aren't arrays of integers.
|
|
83
|
+
*
|
|
84
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11
|
|
85
|
+
* (pentatonic "thirds" alternately up and down, "obtained by reversing the
|
|
86
|
+
* direction of every other pair of notes").
|
|
87
|
+
* @param groups the groups of indexes (from `Pattern.sequence`)
|
|
88
|
+
* @returns the groups, every other one reversed
|
|
89
|
+
* @example
|
|
90
|
+
* Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]
|
|
91
|
+
*/
|
|
92
|
+
declare function alternate(groups: number[][]): number[][];
|
|
93
|
+
/**
|
|
94
|
+
* Repeat each index a number of times, in place: `[0, 1]` twice is
|
|
95
|
+
* `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat
|
|
96
|
+
* the whole list, use `Pattern.sequence` with a step of 0.)
|
|
97
|
+
*
|
|
98
|
+
* It returns `[]` if the list isn't an array of integers or the times isn't
|
|
99
|
+
* a non-negative integer.
|
|
100
|
+
* @param indexes the indexes
|
|
101
|
+
* @param times how many times each index is played
|
|
102
|
+
* @returns the indexes, each repeated
|
|
103
|
+
* @example
|
|
104
|
+
* Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]
|
|
105
|
+
* Pattern.repeat(Pattern.arpeggio("down", 3), 2) // => [2, 2, 1, 1, 0, 0]
|
|
106
|
+
*/
|
|
107
|
+
declare function repeat(indexes: number[], times: number): number[];
|
|
108
|
+
export { ArpeggioName, alternate, arpeggio, arpeggios, repeat, sequence };
|
|
109
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// index.ts
|
|
3
|
+
const isIndex = (n) => typeof n === "number" && Number.isInteger(n);
|
|
4
|
+
const isCount = (n) => isIndex(n) && n >= 0;
|
|
5
|
+
const isIndexList = (list) => Array.isArray(list) && list.every(isIndex);
|
|
6
|
+
const up = (n) => Array.from({ length: n }, (_, i) => i);
|
|
7
|
+
const reversed = (list) => [...list].reverse();
|
|
8
|
+
const bounce = (list) => list.length < 2 ? list : [...list.slice(0, -1), ...reversed(list).slice(0, -1)];
|
|
9
|
+
const bounceRepeat = (list) => [...list, ...reversed(list)];
|
|
10
|
+
const converge = (n) => up(n).map((i) => i % 2 === 0 ? i / 2 : n - 1 - (i - 1) / 2);
|
|
11
|
+
const before = (anchor, list) => list.flatMap((i) => [anchor, i]);
|
|
12
|
+
const after = (anchor, list) => list.flatMap((i) => [i, anchor]);
|
|
13
|
+
const upper = (n) => up(n - 1).map((i) => i + 1);
|
|
14
|
+
const ARPEGGIOS = {
|
|
15
|
+
up: (n) => up(n),
|
|
16
|
+
down: (n) => reversed(up(n)),
|
|
17
|
+
"up-down": (n) => bounce(up(n)),
|
|
18
|
+
"down-up": (n) => bounce(reversed(up(n))),
|
|
19
|
+
"up-down-repeat": (n) => bounceRepeat(up(n)),
|
|
20
|
+
"down-up-repeat": (n) => bounceRepeat(reversed(up(n))),
|
|
21
|
+
converge: (n) => converge(n),
|
|
22
|
+
diverge: (n) => reversed(converge(n)),
|
|
23
|
+
"converge-diverge": (n) => bounce(converge(n)),
|
|
24
|
+
"thumb-up": (n) => before(0, upper(n)),
|
|
25
|
+
"thumb-up-down": (n) => before(0, bounce(upper(n))),
|
|
26
|
+
"pinky-up": (n) => after(n - 1, up(n - 1)),
|
|
27
|
+
"pinky-up-down": (n) => after(n - 1, bounce(up(n - 1)))
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Get the names of the arpeggio orders that `Pattern.arpeggio` knows.
|
|
31
|
+
*
|
|
32
|
+
* @returns the arpeggio names
|
|
33
|
+
* @example
|
|
34
|
+
* Pattern.arpeggios() // => ["up", "down", "up-down", "down-up", "up-down-repeat", "down-up-repeat", "converge", "diverge", "converge-diverge", "thumb-up", "thumb-up-down", "pinky-up", "pinky-up-down"]
|
|
35
|
+
*/
|
|
36
|
+
function arpeggios() {
|
|
37
|
+
return Object.keys(ARPEGGIOS);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get an arpeggio order over n notes: the indexes of the notes (0 is the
|
|
41
|
+
* lowest, n - 1 the highest) in the order they're played, once round the
|
|
42
|
+
* loop. The order adapts to the number of notes: `"up"` is 3 notes over a
|
|
43
|
+
* triad, 4 over a seventh chord. Every index is in 0..n - 1.
|
|
44
|
+
*
|
|
45
|
+
* - `"up"`, `"down"`: lowest to highest, highest to lowest.
|
|
46
|
+
* - `"up-down"`, `"down-up"`: there and back, playing each end once, so the
|
|
47
|
+
* loop doesn't repeat a note (2n - 2 notes). `"up-down-repeat"` and
|
|
48
|
+
* `"down-up-repeat"` play each end twice (2n notes).
|
|
49
|
+
* - `"converge"`: lowest, highest, second lowest, second highest… towards
|
|
50
|
+
* the middle. `"diverge"` is converge backwards, so it ends on the lowest
|
|
51
|
+
* note. `"converge-diverge"` bounces like up-down (2n - 2 notes).
|
|
52
|
+
* - `"thumb-up"`: the lowest note before each of the notes above it, upwards
|
|
53
|
+
* (2n - 2 notes). `"thumb-up-down"` does the same with the notes above going
|
|
54
|
+
* up and down (4n - 8 notes).
|
|
55
|
+
* - `"pinky-up"`: the highest note after each of the notes below it, upwards
|
|
56
|
+
* (2n - 2 notes). `"pinky-up-down"` does the same up and down (4n - 8
|
|
57
|
+
* notes).
|
|
58
|
+
*
|
|
59
|
+
* Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown
|
|
60
|
+
* name, or a count that isn't a non-negative integer. For more than n notes
|
|
61
|
+
* (octaves), ask for an order over more notes, or move one with
|
|
62
|
+
* `Pattern.sequence`: see the README.
|
|
63
|
+
*
|
|
64
|
+
* Sources: the common vocabulary of arpeggiators (up, down, up-down,
|
|
65
|
+
* converge, diverge, thumb, pinky). Products define the ends of up-down and
|
|
66
|
+
* the direction of diverge differently; the definitions here are tonal's.
|
|
67
|
+
* @param name the name of the order (see `Pattern.arpeggios`)
|
|
68
|
+
* @param n the number of notes
|
|
69
|
+
* @returns the indexes of the notes, in order
|
|
70
|
+
* @example
|
|
71
|
+
* Pattern.arpeggio("up-down", 4) // => [0, 1, 2, 3, 2, 1]
|
|
72
|
+
* Pattern.arpeggio("converge", 4) // => [0, 3, 1, 2]
|
|
73
|
+
* Pattern.arpeggio("thumb-up", 4) // => [0, 1, 0, 2, 0, 3]
|
|
74
|
+
* Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"])) // => ["C4", "E4", "G4", "B4", "G4", "E4"]
|
|
75
|
+
*/
|
|
76
|
+
function arpeggio(name, n) {
|
|
77
|
+
if (!Object.prototype.hasOwnProperty.call(ARPEGGIOS, name)) return [];
|
|
78
|
+
if (!isCount(n)) return [];
|
|
79
|
+
if (n < 2) return up(n);
|
|
80
|
+
return ARPEGGIOS[name](n);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Move a cell of indexes by a step, again and again: a melodic sequence.
|
|
84
|
+
* Group i is the cell with i × step added to each index, so the cell keeps
|
|
85
|
+
* its shape and moves through the notes it's realised over (a scale moves
|
|
86
|
+
* diatonically). It returns the groups; `.flat()` gives one list.
|
|
87
|
+
*
|
|
88
|
+
* Indexes can be negative or past the last note: `Scale.steps` realises them
|
|
89
|
+
* in the octaves below and above. A negative step moves the cell down. It
|
|
90
|
+
* returns `[]` if the cell is empty or has a non-integer, or if the step
|
|
91
|
+
* isn't an integer or the count isn't a non-negative integer.
|
|
92
|
+
*
|
|
93
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13
|
|
94
|
+
* (groups of three and four notes, and thirds, through a scale) and Figs
|
|
95
|
+
* 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching
|
|
96
|
+
* of Jazz*, "digital patterns" (1-2-3-5, 1-3-5-3… moved to each chord).
|
|
97
|
+
* @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)
|
|
98
|
+
* @param step how far each group moves from the one before
|
|
99
|
+
* @param count the number of groups
|
|
100
|
+
* @returns the groups of indexes
|
|
101
|
+
* @example
|
|
102
|
+
* Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
|
|
103
|
+
* Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]
|
|
104
|
+
* Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps("C4 major pentatonic")) // => ["C4", "D4", "E4", "D4", "E4", "G4", "E4", "G4", "A4"]
|
|
105
|
+
*/
|
|
106
|
+
function sequence(cell, step, count) {
|
|
107
|
+
if (!isIndexList(cell) || cell.length === 0) return [];
|
|
108
|
+
if (!isIndex(step) || !isCount(count)) return [];
|
|
109
|
+
return Array.from({ length: count }, (_, i) => cell.map((index) => index + i * step));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Reverse every other group, starting with the second: the first group stays
|
|
113
|
+
* as it is, the second is played backwards, and so on. A sequence of thirds
|
|
114
|
+
* becomes thirds alternately up and down.
|
|
115
|
+
*
|
|
116
|
+
* It returns new groups, or `[]` if the groups aren't arrays of integers.
|
|
117
|
+
*
|
|
118
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11
|
|
119
|
+
* (pentatonic "thirds" alternately up and down, "obtained by reversing the
|
|
120
|
+
* direction of every other pair of notes").
|
|
121
|
+
* @param groups the groups of indexes (from `Pattern.sequence`)
|
|
122
|
+
* @returns the groups, every other one reversed
|
|
123
|
+
* @example
|
|
124
|
+
* Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]
|
|
125
|
+
*/
|
|
126
|
+
function alternate(groups) {
|
|
127
|
+
if (!Array.isArray(groups) || !groups.every(isIndexList)) return [];
|
|
128
|
+
return groups.map((group, i) => i % 2 === 0 ? [...group] : reversed(group));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Repeat each index a number of times, in place: `[0, 1]` twice is
|
|
132
|
+
* `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat
|
|
133
|
+
* the whole list, use `Pattern.sequence` with a step of 0.)
|
|
134
|
+
*
|
|
135
|
+
* It returns `[]` if the list isn't an array of integers or the times isn't
|
|
136
|
+
* a non-negative integer.
|
|
137
|
+
* @param indexes the indexes
|
|
138
|
+
* @param times how many times each index is played
|
|
139
|
+
* @returns the indexes, each repeated
|
|
140
|
+
* @example
|
|
141
|
+
* Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]
|
|
142
|
+
* Pattern.repeat(Pattern.arpeggio("down", 3), 2) // => [2, 2, 1, 1, 0, 0]
|
|
143
|
+
*/
|
|
144
|
+
function repeat(indexes, times) {
|
|
145
|
+
if (!isIndexList(indexes) || !isCount(times)) return [];
|
|
146
|
+
return indexes.flatMap((index) => Array(times).fill(index));
|
|
147
|
+
}
|
|
148
|
+
exports.alternate = alternate;
|
|
149
|
+
exports.arpeggio = arpeggio;
|
|
150
|
+
exports.arpeggios = arpeggios;
|
|
151
|
+
exports.repeat = repeat;
|
|
152
|
+
exports.sequence = sequence;
|
|
153
|
+
|
|
154
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../index.ts"],"sourcesContent":["/**\n * The name of an arpeggio order. Other strings are accepted (and give `[]`),\n * so a name read at runtime still type-checks.\n */\nexport type ArpeggioName =\n | \"up\"\n | \"down\"\n | \"up-down\"\n | \"down-up\"\n | \"up-down-repeat\"\n | \"down-up-repeat\"\n | \"converge\"\n | \"diverge\"\n | \"converge-diverge\"\n | \"thumb-up\"\n | \"thumb-up-down\"\n | \"pinky-up\"\n | \"pinky-up-down\";\n\nconst isIndex = (n: unknown): n is number =>\n typeof n === \"number\" && Number.isInteger(n);\n\nconst isCount = (n: unknown): n is number => isIndex(n) && n >= 0;\n\nconst isIndexList = (list: unknown): list is number[] =>\n Array.isArray(list) && list.every(isIndex);\n\n// 0 to n - 1\nconst up = (n: number) => Array.from({ length: n }, (_, i) => i);\n\nconst reversed = (list: number[]) => [...list].reverse();\n\n// The order, then back: without repeating the last note, nor the first one\n// when the loop starts again (up-down)\nconst bounce = (list: number[]) =>\n list.length < 2\n ? list\n : [...list.slice(0, -1), ...reversed(list).slice(0, -1)];\n\n// The order, then back, repeating both ends (up-down-repeat)\nconst bounceRepeat = (list: number[]) => [...list, ...reversed(list)];\n\n// Lowest, highest, second lowest, second highest... towards the middle\nconst converge = (n: number) =>\n up(n).map((i) => (i % 2 === 0 ? i / 2 : n - 1 - (i - 1) / 2));\n\n// The anchor before each note of the order (thumb)\nconst before = (anchor: number, list: number[]) =>\n list.flatMap((i) => [anchor, i]);\n\n// The anchor after each note of the order (pinky)\nconst after = (anchor: number, list: number[]) =>\n list.flatMap((i) => [i, anchor]);\n\n// The notes above the lowest one: 1 to n - 1\nconst upper = (n: number) => up(n - 1).map((i) => i + 1);\n\nconst ARPEGGIOS: Record<ArpeggioName, (n: number) => number[]> = {\n up: (n) => up(n),\n down: (n) => reversed(up(n)),\n \"up-down\": (n) => bounce(up(n)),\n \"down-up\": (n) => bounce(reversed(up(n))),\n \"up-down-repeat\": (n) => bounceRepeat(up(n)),\n \"down-up-repeat\": (n) => bounceRepeat(reversed(up(n))),\n converge: (n) => converge(n),\n diverge: (n) => reversed(converge(n)),\n \"converge-diverge\": (n) => bounce(converge(n)),\n \"thumb-up\": (n) => before(0, upper(n)),\n \"thumb-up-down\": (n) => before(0, bounce(upper(n))),\n \"pinky-up\": (n) => after(n - 1, up(n - 1)),\n \"pinky-up-down\": (n) => after(n - 1, bounce(up(n - 1))),\n};\n\n/**\n * Get the names of the arpeggio orders that `Pattern.arpeggio` knows.\n *\n * @returns the arpeggio names\n * @example\n * Pattern.arpeggios() // => [\"up\", \"down\", \"up-down\", \"down-up\", \"up-down-repeat\", \"down-up-repeat\", \"converge\", \"diverge\", \"converge-diverge\", \"thumb-up\", \"thumb-up-down\", \"pinky-up\", \"pinky-up-down\"]\n */\nexport function arpeggios(): ArpeggioName[] {\n return Object.keys(ARPEGGIOS) as ArpeggioName[];\n}\n\n/**\n * Get an arpeggio order over n notes: the indexes of the notes (0 is the\n * lowest, n - 1 the highest) in the order they're played, once round the\n * loop. The order adapts to the number of notes: `\"up\"` is 3 notes over a\n * triad, 4 over a seventh chord. Every index is in 0..n - 1.\n *\n * - `\"up\"`, `\"down\"`: lowest to highest, highest to lowest.\n * - `\"up-down\"`, `\"down-up\"`: there and back, playing each end once, so the\n * loop doesn't repeat a note (2n - 2 notes). `\"up-down-repeat\"` and\n * `\"down-up-repeat\"` play each end twice (2n notes).\n * - `\"converge\"`: lowest, highest, second lowest, second highest… towards\n * the middle. `\"diverge\"` is converge backwards, so it ends on the lowest\n * note. `\"converge-diverge\"` bounces like up-down (2n - 2 notes).\n * - `\"thumb-up\"`: the lowest note before each of the notes above it, upwards\n * (2n - 2 notes). `\"thumb-up-down\"` does the same with the notes above going\n * up and down (4n - 8 notes).\n * - `\"pinky-up\"`: the highest note after each of the notes below it, upwards\n * (2n - 2 notes). `\"pinky-up-down\"` does the same up and down (4n - 8\n * notes).\n *\n * Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown\n * name, or a count that isn't a non-negative integer. For more than n notes\n * (octaves), ask for an order over more notes, or move one with\n * `Pattern.sequence`: see the README.\n *\n * Sources: the common vocabulary of arpeggiators (up, down, up-down,\n * converge, diverge, thumb, pinky). Products define the ends of up-down and\n * the direction of diverge differently; the definitions here are tonal's.\n * @param name the name of the order (see `Pattern.arpeggios`)\n * @param n the number of notes\n * @returns the indexes of the notes, in order\n * @example\n * Pattern.arpeggio(\"up-down\", 4) // => [0, 1, 2, 3, 2, 1]\n * Pattern.arpeggio(\"converge\", 4) // => [0, 3, 1, 2]\n * Pattern.arpeggio(\"thumb-up\", 4) // => [0, 1, 0, 2, 0, 3]\n * Pattern.arpeggio(\"up-down\", 4).map(Chord.steps([\"C4\", \"maj7\"])) // => [\"C4\", \"E4\", \"G4\", \"B4\", \"G4\", \"E4\"]\n */\nexport function arpeggio(\n name: ArpeggioName | (string & {}),\n n: number,\n): number[] {\n if (!Object.prototype.hasOwnProperty.call(ARPEGGIOS, name)) return [];\n if (!isCount(n)) return [];\n if (n < 2) return up(n);\n return ARPEGGIOS[name as ArpeggioName](n);\n}\n\n/**\n * Move a cell of indexes by a step, again and again: a melodic sequence.\n * Group i is the cell with i × step added to each index, so the cell keeps\n * its shape and moves through the notes it's realised over (a scale moves\n * diatonically). It returns the groups; `.flat()` gives one list.\n *\n * Indexes can be negative or past the last note: `Scale.steps` realises them\n * in the octaves below and above. A negative step moves the cell down. It\n * returns `[]` if the cell is empty or has a non-integer, or if the step\n * isn't an integer or the count isn't a non-negative integer.\n *\n * Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13\n * (groups of three and four notes, and thirds, through a scale) and Figs\n * 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching\n * of Jazz*, \"digital patterns\" (1-2-3-5, 1-3-5-3… moved to each chord).\n * @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)\n * @param step how far each group moves from the one before\n * @param count the number of groups\n * @returns the groups of indexes\n * @example\n * Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]\n * Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]\n * Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps(\"C4 major pentatonic\")) // => [\"C4\", \"D4\", \"E4\", \"D4\", \"E4\", \"G4\", \"E4\", \"G4\", \"A4\"]\n */\nexport function sequence(\n cell: number[],\n step: number,\n count: number,\n): number[][] {\n if (!isIndexList(cell) || cell.length === 0) return [];\n if (!isIndex(step) || !isCount(count)) return [];\n return Array.from({ length: count }, (_, i) =>\n cell.map((index) => index + i * step),\n );\n}\n\n/**\n * Reverse every other group, starting with the second: the first group stays\n * as it is, the second is played backwards, and so on. A sequence of thirds\n * becomes thirds alternately up and down.\n *\n * It returns new groups, or `[]` if the groups aren't arrays of integers.\n *\n * Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11\n * (pentatonic \"thirds\" alternately up and down, \"obtained by reversing the\n * direction of every other pair of notes\").\n * @param groups the groups of indexes (from `Pattern.sequence`)\n * @returns the groups, every other one reversed\n * @example\n * Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]\n */\nexport function alternate(groups: number[][]): number[][] {\n if (!Array.isArray(groups) || !groups.every(isIndexList)) return [];\n return groups.map((group, i) => (i % 2 === 0 ? [...group] : reversed(group)));\n}\n\n/**\n * Repeat each index a number of times, in place: `[0, 1]` twice is\n * `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat\n * the whole list, use `Pattern.sequence` with a step of 0.)\n *\n * It returns `[]` if the list isn't an array of integers or the times isn't\n * a non-negative integer.\n * @param indexes the indexes\n * @param times how many times each index is played\n * @returns the indexes, each repeated\n * @example\n * Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]\n * Pattern.repeat(Pattern.arpeggio(\"down\", 3), 2) // => [2, 2, 1, 1, 0, 0]\n */\nexport function repeat(indexes: number[], times: number): number[] {\n if (!isIndexList(indexes) || !isCount(times)) return [];\n return indexes.flatMap((index) => Array(times).fill(index));\n}\n"],"mappings":"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"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
const isIndex = (n) => typeof n === "number" && Number.isInteger(n);
|
|
3
|
+
const isCount = (n) => isIndex(n) && n >= 0;
|
|
4
|
+
const isIndexList = (list) => Array.isArray(list) && list.every(isIndex);
|
|
5
|
+
const up = (n) => Array.from({ length: n }, (_, i) => i);
|
|
6
|
+
const reversed = (list) => [...list].reverse();
|
|
7
|
+
const bounce = (list) => list.length < 2 ? list : [...list.slice(0, -1), ...reversed(list).slice(0, -1)];
|
|
8
|
+
const bounceRepeat = (list) => [...list, ...reversed(list)];
|
|
9
|
+
const converge = (n) => up(n).map((i) => i % 2 === 0 ? i / 2 : n - 1 - (i - 1) / 2);
|
|
10
|
+
const before = (anchor, list) => list.flatMap((i) => [anchor, i]);
|
|
11
|
+
const after = (anchor, list) => list.flatMap((i) => [i, anchor]);
|
|
12
|
+
const upper = (n) => up(n - 1).map((i) => i + 1);
|
|
13
|
+
const ARPEGGIOS = {
|
|
14
|
+
up: (n) => up(n),
|
|
15
|
+
down: (n) => reversed(up(n)),
|
|
16
|
+
"up-down": (n) => bounce(up(n)),
|
|
17
|
+
"down-up": (n) => bounce(reversed(up(n))),
|
|
18
|
+
"up-down-repeat": (n) => bounceRepeat(up(n)),
|
|
19
|
+
"down-up-repeat": (n) => bounceRepeat(reversed(up(n))),
|
|
20
|
+
converge: (n) => converge(n),
|
|
21
|
+
diverge: (n) => reversed(converge(n)),
|
|
22
|
+
"converge-diverge": (n) => bounce(converge(n)),
|
|
23
|
+
"thumb-up": (n) => before(0, upper(n)),
|
|
24
|
+
"thumb-up-down": (n) => before(0, bounce(upper(n))),
|
|
25
|
+
"pinky-up": (n) => after(n - 1, up(n - 1)),
|
|
26
|
+
"pinky-up-down": (n) => after(n - 1, bounce(up(n - 1)))
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Get the names of the arpeggio orders that `Pattern.arpeggio` knows.
|
|
30
|
+
*
|
|
31
|
+
* @returns the arpeggio names
|
|
32
|
+
* @example
|
|
33
|
+
* Pattern.arpeggios() // => ["up", "down", "up-down", "down-up", "up-down-repeat", "down-up-repeat", "converge", "diverge", "converge-diverge", "thumb-up", "thumb-up-down", "pinky-up", "pinky-up-down"]
|
|
34
|
+
*/
|
|
35
|
+
function arpeggios() {
|
|
36
|
+
return Object.keys(ARPEGGIOS);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get an arpeggio order over n notes: the indexes of the notes (0 is the
|
|
40
|
+
* lowest, n - 1 the highest) in the order they're played, once round the
|
|
41
|
+
* loop. The order adapts to the number of notes: `"up"` is 3 notes over a
|
|
42
|
+
* triad, 4 over a seventh chord. Every index is in 0..n - 1.
|
|
43
|
+
*
|
|
44
|
+
* - `"up"`, `"down"`: lowest to highest, highest to lowest.
|
|
45
|
+
* - `"up-down"`, `"down-up"`: there and back, playing each end once, so the
|
|
46
|
+
* loop doesn't repeat a note (2n - 2 notes). `"up-down-repeat"` and
|
|
47
|
+
* `"down-up-repeat"` play each end twice (2n notes).
|
|
48
|
+
* - `"converge"`: lowest, highest, second lowest, second highest… towards
|
|
49
|
+
* the middle. `"diverge"` is converge backwards, so it ends on the lowest
|
|
50
|
+
* note. `"converge-diverge"` bounces like up-down (2n - 2 notes).
|
|
51
|
+
* - `"thumb-up"`: the lowest note before each of the notes above it, upwards
|
|
52
|
+
* (2n - 2 notes). `"thumb-up-down"` does the same with the notes above going
|
|
53
|
+
* up and down (4n - 8 notes).
|
|
54
|
+
* - `"pinky-up"`: the highest note after each of the notes below it, upwards
|
|
55
|
+
* (2n - 2 notes). `"pinky-up-down"` does the same up and down (4n - 8
|
|
56
|
+
* notes).
|
|
57
|
+
*
|
|
58
|
+
* Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown
|
|
59
|
+
* name, or a count that isn't a non-negative integer. For more than n notes
|
|
60
|
+
* (octaves), ask for an order over more notes, or move one with
|
|
61
|
+
* `Pattern.sequence`: see the README.
|
|
62
|
+
*
|
|
63
|
+
* Sources: the common vocabulary of arpeggiators (up, down, up-down,
|
|
64
|
+
* converge, diverge, thumb, pinky). Products define the ends of up-down and
|
|
65
|
+
* the direction of diverge differently; the definitions here are tonal's.
|
|
66
|
+
* @param name the name of the order (see `Pattern.arpeggios`)
|
|
67
|
+
* @param n the number of notes
|
|
68
|
+
* @returns the indexes of the notes, in order
|
|
69
|
+
* @example
|
|
70
|
+
* Pattern.arpeggio("up-down", 4) // => [0, 1, 2, 3, 2, 1]
|
|
71
|
+
* Pattern.arpeggio("converge", 4) // => [0, 3, 1, 2]
|
|
72
|
+
* Pattern.arpeggio("thumb-up", 4) // => [0, 1, 0, 2, 0, 3]
|
|
73
|
+
* Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"])) // => ["C4", "E4", "G4", "B4", "G4", "E4"]
|
|
74
|
+
*/
|
|
75
|
+
function arpeggio(name, n) {
|
|
76
|
+
if (!Object.prototype.hasOwnProperty.call(ARPEGGIOS, name)) return [];
|
|
77
|
+
if (!isCount(n)) return [];
|
|
78
|
+
if (n < 2) return up(n);
|
|
79
|
+
return ARPEGGIOS[name](n);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Move a cell of indexes by a step, again and again: a melodic sequence.
|
|
83
|
+
* Group i is the cell with i × step added to each index, so the cell keeps
|
|
84
|
+
* its shape and moves through the notes it's realised over (a scale moves
|
|
85
|
+
* diatonically). It returns the groups; `.flat()` gives one list.
|
|
86
|
+
*
|
|
87
|
+
* Indexes can be negative or past the last note: `Scale.steps` realises them
|
|
88
|
+
* in the octaves below and above. A negative step moves the cell down. It
|
|
89
|
+
* returns `[]` if the cell is empty or has a non-integer, or if the step
|
|
90
|
+
* isn't an integer or the count isn't a non-negative integer.
|
|
91
|
+
*
|
|
92
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13
|
|
93
|
+
* (groups of three and four notes, and thirds, through a scale) and Figs
|
|
94
|
+
* 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching
|
|
95
|
+
* of Jazz*, "digital patterns" (1-2-3-5, 1-3-5-3… moved to each chord).
|
|
96
|
+
* @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)
|
|
97
|
+
* @param step how far each group moves from the one before
|
|
98
|
+
* @param count the number of groups
|
|
99
|
+
* @returns the groups of indexes
|
|
100
|
+
* @example
|
|
101
|
+
* Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
|
|
102
|
+
* Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]
|
|
103
|
+
* Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps("C4 major pentatonic")) // => ["C4", "D4", "E4", "D4", "E4", "G4", "E4", "G4", "A4"]
|
|
104
|
+
*/
|
|
105
|
+
function sequence(cell, step, count) {
|
|
106
|
+
if (!isIndexList(cell) || cell.length === 0) return [];
|
|
107
|
+
if (!isIndex(step) || !isCount(count)) return [];
|
|
108
|
+
return Array.from({ length: count }, (_, i) => cell.map((index) => index + i * step));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reverse every other group, starting with the second: the first group stays
|
|
112
|
+
* as it is, the second is played backwards, and so on. A sequence of thirds
|
|
113
|
+
* becomes thirds alternately up and down.
|
|
114
|
+
*
|
|
115
|
+
* It returns new groups, or `[]` if the groups aren't arrays of integers.
|
|
116
|
+
*
|
|
117
|
+
* Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11
|
|
118
|
+
* (pentatonic "thirds" alternately up and down, "obtained by reversing the
|
|
119
|
+
* direction of every other pair of notes").
|
|
120
|
+
* @param groups the groups of indexes (from `Pattern.sequence`)
|
|
121
|
+
* @returns the groups, every other one reversed
|
|
122
|
+
* @example
|
|
123
|
+
* Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]
|
|
124
|
+
*/
|
|
125
|
+
function alternate(groups) {
|
|
126
|
+
if (!Array.isArray(groups) || !groups.every(isIndexList)) return [];
|
|
127
|
+
return groups.map((group, i) => i % 2 === 0 ? [...group] : reversed(group));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Repeat each index a number of times, in place: `[0, 1]` twice is
|
|
131
|
+
* `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat
|
|
132
|
+
* the whole list, use `Pattern.sequence` with a step of 0.)
|
|
133
|
+
*
|
|
134
|
+
* It returns `[]` if the list isn't an array of integers or the times isn't
|
|
135
|
+
* a non-negative integer.
|
|
136
|
+
* @param indexes the indexes
|
|
137
|
+
* @param times how many times each index is played
|
|
138
|
+
* @returns the indexes, each repeated
|
|
139
|
+
* @example
|
|
140
|
+
* Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]
|
|
141
|
+
* Pattern.repeat(Pattern.arpeggio("down", 3), 2) // => [2, 2, 1, 1, 0, 0]
|
|
142
|
+
*/
|
|
143
|
+
function repeat(indexes, times) {
|
|
144
|
+
if (!isIndexList(indexes) || !isCount(times)) return [];
|
|
145
|
+
return indexes.flatMap((index) => Array(times).fill(index));
|
|
146
|
+
}
|
|
147
|
+
export { alternate, arpeggio, arpeggios, repeat, sequence };
|
|
148
|
+
|
|
149
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../index.ts"],"sourcesContent":["/**\n * The name of an arpeggio order. Other strings are accepted (and give `[]`),\n * so a name read at runtime still type-checks.\n */\nexport type ArpeggioName =\n | \"up\"\n | \"down\"\n | \"up-down\"\n | \"down-up\"\n | \"up-down-repeat\"\n | \"down-up-repeat\"\n | \"converge\"\n | \"diverge\"\n | \"converge-diverge\"\n | \"thumb-up\"\n | \"thumb-up-down\"\n | \"pinky-up\"\n | \"pinky-up-down\";\n\nconst isIndex = (n: unknown): n is number =>\n typeof n === \"number\" && Number.isInteger(n);\n\nconst isCount = (n: unknown): n is number => isIndex(n) && n >= 0;\n\nconst isIndexList = (list: unknown): list is number[] =>\n Array.isArray(list) && list.every(isIndex);\n\n// 0 to n - 1\nconst up = (n: number) => Array.from({ length: n }, (_, i) => i);\n\nconst reversed = (list: number[]) => [...list].reverse();\n\n// The order, then back: without repeating the last note, nor the first one\n// when the loop starts again (up-down)\nconst bounce = (list: number[]) =>\n list.length < 2\n ? list\n : [...list.slice(0, -1), ...reversed(list).slice(0, -1)];\n\n// The order, then back, repeating both ends (up-down-repeat)\nconst bounceRepeat = (list: number[]) => [...list, ...reversed(list)];\n\n// Lowest, highest, second lowest, second highest... towards the middle\nconst converge = (n: number) =>\n up(n).map((i) => (i % 2 === 0 ? i / 2 : n - 1 - (i - 1) / 2));\n\n// The anchor before each note of the order (thumb)\nconst before = (anchor: number, list: number[]) =>\n list.flatMap((i) => [anchor, i]);\n\n// The anchor after each note of the order (pinky)\nconst after = (anchor: number, list: number[]) =>\n list.flatMap((i) => [i, anchor]);\n\n// The notes above the lowest one: 1 to n - 1\nconst upper = (n: number) => up(n - 1).map((i) => i + 1);\n\nconst ARPEGGIOS: Record<ArpeggioName, (n: number) => number[]> = {\n up: (n) => up(n),\n down: (n) => reversed(up(n)),\n \"up-down\": (n) => bounce(up(n)),\n \"down-up\": (n) => bounce(reversed(up(n))),\n \"up-down-repeat\": (n) => bounceRepeat(up(n)),\n \"down-up-repeat\": (n) => bounceRepeat(reversed(up(n))),\n converge: (n) => converge(n),\n diverge: (n) => reversed(converge(n)),\n \"converge-diverge\": (n) => bounce(converge(n)),\n \"thumb-up\": (n) => before(0, upper(n)),\n \"thumb-up-down\": (n) => before(0, bounce(upper(n))),\n \"pinky-up\": (n) => after(n - 1, up(n - 1)),\n \"pinky-up-down\": (n) => after(n - 1, bounce(up(n - 1))),\n};\n\n/**\n * Get the names of the arpeggio orders that `Pattern.arpeggio` knows.\n *\n * @returns the arpeggio names\n * @example\n * Pattern.arpeggios() // => [\"up\", \"down\", \"up-down\", \"down-up\", \"up-down-repeat\", \"down-up-repeat\", \"converge\", \"diverge\", \"converge-diverge\", \"thumb-up\", \"thumb-up-down\", \"pinky-up\", \"pinky-up-down\"]\n */\nexport function arpeggios(): ArpeggioName[] {\n return Object.keys(ARPEGGIOS) as ArpeggioName[];\n}\n\n/**\n * Get an arpeggio order over n notes: the indexes of the notes (0 is the\n * lowest, n - 1 the highest) in the order they're played, once round the\n * loop. The order adapts to the number of notes: `\"up\"` is 3 notes over a\n * triad, 4 over a seventh chord. Every index is in 0..n - 1.\n *\n * - `\"up\"`, `\"down\"`: lowest to highest, highest to lowest.\n * - `\"up-down\"`, `\"down-up\"`: there and back, playing each end once, so the\n * loop doesn't repeat a note (2n - 2 notes). `\"up-down-repeat\"` and\n * `\"down-up-repeat\"` play each end twice (2n notes).\n * - `\"converge\"`: lowest, highest, second lowest, second highest… towards\n * the middle. `\"diverge\"` is converge backwards, so it ends on the lowest\n * note. `\"converge-diverge\"` bounces like up-down (2n - 2 notes).\n * - `\"thumb-up\"`: the lowest note before each of the notes above it, upwards\n * (2n - 2 notes). `\"thumb-up-down\"` does the same with the notes above going\n * up and down (4n - 8 notes).\n * - `\"pinky-up\"`: the highest note after each of the notes below it, upwards\n * (2n - 2 notes). `\"pinky-up-down\"` does the same up and down (4n - 8\n * notes).\n *\n * Over one note every order is `[0]`. It returns `[]` for 0 notes, an unknown\n * name, or a count that isn't a non-negative integer. For more than n notes\n * (octaves), ask for an order over more notes, or move one with\n * `Pattern.sequence`: see the README.\n *\n * Sources: the common vocabulary of arpeggiators (up, down, up-down,\n * converge, diverge, thumb, pinky). Products define the ends of up-down and\n * the direction of diverge differently; the definitions here are tonal's.\n * @param name the name of the order (see `Pattern.arpeggios`)\n * @param n the number of notes\n * @returns the indexes of the notes, in order\n * @example\n * Pattern.arpeggio(\"up-down\", 4) // => [0, 1, 2, 3, 2, 1]\n * Pattern.arpeggio(\"converge\", 4) // => [0, 3, 1, 2]\n * Pattern.arpeggio(\"thumb-up\", 4) // => [0, 1, 0, 2, 0, 3]\n * Pattern.arpeggio(\"up-down\", 4).map(Chord.steps([\"C4\", \"maj7\"])) // => [\"C4\", \"E4\", \"G4\", \"B4\", \"G4\", \"E4\"]\n */\nexport function arpeggio(\n name: ArpeggioName | (string & {}),\n n: number,\n): number[] {\n if (!Object.prototype.hasOwnProperty.call(ARPEGGIOS, name)) return [];\n if (!isCount(n)) return [];\n if (n < 2) return up(n);\n return ARPEGGIOS[name as ArpeggioName](n);\n}\n\n/**\n * Move a cell of indexes by a step, again and again: a melodic sequence.\n * Group i is the cell with i × step added to each index, so the cell keeps\n * its shape and moves through the notes it's realised over (a scale moves\n * diatonically). It returns the groups; `.flat()` gives one list.\n *\n * Indexes can be negative or past the last note: `Scale.steps` realises them\n * in the octaves below and above. A negative step moves the cell down. It\n * returns `[]` if the cell is empty or has a non-integer, or if the step\n * isn't an integer or the count isn't a non-negative integer.\n *\n * Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Figs 3.12-3.13\n * (groups of three and four notes, and thirds, through a scale) and Figs\n * 5.5-5.10 (the same patterns over a pentatonic); Jerry Coker, *The Teaching\n * of Jazz*, \"digital patterns\" (1-2-3-5, 1-3-5-3… moved to each chord).\n * @param cell the indexes of the first group (`[0, 1, 2, 4]` is 1-2-3-5)\n * @param step how far each group moves from the one before\n * @param count the number of groups\n * @returns the groups of indexes\n * @example\n * Pattern.sequence([0, 1, 2, 3], 1, 3) // => [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]\n * Pattern.sequence([0, 2], 1, 4) // => [[0, 2], [1, 3], [2, 4], [3, 5]]\n * Pattern.sequence([0, 1, 2], 1, 3).flat().map(Scale.steps(\"C4 major pentatonic\")) // => [\"C4\", \"D4\", \"E4\", \"D4\", \"E4\", \"G4\", \"E4\", \"G4\", \"A4\"]\n */\nexport function sequence(\n cell: number[],\n step: number,\n count: number,\n): number[][] {\n if (!isIndexList(cell) || cell.length === 0) return [];\n if (!isIndex(step) || !isCount(count)) return [];\n return Array.from({ length: count }, (_, i) =>\n cell.map((index) => index + i * step),\n );\n}\n\n/**\n * Reverse every other group, starting with the second: the first group stays\n * as it is, the second is played backwards, and so on. A sequence of thirds\n * becomes thirds alternately up and down.\n *\n * It returns new groups, or `[]` if the groups aren't arrays of integers.\n *\n * Sources: Tim Richards, *Exploring Jazz Piano*, vol. 1, Fig 5.11\n * (pentatonic \"thirds\" alternately up and down, \"obtained by reversing the\n * direction of every other pair of notes\").\n * @param groups the groups of indexes (from `Pattern.sequence`)\n * @returns the groups, every other one reversed\n * @example\n * Pattern.alternate([[0, 2], [1, 3], [2, 4], [3, 5]]) // => [[0, 2], [3, 1], [2, 4], [5, 3]]\n */\nexport function alternate(groups: number[][]): number[][] {\n if (!Array.isArray(groups) || !groups.every(isIndexList)) return [];\n return groups.map((group, i) => (i % 2 === 0 ? [...group] : reversed(group)));\n}\n\n/**\n * Repeat each index a number of times, in place: `[0, 1]` twice is\n * `[0, 0, 1, 1]`. Over an arpeggio, it gives the doubled orders. (To repeat\n * the whole list, use `Pattern.sequence` with a step of 0.)\n *\n * It returns `[]` if the list isn't an array of integers or the times isn't\n * a non-negative integer.\n * @param indexes the indexes\n * @param times how many times each index is played\n * @returns the indexes, each repeated\n * @example\n * Pattern.repeat([0, 1, 2], 2) // => [0, 0, 1, 1, 2, 2]\n * Pattern.repeat(Pattern.arpeggio(\"down\", 3), 2) // => [2, 2, 1, 1, 0, 0]\n */\nexport function repeat(indexes: number[], times: number): number[] {\n if (!isIndexList(indexes) || !isCount(times)) return [];\n return indexes.flatMap((index) => Array(times).fill(index));\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tonaljs/pattern",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Note patterns: arpeggio orders and melodic sequences as lists of indexes",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"arpeggio",
|
|
7
|
+
"pattern",
|
|
8
|
+
"sequence",
|
|
9
|
+
"music",
|
|
10
|
+
"theory"
|
|
11
|
+
],
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"module": "dist/index.mjs",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@tonaljs/chord": "6.8.1",
|
|
21
|
+
"@tonaljs/scale": "4.17.1"
|
|
22
|
+
},
|
|
23
|
+
"author": "danigb@gmail.com",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsdown"
|
|
30
|
+
}
|
|
31
|
+
}
|