midi_util.py
Go to the documentation of this file.
00001 import pygame
00002 import pygame.midi
00003 
00004 class MIDIException(Exception):
00005   pass
00006 
00007 def MIDIParse(message):
00008   """returns (type, index, value)"""
00009   midi_command = message[0][0]
00010   midi_param1 = message[0][1]
00011   midi_param2 = message[0][2]
00012   timestamp = message[1]
00013   command_type = MIDICommand.detect(midi_command)
00014   midi_channel = MIDICommand.getChannel(midi_command)
00015   if command_type == MIDICommand.NOTE_OFF:
00016     return (MIDICommand.NOTE_ON, midi_param1, 0)
00017   elif command_type == MIDICommand.NOTE_ON:
00018     return (MIDICommand.NOTE_ON, midi_param1, midi_param2 / 127.0)
00019   elif command_type == MIDICommand.AFTERTOUCH:
00020     return (MIDICommand.AFTERTOUCH, midi_param1, midi_param2 / 127.0)
00021   elif command_type == MIDICommand.CONTINUOUS_CONTROLLER:
00022     return (MIDICommand.CONTINUOUS_CONTROLLER, midi_param1, midi_param2 / 127.0)
00023   elif command_type == MIDICommand.PATCH_CHANGE:
00024     return (MIDICommand.PATCH_CHANGE, midi_param1, midi_param2 / 127.0)   #??
00025   elif command_type == MIDICommand.CHANNEL_PRESSURE:
00026     return (MIDICommand.CHANNEL_PRESSURE, midi_channel, midi_param1)
00027   elif command_type == MIDICommand.PITCH_BEND:
00028     return (MIDICommand.PITCH_BEND, midi_channel, midi_param1 / 127.0)
00029   else:
00030     raise MIDIException("unknown command type: " + MIDICommand.toStr(command_type))
00031     
00032 class MIDICommand():
00033   NOTE_OFF = 0x80
00034   NOTE_ON = 0x90
00035   AFTERTOUCH = 0xA0
00036   CONTINUOUS_CONTROLLER = 0xB0
00037   PATCH_CHANGE = 0xC0
00038   CHANNEL_PRESSURE = 0xD0
00039   PITCH_BEND = 0xE0
00040   NON_MUSICAL = 0xF0
00041   @classmethod
00042   def detect(cls, val):
00043     types = [cls.NOTE_OFF, cls.NOTE_ON, cls.AFTERTOUCH, cls.CONTINUOUS_CONTROLLER,
00044              cls.PATCH_CHANGE, cls.CHANNEL_PRESSURE, cls.PITCH_BEND,
00045              cls.NON_MUSICAL]
00046     for t in types:
00047       if cls.checkUpperByte(t, val):
00048         return t
00049     raise MIDIException("cannot detect the type of " + str(val))
00050   @classmethod
00051   def allCommands(cls):
00052     return [cls.NOTE_OFF, cls.NOTE_ON, cls.AFTERTOUCH, cls.CONTINUOUS_CONTROLLER,
00053             cls.PATCH_CHANGE, cls.CHANNEL_PRESSURE, cls.PITCH_BEND, 
00054             cls.NON_MUSICAL]
00055   @classmethod
00056   def checkUpperByte(cls, ref, val):
00057     return (ref >> 4) == (val >> 4)
00058   @classmethod
00059   def getChannel(cls, val):
00060     return (0x0F & val)
00061   @classmethod
00062   def toStr(cls, val):
00063     if val == cls.NOTE_OFF:
00064       return "Note-off"
00065     elif val == cls.NOTE_ON:
00066       return "Note-on"
00067     elif val == cls.AFTERTOUCH:
00068       return "Aftertouch"
00069     elif val == cls.CONTINUOUS_CONTROLLER:
00070       return "ContinuousController"
00071     elif val == cls.PATCH_CHANGE:
00072       return "Patch Change"
00073     elif val == cls.CHANNEL_PRESSURE:
00074       return "Channel Pressure"
00075     elif val == cls.PITCH_BEND:
00076       return "Pitch Bend"
00077     elif val == cls.NON_MUSICAL:
00078       return "Non Musical Command"
00079     else:
00080       raise MIDIException("Unknown command: " + str(val))
00081     
00082 def openMIDIInputByName(device_name):
00083   return openMIDIByName(device_name, 1)
00084   
00085 def openMIDIOutputByName(device_name):
00086   return openMIDIByName(device_name, 0)
00087   
00088 def openMIDIByName(device_name, input_output):
00089   devices = pygame.midi.get_count()
00090   for i in range(devices):
00091     info = pygame.midi.get_device_info(i)
00092     if info[1] == device_name and info[2] == input_output:
00093       if input_output == 1:
00094         return pygame.midi.Input(i)
00095       else:
00096         return pygame.midi.Output(i)
00097   raise MIDIException("Cannot find the device: %s" % (device_name))
00098   


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri Apr 19 2019 03:45:43