Popravljena pretvorma midi

main
Jurij Podgoršek 2023-06-14 14:40:05 +02:00
parent 597fab9ef5
commit 61adbf6b60
1 changed files with 57 additions and 8 deletions

View File

@ -6,7 +6,7 @@ const ip = process.argv[2]
const DEBUG = {
midi: true,
osc: false
osc: true
}
let mo = null
@ -45,6 +45,19 @@ const razponTona = [0.1, 0.5]
let rotVisina = 0
// Helper funkcije
const norm = (val, min, max) => {
const d = 0 - min;
const r = max - min
return Math.min(1, (Math.max(min, val) + d) / r);
}
const normSin = val => {
return norm(Math.sin(val), -1, 1);
}
const normMidi = (val, min, max) => Math.round(norm(val, min, max) * 127)
const abs = Math.abs;
oscWS.on('message', ({ address, args }) => {
DEBUG.osc && console.log('MSG OSC', address, args.map(x => x.toFixed(4)))
@ -84,21 +97,38 @@ oscWS.on('message', ({ address, args }) => {
const midiKanal = 144 + index
// Od 24 do 48
const accval = Math.sin((vsota - zaznava[index]) / 3)
//const accval = Math.sin((vsota - zaznava[index]) / 3)
//const accval = (vsota - zaznava[index]) / 3
const accval = vsota - zaznava[index]
//console.log(acX, accval)
// Acc visina
const visina = (midiKanal == 0)
? 24 + Math.round((accval / 2.5) * 36)
: 48 + Math.round((accval / 2.5) * 72)
const visina = (midiKanal == 144)
? 24 + Math.round(norm(accval, 0, 7) * 36)
: 36 + Math.round(norm(accval, 0, 7) * 36)
console.log('posiljam midi', [midiKanal, visina, 127])
mo.send([midiKanal, visina, 127])
let vel = (midiKanal == 144)
? (visina - 24) * (127 / 36)
: (visina - 36) * (127 / 36)
vel = Math.min(Math.round(vel), 127)
console.log('posiljam midi', midiKanal, [midiKanal, visina, vel])
mo.send([midiKanal, visina, vel])
oscWS.send({
address: '/midi',
args: [midiKanal, visina, vel]
})
// Ugasni cez en cas
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
console.log('izklopim', midiKanal, visina, 'cez', izklop)
setTimeout(() => {
mo.send([midiKanal, visina, 0]);
oscWS.send({
address: '/midi',
args: [midiKanal, visina, 0]
})
console.log('izklopi', midiKanal, visina)
}, izklop * 1000)
}
@ -129,6 +159,25 @@ oscWS.on('message', ({ address, args }) => {
utisaj_c[index] = 0
}
}
if (addr == 'midi') {
const args = message
console.log('OSC midi in', args)
mo.send(message)
oscWS.send({
address: '/midi-in',
args: message
})
// Ugasni cez en cas
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
setTimeout(() => {
mo.send([150, message[1], 0])
console.log('izklopi', 150, message[1])
}, izklop * 1000)
}
} else {
console.log(address)
}
@ -148,7 +197,7 @@ mi.on('message', (deltaTime, message) => {
let izklop = razponTona[0] + Math.random() * (razponTona[1] - razponTona[0])
setTimeout(() => {
mo.send([150, message[1], 0])
console.log('izklopi', 150, message[1])
//console.log('izklopi zogice', 150, message[1])
}, izklop * 1000)
})