class Alda::Note

A note event. An Alda::EventContainer containing an Alda::Note can be derived using Alda::EventList sugar. See Alda::EventList#method_missing.

There cannot be tildes and dots in (usual) ruby method names, so use underlines instead.

The accidentals can be added using +@, -@, and ~, or by using exclamation mark, question mark or underline.

Alda::Score.new do
  key_sig! [:d, :major]
  c4_2 d1108ms e2s
  f2!      # F sharp
  g20ms_4? # G flat
  a6_      # A natural
  c__      # C (slur)
  f___     # D natural (slur)
end

Attributes

duration[RW]

The string representing the duration.

It ends with a tilde “~” if the note slurs.

pitch[RW]

The string representing the pitch

Public Class Methods

new(pitch, duration) → Alda::Note click to toggle source

The underlines in duration will be converted to tildes “~”. Exclamation mark and question mark in duration will be interpreted as accidentals in pitch.

The number of underlines at the end of duration means: neither natural nor slur if 0, natural if 1, slur if 2, both natural and slur if 3.

Calls superclass method
# File lib/alda-rb/event.rb, line 510
def initialize pitch, duration
  super()
  @pitch = pitch.to_s
  @duration = duration.to_s.tr ?_, ?~
  case @duration[-1]
  when ?! # sharp
    @pitch.concat ?+
    @duration[-1] = ''
  when ?? # flat
    @pitch.concat ?-
    @duration[-1] = ''
  end
  waves = /(?<str>~+)\z/ =~ @duration ? str.size : return
  @duration[@duration.length - waves..] = ''
  if waves >= 2
    waves -= 2
    @duration.concat ?~
  end
  @pitch.concat ?_ * waves
end

Public Instance Methods

+note → note click to toggle source

Append a sharp sign after pitch.

Alda::Score.new { piano_; +c }.play
# (plays a C sharp note)
# File lib/alda-rb/event.rb, line 539
def +@
  @pitch.concat ?+
  self
end
-note → note click to toggle source

Append a flat sign after pitch.

Alda::Score.new { piano_; -d }.play
# (plays a D flat note)
# File lib/alda-rb/event.rb, line 552
def -@
  @pitch.concat ?-
  self
end
note == other → true or false click to toggle source

Overrides Alda::Event#==. Returns true if other is an Alda::Note and has the same pitch and duration as note (using ==).

Calls superclass method Alda::Event#==
# File lib/alda-rb/event.rb, line 588
def == other
  super || other.is_a?(Alda::Note) && @pitch == other.pitch && @duration == other.duration
end
to_alda_code() → String click to toggle source

Overrides Alda::Event#to_alda_code.

# File lib/alda-rb/event.rb, line 575
def to_alda_code
  result = @pitch + @duration
  result.concat ?*, @count.to_alda_code if @count
  result
end
~note → note click to toggle source

Append a natural sign after pitch.

Alda::Score.new { piano_; key_sig 'f+'; ~f }.play
# (plays an F note)
# File lib/alda-rb/event.rb, line 565
def ~
  @pitch.concat ?_
  self
end