I wrote a Python script that sends MIDI data to a another program on my laptop if the a key is pressed on the keyboard which triggers a piano sound.
My problem is the following: On a real piano if I press a key and I keep it pressed, a single note sounds with sustain. But if I press the a key on my keyboard while running the script, instead of behaving as a real acoustic piano, the note sounds a bunch of times while the key is pressed. I suppose this issue could be resolved with some if and loop logic. I just don't know how.
Could anyone suggest me anything?
My script is this:
import time
import rtmidi
import mido
import keyboard
outport = mido.open_output('loopMIDI 1')
while True:
#Pad A
if keyboard.is_pressed("a"):
msg = mido.Message("note_on", note=36, velocity=100, time=10)
outport.send(msg)
time.sleep(0.05)
else:
msg = mido.Message("note_off", note=36, velocity=100, time=10)
outport.send(msg)