Skip to content

Instantly share code, notes, and snippets.

@diegodorado
Last active April 5, 2022 20:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diegodorado/dba8791968893b2d321b744b7fb07908 to your computer and use it in GitHub Desktop.
Save diegodorado/dba8791968893b2d321b744b7fb07908 to your computer and use it in GitHub Desktop.
//
// defino mis variables
let cc = Array(128).fill(0)
let notes = Array(128).fill(0)
let lastCc = 0
let lastNote = 0
let lastOctave = 0
let lastRelNote = 0
//
// declaro un handler de eventos midi
const midiHandler = (e) => {
const m = e.data
// extraigo el tipo de midi channel message
const type = m[0] & 0xF0
if(type===0x90){
// note on
lastNote = m[1]
// guardo la ultima nota relativa (C:0, C#:1,...B:11)
lastRelNote = lastNote % 12
lastOctave = lastNote / 12
notes[lastNote] = m[2]
}else if(type===0x80){
// note off
// podria ejecutar algo en el note off, pero no me interesa
}else if(type===0xB0){
// control change
lastCc = m[1]
cc[lastCc] = m[2]
}
}
//
// pido acceso a los dispositivos midi
// y seteo el handler anterior a cada input midi
navigator.requestMIDIAccess().then(ma => {
for (var input of ma.inputs.values())
input.onmidimessage = midiHandler
})
//
// cambio de shape segun la ultima nota (relativa) tocada (+3, minimo: triangulo)
// y rotacion por rueda de modulacion
shape(()=>(lastRelNote+3)).rotate(()=>cc[1]/128.0*3.14*2).out()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment