talofer99 icon

play midi

talofer99 | PRO | 11/03/18 09:27:52 PM UTC | 0 ⭐ | 346 👁️ | Never ⏰ | []
Python |

980 B

|

None

|

0 👍

/

0 👎

import pygame.midi
import time
import serial
ser = serial.Serial('COM6', 115200)  # open serial port
 
pygame.midi.init()
 
print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(3)
 
player = pygame.midi.Output(0)
player.set_instrument(0)
 
loopFlag = True
inWaiting = 0
 
 
print 'READY !'
 
while (loopFlag):
    if (ser.in_waiting):
        message = ser.read(3)
        # lets get the command first
        command = ord(message[0])
        note = ord(message[1])
        pitch = ord(message[2])
        print "command - " + str(command) + " note - " + str(note) + " pitch - " + str(pitch)
        #if command is over 143 it menas it start note
        if command > 143:
            #player.set_instrument(command - 144)
            player.note_on(note,pitch,command - 144)
        else :
            #player.set_instrument(command - 128)
            player.note_off(note,pitch,command - 128)
 
 
print 'EXIT'
 
pygame.midi.quit()
ser.close()

Comments