Skip to content

Instantly share code, notes, and snippets.

@igneus
Created December 12, 2016 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igneus/7098ce45ee4cbae0c5324ff91df7e3c1 to your computer and use it in GitHub Desktop.
Save igneus/7098ce45ee4cbae0c5324ff91df7e3c1 to your computer and use it in GitHub Desktop.
"How melismatic chants do my gabc files contain?"
# takes a list of gabc file paths as commandline arguments,
# for each of them prints syllable count, note count and notes per syllable
require 'grely'
class GabcScore
def syllables_count
i = 0
each_syllable {|s| i += 1 }
i
end
def notes_count
i = 0
each_syllable do |s|
s.notes.each do |music_something|
i += 1 if music_something.is_a? GabcNote
end
end
i
end
def each_syllable
music.words.each do |word|
word.each_syllable do |syllable|
next unless contains_any_notes? syllable
yield syllable
end
end
end
def contains_any_notes?(syllable)
note = syllable.notes.find {|n| n.is_a? GabcNote }
not note.nil?
end
end
def csv(*args)
args.collect(&:to_s).join(';')
end
parser = GabcParser.new
puts csv 'file name', 'syllables', 'notes', 'notes per syllable'
ARGV.each do |fname|
result = parser.parse(File.read(fname))
raise "Failed to parse #{fname}." unless result
score = result.create_score
puts csv fname, score.syllables_count, score.notes_count, '%.2f' % (score.notes_count.to_f / score.syllables_count)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment