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
-
#intervalic_ratio ⇒ Object
readonly
Returns the value of attribute intervalic_ratio.
-
#lower_ratio ⇒ Object
readonly
Returns the value of attribute lower_ratio.
-
#upper_ratio ⇒ Object
readonly
Returns the value of attribute upper_ratio.
Instance Method Summary collapse
-
#<=>(rhs) ⇒ Integer
-1, 0, or 1 depending on whether this interval is less than, equal to, or greater than the other interval.
-
#approximate(approximant = 0, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval
The interval selected by the approximant key, from a set of approximations generated by the by_method algorithm.
-
#denominize ⇒ Array<Tonal::Ratio>
The ratios with common denominators.
-
#initialize(*args, reduced: true) ⇒ Tonal::Interval
constructor
The interval of the given ratios.
-
#inspect ⇒ String
A string representation of the interval.
-
#root_interval(power: 1, root: 2, approximant: nil, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval
The interval representing the root of self raised to a power.
-
#to_a ⇒ Array<Tonal::Ratio>
The upper and lower ratios as an array.
Constructor Details
#initialize(*args, reduced: true) ⇒ Tonal::Interval
Returns the interval of the given ratios.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tonal/interval.rb', line 21 def initialize(*args, reduced: true) raise(ArgumentError, "One, two or four arguments required. Either one ratio (the other defaulting to 1/1), two ratios, or two pairs of numerator, denominator", caller[0]) unless [1, 2, 4].include?(args.size) args = [args[0],1/1r] if args.length == 1 klass = reduced ? Tonal::ReducedRatio : Tonal::Ratio @lower_ratio, @upper_ratio = case args.size when 2 [klass.new(args[1].antecedent, args[1].consequent), klass.new(args[0].antecedent, args[0].consequent)] when 4 [klass.new(args[2], args[3]), klass.new(args[0], args[1])] end @intervalic_ratio = @upper_ratio / @lower_ratio end |
Instance Attribute Details
#intervalic_ratio ⇒ Object (readonly)
Returns the value of attribute intervalic_ratio.
7 8 9 |
# File 'lib/tonal/interval.rb', line 7 def intervalic_ratio @intervalic_ratio end |
#lower_ratio ⇒ Object (readonly)
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)
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) ⇒ Integer
Returns -1, 0, or 1 depending on whether this interval is less than, equal to, or greater than the other interval.
99 100 101 |
# File 'lib/tonal/interval.rb', line 99 def <=>(rhs) intervalic_ratio.to_r <=> rhs.intervalic_ratio.to_r end |
#approximate(approximant = 0, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval
Returns the interval selected by the approximant key, from a set of approximations generated by the by_method algorithm.
80 81 82 |
# File 'lib/tonal/interval.rb', line 80 def approximate(approximant=0, by_method: :continued_fraction, from: :lower_ratio) _returning_interval(_approximate(intervalic_ratio, by_method, approximant), from:) end |
#denominize ⇒ Array<Tonal::Ratio>
Returns the ratios with common denominators.
50 51 52 53 54 |
# File 'lib/tonal/interval.rb', line 50 def denominize ratios = to_a lcm = ratios.denominators.lcm ratios.map{|r| Tonal::Ratio.new(lcm / r.denominator * r.numerator, lcm)} end |
#inspect ⇒ String
Returns a string representation of the interval.
89 90 91 |
# File 'lib/tonal/interval.rb', line 89 def inspect "#{intervalic_ratio.label} (#{upper_ratio.label} / #{lower_ratio.label})" end |
#root_interval(power: 1, root: 2, approximant: nil, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval
Returns the interval representing the root of self raised to a power.
66 67 68 69 70 |
# File 'lib/tonal/interval.rb', line 66 def root_interval(power: 1, root: 2, approximant: nil, by_method: :continued_fraction, from: :lower_ratio) root_ratio = intervalic_ratio.class.new(intervalic_ratio.to_r.power(power, root)) resulting_ratio = approximant.nil? ? root_ratio : _approximate(root_ratio, by_method, approximant) _returning_interval(resulting_ratio, from:) end |
#to_a ⇒ Array<Tonal::Ratio>
Returns the upper and lower ratios as an array.
41 42 43 |
# File 'lib/tonal/interval.rb', line 41 def to_a [upper_ratio, lower_ratio] end |