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 flows that cannot be expressed in the supported LilyPond subset.

Whole-flow 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(flow) ⇒ Preflight

Returns a new instance of Preflight.



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

def initialize(flow)
  @flow = flow
end

Instance Attribute Details

#flow ⇒ Object (readonly, private)

Returns the value of attribute flow.



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

def flow
  @flow
end

Class Method Details

.check!(flow) ⇒ Object



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

def self.check!(flow)
  new(flow).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(flow)
  ensure_notes_within_barlines(flow)
  ensure_filled_final_bars
  ensure_pitched_placements
end

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

#ensure_filled_final_bars ⇒ Object (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
  flow.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(flow) ⇒ Object (private) Originally defined in module PreflightChecks

#ensure_pitched_placements ⇒ Object (private)



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

def ensure_pitched_placements
  flow.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_voices ⇒ Object (private)

Raises:



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

def ensure_voices
  return unless flow.voices.empty?

  raise RenderError, "cannot render a flow 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_class ⇒ Object (private)



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

def render_error_class
  RenderError
end