kegel/kegel.js

108 lines
2.1 KiB
JavaScript

const WebSocket = require('ws')
const osc = require('osc')
const ip = process.argv[2]
const DEBUG = {
midi: true,
osc: false
}
//const ws = new WebSocket(`ws://${ip}:6676`)
/*
const wsp = new osc.WebsocketClientPlugin({
host: ip,
port: 6676
})
*/
const oscWS = new osc.WebSocketPort({
url: `ws://${ip}:6676`,
metadata: false
})
oscWS.on('ready', () => {
console.log(`Povezan sem na ${ip}`)
/*
setInterval(() => {
console.log('ping')
oscWS.send({ address: '/ping', args: [true]})
}, 500)
*/
})
let midiOn = false
const zaznava = 0.5
let cakaj = false
const cakajInterval = 1 / 5
const razponTona = [0.1, 0.5]
//const razponTona = [0.001, 0.002]
let rotVisina = 0
oscWS.on('message', ({ address, args }) => {
DEBUG.osc && console.log('MSG OSC', address, args.map(x => x.toFixed(4)))
if (address == '/euler') {
//const eVisina = Math.sin(args[0] + args[1])
const eVisina = Math.sin(args[2])
rotVisina = 24 + Math.round(((eVisina / 2) + 0.5) * 24)
//console.log("rotVisina", rotVisina)
}
if (address == '/accel') {
const acX = args[0]
const acY = args[1]
const acZ = args[2]
const vsota = acX + acY + acZ
if (cakaj) {
//console.log("CAKAM!")
return
}
if (acX > zaznava && !cakaj) {
cakaj = true
// Pavza
setTimeout(() => {
//console.log("nehaj cakat")
cakaj = false
}, cakajInterval * 1000)
// Od 24 do 48
const accval = Math.sin((acX - zaznava) / 3)
console.log(acX, accval)
// Acc visina
const visina = 24 + Math.round((accval / 2.5) * 48)
//const visina = rotVisina
//console.log('posiljam midi', [144, visina, 127])
oscWS.send({
address: '/midi',
args: [144, visina, 127]
})
console.log('vklopi', visina)
midiOn = true
// Ugasni cez en cas
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
setTimeout(() => {
oscWS.send({
address: '/midi',
args: [128, visina, 127]
})
console.log('izklopi', visina)
}, izklop * 1000)
}
}
})
oscWS.open()