Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Bubobubobubobubo/Topos
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubobubobubobubo committed Dec 19, 2023
2 parents b47d041 + 678b330 commit d977f2e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ export class UserAPI {
return this.counters[name].value;
};
$ = this.counter;
count = this.counter;

// =============================================================
// Iterator functions (for loops, with evaluation count, etc...)
Expand Down Expand Up @@ -1737,12 +1738,21 @@ export class UserAPI {
* @param step - The step value of the array
* @returns An array of values between start and end, with a given step
*/
function countPlaces(num: number) {
var text = num.toString();
var index = text.indexOf(".");
return index == -1 ? 0 : (text.length - index - 1);
}
const result: number[] = [];

if ((end > start && step > 0) || (end < start && step < 0)) {
for (let value = start; value <= end; value += step) {
result.push(value);
}
} else if((end > start && step < 0) || (end < start && step > 0)) {
for (let value = start; value >= end; value -= step) {
result.push(parseFloat(value.toFixed(countPlaces(step))));
}
} else {
console.error("Invalid range or step provided.");
}
Expand Down
13 changes: 13 additions & 0 deletions src/documentation/patterns/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ beat(1)::sound(['kick', 'fsnare'].dur(3, 1))
true,
)}
## Iterating over lists
- <ic>counter(name,limit?,step?)</ic>: return the next value on the list based on counter value. The limit is optional and defaults to the length of the list. The step is optional and defaults to 1. Setting / changing limit will reset the counter.
- <ic>$(name,limit?,step?)</ic>: shorter alias for the counter.
${makeExample(
"Using counter to iterate over a list",
`
beat(0.5) :: sound("bd").gain(line(0,1,0.01).$("ramp")).out()
`,
true,
)}
## Manipulating notes and scales
- <ic>pitch()</ic>: convert a list of integers to pitch classes
Expand Down
25 changes: 25 additions & 0 deletions src/extensions/ArrayExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ declare global {
gen(min: number, max: number, times: number): number[];
sometimes(func: Function): number[];
apply(func: Function): number[];
counter(name: string | number, limit?: number, step?: number): number[]
$(name: string | number, limit?: number, step?: number): number[];
}
}

Expand Down Expand Up @@ -398,8 +400,31 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[Math.floor(api.randomGen() * this.length)];
};
Array.prototype.rand = Array.prototype.random;

Array.prototype.counter = function(
name: string | number,
limit?: number,
step?: number) {
/**
* @param n - Returns next item in array until the end, then returns the last value.
*
* @returns the shifted array
*/
const idx = api.counter(name,limit,step);
if(limit) {
return this[idx % this.length];
} else if(idx < this.length) {
return this[idx];
} else {
return this[this.length - 1];
}
};
Array.prototype.$ = Array.prototype.counter;

};



Array.prototype.scale = function (
scale: string = "major",
base_note: number = 0,
Expand Down

0 comments on commit d977f2e

Please sign in to comment.