support midi clock via "clock" control

(not on desktop yet)
This commit is contained in:
Felix Roos 2023-09-27 21:26:24 +02:00
parent cf72e3bba5
commit 68ab43b3ab
2 changed files with 18 additions and 2 deletions

View File

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

View File

@ -90,6 +90,13 @@ Pattern.prototype.midi = function (output) {
enableWebMidi({ enableWebMidi({
onEnabled: ({ outputs }) => { onEnabled: ({ outputs }) => {
const device = getDevice(output, outputs); const device = getDevice(output, outputs);
if (typeof window !== 'undefined') {
window.addEventListener('message', (e) => {
if (e.data === 'strudel-stop') {
device.sendStop();
}
});
}
const otherOutputs = outputs.filter((o) => o.name !== device.name); const otherOutputs = outputs.filter((o) => o.name !== device.name);
logger( logger(
`Midi enabled! Using "${device.name}". ${ `Midi enabled! Using "${device.name}". ${
@ -103,6 +110,7 @@ Pattern.prototype.midi = function (output) {
return this.onTrigger((time, hap, currentTime, cps) => { return this.onTrigger((time, hap, currentTime, cps) => {
if (!WebMidi.enabled) { if (!WebMidi.enabled) {
console.log('not enabled');
return; return;
} }
const device = getDevice(output, WebMidi.outputs); const device = getDevice(output, WebMidi.outputs);
@ -113,7 +121,7 @@ Pattern.prototype.midi = function (output) {
const timeOffsetString = `+${offset}`; 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, clock } = hap.value;
const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity
// note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length // note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length
@ -125,7 +133,7 @@ Pattern.prototype.midi = function (output) {
time: timeOffsetString, time: timeOffsetString,
}); });
} }
if (ccv && ccn) { if (ccv !== undefined && ccn !== undefined) {
if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) {
throw new Error('expected ccv to be a number between 0 and 1'); throw new Error('expected ccv to be a number between 0 and 1');
} }
@ -135,5 +143,12 @@ Pattern.prototype.midi = function (output) {
const scaled = Math.round(ccv * 127); const scaled = Math.round(ccv * 127);
device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString }); device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString });
} }
const begin = hap.whole.begin + 0;
if (begin === 0) {
device.sendStart({ time: timeOffsetString });
}
if (clock) {
device.sendClock({ time: timeOffsetString });
}
}); });
}; };