lib/head_music/pitch.rb in head_music-0.1.1 vs lib/head_music/pitch.rb in head_music-0.1.5
- old
+ new
@@ -11,22 +11,27 @@
@pitches[value] ||= from_name(value) || from_number(value)
end
def self.from_name(name)
return nil unless name == name.to_s
- spelling = HeadMusic::Spelling.get(name)
- octave = name.scan(/-?\d+$/).first.to_i
- new(spelling, octave) if spelling && (-1..9).include?(octave)
+ fetch_or_create(HeadMusic::Spelling.get(name), HeadMusic::Octave.get(name).to_i)
end
def self.from_number(number)
return nil unless number == number.to_i
spelling = HeadMusic::Spelling.from_number(number)
octave = (number.to_i / 12) - 1
- new(spelling, octave) if spelling && (-1..9).include?(octave)
+ fetch_or_create(spelling, octave)
end
+ def self.fetch_or_create(spelling, octave)
+ if spelling && (-1..9).include?(octave)
+ key = [spelling, octave].join
+ @pitches[key] ||= new(spelling, octave)
+ end
+ end
+
def initialize(spelling, octave)
@spelling = HeadMusic::Spelling.get(spelling.to_s)
@octave = octave.to_i
end
@@ -35,9 +40,11 @@
end
def midi_note_number
(octave + 1) * 12 + pitch_class.to_i
end
+
+ alias_method :midi, :midi_note_number
def to_s
name
end