midi_util.py
Go to the documentation of this file.
1 import pygame
2 import pygame.midi
3 
4 class MIDIException(Exception):
5  pass
6 
7 def MIDIParse(message):
8  """returns (type, index, value)"""
9  midi_command = message[0][0]
10  midi_param1 = message[0][1]
11  midi_param2 = message[0][2]
12  timestamp = message[1]
13  command_type = MIDICommand.detect(midi_command)
14  midi_channel = MIDICommand.getChannel(midi_command)
15  if command_type == MIDICommand.NOTE_OFF:
16  return (MIDICommand.NOTE_ON, midi_param1, 0)
17  elif command_type == MIDICommand.NOTE_ON:
18  return (MIDICommand.NOTE_ON, midi_param1, midi_param2 / 127.0)
19  elif command_type == MIDICommand.AFTERTOUCH:
20  return (MIDICommand.AFTERTOUCH, midi_param1, midi_param2 / 127.0)
21  elif command_type == MIDICommand.CONTINUOUS_CONTROLLER:
22  return (MIDICommand.CONTINUOUS_CONTROLLER, midi_param1, midi_param2 / 127.0)
23  elif command_type == MIDICommand.PATCH_CHANGE:
24  return (MIDICommand.PATCH_CHANGE, midi_param1, midi_param2 / 127.0) #??
25  elif command_type == MIDICommand.CHANNEL_PRESSURE:
26  return (MIDICommand.CHANNEL_PRESSURE, midi_channel, midi_param1)
27  elif command_type == MIDICommand.PITCH_BEND:
28  return (MIDICommand.PITCH_BEND, midi_channel, midi_param1 / 127.0)
29  else:
30  raise MIDIException("unknown command type: " + MIDICommand.toStr(command_type))
31 
32 class MIDICommand():
33  NOTE_OFF = 0x80
34  NOTE_ON = 0x90
35  AFTERTOUCH = 0xA0
36  CONTINUOUS_CONTROLLER = 0xB0
37  PATCH_CHANGE = 0xC0
38  CHANNEL_PRESSURE = 0xD0
39  PITCH_BEND = 0xE0
40  NON_MUSICAL = 0xF0
41  @classmethod
42  def detect(cls, val):
43  types = [cls.NOTE_OFF, cls.NOTE_ON, cls.AFTERTOUCH, cls.CONTINUOUS_CONTROLLER,
45  cls.NON_MUSICAL]
46  for t in types:
47  if cls.checkUpperByte(t, val):
48  return t
49  raise MIDIException("cannot detect the type of " + str(val))
50  @classmethod
51  def allCommands(cls):
52  return [cls.NOTE_OFF, cls.NOTE_ON, cls.AFTERTOUCH, cls.CONTINUOUS_CONTROLLER,
54  cls.NON_MUSICAL]
55  @classmethod
56  def checkUpperByte(cls, ref, val):
57  return (ref >> 4) == (val >> 4)
58  @classmethod
59  def getChannel(cls, val):
60  return (0x0F & val)
61  @classmethod
62  def toStr(cls, val):
63  if val == cls.NOTE_OFF:
64  return "Note-off"
65  elif val == cls.NOTE_ON:
66  return "Note-on"
67  elif val == cls.AFTERTOUCH:
68  return "Aftertouch"
69  elif val == cls.CONTINUOUS_CONTROLLER:
70  return "ContinuousController"
71  elif val == cls.PATCH_CHANGE:
72  return "Patch Change"
73  elif val == cls.CHANNEL_PRESSURE:
74  return "Channel Pressure"
75  elif val == cls.PITCH_BEND:
76  return "Pitch Bend"
77  elif val == cls.NON_MUSICAL:
78  return "Non Musical Command"
79  else:
80  raise MIDIException("Unknown command: " + str(val))
81 
82 def openMIDIInputByName(device_name):
83  return openMIDIByName(device_name, 1)
84 
85 def openMIDIOutputByName(device_name):
86  return openMIDIByName(device_name, 0)
87 
88 def openMIDIByName(device_name, input_output):
89  devices = pygame.midi.get_count()
90  for i in range(devices):
91  info = pygame.midi.get_device_info(i)
92  if info[1] == device_name and info[2] == input_output:
93  if input_output == 1:
94  return pygame.midi.Input(i)
95  else:
96  return pygame.midi.Output(i)
97  raise MIDIException("Cannot find the device: %s" % (device_name))
98 
jsk_teleop_joy.midi_util.MIDICommand.CONTINUOUS_CONTROLLER
int CONTINUOUS_CONTROLLER
Definition: midi_util.py:36
jsk_teleop_joy.midi_util.openMIDIInputByName
def openMIDIInputByName(device_name)
Definition: midi_util.py:82
jsk_teleop_joy.midi_util.MIDIParse
def MIDIParse(message)
Definition: midi_util.py:7
jsk_teleop_joy.midi_util.openMIDIByName
def openMIDIByName(device_name, input_output)
Definition: midi_util.py:88
jsk_teleop_joy.midi_util.MIDICommand.NON_MUSICAL
int NON_MUSICAL
Definition: midi_util.py:40
jsk_teleop_joy.midi_util.MIDICommand.toStr
def toStr(cls, val)
Definition: midi_util.py:62
jsk_teleop_joy.midi_util.MIDICommand.getChannel
def getChannel(cls, val)
Definition: midi_util.py:59
jsk_teleop_joy.midi_util.MIDICommand
Definition: midi_util.py:32
jsk_teleop_joy.midi_util.MIDICommand.NOTE_OFF
int NOTE_OFF
Definition: midi_util.py:33
jsk_teleop_joy.midi_util.openMIDIOutputByName
def openMIDIOutputByName(device_name)
Definition: midi_util.py:85
jsk_teleop_joy.midi_util.MIDICommand.detect
def detect(cls, val)
Definition: midi_util.py:42
jsk_teleop_joy.midi_util.MIDICommand.checkUpperByte
def checkUpperByte(cls, ref, val)
Definition: midi_util.py:56
jsk_teleop_joy.midi_util.MIDICommand.PITCH_BEND
int PITCH_BEND
Definition: midi_util.py:39
jsk_teleop_joy.midi_util.MIDIException
Definition: midi_util.py:4
jsk_teleop_joy.midi_util.MIDICommand.PATCH_CHANGE
int PATCH_CHANGE
Definition: midi_util.py:37
jsk_teleop_joy.midi_util.MIDICommand.CHANNEL_PRESSURE
int CHANNEL_PRESSURE
Definition: midi_util.py:38
jsk_teleop_joy.midi_util.MIDICommand.AFTERTOUCH
int AFTERTOUCH
Definition: midi_util.py:35
jsk_teleop_joy.midi_util.MIDICommand.allCommands
def allCommands(cls)
Definition: midi_util.py:51
jsk_teleop_joy.midi_util.MIDICommand.NOTE_ON
int NOTE_ON
Definition: midi_util.py:34


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Wed Jan 24 2024 04:05:49