Class: HeadMusic::Notation::LilyPond::Preflight

Inherits:
Object
  • Object
show all
Includes:
PlacementValidation, PreflightChecks
Defined in:
lib/head_music/notation/lily_pond/preflight.rb

Overview

Rejects compositions that cannot be expressed in the supported LilyPond subset.

Whole-composition problems (no voices, positional gaps, underfilled final bars, barline-crossing notes, unpitched sounds) raise RenderError here, before the Writer assembles any output — so a successful check! is the Writer’s guarantee that assembly cannot fail on these grounds.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composition) ⇒ Preflight

Returns a new instance of Preflight.



18
19
20
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 18

def initialize(composition)
  @composition = composition
end

Instance Attribute Details

#compositionObject (readonly, private)

Returns the value of attribute composition.



32
33
34
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 32

def composition
  @composition
end

Class Method Details

.check!(composition) ⇒ Object



14
15
16
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 14

def self.check!(composition)
  new(composition).check!
end

Instance Method Details

#check!Object



22
23
24
25
26
27
28
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 22

def check!
  ensure_voices
  ensure_contiguous_voices(composition)
  ensure_notes_within_barlines(composition)
  ensure_filled_final_bars
  ensure_pitched_placements
end

#ensure_contiguous_voices(composition) ⇒ Object (private) Originally defined in module PreflightChecks

#ensure_filled_final_barsObject (private)

A bar with some placements that do not fill it would render short of its bar check, which LilyPond rejects at compile time — unlike a bar with none, which the Writer fills with a whole-bar rest.



49
50
51
52
53
54
55
56
57
58
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 49

def ensure_filled_final_bars
  composition.voices.each do |voice|
    placement = voice.last_placement
    next unless placement
    next if placement.next_position == placement.position.start_of_next_bar

    raise RenderError, "the voice ends mid-bar at #{placement.next_position}; " \
      "insert explicit rests to fill the final bar"
  end
end

#ensure_notes_within_barlines(composition) ⇒ Object (private) Originally defined in module PreflightChecks

#ensure_pitched_placementsObject (private)



40
41
42
43
44
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 40

def ensure_pitched_placements
  composition.voices.each do |voice|
    voice.placements.each { |placement| ensure_pitched_sounds(placement) }
  end
end

#ensure_pitched_sounds(placement) ⇒ Object (private) Originally defined in module PlacementValidation

Raises:

  • (render_error_class)

#ensure_voicesObject (private)

Raises:



34
35
36
37
38
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 34

def ensure_voices
  return unless composition.voices.empty?

  raise RenderError, "cannot render a composition with no voices as LilyPond"
end

#raise_gap_error(voice, expected_position, found_placement) ⇒ Object (private) Originally defined in module PreflightChecks

Raises:

  • (render_error_class)

#render_error_classObject (private)



60
61
62
# File 'lib/head_music/notation/lily_pond/preflight.rb', line 60

def render_error_class
  RenderError
end