From 2ad2f8e3233299f1ed0d2398c7dc2d8018176986 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 5 Nov 2023 02:18:55 +0100 Subject: [PATCH] add experimental dur function --- src/ArrayExtensions.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/ArrayExtensions.ts b/src/ArrayExtensions.ts index 575cb96..9715b66 100644 --- a/src/ArrayExtensions.ts +++ b/src/ArrayExtensions.ts @@ -18,6 +18,7 @@ declare global { repeatEven(amount: number): T; repeatOdd(amount: number): T; beat(division: number): T; + dur(durations: number[]): T; b(division: number): T; bar(): T; pick(): T; @@ -170,6 +171,46 @@ export const makeArrayExtensions = (api: UserAPI) => { }; Array.prototype.b = Array.prototype.beat; + // Array.prototype.dur = function(...durations) { + // const timepos = api.app.clock.pulses_since_origin; + // const ppqn = api.ppqn(); + // let adjustedDurations = []; + // for (let i = 0; i < this.length; i++) { + // adjustedDurations.push(durations[i % durations.length]); + // } + // let cumulativeDuration = 0; + // let totalDuration = adjustedDurations.reduce((acc, duration) => acc + duration * ppqn, 0); + // let modulatedTimePos = timepos % totalDuration; + // for (let i = 0; i < this.length; i++) { + // const valueDuration = adjustedDurations[i] as any * ppqn; + // if (modulatedTimePos < cumulativeDuration + valueDuration) { + // return this[i]; + // } + // cumulativeDuration += valueDuration; + // } + // // This point should not be reached if durations are correctly specified + // throw new Error('Durations array does not match the pattern length.'); + // }; + + Array.prototype.dur = function(...durations) { + const timepos = api.app.clock.pulses_since_origin; + const ppqn = api.ppqn(); + const adjustedDurations = this.map((_, index) => durations[index % durations.length]); + const totalDurationInPulses = adjustedDurations.reduce((acc, duration) => acc + duration * ppqn, 0); + const loopPosition = timepos % totalDurationInPulses; + let cumulativeDuration = 0; + for (let i = 0; i < this.length; i++) { + const valueDurationInPulses = adjustedDurations[i] as any * ppqn; + cumulativeDuration += valueDurationInPulses; + if (loopPosition < cumulativeDuration) { + console.log(`Hit on pulse: ${loopPosition}`); + return this[i]; + } + } + throw new Error('Durations array does not match the pattern length.'); + }; + + Array.prototype.shuffle = function() { /** * Shuffles the array in place.