diff options
Diffstat (limited to 'faust2axo.py')
-rwxr-xr-x | faust2axo.py | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/faust2axo.py b/faust2axo.py index 7a65001..c37a507 100755 --- a/faust2axo.py +++ b/faust2axo.py @@ -3,6 +3,7 @@ import sys import argparse import subprocess +import uuid import xml.etree.ElementTree as ET @@ -45,18 +46,28 @@ def process_sliders(xml): else: param_type = "frac32.s.map" - inlets += f"<frac32 name=\"{label}\" />\n" - params += f"<{param_type} name=\"{label}\" />\n" + inlets += f'<frac32 name="{label}" />\n' + params += f'<{param_type} name="{label}" />\n' # TODO: summation and conversion based on type if pitch: - output += f"uint32_t {label} = mtof48k_ext_q31(param_{label} + inlet_{label});\n" + output += ( + f"uint32_t {label} = mtof48k_ext_q31(param_{label} + inlet_{label});\n" + ) else: output += f"uint32_t {label} = param_{label} + inlet_{label};\n" output += f"mdsp.{varname} = fminf(1.0f, q27_to_float({label})) * {max};\n" return (inlets, params, output) +def safe_extract_text(xml, xpath, alternative): + result = xml.find(xpath) + if isinstance(result, ET.Element): + return result.text + else: + return alternative + + def massage_output(oldfile, newfile, xml): """ Process the Faust output. @@ -65,15 +76,35 @@ def massage_output(oldfile, newfile, xml): # - throw everything before <objdefs> away # - throw everything after </objdefs> away # - remove extern "C" chunk (wrapped in "#ifdef __cplusplus") + # - inject params and inlets # - replace scaling factors for sliders # - replace metadata + id = uuid.uuid4() + author = safe_extract_text(xml, "./author", "unknown") + name = safe_extract_text(xml, "./name", "unnamed") + license = safe_extract_text(xml, "./license", "CC-SA 4.0") + description = safe_extract_text( + xml, "./description", "Generated from Faust DSP code." + ) + inlets, params, conversions = process_sliders(xml) - + seen_start = False seen_end = False skip_ifdef = False + replacements = { + "UUID": str(id), + "AUTHOR": author, + "NAME": name, + "LICENSE": license, + "DESCRIPTION": description, + "INLETS": inlets, + "PARAMS": params, + "SLIDERS": conversions, + } + with open(oldfile, "r", encoding="utf-8") as infile, open( newfile, "w", encoding="utf-8" ) as outfile: @@ -87,18 +118,6 @@ def massage_output(oldfile, newfile, xml): return if seen_start: - if "<<INLETS>>" in line: - outfile.write(line.replace("<<INLETS>>", inlets)) - continue - - if "<<PARAMS>>" in line: - outfile.write(line.replace("<<PARAMS>>", params)) - continue - - if "<<SLIDERS>>" in line: - outfile.write(line.replace("<<SLIDERS>>", conversions)) - continue - if line.startswith("#ifdef __cplusplus"): skip_ifdef = True @@ -107,6 +126,10 @@ def massage_output(oldfile, newfile, xml): skip_ifdef = False continue + for key, value in replacements.items(): + if "<<" + key + ">>" in line: + line = line.replace("<<" + key + ">>", value) + outfile.write(line) else: continue |