Skip to content

Commit

Permalink
Added once() method
Browse files Browse the repository at this point in the history
  • Loading branch information
amiika committed Dec 18, 2023
1 parent f46565f commit 331ddab
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export class UserAPI {
public MidiConnection: MidiConnection;
public scale_aid: string | number | undefined = undefined;
public hydra: any;
public onceEvaluator: boolean = true;

load: samples;

constructor(public app: Editor) {
Expand Down Expand Up @@ -912,6 +914,18 @@ export class UserAPI {
// Counter and iteration
// =============================================================

public once = (): boolean => {
/**
* Returns true if the code is being evaluated for the first time.
*
* @returns True if the code is being evaluated for the first time
*/
const firstTime = this.app.api.onceEvaluator;
this.app.api.onceEvaluator = false;

return firstTime;
}

public counter = (
name: string | number,
limit?: number,
Expand Down
2 changes: 2 additions & 0 deletions src/KeyActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const registerOnKeyDown = (app: Editor) => {
if (event.key === "Enter" && event.shiftKey && event.ctrlKey) {
event.preventDefault();
app.currentFile().candidate = app.view.state.doc.toString();
app.api.onceEvaluator = true;
tryEvaluate(app, app.currentFile());
app.flashBackground("#404040", 200);
}
Expand All @@ -114,6 +115,7 @@ export const registerOnKeyDown = (app: Editor) => {
event.preventDefault();
app.api.clearPatternCache();
app.currentFile().candidate = app.view.state.doc.toString();
app.api.onceEvaluator = true;
tryEvaluate(app, app.currentFile());
app.flashBackground("#404040", 200);
}
Expand Down
12 changes: 7 additions & 5 deletions src/documentation/learning/audio_engine/synths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ beat(2) :: sound('zzfx').zzfx([3.62,,452,.16,.1,.21,,2.5,,,403,.05,.29,,,,.17,.3
Topos can also speak using the [Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API). There are two ways to use speech synthesis:
Speech synthesis API can crash your browser if you use it too much. To avoid crashing the calls should be limited using methods like beat() or run it only once using once().
- <ic>speak(text: string, lang: string, voice: number, rate: number, pitch: number, volume: number)</ic>
- <ic>text</ic>: the text you would like to synthesize (_e.g_ <ic>"Wow, Topos can speak!"</ic>).
- <ic>lang</ic>: language code, for example <ic>en</ic> for English, <ic>fr</ic> for French or with the country code for example British English <ic>en-GB</ic>. See supported values from the [list](https://cloud.google.com/speech-to-text/docs/speech-to-text-supported-languages).
Expand All @@ -426,15 +428,15 @@ Topos can also speak using the [Web Speech API](https://developer.mozilla.org/en
${makeExample(
"Hello world!",
`
beat(4) :: speak("Hello world!")
once() && speak("Hello world!")
`,
true,
)}
${makeExample(
"Let's hear people talking about Topos",
`
beat(2) :: speak("Topos!","fr",irand(0,5))
beat(2) && speak("Topos!","fr",irand(0,5))
`,
true,
)}
Expand All @@ -445,7 +447,7 @@ You can also use speech by chaining methods to a string:
${makeExample(
"Foobaba is the real deal",
`
onbeat(4) :: "Foobaba".voice(irand(0,10)).speak()
onbeat(4) && "Foobaba".voice(irand(0,10)).speak()
`,
true,
)}
Expand All @@ -458,7 +460,7 @@ ${makeExample(
const object = ["happy","sad","tired"].pick()
const sentence = subject+" "+verb+" "+" "+object
beat(6) :: sentence.pitch(0).rate(0).voice([0,2].pick()).speak()
beat(6) && sentence.pitch(0).rate(0).voice([0,2].pick()).speak()
`,
true,
)}
Expand All @@ -474,7 +476,7 @@ ${makeExample(
"Flamboyant", "Cosmique", "Croissant!"
];
onbeat(4) :: croissant.bar()
onbeat(4) && croissant.bar()
.lang("fr")
.volume(rand(0.2,2.0))
.rate(rand(.4,.6))
Expand Down
1 change: 1 addition & 0 deletions src/documentation/patterns/probabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ By default chance operators will be evaluated 48 times within a beat. You can ch
- <ic>frequently(beats?: number)</ic>: returns true 90% of the time in given number of beats
- <ic>almostAlways(beats?: number)</ic>: returns true 99% of the time in given number of beats
- <ic>always(beats?: number)</ic>: returns true. Can be handy when switching between different probabilities
- <ic>once()</ic>: returns true once, then false until the code is force evaluated (Shift+Ctrl+Enter)
Examples:
Expand Down

0 comments on commit 331ddab

Please sign in to comment.