Sha256: 51befc1b88c095eb00563f00bc50dc6375060d993ad701e4cd494f49ea1f2fb1

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

module HeadMusic::Style::Annotations
end

class HeadMusic::Style::Annotations::NotesSameLength < HeadMusic::Style::Annotation
  MESSAGE = 'Use consistent rhythmic unit.'

  def marks
    preferred_value = first_most_common_rhythmic_value
    wrong_length_notes = all_but_last_note.select { |note| note.rhythmic_value != preferred_value }
    HeadMusic::Style::Mark.for_each(wrong_length_notes)
  end

  private

  def all_but_last_note
    notes[0..-2]
  end

  def distinct_values
    all_but_last_note.map(&:rhythmic_value).uniq.length
  end

  def first_most_common_rhythmic_value
    candidates = most_common_rhythmic_values
    first_match = notes.detect { |note| candidates.include?(note.rhythmic_value) }
    first_match ? first_match.rhythmic_value : nil
  end

  def most_common_rhythmic_values
    return [] if notes.empty?
    occurrences = occurrences_by_rhythmic_value
    highest_count = occurrences.values.sort.last
    occurrences.select { |_rhythmic_value, count| count == highest_count }.keys
  end

  def occurrences_by_rhythmic_value
    rhythmic_values.inject(Hash.new(0)) { |hash, value| hash[value] += 1; hash }
  end

  def rhythmic_values
    notes.map(&:rhythmic_value)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
head_music-0.14.8 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.14.7 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.14.5 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.14.4 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.14.2 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.14.1 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.13.2 lib/head_music/style/annotations/notes_same_length.rb