press button with midi controleur

moaaaamoaaaa Posts: 16Member
edited November 2019 in GUI

hi sorry for my newbie question but i have search many time before my post...

i understand the method to get midi message and for many node it's work perfectly.

but for press button it's not work and i haven't error message

my test :

func _unhandled_input(event : InputEvent):

    if (event is InputEventMIDI):

        var key_index = event.pitch
        print(key_index)
        if key_index == 30:
            match event.message:
                MIDI_MESSAGE_NOTE_ON:
                    get_node("pad1").set_pressed(true)

i think the problem come from the last line but why...

thanks.

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,637Admin

    Have you added a print statements to check if the rest of the code is working? Something like the following should tell you if the MIDI events are being picked up correctly by the code (untested):

    func _unhandled_input(event):
        if (event is InputEventMIDI):
            var key_index = event.pitch
            print ("MIDI key index: ", key_index)
            if (key_index == 30):
                print ("Code recognized key index 30")
                match event.message:
                    MIDI_MASSAGE_NOTE_ON:
                        print ("Code recognized MIDI message note on")
                        get_node("pad1").set_pressed(true)
    

    That way you should be able to look at the debugger and ensure the code its working. Looking at the code, I think it is probably working, but I have found that double checking never hurts and sometimes can save lots of debugging time.

    Is pad1 a button node? If so, the issue could be that the button is being pressed only for a single frame rather than for the duration of the MIDI note being held. Theoretically, something like this might work around that issue:

    var midi_index_30_held = false
    func _unhandled_input(event):
        if (event is InputEventMIDI):
            var key_index = event.pitch
            if (key_index == 30):
                match event.message:
                    MIDI_MESSAGE_NOTE_ON:
                        if (midi_index_30_held == false):
                            midi_index_30_held = true
                            print ("MIDI 30 was just pressed")
                    MIDI_MESSAGE_NOTE_OFF:
                        if (midi_index_30_held == true):
                            midi_index_30_held = false
                            print ("MIDI 30 was just released")
    
    func _process(_delta):
        get_node("pad1").set_pressed(midi_index_30_held)
    

    I have no idea if that will fix the issue, but it might be worth a shot. The code above should keep the pressed property of the node set to true when the MIDI button is held, and false when the MIDI button is no longer held.
    Hopefully this helps a bit :smile:

  • moaaaamoaaaa Posts: 16Member

    no that don't fix issue i give you my current test script

    my goal is
    midi input ==> press button ==> play sample

    I have the impression that button.set_pressed() don't work correctly

  • TwistedTwiglegTwistedTwigleg Posts: 2,637Admin
    edited November 2019

    Sorry about the delay, life stuff got in the way.

    Right now I do not have the time to pull the script into a test project, but as soon as I can, I will do my best to take a look at it and see what I can do!

  • moaaaamoaaaa Posts: 16Member

    it's one of my test project

  • TwistedTwiglegTwistedTwigleg Posts: 2,637Admin

    I do not have a MIDI device, so I changed the code slightly so it would work with my keyboard. I did some testing and it seems you are right, pressed/set_pressed is indeed causing the issue.

    I found this Godot Q&A answer where erlcdl mentions you can send an input event to simulate a mouse press, which seems to have fixed the issue.

    Here is the code I used (which is a slightly modified version of the code erlcdl posted):

    func _unhandled_input(event : InputEvent):
        if (event is InputEventKey):
            if (event.scancode == KEY_P):
                # Both solutions taken from the following URL
                # https://godotengine.org/qa/7199/simulate-mouse-pressed-on-texturebutton
    
                # Works, but no visuals
                # Might be *slightly* better for performance.
                #get_node("pad1").emit_signal("pressed")
    
                # Works, and has visuals!
                var input_event = InputEventMouseButton.new()
                input_event.pressed = event.pressed
                input_event.button_index = BUTTON_LEFT
                input_event.position = get_node("pad1").rect_global_position
                get_tree().input_event(input_event);
    

    Hopefully this helps! :smile:

  • moaaaamoaaaa Posts: 16Member

    ok the first solution work perfectly but one of my goal is:
    easy to understand so i must be visual

    the second solution after small modifications work with huge latency

    I'can post issue on git but whit over 5000 issue i don't think i'm priority

    so i'm learn to make my own pad button for moment

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file