class ShlispTools::Ratio

A Shlisp-oriented representation of a pitch in just/rational intonation. Numerators are “numes” and denoninators are “denos” as is customary for Shlisp. All terms 0 < n < 256 (“arab” mode in Shlisp).

Attributes

deno[R]

denominator

nume[R]

numerator

rat[R]

rational form

Public Class Methods

new(nume, deno, _mul=nil) click to toggle source
# File lib/shlisp_tools/ratio.rb, line 31
def initialize(nume, deno, _mul=nil)
  @nume = nume
  @deno = deno
  _set_rat
  mul(_mul)
end
parse_new(txt) click to toggle source
# File lib/shlisp_tools/ratio.rb, line 26
def self.parse_new(txt)
  r = parse_ratio(txt)
  Ratio.new(r[:nume], r[:deno])
end
parse_ratio(txt) click to toggle source
# File lib/shlisp_tools/ratio.rb, line 16
def self.parse_ratio(txt)
  parsed = {}
  matches = /(\d+)[\/\:](\d+)/.match(txt)
  if matches && matches.captures.length >= 2
    parsed[:nume] = matches[1].to_i
    parsed[:deno] = matches[2].to_i
  end
  parsed
end

Public Instance Methods

d() click to toggle source

Get the deno(minator).

# File lib/shlisp_tools/ratio.rb, line 51
def d
  @deno
end
d=(deno) click to toggle source

Set the deno(minator).

# File lib/shlisp_tools/ratio.rb, line 56
def d=(deno)
  @deno = deno
  _set_rat
  d
end
mul(factor) click to toggle source

Multiply both nume and deno by same factor for Shnth amplitude scaling.

# File lib/shlisp_tools/ratio.rb, line 63
def mul(factor)
  if factor
    factor = factor.abs
    @nume = (@nume * factor) % LIMIT
    @deno = (@deno * factor) % LIMIT
  end
  self
end
n() click to toggle source

Get the nume(rator).

# File lib/shlisp_tools/ratio.rb, line 39
def n
  @nume
end
n=(nume) click to toggle source

Set the nume(rator).

# File lib/shlisp_tools/ratio.rb, line 44
def n=(nume)
  @nume = nume
  _set_rat
  n
end
reset!() click to toggle source

Return to the canonical (reduced) value.

# File lib/shlisp_tools/ratio.rb, line 77
def reset!
  @nume = @rat.numerator
  @deno = @rat.denominator
end
to_r() click to toggle source

Rational representation

# File lib/shlisp_tools/ratio.rb, line 88
def to_r
  @rat
end
to_s() click to toggle source

String representation

# File lib/shlisp_tools/ratio.rb, line 83
def to_s
  "#{@nume}/#{@deno}"
end