Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add midi clock support #710

Merged
merged 7 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ const generic_params = [
*/
['waveloss'],
// TODO: midi effects?
['midicmd'],
['dur'],
// ['modwheel'],
['expression'],
Expand Down
31 changes: 29 additions & 2 deletions packages/midi/midi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ function getDevice(output, outputs) {
return IACOutput ?? outputs[0];
}

// send start/stop messages to outputs when repl starts/stops
if (typeof window !== 'undefined') {
window.addEventListener('message', (e) => {
if (!WebMidi?.enabled) {
return;
}
if (e.data === 'strudel-stop') {
WebMidi.outputs.forEach((output) => output.sendStop());
}
// cannot start here, since we have no timing info, see sendStart below
});
}

Pattern.prototype.midi = function (output) {
if (isPattern(output)) {
throw new Error(
Expand All @@ -103,6 +116,7 @@ Pattern.prototype.midi = function (output) {

return this.onTrigger((time, hap, currentTime, cps) => {
if (!WebMidi.enabled) {
console.log('not enabled');
return;
}
const device = getDevice(output, WebMidi.outputs);
Expand All @@ -113,7 +127,7 @@ Pattern.prototype.midi = function (output) {
const timeOffsetString = `+${offset}`;

// destructure value
const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value;
const { note, nrpnn, nrpv, ccn, ccv, midichan = 1, midicmd } = hap.value;
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
Expand All @@ -125,7 +139,7 @@ Pattern.prototype.midi = function (output) {
time: timeOffsetString,
});
}
if (ccv && ccn) {
if (ccv !== undefined && ccn !== undefined) {
if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) {
throw new Error('expected ccv to be a number between 0 and 1');
}
Expand All @@ -135,5 +149,18 @@ Pattern.prototype.midi = function (output) {
const scaled = Math.round(ccv * 127);
device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString });
}
if (hap.whole.begin + 0 === 0) {
// we need to start here because we have the timing info
device.sendStart({ time: timeOffsetString });
}
if (['clock', 'midiClock'].includes(midicmd)) {
device.sendClock({ time: timeOffsetString });
} else if (['start'].includes(midicmd)) {
device.sendStart({ time: timeOffsetString });
} else if (['stop'].includes(midicmd)) {
device.sendStop({ time: timeOffsetString });
} else if (['continue'].includes(midicmd)) {
device.sendContinue({ time: timeOffsetString });
}
});
};
1 change: 0 additions & 1 deletion packages/superdough/dspworklet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class MyProcessor extends AudioWorkletProcessor {
if(e.data==='stop') {
this.stopped = true;
} else if(e.data?.dough) {
const deadline = e.data.time-currentTime;
__q.push(e.data)
} else {
msg?.(e.data)
Expand Down
2 changes: 1 addition & 1 deletion packages/superdough/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superdough",
"version": "0.9.8",
"version": "0.9.9",
"description": "simple web audio synth and sampler intended for live coding. inspired by superdirt and webdirt.",
"main": "index.mjs",
"type": "module",
Expand Down
8 changes: 5 additions & 3 deletions website/src/repl/Repl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const supabase = createClient(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBpZHhkc3hwaGxoempuem1pZnRoIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTYyMzA1NTYsImV4cCI6MTk3MTgwNjU1Nn0.bqlw7802fsWRnqU5BLYtmXk_k-D1VFmbkHMywWc15NM',
);

const modules = [
let modules = [
import('@strudel.cycles/core'),
import('@strudel.cycles/tonal'),
import('@strudel.cycles/mini'),
Expand All @@ -45,13 +45,13 @@ const modules = [
import('@strudel.cycles/csound'),
];
if (isTauri()) {
modules.concat([
modules = modules.concat([
import('@strudel/desktopbridge/loggerbridge.mjs'),
import('@strudel/desktopbridge/midibridge.mjs'),
import('@strudel/desktopbridge/oscbridge.mjs'),
]);
} else {
modules.concat([import('@strudel.cycles/midi'), import('@strudel.cycles/osc')]);
modules = modules.concat([import('@strudel.cycles/midi'), import('@strudel.cycles/osc')]);
}

const modulesLoading = evalScope(
Expand Down Expand Up @@ -153,6 +153,8 @@ export function Repl({ embedded = false }) {
if (!play) {
cleanupDraw(false);
window.postMessage('strudel-stop');
} else {
window.postMessage('strudel-start');
}
},
drawContext,
Expand Down