UNPKG

2.12 kBJavaScriptView Raw
1import { Loop } from "./Loop.js";
2import { PatternGenerator } from "./PatternGenerator.js";
3import { optionsFromArguments } from "../core/util/Defaults.js";
4import { noOp } from "../core/util/Interface.js";
5/**
6 * Pattern arpeggiates between the given notes
7 * in a number of patterns.
8 * @example
9 * const pattern = new Tone.Pattern((time, note) => {
10 * // the order of the notes passed in depends on the pattern
11 * }, ["C2", "D4", "E5", "A6"], "upDown");
12 * @category Event
13 */
14export class Pattern extends Loop {
15 constructor() {
16 const options = optionsFromArguments(Pattern.getDefaults(), arguments, [
17 "callback",
18 "values",
19 "pattern",
20 ]);
21 super(options);
22 this.name = "Pattern";
23 this.callback = options.callback;
24 this._values = options.values;
25 this._pattern = PatternGenerator(options.values.length, options.pattern);
26 this._type = options.pattern;
27 }
28 static getDefaults() {
29 return Object.assign(Loop.getDefaults(), {
30 pattern: "up",
31 values: [],
32 callback: noOp,
33 });
34 }
35 /**
36 * Internal function called when the notes should be called
37 */
38 _tick(time) {
39 const index = this._pattern.next();
40 this._index = index.value;
41 this._value = this._values[index.value];
42 this.callback(time, this._value);
43 }
44 /**
45 * The array of events.
46 */
47 get values() {
48 return this._values;
49 }
50 set values(val) {
51 this._values = val;
52 // reset the pattern
53 this.pattern = this._type;
54 }
55 /**
56 * The current value of the pattern.
57 */
58 get value() {
59 return this._value;
60 }
61 /**
62 * The current index of the pattern.
63 */
64 get index() {
65 return this._index;
66 }
67 /**
68 * The pattern type.
69 */
70 get pattern() {
71 return this._type;
72 }
73 set pattern(pattern) {
74 this._type = pattern;
75 this._pattern = PatternGenerator(this._values.length, this._type);
76 }
77}
78//# sourceMappingURL=Pattern.js.map
\No newline at end of file