am-filters/calf2ladspa/calf2lv.py
2019-04-29 17:06:06 +02:00

110 lines
3.2 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import sys
from xml.dom.minidom import parse
import subprocess
import argparse
def parse_params(preset):
return {
par.attributes["name"].value: par.attributes["value"].value
for par in preset.getElementsByTagName("param")
}
# @lru_cache()
def get_filter_url(filtername):
if filtername == "eq8":
return "http://calf.sourceforge.net/plugins/Equalizer8Band"
out = subprocess.check_output("lv2ls").decode("ascii")
urls = [line.strip() for line in out.split("\n")]
for u in urls:
if u.lower().endswith("/" + filtername.lower()):
return u
raise ValueError("filter not found: " + filtername)
def get_params_order(filtername):
filterurl = get_filter_url(filtername)
out = subprocess.check_output(["lv2info", filterurl]).decode("ascii")
params = []
for line in out.split("\n"):
if line.strip().startswith("Symbol:"):
val = line.split()[1].strip()
if val in ("in_l", "in_r", "out_l", "out_r"):
continue
params.append(val)
return params
def usage():
print(
"%s: Convert an XML file created by calfjackhost to "
"ecasound-equivalent code, making use of calf-ladspa" % sys.argv[0]
)
print("\nUsage:")
print(" %s FILENAME.xml [PRESET]" % sys.argv[0])
print(
"\nIf PRESET is omitted, the command will print a list of available "
"presets found in the specified file"
)
print(
"\nIf PRESET is given, the command will convert that preset in "
"ecasound command line"
)
def get_parser():
p = argparse.ArgumentParser()
p.add_argument(
"--function-name",
type=str,
default="calffilter",
help="Generate a liquidsoap function with this name",
)
p.add_argument("fname", nargs=1, help="An XML file generated by calfjackhost")
p.add_argument("presets", nargs="*", help="All the filters you want to use")
p.add_argument(
"--exec", default=False, action="store_true", help="Run, don't print"
)
return p
if __name__ == "__main__":
p = get_parser()
args = p.parse_args()
dom = parse(args.fname[0])
if not args.presets:
print("available presets:")
for preset in dom.getElementsByTagName("preset"):
print("-", preset.attributes["name"].value)
sys.exit(0)
ecasound_args = []
for preset in args.presets:
pr_info = [
p
for p in dom.getElementsByTagName("preset")
if p.attributes["name"].value == preset
][0]
raw_params = parse_params(pr_info)
params_order = get_params_order(pr_info.attributes["plugin"].value)
values = []
for p in params_order:
values.append((p, raw_params[p]))
ecasound_args.append(
"-elv2:%s,%s"
% (
get_filter_url(pr_info.attributes["plugin"].value),
",".join(v[1] for v in values),
)
)
cli = "ecasound -i stdin -o stdout".split()
cli += ecasound_args
if not args.exec:
print(" ".join(cli))
else:
os.execv("/usr/bin/ecasound", cli)