Class: Musa::MusicXML::Builder::Internal::Measure Private
- Extended by:
- Extension::AttributeBuilder
- Includes:
- Extension::With, Helper::ToXML
- Defined in:
- lib/musa-dsl/musicxml/builder/measure.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Measure container for musical content.
Measure represents a single measure (bar) of music, containing musical elements in chronological order: attributes, notes, rests, backup/forward commands, and directions (dynamics, tempo markings, etc.).
Element Order
Elements within a measure follow MusicXML's sequential model:
- Attributes (key, time, clef, divisions) - typically in first measure
- Directions (tempo, dynamics) - before or between notes
- Notes/Rests - musical content
- Backup/Forward - timeline navigation for multiple voices/staves
Multiple Voices and Staves
For piano (grand staff) or polyphonic notation, use backup to rewind the timeline:
measure do
# Right hand (treble clef)
pitch 'C', octave: 5, duration: 4, type: 'quarter', staff: 1
pitch 'D', octave: 5, duration: 4, type: 'quarter', staff: 1
backup 8 # Rewind to start of measure
# Left hand (bass clef)
pitch 'C', octave: 3, duration: 8, type: 'half', staff: 2
end
Divisions
The divisions attribute sets timing resolution (divisions per quarter note).
Higher values allow finer rhythmic subdivisions:
- divisions: 1 → quarter, half, whole only
- divisions: 2 → adds eighths
- divisions: 4 → adds sixteenths
- divisions: 8 → adds thirty-seconds
- divisions: 16 → allows complex tuplets
Duration is calculated as: duration = (note_type_value * divisions) / beat_type
Instance Attribute Summary collapse
-
#elements ⇒ Array<Object>
readonly
private
Ordered list of elements in this measure.
-
#number ⇒ Integer
private
Measure number.
Class Method Summary collapse
-
.attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding complex objects (with multiple parameters) to an array.
-
.attr_complex_adder_to_custom(name, plural: nil, variable: nil) { ... } ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding complex objects with custom construction logic.
-
.attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a getter/setter DSL method for complex objects with multiple parameters.
-
.attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a simple getter/setter DSL method for a single value.
-
.attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding id/value tuples to an array collection.
-
.attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding id/value tuples to a hash collection.
-
.attr_tuple_builder(name, klass, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a getter/setter DSL method for a single id/value tuple.
Instance Method Summary collapse
-
#_to_xml(io, indent:, tabs:) ⇒ void
private
Generates the measure XML element with all contained elements.
-
#attributes { ... } ⇒ Attributes
private
Adds musical attributes to the measure.
-
#backup ⇒ Backup
private
Rewinds the musical timeline.
-
#bracket ⇒ Direction
private
Adds bracket notation.
-
#dashes ⇒ Direction
private
Adds dashed line.
-
#direction { ... } ⇒ Direction
private
Adds a direction element (dynamics, tempo, expressions).
-
#dynamics ⇒ Direction
private
Adds dynamics (p, pp, f, ff, etc.).
-
#forward ⇒ Forward
private
Advances the musical timeline.
-
#initialize(number, divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil) { ... } ⇒ Measure
constructor
private
Creates a new measure.
-
#metronome { ... } ⇒ Direction
private
Adds a metronome (tempo) marking.
-
#octave_shift ⇒ Direction
private
Adds octave shift (8va/8vb).
-
#pedal ⇒ Direction
private
Adds pedal marking.
-
#pitch ⇒ PitchedNote
private
Adds a pitched note.
-
#rest ⇒ Rest
private
Adds a rest.
-
#to_xml(io = nil, indent: nil) ⇒ IO, StringIO
included
from Helper::ToXML
private
Converts the object to MusicXML format.
-
#unpitched ⇒ UnpitchedNote
private
Adds an unpitched note (for percussion).
-
#wedge ⇒ Direction
private
Adds a wedge (crescendo/diminuendo).
-
#with(*value_parameters, keep_block_context: nil, **key_parameters, &block) ⇒ Object
included
from Extension::With
Executes a block with flexible context and parameter handling.
-
#words ⇒ Direction
private
Adds text annotation.
Constructor Details
#initialize(number, divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil) { ... } ⇒ Measure
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new measure.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 127 def initialize(number, divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil, &block) @number = number @elements = [] @attributes = [] if divisions || key_cancel || key_fifths || key_mode || time_senza_misura || time_beats || time_beat_type || clef_sign || clef_line || clef_octave_change add_attributes divisions: divisions, key_cancel: key_cancel, key_fifths: key_fifths, key_mode: key_mode, time_senza_misura: time_senza_misura, time_beats: time_beats, time_beat_type: time_beat_type, clef_sign: clef_sign, clef_line: clef_line, clef_octave_change: clef_octave_change end with &block if block_given? end |
Instance Attribute Details
#elements ⇒ Array<Object> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ordered list of elements in this measure.
158 159 160 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 158 def elements @elements end |
#number ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Measure number.
154 155 156 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 154 def number @number end |
Class Method Details
.attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding complex objects (with multiple parameters) to an array.
Supports both positional and keyword arguments when creating instances.
.attr_complex_adder_to_custom(name, plural: nil, variable: nil) { ... } ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding complex objects with custom construction logic.
The block receives parameters and should construct and add the object.
.attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a getter/setter DSL method for complex objects with multiple parameters.
Supports optional first_parameter that's automatically prepended when constructing.
.attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a simple getter/setter DSL method for a single value.
.attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding id/value tuples to an array collection.
Similar to attr_tuple_adder_to_hash but stores items in an array instead of hash. Useful when order matters or duplicates are allowed.
.attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding id/value tuples to a hash collection.
Generates:
add_#{name}(id, parameter)→ creates instance and adds to hash#{plural}(**parameters)→ batch add or retrieve hash
.attr_tuple_builder(name, klass, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a getter/setter DSL method for a single id/value tuple.
Instance Method Details
#_to_xml(io, indent:, tabs:) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Generates the measure XML element with all contained elements.
Outputs elements in the order they were added, which must follow MusicXML's element ordering rules (attributes, then notes/directions/backup/forward).
408 409 410 411 412 413 414 415 416 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 408 def _to_xml(io, indent:, tabs:) io.puts "#{tabs}<measure number=\"#{@number.to_i}\">" @elements.each do |element| element.to_xml(io, indent: indent + 1) end io.puts "#{tabs}</measure>" end |
#attributes { ... } ⇒ Attributes
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds musical attributes to the measure.
Attributes define key signature, time signature, clef, and timing divisions. Typically appear at the start of the first measure or when they change.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 185 attr_complex_adder_to_custom :attributes, plural: :attributes, variable: :@attributes do | divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil, &block | Attributes.new(divisions: divisions, key_cancel: key_cancel, key_fifths: key_fifths, key_mode: key_mode, time_senza_misura: time_senza_misura, time_beats: time_beats, time_beat_type: time_beat_type, clef_sign: clef_sign, clef_line: clef_line, clef_octave_change: clef_octave_change, &block) \ .tap do |attributes| @attributes << attributes @elements << attributes end end |
#backup ⇒ Backup
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Rewinds the musical timeline.
Backup moves the current time position backward by the specified duration, allowing multiple voices or staves to be layered in the same time span.
253 254 255 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 253 attr_complex_adder_to_custom :backup do |duration| Backup.new(duration).tap { |backup| @elements << backup } end |
#bracket ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds bracket notation.
359 360 361 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 359 attr_complex_adder_to_custom(:bracket) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { bracket *p, **kp, &b } } |
#dashes ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds dashed line.
368 369 370 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 368 attr_complex_adder_to_custom(:dashes) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { dashes *p, **kp, &b } } |
#direction { ... } ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a direction element (dynamics, tempo, expressions).
Directions contain non-note musical instructions like dynamics (p, f), tempo markings, wedges (crescendo/diminuendo), pedal marks, etc.
289 290 291 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 289 attr_complex_adder_to_custom :direction do |*parameters, **key_parameters, &block| Direction.new(*parameters, **key_parameters, &block).tap { |direction| @elements << direction } end |
#dynamics ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds dynamics (p, pp, f, ff, etc.).
336 337 338 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 336 attr_complex_adder_to_custom(:dynamics) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { dynamics *p, **kp, &b } } |
#forward ⇒ Forward
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Advances the musical timeline.
Forward moves the current time position forward without sounding, creating rests or gaps in the timeline.
270 271 272 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 270 attr_complex_adder_to_custom :forward do |duration, voice: nil, staff: nil| Forward.new(duration, voice: voice, staff: staff).tap { |forward| @elements << forward } end |
#metronome { ... } ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a metronome (tempo) marking.
310 311 312 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 310 attr_complex_adder_to_custom(:metronome) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { metronome *p, **kp, &b } } |
#octave_shift ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds octave shift (8va/8vb).
393 394 395 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 393 attr_complex_adder_to_custom(:octave_shift) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { octave_shift *p, **kp, &b } } |
#pedal ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds pedal marking.
349 350 351 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 349 attr_complex_adder_to_custom(:pedal) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { pedal *p, **kp, &b } } |
#pitch ⇒ PitchedNote
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a pitched note.
212 213 214 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 212 attr_complex_adder_to_custom :pitch do | *parameters, **key_parameters | PitchedNote.new(*parameters, **key_parameters).tap { |note| @elements << note } end |
#rest ⇒ Rest
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a rest.
225 226 227 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 225 attr_complex_adder_to_custom :rest do | *parameters, **key_parameters | Rest.new(*parameters, **key_parameters).tap { |rest| @elements << rest } end |
#to_xml(io = nil, indent: nil) ⇒ IO, StringIO Originally defined in module Helper::ToXML
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts the object to MusicXML format.
This method sets up the IO stream and indentation, then delegates to
the private _to_xml method for actual XML generation.
#unpitched ⇒ UnpitchedNote
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an unpitched note (for percussion).
234 235 236 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 234 attr_complex_adder_to_custom :unpitched do | *parameters, **key_parameters | UnpitchedNote.new(*parameters, **key_parameters).tap { |unpitched| @elements << unpitched } end |
#wedge ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a wedge (crescendo/diminuendo).
323 324 325 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 323 attr_complex_adder_to_custom(:wedge) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { wedge *p, **kp, &b } } |
#with(*value_parameters, keep_block_context: nil, **key_parameters, &block) ⇒ Object Originally defined in module Extension::With
The _ parameter is special: when present, it signals "keep caller's context"
and receives self (the object) as its value.
Uses SmartProcBinder internally to handle parameter matching.
Executes a block with flexible context and parameter handling.
#words ⇒ Direction
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds text annotation.
380 381 382 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 380 attr_complex_adder_to_custom(:words) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { words *p, **kp, &b } } |