import { OSCClient , OSCType , OSCServer } from 'ts-osc' ; import { OSCArgument } from 'ts-osc/lib/types' ; export type OscConfig = { serverPort : number ; clientPort : number ; clientAddr : string ; } ; export type OscValue = string | number | boolean | Buffer | OSCArgument < OSCType > [ ] | null ; export type OscMsg = { type : OSCType ; value : OscValue ; } ; export class Osc { private readonly client : OSCClient ; private readonly server : OSCServer ; private readonly controlMap : Map < string , ( msg : OscMsg ) => void > = new Map ( ) ; constructor ( config ) { this . client = new OSCClient ( config . clientAddr , config . clientPort ) ; this . server = new OSCServer ( '0.0.0.0' , config . serverPort ) ; this . server . on ( 'error' , console . error ) ; this . server . on ( 'listening' , ( ) => console . log ( `OSC listening on port ${ config . serverPort }` ) ) ; this . server . on ( 'message' , ( oscMessage ) => { const control = oscMessage . address ; const type = oscMessage . type ; const value = oscMessage . value ; const update = this . controlMap . get ( control ) ; if ( update ) update ( { type : type , value : value } ) ; } ) ; this . server . on ( 'close' , this . server . close ) ; } sendMsg ( control , msg ) { this . client . send ( control , msg . type , msg . value ) ; } addControl ( control , upd , set ? ) { this . controlMap . set ( control , upd ) ; if ( set ) this . sendMsg ( control , set ) ; } removeControl ( control ) { this . controlMap . delete ( control ) ; } }