Skip to content

Instantly share code, notes, and snippets.

@aelkiss
Last active May 12, 2023 02:06
Show Gist options
  • Save aelkiss/391b7ace86e9837e1cbd017fc3d77eeb to your computer and use it in GitHub Desktop.
Save aelkiss/391b7ace86e9837e1cbd017fc3d77eeb to your computer and use it in GitHub Desktop.
remove ties from lilypond
# Useful for adapting editions of early music to create versions without barlines
# Ties must not cross lines
# Before running this, use the "explicit durations" transformation in Frescobaldi
TIE_RE = %r(([a-g][fs]?)([,']?)(\d)\s*~\s*\|\s*\1(\d))
ARGF.readlines.each do |line|
# STDERR.puts "Original line: #{line}"
while(line.match?(TIE_RE))
line.match(TIE_RE) do |m|
(pitch, octave, dur1, dur2) = m.captures
if(dur1 == "1" && dur2 == "1")
newdur = '\breve'
elsif(dur1 == dur2)
newdur = (dur1.to_i / 2).to_s
elsif dur1.to_i == (dur2.to_i / 2)
newdur = "#{dur1}."
elsif dur2.to_i == (dur1.to_i / 2)
newdur = "#{dur2}."
else
STDERR.puts("??? weird tie '#{s}'")
next
end
newstuff = "#{pitch}#{octave}#{newdur}"
STDERR.puts("Replaced #{m} with #{newstuff}")
line = "#{m.pre_match}#{pitch}#{octave}#{newdur}#{m.post_match}"
end
end
if line.match?(/~/)
STDERR.puts("line #{line} still has ties??")
end
puts line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment