Last active
April 29, 2018 19:37
-
-
Save srejv/b7198e25587e2d8e0a66860781b56852 to your computer and use it in GitHub Desktop.
Example of how to get WebMidi input into Godot using GDScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
signal move_player_to(channel, value) | |
signal key_fire() | |
signal key_slowmo() | |
signal key_okay() | |
signal key_cancel() | |
var strOnMidiMessage = """ | |
function Input() { | |
this.name = "midiinput" | |
this.processed=false | |
this.data = [] | |
} | |
midiArray = new Input(); | |
function onMIDIMessage (message) { | |
/* | |
var command = message.data[0]; | |
var note = message.data[1]; | |
var velocity = (message.data.length > 2) ? message.data[2] : 0; // a velocity value might not be included with a noteOff command | |
switch (command) { | |
case 144: // noteOn | |
if (velocity > 0) { | |
noteOn(note, velocity); | |
} else { | |
noteOff(note); | |
} | |
break; | |
case 128: // noteOff | |
noteOff(note); | |
break; | |
// we could easily expand this switch statement to cover other types of commands such as controllers or sysex | |
} | |
*/ | |
midiArray.data.push(message) | |
} | |
function failure () { | |
alert('No access to your midi devices.') | |
} | |
function success (midi) { | |
var inputs = midi.inputs.values(); | |
for (var input = inputs.next(); input && !input.done; input = inputs.next()) { | |
input.value.onmidimessage = onMIDIMessage; | |
} | |
} | |
if (navigator.requestMIDIAccess) | |
{ | |
navigator.requestMIDIAccess().then(success, failure); | |
} else { | |
alert('no midi support, you cannot play'); | |
}""" | |
func _ready(): | |
JavaScript.eval(strOnMidiMessage, true); | |
pass | |
func _process(delta): | |
# # Called every frame. Delta is time since last frame. | |
# # Update game logic here. | |
var midiinput = _fetch_js_input() | |
if midiinput.data == null: | |
return | |
if midiinput.data.size() == 0: | |
return | |
for d in midiinput.data: | |
var type = d[0] & 0xf0 | |
var channel = d[0] & 0x0f | |
if type == 0xb0: | |
var cc = d[1] | |
if cc == 48: | |
emit_signal("move_player_to", channel, d[2]) | |
if (d[1] == 26): | |
if d[2] == 0: | |
pass | |
if d[2] == 127: | |
#print("key_slowmo down") | |
emit_signal("key_slowmo") | |
if (d[1] == 27): | |
if d[2] == 127: | |
#print("key_fire down") | |
emit_signal("key_fire") | |
if type == 0x80: # maybe note | |
emit_signal("key_fire"); | |
#print("Maybe Note on") | |
pass | |
if type == 0x90: # maybe note off | |
#print("Maybe Note Off") | |
pass | |
pass | |
pass | |
func _fetch_js_input(): | |
var input_action=JavaScript.eval("""midiArray.name""") | |
var input_processed=JavaScript.eval("""midiArray.processed""") | |
var number_of_messages = JavaScript.eval("""midiArray.data.length""") | |
if number_of_messages == null: | |
return {"action": input_action, "processed": input_processed, "data": null} | |
var data = [] | |
for i in number_of_messages: | |
data.append(JavaScript.eval("midiArray.data[" + str(i) + "].data")) | |
JavaScript.eval("midiArray.data = []", true) | |
return {"action": input_action, "processed": input_processed, "data": data} | |
func _set_js_input_as_processed(): | |
JavaScript.eval("""midiArray.processed=true""",true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment