Class: HeadMusic::Notation::LilyPond::VoiceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/notation/lily_pond/voice_context.rb

Overview

A Staff, a Voice, or the implicit top level, and the stream the reader collects into while it is open. It yields a voice when it holds music, or when it is an explicit context with no children (an empty \new Staff { } is a legitimate silent voice).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, role, explicit:) ⇒ VoiceContext

Returns a new instance of VoiceContext.



11
12
13
14
15
16
17
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 11

def initialize(document, role, explicit:)
  @document = document
  @role = role
  @stream = document.add_stream(role)
  @explicit = explicit
  @children = 0
end

Instance Attribute Details

#children ⇒ Object

Returns the value of attribute children.



9
10
11
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 9

def children
  @children
end

#document ⇒ Object (readonly, private)

Returns the value of attribute document.



36
37
38
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 36

def document
  @document
end

#explicit ⇒ Object (readonly, private)

Returns the value of attribute explicit.



36
37
38
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 36

def explicit
  @explicit
end

#role ⇒ Object (readonly)

Returns the value of attribute role.



8
9
10
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 8

def role
  @role
end

#stream ⇒ Object (readonly)

Returns the value of attribute stream.



8
9
10
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 8

def stream
  @stream
end

Instance Method Details

#close ⇒ Object

A context that yields no voice may still have collected commands; dropping them silently would lose a key or meter, so they raise.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 21

def close
  stream.finish
  return if voice?

  document.remove_stream(stream)
  event = stream.events.find { |candidate| %i[key time].include?(candidate.kind) }
  return unless event

  raise UnsupportedFeatureError.new(
    "\\key and \\time outside a voice are not supported", line_number: event.line
  )
end

#voice? ⇒ Boolean (private)

Returns:

  • (Boolean)


38
39
40
# File 'lib/head_music/notation/lily_pond/voice_context.rb', line 38

def voice?
  stream.music? || (explicit && children.zero?)
end