time clock improvements

This commit is contained in:
Jade Rowland 2023-08-19 01:08:41 -04:00
parent 10bce85561
commit b5b866a894

View File

@ -6,8 +6,8 @@ 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 } from '@strudel.cycles/core';
import { getAudioContext } from '@strudel.cycles/webaudio';
import { noteToMidi } 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: // if you use WebMidi from outside of this package, make sure to import that instance:
export const { WebMidi } = _WebMidi; export const { WebMidi } = _WebMidi;
@ -47,7 +47,8 @@ export function enableWebMidi(options = {}) {
// const outputByName = (name: string) => WebMidi.getOutputByName(name); // const outputByName = (name: string) => WebMidi.getOutputByName(name);
const outputByName = (name) => WebMidi.getOutputByName(name); const outputByName = (name) => WebMidi.getOutputByName(name);
let midiReady; let midiReady = false;
let prevTime = 0;
// output?: string | number, outputs: typeof WebMidi.outputs // output?: string | number, outputs: typeof WebMidi.outputs
function getDevice(output, outputs) { function getDevice(output, outputs) {
@ -60,7 +61,9 @@ function getDevice(output, outputs) {
if (typeof output === 'string') { if (typeof output === 'string') {
return outputByName(output); return outputByName(output);
} }
return outputs[0]; // attempt to default to IAC device if none is specified
const IACOutput = outputs.find((output) => output.name.includes('IAC'));
return IACOutput ?? outputs[0];
} }
// Pattern.prototype.midi = function (output: string | number, channel = 1) { // Pattern.prototype.midi = function (output: string | number, channel = 1) {
@ -68,21 +71,6 @@ Pattern.prototype.midi = function (output) {
if (!supportsMidi()) { if (!supportsMidi()) {
throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`); throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`);
} }
/* await */ enableWebMidi({
onConnected: ({ outputs }) =>
logger(`Midi device connected! Available: ${outputs.map((o) => `'${o.name}'`).join(', ')}`),
onDisconnected: ({ outputs }) =>
logger(`Midi device disconnected! Available: ${outputs.map((o) => `'${o.name}'`).join(', ')}`),
onReady: ({ outputs }) => {
const device = getDevice(output, outputs);
const otherOutputs = outputs
.filter((o) => o.name !== device.name)
.map((o) => `'${o.name}'`)
.join(' | ');
midiReady = true;
logger(`Midi connected! Using "${device.name}". ${otherOutputs ? `Also available: ${otherOutputs}` : ''}`);
},
});
if (isPattern(output)) { if (isPattern(output)) {
throw new Error( throw new Error(
`.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${ `.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${
@ -90,11 +78,34 @@ Pattern.prototype.midi = function (output) {
}')`, }')`,
); );
} }
return this.onTrigger((time, hap) => {
if (midiReady === false) {
enableWebMidi({
onConnected: ({ outputs }) => {
const device = getDevice(output, outputs);
const otherOutputs = outputs
.filter((o) => o.name !== device.name)
.map((o) => `'${o.name}'`)
.join(' | ');
midiReady = true;
logger(`Midi connected! Using "${device.name}". ${otherOutputs ? `Also available: ${otherOutputs}` : ''}`);
},
onDisconnected: ({ outputs }) =>
logger(`Midi device disconnected! Available: ${outputs.map((o) => `'${o.name}'`).join(', ')}`),
});
}
return this.onTrigger((time, hap, currentTime, cps = 1) => {
if (!midiReady) { if (!midiReady) {
return; return;
} }
const device = getDevice(output, WebMidi.outputs); const device = getDevice(output, WebMidi.outputs);
const current = performance.now();
// console.log(current - prevTime);
prevTime = current;
if (!device) { if (!device) {
throw new Error( throw new Error(
`🔌 MIDI device '${output ? output : ''}' not found. Use one of ${WebMidi.outputs `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${WebMidi.outputs
@ -104,21 +115,21 @@ Pattern.prototype.midi = function (output) {
} }
hap.ensureObjectValue(); hap.ensureObjectValue();
// calculate time const offset = (time - currentTime) * 1000;
const timingOffset = WebMidi.time - getAudioContext().getOutputTimestamp().contextTime * 1000;
time = time * 1000 + timingOffset; // passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output
const timeOffsetString = `+${offset}`;
// destructure value // destructure value
const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value;
const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity
const duration = hap.duration.valueOf() * 1000 - 5; const duration = Math.round(hap.duration.valueOf() * 1000 - 5);
if (note != null) { if (note != null) {
const midiNumber = typeof note === 'number' ? note : noteToMidi(note); const midiNumber = typeof note === 'number' ? note : noteToMidi(note);
device.playNote(midiNumber, midichan, { const midiNote = new Note(midiNumber, { attack: velocity, duration });
time, device.playNote(midiNote, midichan, {
duration, time: timeOffsetString,
attack: velocity,
}); });
} }
if (ccv && ccn) { if (ccv && ccn) {
@ -129,7 +140,7 @@ Pattern.prototype.midi = function (output) {
throw new Error('expected ccn to be a number or a string'); throw new Error('expected ccn to be a number or a string');
} }
const scaled = Math.round(ccv * 127); const scaled = Math.round(ccv * 127);
device.sendControlChange(ccn, scaled, midichan, { time }); device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString });
} }
}); });
}; };