Class: Stretto::MusicElements::Note

Inherits:
MusicElement show all
Includes:
AttackDecay, Duration
Defined in:
lib/stretto/music_elements/note.rb

Overview

A note is one of the most basic elements in Stretto.

It is composed of several elements:

key

Represents the note name (from A to G)

accidental

The modifier of the note, this is, flats, sharps or the natural indicator

(bb, b, n, #, ##)
pitch

The actual numeric pitch of the note, according to MIDI specification (0 to 127)

octave

Octave in which the note is located. (0 to 10) The default octave for a note is 5

Additionally, it holds a duration (see Duration), and attack and decay (see AttackDecay)

Example of valid notes are:

C

Returns the note C, with the default octave, duration, attack and decay

[60]

The same note, represented by its pitch value

D#7

The note D# in the seventh octave

Abwa80d100

The note Ab with a whole note duration, an attack of 80 and decay of 100

Pitch values and octaves are indicated in the table below:

Octave  |   C |  C# |   D |  D# |   E |   F |  F# |   G |  G# |   A |  A# |   B
   0    |   0 |   1 |   2 |   3 |   4 |   5 |   6 |   7 |   8 |   9 |  10 |  11
   1    |  12 |  13 |  14 |  15 |  16 |  17 |  18 |  19 |  20 |  21 |  22 |  23
   2    |  24 |  25 |  26 |  27 |  28 |  29 |  30 |  31 |  32 |  33 |  34 |  35
   3    |  36 |  37 |  38 |  39 |  40 |  41 |  42 |  43 |  44 |  45 |  46 |  47
   4    |  48 |  49 |  50 |  51 |  52 |  53 |  54 |  55 |  56 |  57 |  58 |  59
   5    |  60 |  61 |  62 |  63 |  64 |  65 |  66 |  67 |  68 |  69 |  70 |  71
   6    |  72 |  73 |  74 |  75 |  76 |  77 |  78 |  79 |  80 |  81 |  82 |  83
   7    |  84 |  85 |  86 |  87 |  88 |  89 |  90 |  91 |  92 |  93 |  94 |  95
   8    |  96 |  97 |  98 |  99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107
   9    | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119
   10   | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127

Where pitch can go from 0 to 127.

Constant Summary collapse

PITCHES =
{
  'C' => 0,
  'D' => 2,
  'E' => 4,
  'F' => 5,
  'G' => 7,
  'A' => 9,
  'B' => 11
}
ACCIDENTALS =
{
  'bb' => -2,
  'b'  => -1,
  '#'  =>  1,
  '##' =>  2
}
MAX_PITCH =
127
DEFAULT_OCTAVE =
5

Constants included from AttackDecay

AttackDecay::DEFAULT_ATTACK, AttackDecay::DEFAULT_DECAY

Constants included from Duration

Duration::DEFAULT_DURATION, Duration::DEFAULT_TUPLET_DENOMINATOR, Duration::DEFAULT_TUPLET_NUMERATOR, Duration::DURATIONS

Instance Attribute Summary collapse

Attributes included from AttackDecay

#original_attack, #original_decay

Attributes included from Duration

#end_tie, #start_tie

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Instance Method Summary collapse

Methods included from AttackDecay

#attack, #attack=, #build_attack_and_decay, #decay, #decay=

Methods included from Duration

#build_duration_from_token, #duration, #end_of_tie?, parse_duration, #start_of_tie?, #tied_duration, #tied_elements

Methods inherited from MusicElement

#build_music_string, #duration, #end_of_tie?, #start_of_tie?, #to_s

Constructor Details

#initialize(string_or_options, pattern = nil) ⇒ Note

Returns a new instance of Note.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stretto/music_elements/note.rb', line 77

def initialize(string_or_options, pattern = nil)
  token = case string_or_options
    when String then Stretto::Parser.parse_note!(string_or_options)
    else string_or_options
  end
  super(token[:text_value], pattern)
  @original_key             = token[:key]
  @original_pitch           = token[:pitch]
  @original_accidental      = token[:accidental]
  @original_octave          = token[:octave]
  build_duration_from_token(token[:duration])
  build_attack_and_decay(token[:attack], token[:decay])
end

Instance Attribute Details

#instrumentObject

Returns the value of attribute instrument.



75
76
77
# File 'lib/stretto/music_elements/note.rb', line 75

def instrument
  @instrument
end

#key_signatureObject

Returns the value of attribute key_signature.



74
75
76
# File 'lib/stretto/music_elements/note.rb', line 74

def key_signature
  @key_signature
end

#original_accidentalObject (readonly)

Returns the value of attribute original_accidental.



73
74
75
# File 'lib/stretto/music_elements/note.rb', line 73

def original_accidental
  @original_accidental
end

#original_durationObject (readonly)

Returns the value of attribute original_duration.



73
74
75
# File 'lib/stretto/music_elements/note.rb', line 73

def original_duration
  @original_duration
end

#original_keyObject (readonly)

Returns the value of attribute original_key.



73
74
75
# File 'lib/stretto/music_elements/note.rb', line 73

def original_key
  @original_key
end

#original_octaveObject (readonly)

Returns the value of attribute original_octave.



73
74
75
# File 'lib/stretto/music_elements/note.rb', line 73

def original_octave
  @original_octave
end

Instance Method Details

#+(interval) ⇒ MusicElements::Note

Returns a new note raised by ‘interval` semitones.

Parameters:

  • interval

    The amount of semitones to raise the pitch.

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/stretto/music_elements/note.rb', line 136

def +(interval)
  new_pitch = if @original_pitch
    @original_pitch + interval
  else
    Stretto::Value.new(Stretto::Value::NumericValue.new(pitch + interval))
  end
  Note.new({
    :text_value => "#{@original_string}+#{interval}",
    :pitch      => new_pitch,
    :duration   => @original_duration_token,
    :attack     => @original_attack,
    :decay      => @original_decay}, @pattern)
end

#==(other) ⇒ Boolean

Returns Whether ‘other` is a note and has the same pitch.

Returns:

  • (Boolean)

    Whether ‘other` is a note and has the same pitch



151
152
153
154
# File 'lib/stretto/music_elements/note.rb', line 151

def ==(other)
  # TODO: Revisit the semantics of ==
  other.kind_of?(Note) && other.pitch == pitch
end

#accidentalString

TODO:

Return accidental according to the present key signature

Gets the accidental of the note. If the original string did not contain an accidental, the default accidental for the pitch is returned (refer to the pitch table on MusicElement::Note documentation), always using sharps for raised notes.

Returns:

  • (String)

    The accidental of the note



127
128
129
130
# File 'lib/stretto/music_elements/note.rb', line 127

def accidental
  build_pitch unless @accidental
  @accidental
end

#eql?(other) ⇒ Boolean

Returns Whether ‘other` is a note and has the same pitch.

Returns:

  • (Boolean)

    Whether ‘other` is a note and has the same pitch



157
158
159
160
# File 'lib/stretto/music_elements/note.rb', line 157

def eql?(other)
  # TODO: Revisit the semantics of eql?
  other.kind_of?(Note) && other.pitch.eql?(pitch)
end

#hashBoolean

Returns Whether ‘other` is a note and has the same pitch.

Returns:

  • (Boolean)

    Whether ‘other` is a note and has the same pitch



163
164
165
# File 'lib/stretto/music_elements/note.rb', line 163

def hash
  @pitch.hash
end

#keyString

Gets the key of the note. If the original string did not contain a key note, the key for that pitch is returned (see the pitch table on Stretto::MusicElements::Note documentation.)

Returns:

  • (String)

    The note key, ‘A’ through ‘G’



115
116
117
118
# File 'lib/stretto/music_elements/note.rb', line 115

def key
  build_pitch unless @key
  @key
end

#octaveNumber

Gets the octave for the note. If the original string did not contain an oe, the octave for that pitch is returned (see octave table on Stretto::MusicElements::Note documentation.)

Returns:

  • (Number)

    The octave for the pitch



105
106
107
108
# File 'lib/stretto/music_elements/note.rb', line 105

def octave
  build_pitch unless @octave
  @octave
end

#original_pitchObject

Text value of original pitch



180
181
182
# File 'lib/stretto/music_elements/note.rb', line 180

def original_pitch
  @original_pitch.to_s if @original_pitch
end

#pitchNumber

Gets pitch of the note. If no original pitch is passed, the pitch gets calculated from the key, accidental and octave values.

Returns:

  • (Number)

    The pitch of the note, from 0 to 127



96
97
98
# File 'lib/stretto/music_elements/note.rb', line 96

def pitch
  @pitch || build_pitch
end