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; 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; 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()}: ${ 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 { compose, removeUndefineds, flatten, id, listRange, curry, mod, numeralArgs, parseNumeral } from './util.mjs';
import drawLine from './drawLine.mjs'; import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs';
let stringParser; let stringParser;
// parser is expected to turn a string into a pattern // parser is expected to turn a string into a pattern
@ -1328,22 +1329,25 @@ export class Pattern {
.unit('c') .unit('c')
.slow(factor); .slow(factor);
} }
onTrigger(onTrigger) { onTrigger(onTrigger, dominant = true) {
return this._withHap((hap) => hap.setContext({ ...hap.context, onTrigger }));
}
log(func = id) {
return this._withHap((hap) => return this._withHap((hap) =>
hap.setContext({ hap.setContext({
...hap.context, ...hap.context,
onTrigger: (...args) => { onTrigger: (...args) => {
if (hap.context.onTrigger) { if (!dominant && hap.context.onTrigger) {
hap.context.onTrigger(...args); 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) { logValues(func = id) {
return this.log((_, hap) => func(hap.value)); return this.log((_, hap) => func(hap.value));
} }

View file

@ -17,12 +17,14 @@ export function repl({
interval, interval,
onTrigger: async (hap, deadline, duration) => { onTrigger: async (hap, deadline, duration) => {
try { try {
if (!hap.context.onTrigger) { if (!hap.context.onTrigger || !hap.context.dominantTrigger) {
return await defaultOutput(hap, deadline, duration); 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) { } catch (err) {
logger(`[cyclist] error: ${err.message}`, 'error'); 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, interval: e,
onTrigger: async (E, C, x) => { onTrigger: async (E, C, x) => {
try { try {
if (!E.context.onTrigger) (!E.context.onTrigger || !E.context.dominantTrigger) && await t(E, C, x), E.context.onTrigger && await E.context.onTrigger(a() + C, E, a(), 1);
return await t(E, C, x);
const I = 1;
return await E.context.onTrigger(a() + C, E, a(), I);
} catch (I) { } catch (I) {
Le(`[cyclist] error: ${I.message}`, "error"); 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 highlights.current = highlights.current.concat(haps); // add potential new onsets
view.dispatch({ effects: setHighlights.of(highlights.current) }); // highlight all still active + new active haps view.dispatch({ effects: setHighlights.of(highlights.current) }); // highlight all still active + new active haps
} catch (err) { } catch (err) {
// console.log('error in updateHighlights', err);
view.dispatch({ effects: setHighlights.of([]) }); view.dispatch({ effects: setHighlights.of([]) });
} }
frame = requestAnimationFrame(updateHighlights); frame = requestAnimationFrame(updateHighlights);

View file

@ -43,3 +43,4 @@ currently broken / buggy:
- [x] highlighting seems too late (off by latency ?) - [x] highlighting seems too late (off by latency ?)
- [x] highlighting sometimes drops highlights (zeldasRescue first note) - [x] highlighting sometimes drops highlights (zeldasRescue first note)
- [ ] highlighting still sometimes drops highlights (zeldasRescue somtimes) - [ ] 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', initialCode: '// LOADING',
defaultOutput: webaudioOutput, defaultOutput: webaudioOutput,
getTime, getTime,
autolink: true autolink: true,
}); });
// init code // init code
@ -182,14 +182,16 @@ function App() {
const handleTogglePlay = async () => { const handleTogglePlay = async () => {
await getAudioContext().resume(); // fixes no sound in ios webkit await getAudioContext().resume(); // fixes no sound in ios webkit
if (!started) { if (!started) {
logger('[repl] started. tip: you can also start by pressing ctrl+enter', 'highlight');
activateCode(); activateCode();
} else { } else {
logger('[repl] stopped. tip: you can also stop by pressing ctrl+dot', 'highlight');
stop(); stop();
} }
}; };
const handleUpdate = () => { const handleUpdate = () => {
isDirty && activateCode(); 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 () => { const handleShuffle = async () => {