Class: Tonal::Interval
- Inherits:
-
Object
- Object
- Tonal::Interval
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/interval.rb
Constant Summary collapse
- INTERVAL_OF_EQUIVALENCE =
2
Instance Attribute Summary collapse
-
#interval ⇒ Object
(also: #ratio)
readonly
Returns the value of attribute interval.
-
#lower_ratio ⇒ Object
(also: #lower)
readonly
Returns the value of attribute lower_ratio.
-
#upper_ratio ⇒ Object
(also: #upper)
readonly
Returns the value of attribute upper_ratio.
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #denominize ⇒ Object
-
#initialize(*args, reduced: true) ⇒ Tonal::Interval
constructor
The interval of the given ratios.
- #inspect ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(*args, reduced: true) ⇒ Tonal::Interval
Returns the interval of the given ratios.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tonal/interval.rb', line 17 def initialize(*args, reduced: true) klass = reduced ? Tonal::ReducedRatio : Tonal::Ratio raise(ArgumentError, "Two or four arguments required. Either two ratios, or two pairs of numerator, denominator", caller[0]) unless [2, 4].include?(args.size) @lower_ratio, @upper_ratio = case args.size when 2 [klass.new(args[0].antecedent, args[0].consequent), klass.new(args[1].antecedent, args[1].consequent)] when 4 [klass.new(args[0],args[1]), klass.new(args[2], args[3])] end @interval = @upper_ratio / @lower_ratio end |
Instance Attribute Details
#interval ⇒ Object (readonly) Also known as: ratio
Returns the value of attribute interval.
7 8 9 |
# File 'lib/tonal/interval.rb', line 7 def interval @interval end |
#lower_ratio ⇒ Object (readonly) Also known as: lower
Returns the value of attribute lower_ratio.
7 8 9 |
# File 'lib/tonal/interval.rb', line 7 def lower_ratio @lower_ratio end |
#upper_ratio ⇒ Object (readonly) Also known as: upper
Returns the value of attribute upper_ratio.
7 8 9 |
# File 'lib/tonal/interval.rb', line 7 def upper_ratio @upper_ratio end |
Instance Method Details
#<=>(rhs) ⇒ Object
48 49 50 |
# File 'lib/tonal/interval.rb', line 48 def <=>(rhs) interval.to_r <=> rhs.interval.to_r end |
#denominize ⇒ Object
38 39 40 41 42 |
# File 'lib/tonal/interval.rb', line 38 def denominize ratios = to_a lcm = ratios.denominators.lcm ratios.map{|r| Tonal::Ratio.new(lcm / r.denominator * r.numerator, lcm)} end |
#inspect ⇒ Object
44 45 46 |
# File 'lib/tonal/interval.rb', line 44 def inspect "#{interval} (#{upper} / #{lower})" end |
#to_a ⇒ Object
34 35 36 |
# File 'lib/tonal/interval.rb', line 34 def to_a [lower_ratio, upper_ratio] end |