-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduleprocess.py
177 lines (153 loc) · 5.2 KB
/
moduleprocess.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
175
176
177
import re
import sys
class Definition:
def __init__(_):
_.state = None
_.indentation = 0
_.parent_indent = 0
_.name = None
_.stereo = False
_.parameters = []
_.fill = ""
_.misc = ""
_.output_range = ""
_.initialize = ""
_.members = []
_.input_range = []
def saveTranslation(_):
print """
class %s : public %sModule
{
public:
%s(vector<ModuleParam *> parameters)
{""" % (_.name, _.stereo, _.name)
n = 0
for p in _.parameters:
print " m_%s = parameters[%d]->m_%s;" % (p[0], n, p[1].lower())
n += 1
for m in _.members: print " m_%s = %s;" % (m[0], m[2])
print _.initialize
print """ }
static Module *create(vector<ModuleParam *> parameters)
{
return new %s(parameters);
}
""" % _.name
print " const char *moduleName() { return \"%s\"; }" % _.name
print _.misc
print """
void fill(float last_fill, int samples)
{
float *output = m_output;"""
for p in _.parameters:
if p[1].endswith("Module"):
print " const float *%s = m_%s->output" % (p[0], p[0]) + \
"(last_fill, samples, m_sample_rate);"
print """
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
%s }
g_profiler.addTime("%s", hires_time()-time);
}""" % (_.fill, _.name)
print """
void getOutputRange(float *out_min, float *out_max)
{
%s }
void validateInputRange()
{""" % (_.output_range)
for i in _.input_range:
print " validateWithin(*m_%s, %s, %s);" % i
print " }"
print " private:"
for p in _.parameters:
type = p[1]+" *" if p[1].endswith("Module") else p[1]+" "
print " %sm_%s;" % (type, p[0])
for m in _.members: print " %s m_%s;" % (m[1], m[0])
print "};"
def parseModuleLine(_, line):
m = re.match("(stereo)?module (.+)\((.*)\):", line)
if not m: raise "expected module definition in "+line
stereo, _.name, params = m.groups()
if stereo: _.stereo = "Stereo"
else: _.stereo = ""
for p in re.split(",\s+", params):
tp = p.split(" ")
name = tp[0]
type = "Module"
if len(tp) > 1: type, name = tp
default = None
name_default = name.split("=")
if len(name_default) > 1: name, default = name_default
if name!="": _.parameters.append((name, type, default))
def parseMemberLine(_, line):
line = line.strip()
space = line.find(" ")
assert space > -1
type = line[:space]
for m in line[space+1:].split(","):
member = m.split("=")
name = member[0].strip()
value = member[1].strip() if len(member) > 1 else None
_.members.append((name, type, value))
def parseFillLine (_, line): _.fill += " " + line + "\n"
def parseMiscLine (_, line): _.misc += line + "\n"
def parseOutputRangeLine(_, line): _.output_range += " " + line + "\n"
def parseInitializeLine (_, line): _.initialize += " " + line + "\n"
def parseInputRangeLine(_, line):
name, params = line.split(":")
params = params.split(",")
_.input_range.append((name.strip(), params[0].strip(), params[1].strip()))
def parseLine(_, line):
if len(line.strip()) == 0: return
_.indent = len(line) - len(line.lstrip())
if _.parent_indent == 0: _.parent_indent = _.indent
if _.indent == _.parent_indent:
_.state = None
_.indentation
if line.startswith("module") or line.startswith("stereomodule"):
_.parseModuleLine(line)
return
if _.state == None and line.endswith(":"):
_.state = line[:-1].lstrip()
return
if _.state == "members": _.parseMemberLine (line)
elif _.state == "fill": _.parseFillLine (line)
elif _.state == "initialize": _.parseInitializeLine (line)
elif _.state == "output_range": _.parseOutputRangeLine(line)
elif _.state == "input_range": _.parseInputRangeLine (line)
elif _.state == "misc": _.parseMiscLine (line)
else:
raise "Didn't know what to do with " + line + " in state: " + str(_.state)
def writeModuleMetadata(modules):
print """void fillModuleList()
{""";
for m in modules:
print " g_module_infos[\"%s\"] = new ModuleInfo(\"%s\", %s::create);" \
% (m.name, m.name, m.name)
for p in m.parameters:
print " g_module_infos[\"%s\"]->addParameter(\"%s\", \"%s\"%s);" % \
(m.name, p[0], p[1], ", %s"%p[2] if p[2] else "")
print "}"
def main():
print "// Generated code! Poke around in modules.pre and " + \
"moduleprocess.py, not here."
module_list = []
definition = None
for line in sys.stdin.xreadlines():
if definition == None:
if line[0] == "@":
line = line[1:]
definition = Definition()
else:
print line.rstrip()
if definition != None:
if line[0] == "@":
definition.saveTranslation()
module_list.append(definition)
definition = None
else:
definition.parseLine(line.rstrip())
writeModuleMetadata(module_list)
if __name__ == "__main__": main()