Class: HeadMusic::Style::Guidelines::NoteCountPerBar

Inherits:
Annotation
  • Object
show all
Defined in:
lib/head_music/style/guidelines/note_count_per_bar.rb

Overview

Checks that each middle bar (not first or last) contains an exact number of notes of a given rhythmic value. Configurable via the count: and rhythmic_value: options; subclasses may set COUNT and RHYTHMIC_VALUE defaults.

Direct Known Subclasses

FourPerBar, OnePerBar, ThreePerBar, TwoPerBar

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from HeadMusic::Style::Annotation

Instance Method Details

#check_middle_bar(bar_number) ⇒ Object (private)



32
33
34
35
36
37
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 32

def check_middle_bar(bar_number)
  bar_notes = notes_in_bar(bar_number)
  return if bar_notes.length == count && bar_notes.all? { |note| note.rhythmic_value == rhythmic_value }

  mark_bar(bar_number)
end

#countObject (private)



20
21
22
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 20

def count
  options.fetch(:count) { self.class::COUNT }
end

#mark_bar(bar_number) ⇒ Object (private)



50
51
52
53
54
55
56
57
58
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 50

def mark_bar(bar_number)
  bar_placements = notes_in_bar(bar_number)
  if bar_placements.any?
    HeadMusic::Style::Mark.for_all(bar_placements)
  else
    cf_note = cantus_firmus.notes.detect { |note| note.position.bar_number == bar_number }
    HeadMusic::Style::Mark.for(cf_note) if cf_note
  end
end

#marksObject



8
9
10
11
12
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 8

def marks
  return [] unless cantus_firmus&.notes&.any?

  middle_bars.filter_map { |bar_number| check_middle_bar(bar_number) }
end

#messageObject



14
15
16
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 14

def message
  "Use #{count.humanize} #{rhythmic_unit} #{(count == 1) ? "note" : "notes"} in each middle bar."
end

#middle_barsObject (private)



39
40
41
42
43
44
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 39

def middle_bars
  cf_notes = cantus_firmus.notes
  return [] if cf_notes.length <= 2

  cf_notes[1..-2].map { |note| note.position.bar_number }
end

#notes_in_bar(bar_number) ⇒ Object (private)



46
47
48
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 46

def notes_in_bar(bar_number)
  notes.select { |note| note.position.bar_number == bar_number }
end

#rhythmic_unitObject (private)



24
25
26
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 24

def rhythmic_unit
  options.fetch(:rhythmic_value) { self.class::RHYTHMIC_VALUE }
end

#rhythmic_valueObject (private)



28
29
30
# File 'lib/head_music/style/guidelines/note_count_per_bar.rb', line 28

def rhythmic_value
  @rhythmic_value ||= HeadMusic::Rudiment::RhythmicValue.get(rhythmic_unit)
end