Skip to content

Commit

Permalink
add experimental dur function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubobubobubobubo committed Nov 5, 2023
1 parent 578cc4e commit 2ad2f8e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/ArrayExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Check failure on line 199 in src/ArrayExtensions.ts

View workflow job for this annotation

GitHub Actions / Build

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
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.
Expand Down

0 comments on commit 2ad2f8e

Please sign in to comment.