Voice class

Represents a voice in polyphonic notation.

In polyphonic music, multiple independent melodic lines (voices) are notated on the same staff. Each voice is typically distinguished by:

  • Stem direction (voice 1: up, voice 2: down)
  • Horizontal offset (the stem-down voice shifted right on unisons/seconds)
  • Different beaming groups

Examples:

  • Bach fugues (3-4 voices on one staff)
  • Piano music (2 voices per hand)
  • Guitar fingerstyle (melody + accompaniment)
  • Counterpoint exercises

Convention (Gould, Behind Bars, "Two or more parts on a staff"):

  • Odd voices (1, 3): stems up, no horizontal offset
  • Even voices (2, 4): stems down, offset to the right when they collide

A Voice does not mutate the elements it receives: Note.voice is final, so the voice number must be set when the note is constructed. Use elementsCarryVoiceNumber / validate to check that this was done.

Constructors

Voice({required int number, List<MusicalElement>? elements, String? name, StemDirection? forcedStemDirection, double? horizontalOffset, String? color})
Voice.voice1({List<MusicalElement>? elements, String? name})
Factory: Create voice 1 (top voice, stems up)
factory
Voice.voice2({List<MusicalElement>? elements, String? name})
Factory: Create voice 2 (bottom voice, stems down, offset right)
factory

Properties

chords → List<Chord>
Get all chords in this voice
no setter
color → String?
Color for this voice (optional, for visual distinction)
final
elements → List<MusicalElement>
Musical elements in this voice (notes, rests, chords)
final
elementsCarryVoiceNumber → bool
True when every Note/Chord in this voice already declares voice == number.
no setter
forcedStemDirection → StemDirection?
Preferred stem direction for this voice
final
hashCode → int
The hash code for this object.
no setterinherited
hasNotes → bool
Check if this voice contains any notes (not just rests)
no setter
horizontalOffset → double?
Horizontal offset for collision avoidance (in staff spaces)
final
name → String?
Optional name for the voice (e.g., "Soprano", "Melody")
final
notes → List<Note>
Get all notes in this voice
no setter
number → int
Voice number (1-based)
final
rests → List<Rest>
Get all rests in this voice
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(MusicalElement element) → void
Add element to this voice.
getHorizontalOffset(double staffSpace) → double
Get horizontal offset for this voice, in pixels.
getStemDirection() → StemDirection
Get stem direction for this voice.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
validate() → List<String>
Checks that the voice number was propagated to the elements.

Operators

operator ==(Object other) → bool
The equality operator.
inherited