Skip to content

Commit

Permalink
Merge pull request #81 from napulen/rntxt_fix
Browse files Browse the repository at this point in the history
Improve the rntxt output writer.
  • Loading branch information
napulen authored Jun 20, 2022
2 parents c293a90 + be76bbb commit c67d5d4
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions AugmentedNet/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,6 @@ def resolveRomanNumeral(b, t, a, s, pcs, key, tonicizedKey):
return rn, chordLabel


def _correctRomanText(rntxt):
modified = rntxt.split("\n")
for firstMeasureLine, line in enumerate(modified):
if line.startswith("m"):
break
m = modified[firstMeasureLine].split()
# If there is no Measure 1, inject one
if m[0] not in ["m0", "m1"]:
print("\tInjected measure 1")
inject = f"m1 {' '.join(m[1:4])}"
modified.insert(firstMeasureLine, inject)
# If there is no Beat 1, inject one
m = modified[firstMeasureLine].split()
if m[0] == "m1" and m[2] != "b1":
print("\tInjected beat 1")
inject = f"b1 {m[3]}"
m.insert(2, inject)
modified[firstMeasureLine] = " ".join(m)
return "\n".join(modified)


def generateRomanText(h):
metadata = h.metadata
metadata.composer = metadata.composer or "Unknown"
Expand All @@ -137,19 +116,21 @@ def generateRomanText(h):
key = ""
measure = n.measureNumber
beat = float(n.beat)
if beat.is_integer():
beat = int(beat)
newts = ts.get((measure, beat), None)
if newts:
rntxt += f"\nTime Signature: {newts}\n"
if abs(beat - int(beat)) < 0.001:
beat = int(beat)
if ":" in rn:
key, rn = rn.split(":")
if measure != currentMeasure:
rntxt += f"\nm{measure}"
currentMeasure = measure
if beat != 1:
rntxt += f" b{round(beat, 3)}"
if key:
rntxt += f" {key.replace('-', 'b')}:"
rntxt += f" b{round(beat, 3)} {rn}"
rntxt += f" {rn}"
return rntxt


Expand Down Expand Up @@ -216,8 +197,6 @@ def predict(model, inputPath):
bass.addLyric(formatRomanNumeral(rn2fig, thiskey))
bass.addLyric(formatChordLabel(chordLabel))
rntxt = generateRomanText(s)
rntxt = _correctRomanText(rntxt)
print(rntxt)
filename, _ = inputPath.rsplit(".", 1)
annotatedScore = f"{filename}_annotated.musicxml"
annotationCSV = f"{filename}_annotated.csv"
Expand Down

0 comments on commit c67d5d4

Please sign in to comment.