kegel/kegel.js

173 lines
3.9 KiB
JavaScript
Raw Normal View History

2022-05-07 16:57:02 +02:00
const WebSocket = require('ws')
const osc = require('osc')
const ip = process.argv[2]
2022-05-22 16:08:51 +02:00
const DEBUG = {
midi: true,
osc: false
}
2022-05-07 16:57:02 +02:00
//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)
*/
})
2022-09-17 14:25:02 +02:00
const zaznava = [2, 2.5, 2, 2, 2, 2]
const utisaj_vse = 0.2
let utisaj_c = [0, 0, 0, 0, 0, 0]
let cakaj = [false, false, false, false, false, false]
2022-05-07 16:57:02 +02:00
const cakajInterval = 1 / 5
2022-05-22 16:08:51 +02:00
const razponTona = [0.1, 0.5]
//const razponTona = [0.001, 0.002]
2022-05-07 16:57:02 +02:00
2022-05-22 16:08:51 +02:00
let rotVisina = 0
2022-05-07 16:57:02 +02:00
oscWS.on('message', ({ address, args }) => {
2022-05-22 16:08:51 +02:00
DEBUG.osc && console.log('MSG OSC', address, args.map(x => x.toFixed(4)))
2022-05-27 13:23:36 +02:00
2022-06-28 19:16:48 +02:00
// Wavey Wind sporočila
2022-05-27 13:23:36 +02:00
const re = /\/ww\/(\d+)\/([a-zA-Z]+)/
const m = address.match(re)
2022-06-28 19:16:48 +02:00
if (m) {
2022-09-17 14:25:02 +02:00
const index = parseInt(m[1])
2022-06-28 19:16:48 +02:00
const addr = m[2]
2022-05-27 13:23:36 +02:00
2022-06-28 19:16:48 +02:00
if (addr == '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)
}
2022-05-22 16:08:51 +02:00
2022-06-28 19:16:48 +02:00
if (addr == 'accel') {
const acX = args[0]
const acY = args[1]
const acZ = args[2]
2022-05-07 16:57:02 +02:00
2022-06-28 19:16:48 +02:00
// Y os je kegel po dolgem - ignoriramo
2022-09-17 14:25:02 +02:00
const vsota = Math.abs(acX) + Math.abs(acZ)
2022-05-07 16:57:02 +02:00
2022-09-17 14:25:02 +02:00
if (vsota > zaznava[index] && !cakaj[index]) {
2022-05-07 16:57:02 +02:00
2022-09-17 14:25:02 +02:00
cakaj[index] = true
2022-05-22 16:43:15 +02:00
2022-06-28 19:16:48 +02:00
// Pavza
setTimeout(() => {
//console.log("nehaj cakat")
2022-09-17 14:25:02 +02:00
cakaj[index] = false
2022-06-28 19:16:48 +02:00
}, cakajInterval * 1000)
2022-05-22 16:43:15 +02:00
2022-09-17 14:25:02 +02:00
const midiKanal = 144 + index
2022-05-22 16:43:15 +02:00
2022-06-28 19:16:48 +02:00
// Od 24 do 48
2022-09-17 14:25:02 +02:00
const accval = Math.sin((vsota - zaznava[index]) / 3)
2022-06-28 19:16:48 +02:00
//console.log(acX, accval)
// Acc visina
const visina = (midiKanal == 0)
? 24 + Math.round((accval / 2.5) * 36)
: 48 + Math.round((accval / 2.5) * 72)
2022-05-22 16:43:15 +02:00
2022-06-28 19:16:48 +02:00
//const visina = rotVisina
2022-05-07 16:57:02 +02:00
2022-06-28 19:16:48 +02:00
//console.log('posiljam midi', [144, visina, 127])
/*
2022-05-07 16:57:02 +02:00
oscWS.send({
address: '/midi',
2022-06-28 19:16:48 +02:00
args: [192, 0, 127]
2022-05-07 16:57:02 +02:00
})
2022-06-28 19:16:48 +02:00
*/
oscWS.send({
address: '/midi',
args: [midiKanal, visina, 127]
})
console.log('vklopi', midiKanal, visina)
// Ugasni cez en cas
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
2022-09-17 14:25:02 +02:00
console.log('izklopim', midiKanal, visina, 'cez', izklop)
2022-06-28 19:16:48 +02:00
setTimeout(() => {
oscWS.send({
address: '/midi',
args: [midiKanal, visina, 0]
})
console.log('izklopi', midiKanal, visina)
}, izklop * 1000)
}
2022-09-17 13:20:44 +02:00
// Ce izrazito ni gibanja, utisaj vse!
2022-09-17 14:25:02 +02:00
if (!utisaj_c[index]) {
utisaj_c[index] = 0
}
2022-09-17 13:20:44 +02:00
if (vsota < utisaj_vse) {
2022-09-17 14:25:02 +02:00
if (utisaj_c[index] > 10) {
const midiKanal = 144 + index
const visina = (midiKanal == 0)
? 24
: 48
const visina_razpon = (midiKanal == 0)
? 36
: 72
console.log("UTISAJ", index)
for (let i = visina; i < visina + visina_razpon; i++) {
oscWS.send({
address: '/midi',
args: [midiKanal, i, 0]
})
}
utisaj_c[index] = 0
} else {
// console.log('.')
utisaj_c[index] += 1
}
} else {
utisaj_c[index] = 0
2022-09-17 13:20:44 +02:00
}
2022-05-07 16:57:02 +02:00
}
2022-06-28 19:16:48 +02:00
} else {
console.log(address)
}
if (address === '/midi-in') {
console.log(args)
oscWS.send({
address: '/midi',
args: [150, args[1], args[2]]
})
// Ugasni cez en cas
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
setTimeout(() => {
oscWS.send({
address: '/midi',
args: [150, args[1], 0]
})
console.log('izklopi', 150, args[1])
}, izklop * 1000)
2022-05-07 16:57:02 +02:00
}
})
oscWS.open()