Compare commits

..

6 Commits

Author SHA1 Message Date
alex 62b8ec2402 fix typo 2026-11-20 13:45:53 +00:00
alex 24baaf7933 Merge branch 'websocket-client' of ssh://codeberg.org/uzu/strudel into websocket-client 2026-11-19 16:03:32 +00:00
alex 55dc8650f1 minor change to kick codeberg into action 2026-11-19 16:00:43 +00:00
Alex McLean 3b2208bc36 Merge branch 'main' into websocket-client 2026-11-19 16:58:43 +01:00
alex d6e231a9fd copy + paste error 2026-11-19 15:57:49 +00:00
alex 6c345ed827 simple websocket client thingie 2026-11-19 15:55:40 +00:00
24 changed files with 165 additions and 100 deletions
-23
View File
@@ -1,23 +0,0 @@
In line with [our humane policy](POLICY.md), code and documentation
should not be written by LLM agents. To this end, agents should not
edit the codebase or make any commits that are intended to be
contributed upstream. Likewise, LLM output should not be pasted into
issues, comments, pull requests and so on.
A contributor's personal use of LLMs and related technology is their
own business, whether for research, analysis, bug discovery or other
assistive technology, so long as the output is not included in any
contributions to our projects.
Strudel is AGPL licensed, and so any code integrated with it also
comes under that license. This should be carefully considered before
distributing software integrated with strudel, including making it
available over a network.
Strudel has purposefully moved away from github.com, as a closed
source platform with ethical issues. Please do not fork strudel back
to that platform, or similar corporate platforms.
Projects using strudel must not use "strudel" in their name, unless
they are 'official' projects, or have the blessing of the strudel
community.
-9
View File
@@ -73,15 +73,6 @@ export function registerControl(names, ...aliases) {
return bag;
}
// Constructs a superdirt effect bus control
const sdbus = register('sdbus', function (busid, control_name, busval, pat) {
return pat[control_name]('c' + busid).withValue((val) => {
const newval = { ...val };
newval['^' + control_name] = [busid, busval];
return newval;
});
});
export function registerMultiControl(names, maxControls, ...aliases) {
names = Array.isArray(names) ? names : [names];
let bag = {};
+5 -5
View File
@@ -74,12 +74,12 @@ export class Cyclist {
// query the pattern for events
const haps = this.pattern
.queryArc(begin, end, { _cps: this.cps, cyclist: 'cyclist' })
.filter((hap) => hap.hasOnset() || hap.context.processParts)
.filter((hap) => hap.hasOnset())
// Sort the haps based on their onset, so cps changes within the
// tick duration happen in the correct order.
.sort((a, b) => {
const aBegin = a.part.begin;
const bBegin = b.part.begin;
const aBegin = a.whole.begin;
const bBegin = b.whole.begin;
// If haps happen at the same time, trigger the haps that cause
// cps changes first, so the durations of the simultaneous haps
// are adjusted accordingly.
@@ -96,7 +96,7 @@ export class Cyclist {
});
haps.forEach((hap) => {
const targetTime = (hap.part.begin - this.checkpoint.cycles) / this.cps + this.checkpoint.seconds;
const targetTime = (hap.whole.begin - this.checkpoint.cycles) / this.cps + this.checkpoint.seconds;
if (tickEndTime < targetTime) {
// If the targetTime exceeds the tickEndTime, this means
// the cps has slowed down, and the latter haps from the query
@@ -110,7 +110,7 @@ export class Cyclist {
this.cps = hap.value.cps;
this.num_ticks_since_checkpoint = 0;
this.checkpoint = {
cycles: hap.part.begin,
cycles: hap.whole.begin,
seconds: targetTime,
};
}
+1 -5
View File
@@ -16,7 +16,7 @@ export class Hap {
then the whole will be returned as None, in which case the given
value will have been sampled from the point halfway between the
start and end of the 'part' timespan.
The context is to store metadata, including a list of source code locations causing the event.
The context is to store a list of source code locations causing the event.
The word 'Event' is more or less a reserved word in javascript, hence this
class is named called 'Hap'.
@@ -161,10 +161,6 @@ export class Hap {
return new Hap(this.whole, this.part, this.value, context);
}
withContext(f) {
return new Hap(this.whole, this.part, this.value, f(this.context));
}
ensureObjectValue() {
/* if (isNote(hap.value)) {
// supports primitive hap values that look like notes
-5
View File
@@ -64,12 +64,7 @@ export class Pattern {
this.__steps = steps === undefined ? undefined : Fraction(steps);
}
_processParts() {
return this.withHap((hap) => hap.withContext((c) => ({ ...c, processParts: true })));
}
setSteps(steps) {
// TODO shouldn't this be pure and return a new pattern?
this._steps = steps;
return this;
}
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+7 -35
View File
@@ -52,7 +52,6 @@ export function parseControlsFromHap(hap, cps) {
controls.unit === 'c' && controls.speed != null && (controls.speed = controls.speed / cps);
const channels = controls.channels;
channels != undefined && (controls.channels = JSON.stringify(channels));
return controls;
}
@@ -78,42 +77,16 @@ export async function oscOutput(hap, targetTime, cps = 1) {
const controls = parseControlsFromHap(hap, cps);
const keyvals = Object.entries(controls).flat();
const msg = { address: '/dirt/play', args: keyvals, timestamp: ts };
if ('oschost' in hap.value) {
msg['host'] = hap.value['oschost'];
}
if ('oscport' in hap.value) {
msg['port'] = hap.value['oscport'];
}
ws.send(JSON.stringify(msg));
}
export async function superdirtTrigger(hap, currentTime, cps = 1, targetTime) {
const ws = await connect();
const controls = parseControlsFromHap(hap, cps);
const ts = collator.calculateTimestamp(currentTime, targetTime) * 1000;
let oschost = '127.0.0.1';
let oscport = 57120;
let bushost = oschost;
let busport = 57110;
for (const [k, v] of Object.entries(controls)) {
if (k.startsWith('^')) {
const bus_idval = v;
const msg = JSON.stringify({
oschost: bushost,
oscport: busport,
address: '/c_set',
args: bus_idval,
timestamp: ts,
});
// console.log('bus message', msg);
ws.send(msg);
// So they aren't sent with trigger messages
delete controls[k];
}
}
if (hap.hasOnset()) {
const keyvals = Object.entries(controls).flat();
ws.send(JSON.stringify({ oschost, oscport, address: '/dirt/play', args: keyvals, timestamp: ts }));
}
}
/**
*
* Sends each hap as an OSC message, which can be picked up by SuperCollider or any other OSC-enabled software.
@@ -128,4 +101,3 @@ export const osc = register('osc', (pat) => {
ensureMinimalOutput();
return pat.onSchedule(oscOutput);
});
export const superdirt = register('superdirt', (pat) => pat._processParts().onTrigger(superdirtTrigger));
+13 -9
View File
@@ -12,13 +12,18 @@ import { WebSocketServer } from 'ws';
import osc from 'osc';
const WS_PORT = 8080; // WebSocket server port
const OSC_REMOTE_IP = '127.0.0.1';
const OSC_REMOTE_PORT = 57120;
const udpPort = new osc.UDPPort({
localAddress: '0.0.0.0',
localPort: 0,
remoteAddress: OSC_REMOTE_IP,
remotePort: OSC_REMOTE_PORT,
});
udpPort.open();
console.log(`[Sending OSC] ${OSC_REMOTE_IP}:${OSC_REMOTE_PORT}`);
udpPort.on('error', (e) => {
console.log('Error: ', e);
@@ -31,24 +36,23 @@ wss.on('connection', (ws) => {
console.log('New WebSocket connection');
ws.on('message', (message) => {
let oschost = '127.0.0.1';
let oscport = 57120;
let osc_host = '127.0.0.1';
let osc_port = 57120;
try {
const data = JSON.parse(message);
if ('oschost' in data) {
oschost = data['oschost'];
delete data['oschost'];
if ('host' in data) {
osc_host = data['host'];
}
if ('oscport' in data) {
oscport = data['oscport'];
delete data['oscport'];
if ('port' in data) {
osc_port = data['port'];
}
let msg = { address: data['address'], args: data['args'] };
if ('timestamp' in data) {
msg = { timeTag: osc.timeTag(0, data['timestamp']), packets: [msg] };
}
udpPort.send(msg, oschost, oscport);
udpPort.send(msg, osc_host, osc_port);
} catch (err) {
console.error('Error parsing message:', err);
}
+2 -1
View File
@@ -43,7 +43,8 @@
"@strudel/soundfonts": "workspace:*",
"@strudel/tonal": "workspace:*",
"@strudel/transpiler": "workspace:*",
"@strudel/webaudio": "workspace:*"
"@strudel/webaudio": "workspace:*",
"@strudel/websocket": "workspace:*"
},
"devDependencies": {
"@rollup/plugin-replace": "^6.0.2",
+1
View File
@@ -15,6 +15,7 @@ export async function prebake() {
import('@strudel/hydra'),
import('@strudel/soundfonts'),
import('@strudel/midi'),
import('@strudel/websocket'),
// import('@strudel/xen'),
// import('@strudel/serial'),
// import('@strudel/csound'),
+1 -1
View File
@@ -16,7 +16,7 @@
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"titdalcycles",
"tidalcycles",
"strudel",
"pattern",
"livecoding",
+6
View File
@@ -0,0 +1,6 @@
# @strudel/websocket
This package adds websocket input to strudel Patterns.
It connects to a websocket server and exposes the latest received value (parsed as JSON) as a pattern.
+3
View File
@@ -0,0 +1,3 @@
import './websocket.mjs';
export * from './websocket.mjs';
+40
View File
@@ -0,0 +1,40 @@
{
"name": "@strudel/websocket",
"version": "1.0.0",
"description": "Websocket input for strudel",
"main": "index.mjs",
"type": "module",
"publishConfig": {
"main": "dist/index.mjs"
},
"scripts": {
"build": "vite build",
"prepublishOnly": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://codeberg.org/uzu/strudel.git"
},
"keywords": [
"tidalcycles",
"strudel",
"pattern",
"livecoding",
"algorave"
],
"author": "Alex McLean <alex@slab.org>",
"license": "AGPL-3.0-or-later",
"bugs": {
"url": "https://codeberg.org/uzu/strudel/issues"
},
"homepage": "https://codeberg.org/uzu/strudel#readme",
"dependencies": {
"@strudel/core": "workspace:*"
},
"devDependencies": {
"vite": "^6.0.11"
},
"engines": {
"node": ">=18.0.0"
}
}
+19
View File
@@ -0,0 +1,19 @@
import { defineConfig } from 'vite';
import { dependencies } from './package.json';
import { resolve } from 'path';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [],
build: {
lib: {
entry: resolve(__dirname, 'index.mjs'),
formats: ['es'],
fileName: (ext) => ({ es: 'index.mjs' })[ext],
},
rollupOptions: {
external: [...Object.keys(dependencies)],
},
target: 'esnext',
},
});
+43
View File
@@ -0,0 +1,43 @@
/*
websocket.mjs - simple websocket client as a pattern
Copyright (C) 2026 Strudel contributors -- see <https://codeberg.org/uzu/strudel/src/branch/main/packages/websocket/websocket.mjs>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { logger, signal } from '@strudel/core';
const connections = {};
const values = {};
function connect(addr) {
if (connections[addr]) {
return connections[addr];
}
const ws = new WebSocket(addr);
connections[addr] = ws;
ws.addEventListener('open', () => {
logger(`[websocket] connected to ${addr}`);
});
ws.addEventListener('message', (event) => {
try {
console.log(event.data);
values[addr] = JSON.parse(event.data);
} catch {
logger(`[websocket] received invalid json on ${addr}`);
}
});
ws.addEventListener('close', () => {
logger(`[websocket] disconnected from ${addr}`);
delete connections[addr];
});
ws.addEventListener('error', () => {
logger(`[websocket] failed to connect to ${addr}`);
delete connections[addr];
});
return ws;
}
export function websocket(addr) {
connect(addr);
return signal(() => values[addr] ?? {});
}
+16
View File
@@ -538,6 +538,9 @@ importers:
'@strudel/webaudio':
specifier: workspace:*
version: link:../webaudio
'@strudel/websocket':
specifier: workspace:*
version: link:../websocket
devDependencies:
'@rollup/plugin-replace':
specifier: ^6.0.2
@@ -722,6 +725,16 @@ importers:
specifier: ^6.0.11
version: 6.0.11(@types/node@22.10.10)(jiti@2.4.2)(lightningcss@1.29.1)(terser@5.37.0)(yaml@2.7.0)
packages/websocket:
dependencies:
'@strudel/core':
specifier: workspace:*
version: link:../core
devDependencies:
vite:
specifier: ^6.0.11
version: 6.0.11(@types/node@22.10.10)(jiti@2.4.2)(lightningcss@1.29.1)(terser@5.37.0)(yaml@2.7.0)
packages/xen:
dependencies:
'@strudel/core':
@@ -857,6 +870,9 @@ importers:
'@strudel/webaudio':
specifier: workspace:*
version: link:../packages/webaudio
'@strudel/websocket':
specifier: workspace:*
version: link:../packages/websocket
'@strudel/xen':
specifier: workspace:*
version: link:../packages/xen
+1
View File
@@ -48,6 +48,7 @@
"@strudel/tonal": "workspace:*",
"@strudel/transpiler": "workspace:*",
"@strudel/webaudio": "workspace:*",
"@strudel/websocket": "workspace:*",
"@strudel/mondo": "workspace:*",
"@strudel/xen": "workspace:*",
"@supabase/supabase-js": "^2.48.1",
@@ -11,7 +11,7 @@ import QA from '@components/QA';
Up until now, most of the functions we've seen are what other music programs are typically capable of: sequencing sounds, playing notes, controlling effects.
In this chapter, we are going to look at functions that are more unique to Strudel.
In this chapter, we are going to look at functions that are more unique to tidal.
**reverse patterns with rev**
+1 -1
View File
@@ -97,7 +97,7 @@ export function loadModules() {
import('@strudel/desktopbridge/oscbridge.mjs'),
]);
} else {
modules = modules.concat([import('@strudel/midi'), import('@strudel/osc')]);
modules = modules.concat([import('@strudel/midi'), import('@strudel/osc'), import('@strudel/websocket')]);
}
return evalScope(settingPatterns, ...modules);