Class: Coltrane::Theory::FrequencyInterval
- Inherits:
-
Object
- Object
- Coltrane::Theory::FrequencyInterval
- Includes:
- Comparable
- Defined in:
- lib/coltrane/theory/frequency_interval.rb
Overview
Interval describe the logarithmic distance between 2 frequencies. It’s measured in cents.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cents ⇒ Object
(also: #hash)
readonly
Returns the value of attribute cents.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #ascending ⇒ Object
- #ascending? ⇒ Boolean
- #descending ⇒ Object
- #descending? ⇒ Boolean
-
#initialize(cents) ⇒ FrequencyInterval
constructor
A new instance of FrequencyInterval.
- #interval_class ⇒ Object
- #inversion ⇒ Object
- #opposite ⇒ Object
- #semitones ⇒ Object
Constructor Details
#initialize(cents) ⇒ FrequencyInterval
Returns a new instance of FrequencyInterval.
16 17 18 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 16 def initialize(cents) @cents = cents.round end |
Instance Attribute Details
#cents ⇒ Object (readonly) Also known as: hash
Returns the value of attribute cents.
10 11 12 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 10 def cents @cents end |
Instance Method Details
#+(other) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 60 def +(other) case other when Numeric then FrequencyInterval[cents + other] when Interval then FrequencyInterval[cents + other.cents] end end |
#-(other) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 67 def -(other) case other when Numeric then FrequencyInterval[cents - other] when Interval then FrequencyInterval[cents - other.cents] end end |
#-@ ⇒ Object
74 75 76 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 74 def -@ FrequencyInterval[-cents] end |
#<=>(other) ⇒ Object
78 79 80 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 78 def <=>(other) cents <=> other.cents end |
#==(other) ⇒ Object Also known as: eql?
52 53 54 55 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 52 def ==(other) return false unless other.is_a? FrequencyInterval cents == other.cents end |
#ascending ⇒ Object
24 25 26 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 24 def ascending self.class[cents.abs] end |
#ascending? ⇒ Boolean
32 33 34 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 32 def ascending? cents > 0 end |
#descending ⇒ Object
28 29 30 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 28 def descending self.class[-cents.abs] end |
#descending? ⇒ Boolean
36 37 38 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 36 def descending? cents < 0 end |
#interval_class ⇒ Object
48 49 50 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 48 def interval_class IntervalClass.new(semitones) end |
#inversion ⇒ Object
40 41 42 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 40 def inversion self.class[(-cents.abs % 1200) * (ascending? ? +1 : -1)] end |
#opposite ⇒ Object
44 45 46 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 44 def opposite self.class.new(-cents) end |
#semitones ⇒ Object
20 21 22 |
# File 'lib/coltrane/theory/frequency_interval.rb', line 20 def semitones (cents.to_f / 100).round end |