diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index d1a9ce7f..6cac6e54 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -1066,7 +1066,6 @@ const generic_params = [ */ ['waveloss'], // TODO: midi effects? - ['midicmd'], ['dur'], // ['modwheel'], ['expression'], diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index e3efb427..598280bc 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -2343,3 +2343,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 07e6e65c..ab59c2e4 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th */ import * as _WebMidi from 'webmidi'; -import { Pattern, isPattern, logger } from '@strudel.cycles/core'; +import { Pattern, isPattern, logger, ref } from '@strudel.cycles/core'; import { noteToMidi } from '@strudel.cycles/core'; import { Note } from 'webmidi'; // if you use WebMidi from outside of this package, make sure to import that instance: @@ -15,8 +15,8 @@ function supportsMidi() { return typeof navigator.requestMIDIAccess === 'function'; } -function getMidiDeviceNamesString(outputs) { - return outputs.map((o) => `'${o.name}'`).join(' | '); +function getMidiDeviceNamesString(devices) { + return devices.map((o) => `'${o.name}'`).join(' | '); } export function enableWebMidi(options = {}) { @@ -52,30 +52,28 @@ export function enableWebMidi(options = {}) { }); }); } -// const outputByName = (name: string) => WebMidi.getOutputByName(name); -const outputByName = (name) => WebMidi.getOutputByName(name); -// output?: string | number, outputs: typeof WebMidi.outputs -function getDevice(output, outputs) { - if (!outputs.length) { +function getDevice(indexOrName, devices) { + if (!devices.length) { throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); } - if (typeof output === 'number') { - return outputs[output]; + if (typeof indexOrName === 'number') { + return devices[indexOrName]; } - if (typeof output === 'string') { - return outputByName(output); + const byName = (name) => devices.find((output) => output.name.includes(name)); + if (typeof indexOrName === 'string') { + return byName(indexOrName); } // attempt to default to first IAC device if none is specified - const IACOutput = outputs.find((output) => output.name.includes('IAC')); - const device = IACOutput ?? outputs[0]; + const IACOutput = byName('IAC'); + const device = IACOutput ?? devices[0]; if (!device) { throw new Error( - `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${getMidiDeviceNamesString(WebMidi.outputs)}`, + `🔌 MIDI device '${device ? device : ''}' not found. Use one of ${getMidiDeviceNamesString(devices)}`, ); } - return IACOutput ?? outputs[0]; + return IACOutput ?? devices[0]; } // send start/stop messages to outputs when repl starts/stops @@ -164,3 +162,31 @@ Pattern.prototype.midi = function (output) { } }); }; + +let listeners = {}; +const refs = {}; + +export async function midin(input) { + const initial = await enableWebMidi(); // only returns on first init + const device = getDevice(input, WebMidi.inputs); + + if (initial) { + const otherInputs = WebMidi.inputs.filter((o) => o.name !== device.name); + logger( + `Midi enabled! Using "${device.name}". ${ + otherInputs?.length ? `Also available: ${getMidiDeviceNamesString(otherInputs)}` : '' + }`, + ); + refs[input] = {}; + } + 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]; + refs[input] && (refs[input][cc] = v / 127); + }; + device.addListener('midimessage', listeners[input]); + return cc; +}