can now have multiple triggers

+ Pattern.log now makes sense
This commit is contained in:
Felix Roos 2022-11-12 16:52:00 +01:00
parent 27fb2d2d5b
commit ff5d14fb63
9 changed files with 32 additions and 22 deletions

View File

@ -55,7 +55,8 @@ Fraction.prototype.min = function (other) {
return this.lt(other) ? this : other;
};
Fraction.prototype.show = function () {
Fraction.prototype.show = function (/* excludeWhole = false */) {
// return this.toFraction(excludeWhole);
return this.s * this.n + '/' + this.d;
};

View File

@ -85,9 +85,13 @@ export class Hap {
);
}
showWhole() {
showWhole(compact = false) {
return `${this.whole == undefined ? '~' : this.whole.show()}: ${
typeof this.value === 'object' ? JSON.stringify(this.value) : this.value
typeof this.value === 'object'
? compact
? JSON.stringify(this.value).slice(1, -1).replaceAll('"', '').replaceAll(',', ' ')
: JSON.stringify(this.value)
: this.value
}`;
}

View File

@ -12,6 +12,7 @@ import { unionWithObj } from './value.mjs';
import { compose, removeUndefineds, flatten, id, listRange, curry, mod, numeralArgs, parseNumeral } from './util.mjs';
import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs';
let stringParser;
// parser is expected to turn a string into a pattern
@ -1328,22 +1329,25 @@ export class Pattern {
.unit('c')
.slow(factor);
}
onTrigger(onTrigger) {
return this._withHap((hap) => hap.setContext({ ...hap.context, onTrigger }));
}
log(func = id) {
onTrigger(onTrigger, dominant = true) {
return this._withHap((hap) =>
hap.setContext({
...hap.context,
onTrigger: (...args) => {
if (hap.context.onTrigger) {
if (!dominant && hap.context.onTrigger) {
hap.context.onTrigger(...args);
}
console.log(func(...args));
onTrigger(...args);
},
// we need this to know later if the default trigger should still fire
dominantTrigger: dominant,
}),
);
}
log(func = (_, hap) => `[hap] ${hap.showWhole(true)}`) {
return this.onTrigger((...args) => logger(func(...args)), false);
}
logValues(func = id) {
return this.log((_, hap) => func(hap.value));
}

View File

@ -17,12 +17,14 @@ export function repl({
interval,
onTrigger: async (hap, deadline, duration) => {
try {
if (!hap.context.onTrigger) {
return await defaultOutput(hap, deadline, duration);
if (!hap.context.onTrigger || !hap.context.dominantTrigger) {
await defaultOutput(hap, deadline, duration);
}
if (hap.context.onTrigger) {
const cps = 1;
// call signature of output / onTrigger is different...
await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
}
const cps = 1; // TODO: fix
// call signature of output / onTrigger is different...
return await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
} catch (err) {
logger(`[cyclist] error: ${err.message}`, 'error');
}

File diff suppressed because one or more lines are too long

View File

@ -303,10 +303,7 @@ function eu({
interval: e,
onTrigger: async (E, C, x) => {
try {
if (!E.context.onTrigger)
return await t(E, C, x);
const I = 1;
return await E.context.onTrigger(a() + C, E, a(), I);
(!E.context.onTrigger || !E.context.dominantTrigger) && await t(E, C, x), E.context.onTrigger && await E.context.onTrigger(a() + C, E, a(), 1);
} catch (I) {
Le(`[cyclist] error: ${I.message}`, "error");
}

View File

@ -22,7 +22,6 @@ function useHighlighting({ view, pattern, active, getTime }) {
highlights.current = highlights.current.concat(haps); // add potential new onsets
view.dispatch({ effects: setHighlights.of(highlights.current) }); // highlight all still active + new active haps
} catch (err) {
// console.log('error in updateHighlights', err);
view.dispatch({ effects: setHighlights.of([]) });
}
frame = requestAnimationFrame(updateHighlights);

View File

@ -43,3 +43,4 @@ currently broken / buggy:
- [x] highlighting seems too late (off by latency ?)
- [x] highlighting sometimes drops highlights (zeldasRescue first note)
- [ ] highlighting still sometimes drops highlights (zeldasRescue somtimes)
- [ ] highlighting out of range error is back (delete large chunk at the top while highlighting below is triggered)

View File

@ -134,7 +134,7 @@ function App() {
initialCode: '// LOADING',
defaultOutput: webaudioOutput,
getTime,
autolink: true
autolink: true,
});
// init code
@ -182,14 +182,16 @@ function App() {
const handleTogglePlay = async () => {
await getAudioContext().resume(); // fixes no sound in ios webkit
if (!started) {
logger('[repl] started. tip: you can also start by pressing ctrl+enter', 'highlight');
activateCode();
} else {
logger('[repl] stopped. tip: you can also stop by pressing ctrl+dot', 'highlight');
stop();
}
};
const handleUpdate = () => {
isDirty && activateCode();
logger('Code updated! Tip: You can also update the code by pressing ctrl+enter.');
logger('[repl] code updated! tip: you can also update the code by pressing ctrl+enter', 'highlight');
};
const handleShuffle = async () => {