Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions hydra/app/extensions/hydra-midi/index.js
Original file line number Diff line number Diff line change
@@ -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


4 changes: 4 additions & 0 deletions hydra/app/startup-pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down