adding new important method

This commit is contained in:
2023-08-13 12:03:53 +02:00
parent 8aa4255cff
commit 0cb03c7473
3 changed files with 14 additions and 6 deletions

View File

@ -645,12 +645,12 @@ export class UserAPI {
return this.app.clock.time_position.beat
}
get t_beat(): number {
get ebeat(): number {
/**
* Returns the current beat number since the origin of time
* TODO: fix! Why is this not working?
*/
return Math.floor(this.app.clock.tick / this.app.clock.ppqn)
return this.app.clock.beats_since_origin
}

View File

@ -47,7 +47,7 @@ export class Clock {
this.time_position = { bar: 0, beat: 0, pulse: 0 }
this.bpm = 120;
this.time_signature = [4, 4];
this.ppqn = 48*2;
this.ppqn = 48;
ctx.audioWorklet.addModule(TransportProcessor).then((e) => {
this.transportNode = new TransportNode(ctx, {}, this.app);
this.transportNode.connect(ctx.destination);
@ -65,6 +65,10 @@ export class Clock {
return this.time_signature[0];
}
get beats_since_origin(): number {
return (this.time_position.bar - 1) * this.beats_per_bar + this.time_position.beat;
}
get pulse_duration(): number {
/**
* Returns the duration of a pulse in seconds.

View File

@ -1,6 +1,10 @@
import type { Editor } from './main';
import type { File } from './AppSettings';
function codeInterceptor(code: string) {
return code.replace(/->/g, "&&")
}
const delay = (ms: number) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms));
const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> => {
@ -15,7 +19,7 @@ const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> =>
*/
return new Promise((resolve, _) => {
try {
Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api);
Function(`with (this) {try{${codeInterceptor(code)}} catch (e) {console.log(e)}};`).call(application.api);
resolve(true);
} catch (error) {
console.log(error);
@ -43,7 +47,7 @@ export const tryEvaluate = async (
code.evaluations!++;
const isCodeValid = await Promise.race([tryCatchWrapper(
application,
`let i = ${code.evaluations};` + code.candidate,
`let i = ${code.evaluations};` + codeInterceptor(code.candidate as string),
), delay(timeout)]);
if (isCodeValid) {
@ -66,7 +70,7 @@ export const evaluate = async (application: Editor, code: File, timeout = 1000):
* @returns A promise that resolves to void
*/
try {
await Promise.race([tryCatchWrapper(application, code.committed as string), delay(timeout)]);
await Promise.race([tryCatchWrapper(application, codeInterceptor(code.committed as string)), delay(timeout)]);
if (code.evaluations)
code.evaluations++;
} catch (error) {