forked from enzienaudio/hvcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hvcc.py
409 lines (363 loc) · 15.3 KB
/
hvcc.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (C) 2014-2018 Enzien Audio, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
from collections import OrderedDict
import json
import os
import re
import time
import interpreters.pd2hv.pd2hv as pd2hv
import interpreters.max2hv.max2hv as max2hv
import core.hv2ir.hv2ir as hv2ir
import generators.ir2c.ir2c as ir2c
import generators.ir2c.ir2c_perf as ir2c_perf
import generators.c2bela.c2bela as c2bela
import generators.c2fabric.c2fabric as c2fabric
import generators.c2js.c2js as c2js
import generators.c2pdext.c2pdext as c2pdext
import generators.c2wwise.c2wwise as c2wwise
import generators.c2unity.c2unity as c2unity
import generators.c2vst2.c2vst2 as c2vst2
class Colours:
purple = "\033[95m"
cyan = "\033[96m"
dark_cyan = "\033[36m"
blue = "\033[94m"
green = "\033[92m"
yellow = "\033[93m"
red = "\033[91m"
bold = "\033[1m"
underline = "\033[4m"
end = "\033[0m"
def add_error(results, error):
if "hvcc" in results:
results["hvcc"]["notifs"]["errors"].append({"message": error})
else:
results["hvcc"] = {
"stage": "hvcc",
"notifs": {
"has_error": True,
"exception": None,
"errors": [{"message": error}],
"warnings": []
}
}
return results
def check_extern_name_conflicts(extern_type, extern_list, results):
""" In most of the generator code extern names become capitalised when used
as enums. This method makes sure that there are no cases where two unique
keys become the same after being capitalised.
Note(joe): hvcc is probably the best place to check as at this point we
have a list of all extern names.
"""
for i, v in enumerate(extern_list):
for j, u in enumerate(extern_list[i+1:]):
if v[0].upper() == u[0].upper():
add_error(results,
"Conflicting {0} names '{1}' and '{2}', make sure that "
"capital letters are not the only difference.".format(
extern_type, v[0], u[0]))
def generate_extern_info(hvir, results):
""" Simplifies the receiver/send and table lists by only containing values
externed with @hv_param, @hv_event or @hv_table
"""
# Exposed input parameters
in_parameter_list = [(k, v) for k, v in hvir["control"]["receivers"].iteritems() if v.get("extern", None) == "param"]
in_parameter_list.sort(key=lambda x: x[0])
check_extern_name_conflicts("input parameter", in_parameter_list, results)
# Exposed input events
in_event_list = [(k, v) for k, v in hvir["control"]["receivers"].iteritems() if v.get("extern", None) == "event"]
in_event_list.sort(key=lambda x: x[0])
check_extern_name_conflicts("input event", in_event_list, results)
# Exposed output parameters
out_parameter_list = [(v["name"], v) for v in hvir["control"]["sendMessage"] if v.get("extern", None) == "param"]
# remove duplicate output parameters/events
# NOTE(joe): is the id argument important here? We'll only take the first one in this case.
out_parameter_list = list(dict(out_parameter_list).iteritems())
out_parameter_list.sort(key=lambda x: x[0])
check_extern_name_conflicts("output parameter", out_parameter_list, results)
# Exposed output events
out_event_list = [(v["name"], v) for v in hvir["control"]["sendMessage"] if v.get("extern", None) == "event"]
out_event_list = list(dict(out_event_list).iteritems())
out_event_list.sort(key=lambda x: x[0])
check_extern_name_conflicts("output event", out_event_list, results)
# Exposed tables
table_list = [(k, v) for k, v in hvir["tables"].iteritems() if v.get("extern", None) == True]
table_list.sort(key=lambda x: x[0])
check_extern_name_conflicts("table", table_list, results)
return {
"parameters": {
"in": in_parameter_list,
"out": out_parameter_list
},
"events": {
"in": in_event_list,
"out": out_event_list
},
"tables": table_list,
# generate patch heuristics to ensure enough memory allocated for the patch
"memoryPoolSizesKb": {
"internal": 10, # TODO(joe): should this increase if there are a lot of internal connections?
"inputQueue": max(2, int(len(in_parameter_list) + len(in_event_list) / 4)),
"outputQueue": max(2, int(len(out_parameter_list) + len(out_event_list) / 4)),
}
}
def compile_dataflow(in_path, out_dir, patch_name=None,
search_paths=None, generators=None, verbose=False,
copyright=None, hvir=None):
results = OrderedDict() # default value, empty dictionary
# basic error checking on input
if os.path.isfile(in_path):
if not in_path.endswith((".pd", ".maxpat")):
return add_error(results, "Can only process Pd or Max files.")
elif os.path.isdir(in_path):
if not os.path.basename("c"):
return add_error(results, "Can only process c directories.")
else:
return add_error(results, "Unknown input path {0}".format(in_path))
patch_name = patch_name or "heavy"
generators = generators or {"c"}
if in_path.endswith((".pd", ".maxpat")):
if verbose:
print "--> Generating C"
if in_path.endswith(".pd"):
results["pd2hv"] = pd2hv.pd2hv.compile(
pd_path=in_path,
hv_dir=os.path.join(out_dir, "hv"),
search_paths=search_paths,
verbose=verbose)
elif in_path.endswith(".maxpat"):
results["max2hv"] = max2hv.max2hv.compile(
max_path=in_path,
hv_dir=os.path.join(out_dir, "hv"),
search_paths=search_paths,
verbose=verbose)
# check for errors
if results.values()[0]["notifs"].get("has_error", False):
return results
results["hv2ir"] = hv2ir.hv2ir.compile(
hv_file=os.path.join(results.values()[0]["out_dir"], results.values()[0]["out_file"]),
# ensure that the ir filename has no funky characters in it
ir_file=os.path.join(out_dir, "ir", re.sub("\W", "_", patch_name)+".heavy.ir.json"),
patch_name=patch_name,
verbose=verbose)
# check for errors
if results["hv2ir"]["notifs"].get("has_error", False):
return results
# get the hvir data
hvir = results["hv2ir"]["ir"]
patch_name = hvir["name"]["escaped"]
externs = generate_extern_info(hvir, results)
c_src_dir = os.path.join(out_dir, "c")
results["ir2c"] = ir2c.ir2c.compile(
hv_ir_path=os.path.join(results["hv2ir"]["out_dir"], results["hv2ir"]["out_file"]),
static_dir=os.path.join(os.path.dirname(__file__), "generators/ir2c/static"),
output_dir=c_src_dir,
externs=externs,
copyright=copyright)
# check for errors
if results["ir2c"]["notifs"].get("has_error", False):
return results
# ir2c_perf
results["ir2c_perf"] = {
"stage": "ir2c_perf",
"obj_counter": ir2c_perf.ir2c_perf.perf(results["hv2ir"]["ir"], verbose=verbose),
"in_dir": results["hv2ir"]["out_dir"],
"in_file": results["hv2ir"]["out_file"],
"notifs": {}
}
# reconfigure such that next stage is triggered
in_path = c_src_dir
if os.path.isdir(in_path) and os.path.basename(in_path) == "c":
# the c code is provided
c_src_dir = in_path
if hvir is None:
# if hvir ir not provided, load it from the ir path
try:
# hvir_dir == project/c/../ir == project/ir
hvir_dir = os.path.join(in_path, "..", "ir")
hvir_path = os.path.join(hvir_dir, os.listdir(hvir_dir)[0])
if os.path.isfile(hvir_path):
with open(hvir_path, "r") as f:
hvir = json.load(f)