Added random array generator and force eval using Shift+Ctrl+Enter

This commit is contained in:
2023-09-08 17:18:30 +03:00
parent d2161eb5bc
commit 61096a5510
5 changed files with 40 additions and 8 deletions

View File

@ -26,6 +26,7 @@ declare global {
in(value: T): boolean;
square(): number[];
sqrt(): number[];
gen(min: number, max: number, times: number): number[];
}
}
@ -101,6 +102,20 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[(api.app.clock.beats_since_origin / beat) % this.length];
};
Array.prototype.gen = function (min: number, max: number, times: number) {
/**
* Returns an array of random numbers.
* @param min - The minimum value of the random numbers
* @param max - The maximum value of the random numbers
* @param times - The number of random numbers to generate
* @returns An array of random numbers
*/
if(times < 1) {
return [];
}
return Array.from({ length: times }, () => Math.floor(api.randomGen() * (max - min + 1)) + min);
};
Array.prototype.bar = function () {
/**
* Returns an element from an array based on the current bar.
@ -137,7 +152,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
let currentIndex = this.length,
randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
randomIndex = Math.floor(api.randomGen() * currentIndex);
currentIndex--;
[this[currentIndex], this[randomIndex]] = [
this[randomIndex],
@ -201,7 +216,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
}
for (let i = 0; i < this.length; ) {
const rand = Math.random() * 100;
const rand = api.randomGen() * 100;
if (rand < amount) {
if (this.length > 1) {
this.splice(i, 1);
@ -329,7 +344,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
*
* @returns A random element from the array
*/
return this[Math.floor(Math.random() * this.length)];
return this[Math.floor(api.randomGen() * this.length)];
};
Array.prototype.rand = Array.prototype.random;
};

View File

@ -24,7 +24,7 @@ Every Topos session is composed of several small scripts. A set of scripts is ca
)} to ${key_shortcut("F9")}).
- **the init script** (${key_shortcut(
"Ctrl + I"
)}): _Evaluated on program load_. Used to set up the software the session to the desired state before playing (_bpm_, etc...). You can also access that script using the ${key_shortcut(
)}): _Evaluated on program load_. Used to set up the software the session to the desired state before playing, for example changing bpm or to initialize global variables (See Functions). You can also access that script using the ${key_shortcut(
"F11"
)} key.
- **the note file** (${key_shortcut(

View File

@ -40,9 +40,8 @@ Topos is made to be controlled entirely with a keyboard. It is recommanded to st
| Shortcut | Key | Description |
|----------|-------|------------------------------------------------------------|
|Evaluate|${key_shortcut("Ctrl + Enter")}| Evaluate the current script |
|Local Eval|${key_shortcut("Ctrl + F1")} to ${key_shortcut(
"Ctrl + F9"
)}|Local File Evaluation|
|Local Eval|${key_shortcut("Ctrl + F1")} to ${key_shortcut("Ctrl + F9")}|Local File Evaluation|
|Force Eval|${key_shortcut("Ctrl + Shift + Enter")}|Force evaluation of the current script|
## Special

View File

@ -98,7 +98,7 @@ mod([1,.5,.25].beat()) :: snd('sine')
- <ic>random(index: number)</ic>: pick a random element in the given list.
- <ic>rand(index: number)</ic>: shorter alias for the same method.
- <ic>pick()</ic>: pick a random element in the list.
- <ic>gen(min,max,length)</ic>: generate a list of random numbers between _min_ and _max_ with a given _length_.
${makeExample(
"Sipping some gasoline at the robot bar",
@ -114,6 +114,14 @@ mod([.5, 1].random() / 2) :: snd(
true
)}
${makeExample(
"Generate a list of random numbers",
`
mod(0.5) && sound('arp').freq([].gen(300,600,10).div(3)).out()
`,
true
)}
- <ic>degrade(amount: number)</ic>: removes _n_% of the list elements. Lists can be degraded as long as one element remains. The amount of degradation is given as a percentage.
${makeExample(

View File

@ -358,6 +358,16 @@ export class Editor {
this.flashBackground("#404040", 200);
}
// Shift + Ctrl + Enter: Evaluate the currently visible code block
if (
(event.key === "Enter" && event.shiftKey && event.ctrlKey))
{
event.preventDefault();
this.currentFile().candidate = this.view.state.doc.toString();
tryEvaluate(this, this.currentFile());
this.flashBackground("#404040", 200);
}
// This is the modal to switch between universes
if (event.ctrlKey && event.key === "b") {
event.preventDefault();