Class: HeadMusic::Notation::LilyPond::Document

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

Overview

Everything the reader learned from a document, before any flow exists: the header's identity fields and one event stream per voice. The flow cannot be constructed until the opening key and meter are known, and they arrive inside the first voice, so the document holds the streams until the reader is done.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Document

Returns a new instance of Document.



12
13
14
# File 'lib/head_music/notation/lily_pond/document.rb', line 12

def initialize
  @streams = []
end

Instance Attribute Details

#composer ⇒ Object

Returns the value of attribute composer.



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

def composer
  @composer
end

#streams ⇒ Object (readonly)

Returns the value of attribute streams.



10
11
12
# File 'lib/head_music/notation/lily_pond/document.rb', line 10

def streams
  @streams
end

#title ⇒ Object

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_stream(role = nil) ⇒ Object



16
17
18
19
20
# File 'lib/head_music/notation/lily_pond/document.rb', line 16

def add_stream(role = nil)
  stream = VoiceStream.new(role)
  @streams << stream
  stream
end

#first_key_signature ⇒ Object



26
27
28
# File 'lib/head_music/notation/lily_pond/document.rb', line 26

def first_key_signature
  leading_event(:key)&.key_signature
end

#first_meter ⇒ Object



30
31
32
# File 'lib/head_music/notation/lily_pond/document.rb', line 30

def first_meter
  leading_event(:time)&.meter
end

#leading_event(kind) ⇒ Object (private)

The first command of a kind that precedes any music in its stream, taken from the first stream that has one.



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

def leading_event(kind)
  streams.each do |stream|
    event = stream.events.take_while { |candidate| !candidate.music? }.find { |candidate| candidate.kind == kind }
    return event if event
  end
  nil
end

#remove_stream(stream) ⇒ Object



22
23
24
# File 'lib/head_music/notation/lily_pond/document.rb', line 22

def remove_stream(stream)
  @streams.delete(stream)
end