From 49b7b20b20eaf00d8129c0eb643b060c74b8de28 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Fri, 8 Sep 2023 16:55:03 +0200 Subject: [PATCH] adding the binrhythm function --- src/API.ts | 15 ++++++++++++++- src/documentation/time.ts | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/API.ts b/src/API.ts index e366308..ce4da5f 100644 --- a/src/API.ts +++ b/src/API.ts @@ -1072,7 +1072,7 @@ export class UserAPI { integral_part = integral_part == 0 ? this.nominator() : integral_part; let decimal_part = Math.floor((beat - integral_part) * this.ppqn() + 1); // This was once revelead to me in a dream - if (decimal_part <= 0) + if (decimal_part <= 0) decimal_part = decimal_part + this.ppqn() * this.nominator(); final_pulses.push( integral_part === this.beat() && this.pulse() === decimal_part @@ -1219,6 +1219,19 @@ export class UserAPI { return tobin[iterator % tobin.length]; }; + public binrhythm = (div: number, n: number): boolean => { + /** + * Returns a binary cycle of size n, divided by div. + * + * @param div - The divisor of the binary cycle + * @param n - The number to convert to binary + * @returns boolean value based on the binary sequence + */ + let convert: string = n.toString(2); + let tobin: boolean[] = convert.split("").map((x: string) => x === "1"); + return this.mod(div) && tobin.div(div); + }; + // ============================================================= // Low Frequency Oscillators // ============================================================= diff --git a/src/documentation/time.ts b/src/documentation/time.ts index b61b39d..93ca4be 100644 --- a/src/documentation/time.ts +++ b/src/documentation/time.ts @@ -182,6 +182,7 @@ rhythm(speed, 7, 12) :: snd('east').n(9).out() - bin(iterator: number, n: number): boolean: a binary rhythm generator. It transforms the given number into its binary representation (_e.g_ 34 becomes 100010). It then returns a boolean value based on the iterator in order to generate a rhythm. +- binrhythm(divisor: number, n: number): boolean: boolean: iterator-less version of the binary rhythm generator. ${makeExample( "Change the integers for a surprise rhythm!", @@ -192,6 +193,15 @@ mod(.5) && bin($(2), 48) && snd('sd').out() true )} +${makeExample( + "binrhythm for fast cool binary rhythms!", + ` +binrhythm(.5, 13) && snd('kick').out() +binrhythm(.5, 18) && snd('sd').out() +`, + true +)} + ${makeExample( "Calling 911", `