Audio samples are now played as one shots in the documentation

This commit is contained in:
2023-08-27 22:14:12 +02:00
parent 26099758c0
commit 9792d4458e
3 changed files with 24 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import { seededRandom } from "zifferjs"; import { seededRandom } from "zifferjs";
import { MidiConnection } from "./IO/MidiConnection"; import { MidiConnection } from "./IO/MidiConnection";
import { tryEvaluate } from "./Evaluator"; import { tryEvaluate, evaluateOnce } from "./Evaluator";
import { DrunkWalk } from "./Utils/Drunk"; import { DrunkWalk } from "./Utils/Drunk";
import { scale } from "./Scales"; import { scale } from "./Scales";
import { Editor } from "./main"; import { Editor } from "./main";
@ -80,6 +80,13 @@ export class UserAPI {
); );
}; };
_playDocExampleOnce = (code?: string) => {
this.play();
console.log("Executing documentation example: " + this.app.selectedExample);
evaluateOnce(this.app, code as string);
};
_all_samples = (): object => { _all_samples = (): object => {
return soundMap.get(); return soundMap.get();
}; };

View File

@ -22,14 +22,14 @@ const samples_to_markdown = (application: Editor) => {
// Adding new examples for each sample folder! // Adding new examples for each sample folder!
const codeId = `sampleExample${i}`; const codeId = `sampleExample${i}`;
application.api.codeExamples[codeId] = `mod(.5) :: sound("${keys[i]}").n(irand(1,100)).end(1).out()`; application.api.codeExamples[codeId] = `sound("${keys[i]}").n(irand(1, 5)).end(1).out()`;
// @ts-ignore // @ts-ignore
const howMany = samples[keys[i]].data.samples.length; const howMany = samples[keys[i]].data.samples.length;
markdownList += ` markdownList += `
<button <button
class="hover:bg-neutral-500 inline px-4 py-2 bg-neutral-700 text-orange-300 text-xl" class="hover:bg-neutral-500 inline px-4 py-2 bg-neutral-700 text-orange-300 text-xl"
onclick="app.api._playDocExample(app.api.codeExamples['${codeId}'])" onclick="app.api._playDocExampleOnce(app.api.codeExamples['${codeId}'])"
> >
${keys[i]} ${keys[i]}
<b class="text-white">(${howMany})</b> <b class="text-white">(${howMany})</b>

View File

@ -95,3 +95,17 @@ export const evaluate = async (
console.log(error); console.log(error);
} }
}; };
export const evaluateOnce = async (
application: Editor,
code: string
): Promise<void> => {
/**
* Evaluates the code once without any caching or error-handling mechanisms besides the tryCatchWrapper.
*
* @param application - The application object that contains the Editor API.
* @param code - The code to be evaluated.
* @returns A promise that resolves when the code has been evaluated.
*/
await tryCatchWrapper(application, code);
};