lib/music-transcription/pitch.rb in music-transcription-0.7.3 vs lib/music-transcription/pitch.rb in music-transcription-0.8.0
- old
+ new
@@ -26,13 +26,11 @@
# the twelve-tone equal temperment tuning system.
SEMITONES_PER_OCTAVE = 12
# The base ferquency is C0
BASE_FREQ = 16.351597831287414
-
- # A new instance of Pitch.
- # @raise [NonPositiveFrequencyError] if base_freq is not > 0.
+
def initialize octave:0, semitone:0
@octave = octave
@semitone = semitone
normalize!
end
@@ -58,13 +56,15 @@
return (@octave * SEMITONES_PER_OCTAVE) + @semitone
end
# Set the Pitch ratio according to a total number of semitones.
# @param [Fixnum] semitone The total number of semitones to use.
- # @raise [ArgumentError] if semitone is not an Integer
+ # @raise [NonIntegerError] if semitone is not an Integer
def total_semitone= semitone
- raise ArgumentError, "semitone is not a Integer" if !semitone.is_a?(Integer)
+ unless semitone.is_a?(Integer)
+ raise NonIntegerError, "semitone #{semitone} is not a Integer"
+ end
@octave, @semitone = 0, semitone
normalize!
end
# Calculate the pitch ratio. Raises 2 to the power of the total semitone
@@ -74,13 +74,13 @@
2.0**(self.total_semitone.to_f / SEMITONES_PER_OCTAVE)
end
# Represent the Pitch ratio according to a ratio.
# @param [Numeric] ratio The ratio to represent.
- # @raise [RangeError] if ratio is less than or equal to 0.0
+ # @raise [NonPositiveError] unless ratio is > 0
def ratio= ratio
- raise RangeError, "ratio #{ratio} is less than or equal to 0.0" if ratio <= 0.0
+ raise NonPositiveError, "ratio #{ratio} is not > 0" unless ratio > 0
x = Math.log2 ratio
self.total_semitone = (x * SEMITONES_PER_OCTAVE).round
end
@@ -155,14 +155,12 @@
pitch.ratio = freq / BASE_FREQ
return pitch
end
def self.make_from_semitone semitones
- if semitones.is_a?(Integer)
- return Pitch.new(semitone: semitones)
- else
- raise ArgumentError, "Cannot make Pitch from #{semitones}"
- end
+ pitch = Pitch.new()
+ pitch.total_semitone = semitones
+ return pitch
end
end
end
end