Advertisement
talofer99

play midi

Nov 3rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import pygame.midi
  2. import time
  3. import serial
  4. ser = serial.Serial('COM6', 115200)  # open serial port
  5.  
  6. pygame.midi.init()
  7.  
  8. print pygame.midi.get_default_output_id()
  9. print pygame.midi.get_device_info(3)
  10.  
  11. player = pygame.midi.Output(0)
  12. player.set_instrument(0)
  13.  
  14. loopFlag = True
  15. inWaiting = 0
  16.  
  17.  
  18. print 'READY !'
  19.  
  20. while (loopFlag):
  21.     if (ser.in_waiting):
  22.         message = ser.read(3)
  23.         # lets get the command first
  24.         command = ord(message[0])
  25.         note = ord(message[1])
  26.         pitch = ord(message[2])
  27.         print "command - " + str(command) + " note - " + str(note) + " pitch - " + str(pitch)
  28.         #if command is over 143 it menas it start note
  29.         if command > 143:
  30.             #player.set_instrument(command - 144)
  31.             player.note_on(note,pitch,command - 144)
  32.         else :
  33.             #player.set_instrument(command - 128)
  34.             player.note_off(note,pitch,command - 128)
  35.  
  36.  
  37. print 'EXIT'
  38.  
  39. pygame.midi.quit()
  40. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement