midi_write.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import pygame
3 import pygame.midi
4 import select
5 import sys
6 import yaml
7 import rospy
8 import roslib
9 import time
10 import re
11 from optparse import OptionParser
12 
13 roslib.load_manifest('jsk_teleop_joy')
14 from jsk_teleop_joy.midi_util import MIDICommand, MIDIParse
15 
16 class MIDIWriteError(Exception):
17  pass
18 
19 def parseRangedNumber(string):
20  """
21  string = 0-9 or [number-number] and this returns list of the numbers
22  """
23  try: #0-0
24  val = int(string)
25  return [val]
26  except ValueError:
27  if string.startswith("[") and string.endswith("]"):
28  # range representation
29  r = re.compile("\[([0-9]+)-([0-9]+)\]")
30  m = r.search(string)
31  if m:
32  first_number = m.group(1)
33  last_number = m.group(2)
34  return range(int(first_number), int(last_number) + 1)
35  else:
36  raise MIDIWriteError("failed to parse string: %s" % (string))
37  else:
38  raise MIDIWriteError("failed to parse string: %s" % (string))
39 
40 
41 def main():
42 
43  all_midi_commands = ""
44  for c in MIDICommand.allCommands():
45  midi_command_str = "%s (%d)" % (MIDICommand.toStr(c), c)
46  if len(all_midi_commands) > 0:
47  all_midi_commands = "%s, %s (%d)" % (all_midi_commands, MIDICommand.toStr(c),
48  c)
49  else:
50  all_midi_commands = midi_command_str
51  parser = OptionParser()
52  parser.add_option("--device_index", "-d", help="device index", type="int")
53  parser.add_option("--midi_command", "-m",
54  help="midi command: candidates are: " + all_midi_commands)
55  parser.add_option("--channel", "-c", help="channel. You can specify the number in 0-9 or [from-to] format", default="0")
56  parser.add_option("--param1", "-p", help="param1. You can specify the number in 0-9 or [from-to] format")
57  parser.add_option("--param2", "-P", help="param2. You can specify the number in 0-9 or [from-to] format")
58  parser.add_option("--interval", "-i", help="interval between each command", default=0.1, type="float")
59  parser.add_option("--write", "-w", help="insert this output configuration into the configuration yaml")
60  parser.add_option("--use-param1", help="this option is only available with -w option. It specifies to use parameter1 for the intensity", action="store_true")
61  parser.add_option("--verbose", "-v", help="verbose", action="store_true")
62  (options, args) = parser.parse_args()
63 
64  # check the argument
65  if options.device_index == None:
66  raise MIDIWriteError("You need to specify --device_index option")
67  if options.midi_command == None:
68  raise MIDIWriteError("You need to specify --midi_command option")
69  if not options.write:
70  if options.param1 == None:
71  raise MIDIWriteError("You need to specify --param1 option")
72  if options.param2 == None:
73  raise MIDIWriteError("You need to specify --param2 option")
74  else:
75  pass
76 
77  # parse midi command
78  midi_command = None
79  try:
80  midi_command = int(options.midi_command)
81  except ValueError:
82  # failed to parse integer, it might be a string
83  for candidate in MIDICommand.allCommands():
84  if MIDICommand.toStr(candidate).lower() == options.midi_command.lower():
85  midi_command = candidate
86 
87  if midi_command == None:
88  raise MIDIWriteError("Unknown midi command: %s" % (options.midi_command))
89 
90  # adding channel to midi_command
91  pygame.midi.init()
92  controller = pygame.midi.Output(options.device_index)
93  if not options.write:
94  for channel in parseRangedNumber(options.channel):
95  midi_command_w_channel = midi_command | channel
96  for param1 in parseRangedNumber(options.param1):
97  for param2 in parseRangedNumber(options.param2):
98  if options.verbose:
99  print("Writing [%d, %d, %d] ([0x%X, 0x%X, 0x%X])" % (midi_command_w_channel, param1, param2,
100  midi_command_w_channel, param1, param2))
101  controller.write_short(midi_command_w_channel, param1, param2)
102  rospy.sleep(options.interval)
103  else:
104  # open the yaml
105  # type, id, intensity
106  # configuration := command, channel, use_param1[, param1_value]
107  with open(options.write, "r+") as f:
108  config = yaml.load(f)
109  if config.has_key("output"):
110  output = config["output"]
111  else:
112  output = []
113  for channel in parseRangedNumber(options.channel):
114  if options.use_param1:
115  configuration = (midi_command, channel, True)
116  if configuration not in output:
117  output.append(configuration)
118  else:
119  for param1 in parseRangedNumber(options.param1):
120  configuration = (midi_command, channel, False, param1)
121  if configuration not in output:
122  output.append(configuration)
123  print(configuration)
124  config["output"] = output
125  f.write(yaml.dump(config))
126  f.close()
127 if __name__ == "__main__":
128  main()
def main()
Definition: midi_write.py:41
def parseRangedNumber(string)
Definition: midi_write.py:19


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri May 14 2021 02:52:11