Class: Mext::Music::NoteName

Inherits:
Object
  • Object
show all
Defined in:
lib/mext/music/note_name.rb

Constant Summary collapse

DEFAULT_NOTE_NAME_OFFSET =

central ‘A’ 440 is 8.09, isn’t it?

9
LILY_NOTE_NAMES =
%w( a b c d e f )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, idx) ⇒ NoteName

Returns a new instance of NoteName.



19
20
21
22
# File 'lib/mext/music/note_name.rb', line 19

def initialize(name, idx)
  @name = name
  @index = idx
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



17
18
19
# File 'lib/mext/music/note_name.rb', line 17

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/mext/music/note_name.rb', line 17

def name
  @name
end

Instance Method Details

#to_frequency(octave = 8) ⇒ Object



37
38
39
# File 'lib/mext/music/note_name.rb', line 37

def to_frequency(octave = 8)
  self.to_pitch_class(octave).pchcps
end

#to_lily(octave = 8, other = '') ⇒ Object

8 is the central octave

Raises:



24
25
26
27
28
29
30
31
# File 'lib/mext/music/note_name.rb', line 24

def to_lily(octave = 8, other = '') # 8 is the central octave
  nn = self.name.sub(/\A\s*(.).*$/, '\1').downcase
  raise NotALilypondName, "\"#{self.name}\" does not contain a note" unless LILY_NOTE_NAMES.include?(nn)
  sharps = (self.name.match?(/\A\s*\w#/) && self.name.sub(/\A\s*\w(#+).*$/, '\1').gsub(/#/, 'is')) || nil
  flats = (self.name.match?(/\A\s*\w[Ff]/) && self.name.sub(/\A\s*\w([Ff]+).*$/, '\1').gsub(/[Ff]/, 'es')) || nil
  accs = sharps ? sharps : (flats ? flats : '')
  "#{nn}#{accs}#{octave}#{other}"
end

#to_pitch_class(octave = 8) ⇒ Object

8 is the central octave



33
34
35
# File 'lib/mext/music/note_name.rb', line 33

def to_pitch_class(octave = 8) # 8 is the central octave
  octave + (self.index/100.0)
end