- Edited
Hello everyone!
What I'm trying to do is create a visualization of a piano that responds to midi input. So far so good, but I am trying to figure out how to use the value of midi_event.pitch (which is a number corresponding to which note was pressed) and use that to pick an object (the correct piano key mesh) and move that object accordingly. Here's what I have so far, pretty straightforward, my node tree contains a node3d, with a child called KB, which has (for now) just two mesh3d instances, named 48 and 50, which corresponds to a C and D note respectively.
func _ready():
OS.open_midi_inputs()
print(OS.get_connected_midi_inputs())
print(" ")
func _input(input_event):
if input_event is InputEventMIDI:
_keymove(input_event)
func _keymove(midi_event: InputEventMIDI):
print (str(midi_event.pitch))
if midi_event.message == 9:
$"KB/50".rotation.x = deg*92.5
elif midi_event.message == 8:
$"KB/50".rotation.x = deg*90
Of course key #50 is the only one that moves, no matter which note is played, which is as expected.
Now, I could include a condition on the if/elif statements to see whether it is the correct key, and this does move key #50 only when the correct note is pressed:
func _keymove(midi_event: InputEventMIDI):
print (str(midi_event.pitch))
if midi_event.message == 9 && midi_event.pitch==50:
$"KB/50".rotation.x = deg*92.5
elif midi_event.message == 8:
$"KB/50".rotation.x = deg*90
but obviously this is grossly inefficient, since I'd end up with an if/elif for every single key that can be pressed, and whether the signal is a "note on" or "note off" message. On an 88 key controller that would mean a few hundred lines of elif statements. That's like sending a postman with only one letter and having him knock at every single door in town asking if he's at the right address!
I have also tried many things to include the value in a string within the node path (such as having the value sent to a string as well as inluding it directly) but it doesn't seem like that is possible.
What I'd like to do is take the value of midi_event.pitch and use that to choose which key moves. Thanks for any advice =)