initial support for osc (buggy)

This commit is contained in:
2023-11-22 12:12:36 +01:00
parent bbd7ccdeaa
commit fa67fdc2e5
8 changed files with 610 additions and 1 deletions

52
src/IO/OSC.ts Normal file
View File

@ -0,0 +1,52 @@
export let socket = new WebSocket('ws://localhost:3000')
export interface OSCMessage {
address: string
message: object
timetag: number
}
// @ts-ignore
socket.onopen = function(event) {
console.log("Connected to WebSocket Server")
// Send an OSC-like message
socket.send(JSON.stringify({
address: '/test',
args: [1, 2, 3]
}))
socket.onerror = function(error) {
console.log("Websocket Error:", error);
}
socket.onmessage = function(event) {
console.log("Received: ", event.data)
}
}
// export function sendToServer(message: OSCMessage) {
// socket.send(JSON.stringify(message));
// }
export function sendToServer(message: OSCMessage) {
// Check if the WebSocket is open
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify(message));
} else {
// Reconnect if the WebSocket is not open
console.log('WebSocket is not open. Attempting to reconnect...');
// Close the existing socket if necessary
if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
socket.close();
}
// Create a new WebSocket connection
socket = new WebSocket('ws://localhost:3000');
// Send the message once the socket is open
socket.onopen = () => {
socket.send(JSON.stringify(message));
};
}
}

View File

@ -1,5 +1,6 @@
import { type Editor } from "../main";
import { AudibleEvent } from "./AbstractEvents";
import { sendToServer, type OSCMessage } from "../IO/OSC";
import {
filterObject,
arrayOfObjectsToObjectWithArrays,
@ -46,6 +47,7 @@ export class SoundEvent extends AudibleEvent {
pitchJumpTime: ["pitchJumpTime", "pjt"],
lfo: ["lfo"],
znoise: ["znoise"],
address: ["address", "add"],
noise: ["noise"],
zmod: ["zmod"],
zcrush: ["zcrush"],
@ -452,4 +454,24 @@ export class SoundEvent extends AudibleEvent {
superdough(filteredEvent, this.nudge - this.app.clock.deviation, filteredEvent.dur);
}
};
osc = (orbit?: number | number[]): void => {
if (orbit) this.values["orbit"] = orbit;
const events = objectWithArraysToArrayOfObjects(this.values, [
"parsedScale",
]);
for (const event of events) {
const filteredEvent = event;
let oscAddress = this.values["address"]?.startsWith('/') ? this.values["address"] : `/${this.values["address"]}` || "/topos";
if (filteredEvent.freq) { delete filteredEvent.note; }
sendToServer({
address: oscAddress,
message: event,
timetag: Math.round(Date.now() + this.nudge - this.app.clock.deviation)
} as OSCMessage)
}
}
}

View File

@ -3,6 +3,7 @@ import { EditorState, Compartment } from "@codemirror/state";
import { javascript } from "@codemirror/lang-javascript";
import { markdown } from "@codemirror/lang-markdown";
import { Extension } from "@codemirror/state";
import { socket } from "./IO/OSC";
import {
initializeSelectedUniverse,
AppSettings,
@ -91,6 +92,9 @@ export class Editor {
manualPlay: boolean = false;
isPlaying: boolean = false;
// OSC
socket: WebSocket = socket
// Hydra
public hydra_backend: any;
public hydra: any;
@ -186,6 +190,7 @@ export class Editor {
// Loading universe from URL (if needed)
loadUniverserFromUrl(this);
}
private getBuffer(type: string): any {