Expand source code
def run_timebase(files: t.List[str], gcd: int) -> t.List[str]:
tmp_paths = []
for f in files:
with open(f) as inf:
data = inf.read()
recips = re.findall(r"\d%(\d+)", data)
if recips:
# because `timebase`, being from the original humdrum toolkit, doesn't
# understand the "%" notation (e.g., 2%3), we first use rscale to scale up
# the file to the gcd of the recips (ensuring there will be no %), and
# then afterwards scale it back
recip = math.gcd(*[int(r) for r in recips])
_, tmp_path = tempfile.mkstemp(suffix=".krn")
rscale1_result = sh.rscale("-f", f"1/{recip}", f) # type:ignore
with open(tmp_path, "w") as outf:
outf.write(rscale1_result)
timebase_result = sh.timebase( # type:ignore
"-t", str(gcd), tmp_path
)
with open(tmp_path, "w") as outf:
outf.write(timebase_result)
rscale2_result = sh.rscale("-f", f"{recip}", tmp_path) # type:ignore
with open(tmp_path, "w") as outf:
outf.write(rscale2_result)
tmp_paths.append(tmp_path)
else:
result = sh.timebase("-t", str(gcd), f) # type:ignore
_, tmp_path = tempfile.mkstemp(suffix=".krn")
with open(tmp_path, "w") as outf:
outf.write(result)
tmp_paths.append(tmp_path)
return tmp_paths