mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-27 13:38:40 +00:00
First go at serial output
This commit is contained in:
parent
83a46c68dc
commit
b52c42c31d
4
packages/serial/README.md
Normal file
4
packages/serial/README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# @strudel.cycles/serial
|
||||||
|
|
||||||
|
This package adds webserial functionality to strudel Patterns, for e.g. sending messages to arduino microcontrollers.
|
||||||
|
|
||||||
24
packages/serial/package.json
Normal file
24
packages/serial/package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "@strudel.cycles/serial",
|
||||||
|
"version": "0.0.6",
|
||||||
|
"description": "Webserial API for strudel",
|
||||||
|
"main": "serial.mjs",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/tidalcycles/strudel.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"titdalcycles",
|
||||||
|
"strudel",
|
||||||
|
"pattern",
|
||||||
|
"livecoding",
|
||||||
|
"algorave"
|
||||||
|
],
|
||||||
|
"author": "Alex McLean <alex@slab.org>",
|
||||||
|
"license": "GPL-3.0-or-later",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/tidalcycles/strudel/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/tidalcycles/strudel#readme",
|
||||||
|
"dependencies": {}
|
||||||
|
}
|
||||||
55
packages/serial/serial.mjs
Normal file
55
packages/serial/serial.mjs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import { Pattern, isPattern } from '@strudel.cycles/core';
|
||||||
|
|
||||||
|
var serialWriter;
|
||||||
|
var choosing = false;
|
||||||
|
|
||||||
|
export async function getWriter() {
|
||||||
|
if (choosing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
choosing = true;
|
||||||
|
if (serialWriter) {
|
||||||
|
return serialWriter;
|
||||||
|
}
|
||||||
|
if ('serial' in navigator) {
|
||||||
|
const port = await navigator.serial.requestPort();
|
||||||
|
await port.open({ baudRate: 115200 });
|
||||||
|
const textEncoder = new TextEncoderStream();
|
||||||
|
const writableStreamClosed = textEncoder.readable.pipeTo(port.writable);
|
||||||
|
const writer = textEncoder.writable.getWriter();
|
||||||
|
serialWriter = function (message) {
|
||||||
|
writer.write(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw('Webserial is not available in this browser.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const latency = 0.1;
|
||||||
|
|
||||||
|
// Pattern.prototype.midi = function (output: string | number, channel = 1) {
|
||||||
|
Pattern.prototype.serial = function () {
|
||||||
|
return this._withEvent((event) => {
|
||||||
|
getWriter();
|
||||||
|
if (!serialWriter) {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
const onTrigger = (time, event, currentTime) => {
|
||||||
|
var message = "";
|
||||||
|
if (typeof event.value === 'object') {
|
||||||
|
for (const [key, val] of Object.entries(event.value).flat()) {
|
||||||
|
message += `${key}:${val};`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
message = event.value;
|
||||||
|
}
|
||||||
|
const offset = (time - currentTime + latency) * 1000;
|
||||||
|
//const ts = Math.floor(Date.now() + offset);
|
||||||
|
console.log(`sending ${message}`)
|
||||||
|
window.setTimeout(serialWriter, offset, message)
|
||||||
|
};
|
||||||
|
return event.setContext({ ...event.context, onTrigger });
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -30,6 +30,7 @@ import '@strudel.cycles/core/speak.mjs';
|
|||||||
import '@strudel.cycles/tone/pianoroll.mjs';
|
import '@strudel.cycles/tone/pianoroll.mjs';
|
||||||
import '@strudel.cycles/tone/draw.mjs';
|
import '@strudel.cycles/tone/draw.mjs';
|
||||||
import '@strudel.cycles/osc/osc.mjs';
|
import '@strudel.cycles/osc/osc.mjs';
|
||||||
|
import '@strudel.cycles/serial/serial.mjs';
|
||||||
import controls from '@strudel.cycles/core/controls.mjs';
|
import controls from '@strudel.cycles/core/controls.mjs';
|
||||||
|
|
||||||
extend(
|
extend(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user