From e86b71ec78988cf7f60c8b4201f74458e8fcd565 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Tue, 15 Aug 2023 17:30:35 +0200 Subject: [PATCH] experimental sequencer --- src/API.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/Clock.ts | 2 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/API.ts b/src/API.ts index d0b8fed..0d2fe2c 100644 --- a/src/API.ts +++ b/src/API.ts @@ -433,6 +433,56 @@ export class UserAPI { return btoa(JSON.stringify(pattern)); } + public seqmod(...input: any[]) { + if (cache.has(this._sequence_key_generator(input))) { + let sequence = cache.get( + this._sequence_key_generator(input) + ) as Pattern; + + sequence.options.currentIteration++; + + if (sequence.options.currentIteration === sequence.options.nextTarget) { + sequence.options.index++; + sequence.options.nextTarget = input[sequence.options.index % input.length]; + sequence.options.currentIteration = 0; + } + + cache.set(this._sequence_key_generator(input), { + pattern: input as any[], + options: sequence.options + }); + + return sequence.options.currentIteration === 0; + + } else { + let pattern_options = { + index: 0, nextTarget: input[0], + currentIteration: 0 + }; + if (typeof input[input.length - 1] === "object") { + pattern_options = { + ...input.pop(), + ...pattern_options as object + }; + } + + pattern_options.currentIteration++; + + if (pattern_options.currentIteration === pattern_options.nextTarget) { + pattern_options.index++; + pattern_options.nextTarget = input[pattern_options.index % input.length]; + pattern_options.currentIteration = 0; + } + + cache.set(this._sequence_key_generator(input), { + pattern: input as any[], + options: pattern_options + }); + + return pattern_options.currentIteration === 0; + } + } + public seq(...input: any[]) { /** * Returns a value in a sequence stored using an LRU Cache. @@ -447,9 +497,7 @@ export class UserAPI { * containing options for the sequence function. * @returns A value in a sequence stored using an LRU Cache */ - if (cache.has(this._sequence_key_generator(input))) { - let sequence = cache.get(this._sequence_key_generator(input)) as Pattern; sequence.options.index += 1; cache.set(this._sequence_key_generator(input), sequence); @@ -465,7 +513,6 @@ export class UserAPI { pattern: input as any[], options: pattern_options }); - return cache.get(this._sequence_key_generator(input)); } } diff --git a/src/Clock.ts b/src/Clock.ts index 09e04c4..82827d4 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -47,7 +47,7 @@ export class Clock { this.time_position = { bar: 0, beat: 0, pulse: 0 } this.bpm = 120; this.time_signature = [4, 4]; - this.ppqn = 8; + this.ppqn = 48; ctx.audioWorklet.addModule(TransportProcessor).then((e) => { this.transportNode = new TransportNode(ctx, {}, this.app); this.transportNode.connect(ctx.destination);