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.

# File lib/alda-rb/event.rb, line 375
def initialize pitch, duration
  @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 403
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 416
def -@
  @pitch.concat ?-
  self
end
to_alda_code() click to toggle source
# File lib/alda-rb/event.rb, line 434
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 429
def ~
  @pitch.concat ?_
  self
end