lib/music-transcription/pitch.rb in music-transcription-0.7.0 vs lib/music-transcription/pitch.rb in music-transcription-0.7.1
- old
+ new
@@ -100,11 +100,12 @@
return self.total_semitone
end
# Compare pitch equality using total semitone
def ==(other)
- self.total_semitone == other.total_semitone
+ return (self.class == other.class &&
+ self.total_semitone == other.total_semitone)
end
def eql?(other)
self == other
end
@@ -146,30 +147,10 @@
semitoneTotal -= @octave * SEMITONES_PER_OCTAVE
@semitone = semitoneTotal
return self
end
-
- # Produce a string representation of a pitch (e.g. "C2")
- def to_s
- semitone_str = case @semitone
- when 0 then "C"
- when 1 then "Db"
- when 2 then "D"
- when 3 then "Eb"
- when 4 then "E"
- when 5 then "F"
- when 6 then "Gb"
- when 7 then "G"
- when 8 then "Ab"
- when 9 then "A"
- when 10 then "Bb"
- when 11 then "B"
- end
-
- return semitone_str + @octave.to_s
- end
def self.make_from_freq(freq)
pitch = Pitch.new()
pitch.ratio = freq / BASE_FREQ
return pitch
@@ -183,49 +164,6 @@
end
end
end
end
-end
-
-class String
- # 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).
- 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
-
- private
-
- def letter_to_semitone letter
- semitone = case letter
- when /[Cc]/ then 0
- when /[Dd]/ then 2
- when /[Ee]/ then 4
- when /[Ff]/ then 5
- when /[Gg]/ then 7
- when /[Aa]/ then 9
- when /[Bb]/ then 11
- else raise ArgumentError, "invalid letter \"#{letter}\" given"
- end
-
- return semitone
- end
-
end