-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcodeklavier.py
executable file
·174 lines (139 loc) · 4.98 KB
/
codeklavier.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"""
CodeKlavier Masterscript
This script will help you run the Codeklavier
"""
import configparser
import getopt
import sys
import time
import importlib
from CK_Setup import Setup, BColors
from CK_rec import CK_Rec
import CK_config
ck_deltatime_mem = []
VERSIONS = ('ckar', 'ckalculator', 'hybrid')
def doHelp():
"""
Show the help for running this script.
"""
print(BColors.HEADER + 'This script will help you to run the 🎹 CodeKlavier 🎹' + BColors.ENDC)
print('')
print(BColors.CKGREEN + 'Usage: ./codeklaver.py [OPTION]' + BColors.ENDC)
print('Where [OPTION] is:')
print(BColors.BOLD + '-h | --help' + BColors.ENDC)
print('Show this help text.')
print('')
print(BColors.BOLD + '-p | --play' + BColors.WARNING + ' <<name>>' + BColors.ENDC)
print('Boot CodeKlavier with version <<name>>')
print('These versions are available: ' + (', ').join(VERSIONS))
print('')
print(BColors.BOLD + '-i | --ini' + BColors.WARNING + ' <<filename.ini>>' + BColors.ENDC)
print('Boot CodeKlavier with configuration from <<filename.ini>>')
print('')
print(BColors.BOLD + '-r | --rec' + BColors.ENDC)
print('Boot CodeKlavier to record MIDI for machine learning')
print('')
print(BColors.BOLD + '-t | --test' + BColors.ENDC)
print('Test and visualize if Codeklavier is receving MIDI')
print('')
print(BColors.UNDERLINE + 'Example:' + BColors.ENDC)
print(BColors.CKGREEN + './codeklavier.py -p hybrid' + BColors.ENDC)
print('')
def miditest(configfile='default_setup.ini'):
"""
Run a basic miditest to see how the CodeKlavier is receiving your midi.
:param string configfile: Path to the configuration file (default: default_setup.ini)
"""
#Read config and settings
config = configparser.ConfigParser()
config.read(configfile, encoding='utf8')
try:
myPort = config['midi'].getint('port')
device_id = config['midi'].getint('noteon_id')
pedal = config['midi'].getint('pedal_id')
sostenuto = config['midi'].getint('pedal_midi_id')
except KeyError:
raise LookupError('Missing key information in the config file.')
if (myPort == None or device_id == None):
raise LookupError('Missing key information in the config file.')
codeK = Setup()
codeK.open_port(myPort)
print('your device id is: ', device_id, '\n')
print("CodeKlavier is ON. Press Control-C to exit.")
try:
while True:
msg = codeK.get_message()
if msg:
message, deltatime = msg
if message[0] in [device_id, pedal, sostenuto]:
print('deltatime: ', deltatime, 'msg: ', message)
time.sleep(0.01)
except KeyboardInterrupt:
print('')
finally:
print("Bye-Bye :(")
codeK.end()
def boot(configfile='default_setup.ini', version=None):
"""
Boot a specific version from the CodeKlavier
:param string configfile: Path to the configuration file (default: default_setup.ini)
:param string version: name of the version to boot
"""
if (version not in VERSIONS):
raise ValueError(BColors.WARNING + 'This version doesn\'t exist. Please retry.' + BColors.ENDC)
if version == 'ckar':
version = 'ckalculator'
module = importlib.import_module(version)
version = getattr(module, version)
eval(version.main(ar_hook=True))
else:
module = importlib.import_module(version)
version = getattr(module, version)
eval(version.main())
if __name__ == '__main__':
"""
Catch the arguments that were used to start this function.
Options are h, c, p, t, r
"""
showHelp = False
test = False
record = False
config = None
play = None
try:
options, args = getopt.getopt(sys.argv[1:],'h:p:i:rt',['help', 'play=', 'ini=', 'rec', 'test'])
selected_options = [x[0] for x in options]
for o, a in options:
if o in ('-h', '--help'):
showHelp = True
if o in ('-p', '--play'):
play = a
if o in ('-i', '--ini'):
config = a
if o in ('-t', '--test'):
test = True
if o in ('-r', '--rec'):
record = True
except getopt.GetoptError:
print('Something went wrong parsing the optional arguments')
if showHelp:
doHelp()
sys.exit(0)
if config == None:
CK_config.inifile = 'default_setup.ini'
else:
CK_config.inifile = config
if test:
miditest(configfile=CK_config.inifile)
sys.exit(0)
if record:
rec = CK_Rec(configfile=CK_config.inifile)
rec.record(framesize=1)
sys.exit(0)
if play:
boot(configfile=config, version=play)
sys.exit(0)
#no arguments -> print help
doHelp()
sys.exit(0)