Module: Stretto::MusicElements::Duration

Included in:
Chord, Note, Rest
Defined in:
lib/stretto/music_elements/modifiers/duration.rb

Overview

This module encapsulates behavior for elements that specify a duration.

Duration can modify notes, chords and rests. It is specified after the note string, and its relative to the duration of the whole note (see Tempo). That means that a duration of 1.0 represents a whole note, 0.5 a half note, and 1.5 a whole and a half note, for instance.

A duration may be indicated by the duration letters, or literally as a numeric value.

In the first way, the letter for the duration is indicated, with the following values:

whole

w

half

h

quarter

q

eighth

i

sixteenth

s

thity-second

t

sixty-fourth

x

one-twenty-eighth

o

The duration can be concatenated (so a duration of hq qould be three quarters), and you may append one or more dot modifiers, that add half the value of the note succesively (see en.wikipedia.org/wiki/Dotted_note).

Additionally, tuplets can be built by appending a tuplet notation, specified by the syntax *numerator:denominator (see en.wikipedia.org/wiki/Tuplet for more information about tuplets). If omitter, the numerator and denominator will default to 3 and 2, respectively (most commonly called a triplet).

The other way to specify a duration is to indicate its numeric value after a slash. For example, a C note with half duration can be expressed as C/0.5 This notation does not accept dots or tuplets.

The duration of a note can be tied, so its tied duration will be added to the next tied elements. To indicate the start or end of a tie, add a dash after or before the duration (For example, +Cw- C-w- C-h will tie two whole notes and a half note together). When added to a pattern, one can get the total duration of a note by calling #tied_duration. All elements other than notes, chords or rests are ignored within tied notes (most notably measures)

Constant Summary collapse

DURATIONS =
{ 'w' => Rational(1),
'h' => Rational(1, 2),
'q' => Rational(1, 4),
'i' => Rational(1, 8),
's' => Rational(1, 16),
't' => Rational(1, 32),
'x' => Rational(1, 64),
'o' => Rational(1, 128) }
DEFAULT_DURATION =
DURATIONS['q']
DEFAULT_TUPLET_NUMERATOR =
3
DEFAULT_TUPLET_DENOMINATOR =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#end_tieObject (readonly)

Returns the value of attribute end_tie.



62
63
64
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 62

def end_tie
  @end_tie
end

#original_durationObject (readonly)

Returns the value of attribute original_duration.



61
62
63
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 61

def original_duration
  @original_duration
end

#start_tieObject (readonly)

Returns the value of attribute start_tie.



62
63
64
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 62

def start_tie
  @start_tie
end

Class Method Details

.parse_duration(duration_token, default_duration = DEFAULT_DURATION) ⇒ Rational, Number

Parses duration from a token, or returning the default duration

Returns:

  • (Rational, Number)


128
129
130
131
132
133
134
135
136
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 128

def self.parse_duration(duration_token, default_duration = DEFAULT_DURATION)
  if !duration_token
    default_duration
  elsif duration_token.value
    duration_token.value
  else
    parse_duration_token(duration_token)
  end
end

Instance Method Details

#build_duration_from_token(duration_token, default_duration = DEFAULT_DURATION) ⇒ Object

Parameters:

  • duration_token (Tokens::DurationToken, nil)
  • default_duration (Rational, Number) (defaults to: DEFAULT_DURATION)

    The default duration if there’s no token



66
67
68
69
70
71
72
73
74
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 66

def build_duration_from_token(duration_token, default_duration = DEFAULT_DURATION)
  @original_duration_token = duration_token
  @original_duration       = duration_token ? duration_token.text_value : ''
  @processed_value         = Duration.parse_duration(@original_duration_token, default_duration)
  if @original_duration_token
    @start_of_tie          = @original_duration_token.start_of_tie?
    @end_of_tie            = @original_duration_token.end_of_tie?
  end
end

#durationNumber

Duration value is proportional to a whole note (one whole note = 1.0 duration).

This method will coerce the duration to a float, but internally the elements can handle it as a rational number, to perform calculations more accurately.

Returns:

  • (Number)

    The value of the duration



82
83
84
85
86
87
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 82

def duration
  case @processed_value
    when Stretto::Value then @processed_value.to_f(@pattern)
    else @processed_value
  end
end

#end_of_tie?Boolean

Returns If the note ends a tie, indicating the duration should extend the duration of the previous element.

Returns:

  • (Boolean)

    If the note ends a tie, indicating the duration should extend the duration of the previous element



97
98
99
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 97

def end_of_tie?
  @end_of_tie.present?
end

#start_of_tie?Boolean

Returns If the note starts a tie, indicating the duration should continue onto the next element.

Returns:

  • (Boolean)

    If the note starts a tie, indicating the duration should continue onto the next element



91
92
93
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 91

def start_of_tie?
  @start_of_tie.present?
end

#tied_durationNumber

Returns the total duration of the tied elements

Returns:

  • (Number)

See Also:



121
122
123
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 121

def tied_duration
  tied_elements.inject(0){|sum, element| sum + element.duration}
end

#tied_elementsArray(MusicElements::MusicElement)

Returns an array containing all the elements that are tied to the right

It can return an array consisiting on just the current element, if it is not tied or simply it doesn’t belong to a pattern

Returns:



107
108
109
110
111
112
113
114
115
# File 'lib/stretto/music_elements/modifiers/duration.rb', line 107

def tied_elements
  current = self
  elements = [current]
  while next_is_tied?(current)
    current = current.next
    elements << current unless current.kind_of?(Measure)
  end
  elements
end