From a6dc9f4f43d796ab790d77540f8fafe94bd866c3 Mon Sep 17 00:00:00 2001 From: Bruce Date: Sun, 20 Oct 2019 16:14:53 +0200 Subject: [PATCH] added MidiManager --- hydra/app/extensions/hydra-midi/index.js | 37 ++++++++++++++++++++++++ hydra/app/startup-pixels.js | 4 +++ 2 files changed, 41 insertions(+) create mode 100644 hydra/app/extensions/hydra-midi/index.js diff --git a/hydra/app/extensions/hydra-midi/index.js b/hydra/app/extensions/hydra-midi/index.js new file mode 100644 index 00000000..a55a72b8 --- /dev/null +++ b/hydra/app/extensions/hydra-midi/index.js @@ -0,0 +1,37 @@ + + +class Midi { + constructor() { + console.log('midi ctor'); + this.onMIDISuccess = this.onMIDISuccess.bind(this) + this.getMIDIMessage = this.getMIDIMessage.bind(this) + + navigator.requestMIDIAccess().then(this.onMIDISuccess, this.onMIDIFailure); + //create an array to hold our cc values and init to a normalized value + this.cc = Array(128).fill(0.5); + }; + + onMIDISuccess(midiAccess) { + console.log(midiAccess); + var inputs = midiAccess.inputs; + var outputs = midiAccess.outputs; + for (var input of midiAccess.inputs.values()) { + input.onmidimessage = this.getMIDIMessage; + } + } + onMIDIFailure() { + console.log('Could not access your MIDI devices.'); + } + + getMIDIMessage(midiMessage) { + var arr = midiMessage.data + var index = arr[1] + console.log('Midi received on cc#' + index + ' value:' + arr[2]) + var val = (arr[2] + 1) / 128.0 // normalize CC values to 0.0 - 1.0 + this.cc[index] = val + } +} + +module.exports = Midi + + diff --git a/hydra/app/startup-pixels.js b/hydra/app/startup-pixels.js index a5d0643f..7d7b83c5 100644 --- a/hydra/app/startup-pixels.js +++ b/hydra/app/startup-pixels.js @@ -8,6 +8,7 @@ const Editor = require('./core/hydra-editor') // Extensions const PixelSynth = require('./extensions/hydra-pixels') const OscManager = require('./extensions/hydra-osc') +const MidiManager = require('./extensions/hydra-midi') function init () { // init hydra @@ -20,6 +21,9 @@ function init () { window.pixels = new PixelSynth({ gl: hydra.regl._gl, parent: document.body}) window.msg = new OscManager() + // use like: osc( () => midi.cc[18] ).out() + window.midi = new MidiManager() + editor = new Editor({ loadFromStorage: true}) // loop function run on each frame