-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paths3midi
executable file
·141 lines (130 loc) · 5.48 KB
/
s3midi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import argparse, os, sys, time, atexit
#from s3turbo.MidiHandler import MidiHandler as MidiHandler
from s3turbo.RtMidiHandler import RtMidiHandler as MidiHandler
from s3turbo.SysExParser import SysExParser
from s3turbo.MSCEIMessage import MSCEIMessage
from s3turbo.SampleDumpHandler import SampleDumpHandler
from s3turbo.StandardHandler import StandardHandler
from s3turbo.S3Turbo import S3Exception
from s3turbo.Util import is_sysex, str2file, str2hex
def main():
# command-line parsing
parser = argparse.ArgumentParser(
description='Send and receive midi messages')
parser.add_argument('-i', '--indev', type=int, default=-1, action='store',
help='MIDI input device')
parser.add_argument('-o', '--outdev', type=int, default=-1, action='store',
help='MIDI output device')
parser.add_argument('-l', '--listdev', action='store_true',
help='list MIDI devices and exit')
parser.add_argument('-c', '--command', type=str, action='store',
help='send MIDI command')
parser.add_argument('-v', '--verbose', action='store_true',
help='enable verbose output')
parser.add_argument('-e', '--exit', action='store_true',
help='exit after executing command')
parser.add_argument('--samples', nargs="+",
help='list of SDS files for sample upload')
parser.add_argument('-f', '--file', type=str2file, action='append',
dest='payload', metavar="FILE",
help='add filename to payload' +
'(will be whitespace-filled to 11 characters length)')
parser.add_argument('-p', '--path', type=str2hex, action='append',
dest='payload', metavar='PATH',
help='add path to payload' +
'(will be 0-terminated, max 80 chars)')
parser.add_argument('-d', '--int', type=int, action='append',
dest='payload', nargs='+', metavar='NUMBER',
help='add integer to payload')
parser.add_argument('-x', '--hex', type=lambda x: int(x, 16), nargs='+',
action='append', dest='payload', metavar='HEXNUMBER',
help='add hexadecimal integer to payload')
# set defaults from configuration file
confargs = {}
conffile = os.path.join( os.environ["HOME"], ".s3turbo.conf")
if os.path.exists(conffile):
for line in open(conffile):
k,v = line.strip().split(':')
confargs[k] = v
parser.set_defaults(**confargs)
args = parser.parse_args()
payload = []
if args.payload:
for i in args.payload:
if type(i) == type(list()):
payload += i
else:
payload.append(i)
midi = MidiHandler()
atexit.register(midi.stop)
try:
# list MIDI devices and exit
if args.listdev:
print "Inputs:"
midi.print_dev(MidiHandler.INPUT)
print "Outputs:"
midi.print_dev(MidiHandler.OUTPUT)
sys.exit(0)
# MidiHandler, filter clock messages
filter = [
[ 0xf8 ], # midi clock
]
midi.initialize(indev=args.indev, outdev=args.outdev,
msgfilter=filter, debug=args.verbose)
# SysExParser
parser = SysExParser(send_func=midi.send,debug=args.verbose)
if args.command:
parser.sendSysEx(MSCEIMessage(*payload, fromName=args.command))
elif len(payload):
midi.send( (0,payload))
standard_handler = StandardHandler(debug=args.verbose)
# poll for incoming messages
currentHandler = None
while True:
if midi.poll(1):
timestamp, msg = midi.recv()
if not len(msg):
continue
if not is_sysex(msg):
if not standard_handler.handle(msg,timestamp): break
else: continue
if msg[1] == 0x7E:
# SampleDumpHeader, SampleDumpDataPacket, SampleDumpRequest
if msg[3] in [ 0x1, 0x2, 0x3 ]:
if not currentHandler:
currentHandler = SampleDumpHandler(
debug=args.verbose,samplelist=args.samples)
midi.send((0,currentHandler.parse(msg)))
# WAIT
elif msg[3] == 0x7C:
pass
# CANCEL
elif msg[3] == 0x7D:
if currentHandler:
del currentHandler
currentHandler = None
# ACK
elif msg[3] == 0x7F:
if currentHandler:
midi.send((0,currentHandler.parse(msg)))
else:
if not parser.parse(msg, timestamp): break
elif parser.dump_ram:
parser.stopDump()
break
elif args.exit:
break
except EOFError, e:
print "EOFError", e
raise
sys.exit(1)
except KeyboardInterrupt:
if parser.dump_on:
parser.sendSysEx(MSCEIMessage(fromName="F_CANCEL"))
print
except S3Exception, e:
print e
sys.exit(1)
if __name__ == '__main__':
main()