diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 7e694f49..d29fde3f 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -2336,3 +2336,9 @@ export const fit = register('fit', (pat) => export const { loopAtCps, loopatcps } = register(['loopAtCps', 'loopatcps'], function (factor, cps, pat) { return _loopAt(factor, pat, cps); }); + +/** exposes a custom value at query time. basically allows mutating state without evaluation */ +export const ref = (accessor) => + pure(1) + .withValue(() => reify(accessor())) + .innerJoin(); diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index 831c3afd..95af837e 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -8,7 +8,6 @@ import * as _WebMidi from 'webmidi'; import { Pattern, isPattern, logger } from '@strudel.cycles/core'; import { noteToMidi } from '@strudel.cycles/core'; import { Note } from 'webmidi'; -import EventEmitter from 'events'; // if you use WebMidi from outside of this package, make sure to import that instance: export const { WebMidi } = _WebMidi; @@ -137,7 +136,10 @@ Pattern.prototype.midi = function (output) { }); }; -export async function midiIn(input) { +let listeners = {}; +const refs = {}; + +export async function midin(input) { console.log('midi in...'); const initial = await enableWebMidi(); // only returns on first init const device = getDevice(input, WebMidi.inputs); @@ -149,12 +151,18 @@ export async function midiIn(input) { otherInputs?.length ? `Also available: ${getMidiDeviceNamesString(otherInputs)}` : '' }`, ); + refs[input] = {}; } - return (fn) => { - device.addListener(EventEmitter.ANY_EVENT, (...args) => { - console.log('event!', args); - fn(args); - }); - return; + const cc = (cc) => ref(() => refs[input][cc] || 0); + + listeners[input] && device.removeListener('midimessage', listeners[input]); + listeners[input] = (e) => { + const cc = e.dataBytes[0]; + const v = e.dataBytes[1]; + console.log(cc, v); + refs[input][cc] = v / 127; }; + device.addListener('midimessage', listeners[input]); + //return { cc }; + return cc; }