class MTK::Core::Pitch
A frequency represented by a {PitchClass}, an integer octave, and an offset in semitones.
@see Lang::Pitches
Attributes
octave[R]
offset[R]
pitch_class[R]
Public Class Methods
[](pitch_class, octave)
click to toggle source
Return a pitch with no offset, only constructing a new instance when not already in the flyweight cache
# File lib/mtk/core/pitch.rb, line 21 def self.[](pitch_class, octave) pitch_class = MTK.PitchClass(pitch_class) @flyweight[[pitch_class,octave]] ||= new(pitch_class, octave) end
from_f( f )
click to toggle source
Convert a Numeric
semitones value into a Pitch
# File lib/mtk/core/pitch.rb, line 55 def self.from_f( f ) i, offset = f.floor, f%1 # split into int and fractional part pitch_class = PitchClass.from_i(i) octave = i/12 - 1 if offset == 0 self[pitch_class, octave] else new( pitch_class, octave, offset ) end end
from_h(hash)
click to toggle source
# File lib/mtk/core/pitch.rb, line 66 def self.from_h(hash) new hash[:pitch_class], hash[:octave], hash.fetch(:offset,0) end
from_i( i )
click to toggle source
from_s( name )
click to toggle source
Lookup a pitch by name, which consists of any {PitchClass::VALID_NAMES} and an octave number. The name may also be optionally suffixed by +/-###cents (where ### is any number). @example get the Pitch
for middle C :
Pitch.from_s('C4')
@example get the Pitch
for middle C + 50 cents:
Pitch.from_s('C4+50cents')
# File lib/mtk/core/pitch.rb, line 32 def self.from_s( name ) s = name.to_s s = s[0..0].upcase + s[1..-1].downcase # normalize name if s =~ /^([A-G](#|##|b|bb)?)(-?\d+)(\+(\d+(\.\d+)?)cents)?$/ pitch_class = PitchClass.from_s($1) if pitch_class octave = $3.to_i offset_in_cents = $5.to_f if offset_in_cents.nil? or offset_in_cents.zero? return self[pitch_class, octave] else return new( pitch_class, octave, offset_in_cents/100.0 ) end end end raise ArgumentError.new("Invalid pitch name: #{name.inspect}") end
Also aliased as: from_name
new( pitch_class, octave, offset=0 )
click to toggle source
# File lib/mtk/core/pitch.rb, line 13 def initialize( pitch_class, octave, offset=0 ) @pitch_class, @octave, @offset = pitch_class, octave, offset @value = @pitch_class.to_i + 12*(@octave+1) + @offset end
Public Instance Methods
+(interval_in_semitones)
click to toggle source
# File lib/mtk/core/pitch.rb, line 110 def + interval_in_semitones self.class.from_f( @value + interval_in_semitones.to_f ) end
Also aliased as: transpose
-(interval_in_semitones)
click to toggle source
# File lib/mtk/core/pitch.rb, line 115 def - interval_in_semitones self.class.from_f( @value - interval_in_semitones.to_f ) end
<=>(other)
click to toggle source
# File lib/mtk/core/pitch.rb, line 106 def <=> other @value <=> other.to_f end
==( other )
click to toggle source
# File lib/mtk/core/pitch.rb, line 101 def ==( other ) other.respond_to? :pitch_class and other.respond_to? :octave and other.respond_to? :offset and other.pitch_class == @pitch_class and other.octave == @octave and other.offset == @offset end
clone_with(hash)
click to toggle source
# File lib/mtk/core/pitch.rb, line 131 def clone_with(hash) self.class.from_h(to_h.merge hash) end
coerce(other)
click to toggle source
# File lib/mtk/core/pitch.rb, line 127 def coerce(other) return self.class.from_f(other.to_f), self end
inspect()
click to toggle source
# File lib/mtk/core/pitch.rb, line 97 def inspect "#<#{self.class}:#{object_id} @value=#{@value}>" end
invert(center_pitch)
click to toggle source
# File lib/mtk/core/pitch.rb, line 119 def invert(center_pitch) self + 2*(center_pitch.to_f - to_f) end
nearest(pitch_class)
click to toggle source
# File lib/mtk/core/pitch.rb, line 123 def nearest(pitch_class) self + self.pitch_class.distance_to(pitch_class) end
offset_in_cents()
click to toggle source
# File lib/mtk/core/pitch.rb, line 85 def offset_in_cents @offset * 100 end
to_f()
click to toggle source
The numerical value of this pitch
# File lib/mtk/core/pitch.rb, line 76 def to_f @value end
to_h()
click to toggle source
# File lib/mtk/core/pitch.rb, line 89 def to_h {:pitch_class => @pitch_class, :octave => @octave, :offset => @offset} end
to_i()
click to toggle source
The numerical value for the nearest semitone
# File lib/mtk/core/pitch.rb, line 81 def to_i @value.round end
to_s()
click to toggle source
# File lib/mtk/core/pitch.rb, line 93 def to_s "#{@pitch_class}#{@octave}" + (@offset.zero? ? '' : "+#{offset_in_cents.round}cents") end