Class: HeadMusic::Notation::ABC::VoiceRegistry
- Inherits:
-
Object
- Object
- HeadMusic::Notation::ABC::VoiceRegistry
- Includes:
- Enumerable
- Defined in:
- lib/head_music/notation/abc/voice_registry.rb
Overview
Owns the tune’s per-voice interpretation states and tracks which voice the body is currently writing into.
A tune may declare its voices in V: header fields, switch between them with V: body lines, or use neither — in which case all the music belongs to one unnamed voice.
Instance Method Summary collapse
-
#current ⇒ Object
Body music before any V: line falls into a default unnamed voice, created here on demand so a leading V: line doesn’t force an empty one into the composition.
-
#declare(roles, default:) ⇒ Object
Creates a state for each declared role, or — when nothing is declared and the body never switches voices — the single unnamed voice that tune uses, and makes the first of them current.
-
#each(&block) ⇒ Object
-
#initialize(composition, key_signature, duration_resolver) ⇒ VoiceRegistry
constructor
A new instance of VoiceRegistry.
-
#state(role) ⇒ Object
-
#switch_to(role) ⇒ Object
Constructor Details
#initialize(composition, key_signature, duration_resolver) ⇒ VoiceRegistry
Returns a new instance of VoiceRegistry.
12 13 14 15 16 17 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 12 def initialize(composition, key_signature, duration_resolver) @composition = composition @key_signature = key_signature @duration_resolver = duration_resolver @states = {} end |
Instance Method Details
#current ⇒ Object
Body music before any V: line falls into a default unnamed voice, created here on demand so a leading V: line doesn’t force an empty one into the composition.
43 44 45 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 43 def current @current ||= state(nil) end |
#declare(roles, default:) ⇒ Object
Creates a state for each declared role, or — when nothing is declared and the body never switches voices — the single unnamed voice that tune uses, and makes the first of them current.
26 27 28 29 30 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 26 def declare(roles, default:) roles.each { |role| state(role) } state(nil) if @states.empty? && default @current = @states.values.first end |
#each(&block) ⇒ Object
19 20 21 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 19 def each(&block) @states.each_value(&block) end |
#state(role) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 32 def state(role) @states[role] ||= VoiceState.new( @composition.add_voice(role: role), PitchBuilder.new(@key_signature), @duration_resolver ) end |
#switch_to(role) ⇒ Object
47 48 49 |
# File 'lib/head_music/notation/abc/voice_registry.rb', line 47 def switch_to(role) @current = state(role) end |