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
The measure the per-method examples are written against
measure = Measure.new(1, divisions: 4)
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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 155 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.
186 187 188 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 186 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.
182 183 184 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 182 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).
436 437 438 439 440 441 442 443 444 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 436 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.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 213 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.
281 282 283 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 281 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.
387 388 389 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 387 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.
396 397 398 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 396 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.
317 318 319 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 317 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.).
364 365 366 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 364 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.
298 299 300 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 298 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.
338 339 340 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 338 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).
421 422 423 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 421 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.
377 378 379 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 377 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.
240 241 242 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 240 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.
253 254 255 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 253 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).
262 263 264 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 262 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).
351 352 353 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 351 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.
408 409 410 |
# File 'lib/musa-dsl/musicxml/builder/measure.rb', line 408 attr_complex_adder_to_custom(:words) { |*p, placement: nil, offset: nil, **kp, &b| direction(placement: placement, offset: offset) { words *p, **kp, &b } } |