From 876c2d271a84693da15ef0678b456b758c8ad8fe Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Tue, 30 Apr 2024 10:03:38 +0200 Subject: Fill metadata from the Faust source file. --- faust-ksoloti-object.c | 8 ++++---- faust2axo.py | 55 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/faust-ksoloti-object.c b/faust-ksoloti-object.c index 97128fe..8524906 100644 --- a/faust-ksoloti-object.c +++ b/faust-ksoloti-object.c @@ -1,8 +1,8 @@ - - Moog VCF (moog_vcf_2b) - Ricardo Wurmus (Ksoloti), Julius O. Smith III (Faust) - STK-4.3 + + <> + <> + <> math.h 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"\n" - params += f"<{param_type} name=\"{label}\" />\n" + inlets += f'\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 away # - throw everything after 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 "<>" in line: - outfile.write(line.replace("<>", inlets)) - continue - - if "<>" in line: - outfile.write(line.replace("<>", params)) - continue - - if "<>" in line: - outfile.write(line.replace("<>", 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 -- cgit v1.2.3