Merge pull request #699 from tidalcycles/midi-in

Midi in
This commit is contained in:
Felix Roos 2023-09-28 11:03:09 +02:00 committed by GitHub
commit 8a741952df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 17 deletions

View File

@ -1066,7 +1066,6 @@ const generic_params = [
*/ */
['waveloss'], ['waveloss'],
// TODO: midi effects? // TODO: midi effects?
['midicmd'],
['dur'], ['dur'],
// ['modwheel'], // ['modwheel'],
['expression'], ['expression'],

View File

@ -2343,3 +2343,9 @@ export const fit = register('fit', (pat) =>
export const { loopAtCps, loopatcps } = register(['loopAtCps', 'loopatcps'], function (factor, cps, pat) { export const { loopAtCps, loopatcps } = register(['loopAtCps', 'loopatcps'], function (factor, cps, pat) {
return _loopAt(factor, pat, cps); 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();

View File

@ -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 * 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 { noteToMidi } from '@strudel.cycles/core';
import { Note } from 'webmidi'; import { Note } from 'webmidi';
// if you use WebMidi from outside of this package, make sure to import that instance: // 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'; return typeof navigator.requestMIDIAccess === 'function';
} }
function getMidiDeviceNamesString(outputs) { function getMidiDeviceNamesString(devices) {
return outputs.map((o) => `'${o.name}'`).join(' | '); return devices.map((o) => `'${o.name}'`).join(' | ');
} }
export function enableWebMidi(options = {}) { 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(indexOrName, devices) {
function getDevice(output, outputs) { if (!devices.length) {
if (!outputs.length) {
throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`);
} }
if (typeof output === 'number') { if (typeof indexOrName === 'number') {
return outputs[output]; return devices[indexOrName];
} }
if (typeof output === 'string') { const byName = (name) => devices.find((output) => output.name.includes(name));
return outputByName(output); if (typeof indexOrName === 'string') {
return byName(indexOrName);
} }
// attempt to default to first IAC device if none is specified // attempt to default to first IAC device if none is specified
const IACOutput = outputs.find((output) => output.name.includes('IAC')); const IACOutput = byName('IAC');
const device = IACOutput ?? outputs[0]; const device = IACOutput ?? devices[0];
if (!device) { if (!device) {
throw new Error( 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 // 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;
}