Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/brevity/parsing/note/note_nodes.rb,
lib/brevity/parsing/note/pitch_node.rb

Instance Method Summary collapse

Instance Method Details

#to_accentObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/brevity/parsing/note/note_nodes.rb', line 2

def to_accent
  case self
  when "."
    accent = Music::Transcription::Accent::Staccato.new
  when "'"
    accent = Music::Transcription::Accent::Staccatissimo.new
  when ">"
    accent = Music::Transcription::Accent::Marcato.new
  when "^"
    accent = Music::Transcription::Accent::Martellato.new
  when "_"
    accent = Music::Transcription::Accent::Tenuto.new
  else
    raise RuntimeError, "no accent found for #{self}"
  end
end

#to_pitchObject

Create a Pitch object from a string (e.g. “C2”). String can contain a letter (A-G), to indicate the semitone, followed by an optional sharp/flat (#/b) and then the octave number (non-negative integer).



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/brevity/parsing/note/pitch_node.rb', line 5

def to_pitch
  string = self
  if string =~ /[AaBbCcDdEeFfGg][#b][\d]+/
    semitone = letter_to_semitone string[0]
    semitone = case string[1]
    when "#" then semitone + 1
    when "b" then semitone - 1
    else raise ArgumentError, "unexpected symbol found"
    end
    octave = string[2..-1].to_i
    return Music::Transcription::Pitch.new(:octave => octave, :semitone => semitone)
  elsif string =~ /[AaBbCcDdEeFfGg][\d]+/
    semitone = letter_to_semitone string[0]
    octave = string[1..-1].to_i
    return Music::Transcription::Pitch.new(:octave => octave, :semitone => semitone)
  else
    raise ArgumentError, "string #{string} cannot be converted to a pitch"
  end    
end