Sha256: 71c1284a16785862db50ea1504f2c3ee32939bedb92bdf2b779fcddf529da81b

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

module HeadMusic::Style::Annotations
end

class HeadMusic::Style::Annotations::NotesSameLength < HeadMusic::Style::Annotation
  def message
    'Use consistent rhythmic unit.'
  end

  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

9 entries across 9 versions & 1 rubygems

Version Path
head_music-0.11.9 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.8 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.7 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.6 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.5 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.4 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.3 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.2 lib/head_music/style/annotations/notes_same_length.rb
head_music-0.11.1 lib/head_music/style/annotations/notes_same_length.rb